Skip to content

Unify JOSE header merging and validation - #405

Merged
simo5 merged 2 commits into
latchset:mainfrom
simo5:prothead
Nov 11, 2026
Merged

simo5 merged 2 commits into
latchset:mainfrom
simo5:prothead

Conversation

@simo5

@simo5 simo5 commented Nov 10, 2026

Copy link
Copy Markdown
Member

Extract header merging, critical header checks, and registry validation into shared helper functions in common.py to deduplicate logic between JWS and JWE.

Update JWS and JWE implementations to use the new shared helpers and handle InvalidJWSEHeader exceptions. Additionally, mark the 'zip' header parameter as required to be protected in JWE, and add unit tests for common header processing and critical header validation.

Note this PR was create in response to a private issue originally reported by @shlee1223 about the fact that the "zip" and "crit" header could be places outside the protected header.
Note that the application always controls what to place in which headers, so the only issue is if a malicious endpoint were to add either header in the unprotected header in transit. The only outcome would like be a denial of service, which his already possible by corrupting the JWE in any way, so this is treated just as a regular bug.

Remove the Python 3.9 test configuration and environment from the GitHub
Actions build workflow.
Github moved to Ubuntu 26.04 which dropped 3.9

Assisted-by: Gemini 3.8 Flash
Signed-off-by: Simo Sorce <simo@detuned.org>
Extract header merging, critical header checks, and registry
validation into shared helper functions in common.py to deduplicate
logic between JWS and JWE.

Update JWS and JWE implementations to use the new shared helpers and
handle InvalidJWSEHeader exceptions. Additionally, mark the 'zip'
header parameter as required to be protected in JWE, and add unit
tests for common header processing and critical header validation.

Assisted-by: Gemini 3.8 Flash
Signed-off-by: Simo Sorce <simo@detuned.org>

@rjeffman rjeffman left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm approving the patch, with a "style" comment.

Feel free to change, argue or ignore it.

Comment thread jwcrypto/common.py
Comment on lines +314 to +318
if registry is not None:
for hdr in merged:
if hdr in registry:
if not registry.check_header(hdr, target):
raise InvalidJWSEHeader('Failed header check')

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Changes look good to me, but I prefer to have the raise directly attached to the outer if, instead of deeply nested as in:

    if registry is not None and any(
        hdr for hdr in merged
        if hdr in registry and not registry.check_header(hdr, target)
    ):
        raise InvalidJWSEHeader('Failed header check')

any still ensures it breaks on first item that is true for the check.

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.

Sorry if I am old school, but I find your version too hard to read and understand quickly, and reading code is the most important thing ( after using it :-) ).

I find especially bad to use "perl style" if condition after the action, it is really easy to misread that kind of statement, in fact whenever I use AI and it does that I manually undo it.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I find the generator style easier to read, but as I'm not the only one reading, I'm ok with different views.

@simo5
simo5 merged commit bd93cf2 into latchset:main Nov 11, 2026
15 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.

2 participants