Skip to content

Wrap JWKSet parsing errors in InvalidJWKValue#379

Merged
simo5 merged 1 commit into
latchset:mainfrom
simo5:jwkset_378
May 6, 2026
Merged

Wrap JWKSet parsing errors in InvalidJWKValue#379
simo5 merged 1 commit into
latchset:mainfrom
simo5:jwkset_378

Conversation

@simo5
Copy link
Copy Markdown
Member

@simo5 simo5 commented May 5, 2026

Moved the dictionary iteration and key creation logic inside the try-except block. This ensures that any exceptions raised during the instantiation of individual JWK objects or validation checks are properly caught and safely re- raised as an InvalidJWKValue exception, rather than leaking unhandled errors.

Fixes: #378

Moved the dictionary iteration and key creation logic inside the try-except
block. This ensures that any exceptions raised during the instantiation of
individual JWK objects or validation checks are properly caught and safely re-
raised as an InvalidJWKValue exception, rather than leaking unhandled errors.

Assisted-by: Gemini <gemini@google.com>
Signed-off-by: Simo Sorce <simo@redhat.com>
@rjeffman
Copy link
Copy Markdown
Collaborator

rjeffman commented May 5, 2026

LGTM. I'll give some time for reporter to take a look at this before merging.

Comment thread jwcrypto/jwk.py
Comment on lines +1359 to +1367
if 'keys' not in jwkset:
raise ValueError("'keys' not in set")

for k, v in jwkset.items():
if k == 'keys':
for jwk in v:
self['keys'].add(JWK(**jwk))
else:
self[k] = v
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're changing this around, can't we also improve it?
Something like:

Suggested change
if 'keys' not in jwkset:
raise ValueError("'keys' not in set")
for k, v in jwkset.items():
if k == 'keys':
for jwk in v:
self['keys'].add(JWK(**jwk))
else:
self[k] = v
self["keys"].update(JWK(**jwk) for jwk in jwkset.pop("keys"))
self.update(jwkset)

We don't need to raise a ValueError, because we'll get a KeyError in pop("keys") that'll be caught in the except block and re-raised.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to keep things readable, and I can't drop the 'keys' check as then I would miss the case when jwkset is an empty dict, which is also invalid.

@simo5 simo5 merged commit dfd1bb2 into latchset:main May 6, 2026
29 of 30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

insufficient validation in JWKSet.from_json (import_keyset)

3 participants