Skip to content

[jwe] Add option to explicitly clear per-recipient headers ("header") for flattened JSON serialization#1477

Merged
lestrrat merged 9 commits intodevelop/v3from
gh-1470
Oct 13, 2025
Merged

[jwe] Add option to explicitly clear per-recipient headers ("header") for flattened JSON serialization#1477
lestrrat merged 9 commits intodevelop/v3from
gh-1470

Conversation

@lestrrat
Copy link
Collaborator

fixes #1470

After much going back and forth, I have nailed down that the ONLY time the current implementation creates duplicate entries (that matter) are when flattened JSON serialization is used.

For full JSON serialization, we didn't merge anything to start with. This was fine. It's the user's responsibility to figure out how to create headers that are completely disjoint. But for flattened JSON, we merged the per-recipient headers and the protected header, and yet kept the per-recipient headers in tact. The fix is to nuke the headers stored in the per-recipient headers after merging -- the merge is NOT necessary, but it is my understanding that we would be erring on the safer side if we merged everything into the protected header so that the entirety of this metadata is used as AAD, thus making the flattened JSON serialization that we produce analogous to the compact format.

Now, I'm sure there is code somewhere that already relies on this behavior, so instead of just "fixing" this, we introduced an option to explicitly enable the new behavior. Code that wants to generate a flattened JSON serialization that clears the "header" value should call jwe.Encrypt() with jwe.WithLegacyHeaderMerging(false).

@ilya-korotya
Copy link

ilya-korotya commented Oct 13, 2025

Now everything works as expected 🎉

@lestrrat lestrrat merged commit 8923c67 into develop/v3 Oct 13, 2025
23 checks passed
@lestrrat lestrrat deleted the gh-1470 branch October 13, 2025 22:46
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.

Duplicate header params across protected & per-recipient headers (violates RFC 7516 §7.2.1)

2 participants