Skip to content

fix(encryption): refuse "dir" JWEs without protected header and without "aad" - #807

Draft
Spomky wants to merge 3 commits into
5.0.xfrom
fix/dir-jwe-empty-aad-5.0.x
Draft

Spomky wants to merge 3 commits into
5.0.xfrom
fix/dir-jwe-empty-aad-5.0.x

Conversation

@Spomky

@Spomky Spomky commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

Summary

Follow-up of #796 for 5.0.0. 4.3.0 deprecates building and decrypting a JSON dir JWE that has neither a protected header nor an aad member (#760). Its content is encrypted with an empty AAD, like an AES-GCM key wrap: under a key that serves both dir and an A*GCMKW algorithm, one passes for the other. This PR turns both deprecations into refusals.

Draft until the merge-up of 4.3.x into 5.0.x is pushed, see "Branch layout".

The change

  • JWEBuilder::build() throws Jose\Component\Core\Exception\InvalidArgumentException before it encrypts the content: A "dir" JWE must have a protected header or additional authenticated data: set "alg" and "enc" in the shared protected header.
  • JWEDecrypter refuses such a JWE with every key. The check runs in the key loop, next to the other checks and before the CEK is obtained, as for a filled encrypted key in direct mode. decrypt() returns a failed result, the error callback receives the InvalidArgumentException (A "dir" JWE must have a protected header or additional authenticated data.) once per key, and JWELoader::loadAndDecrypt() keeps it as the previous exception of its InvalidTokenException.
  • The rule is the one of the deprecations: the encoded protected header is empty and there is no aad member, whatever the content encryption algorithm. An empty protected member is refused; e30 ({}) and an aad member, even empty, are accepted. Compact JWEs always have a protected header and are not concerned.

The two methods are private: no public or protected signature changes.

BC break

Announced by the 4.3.0 deprecations of #796.

JWE 4.3 5.0
dir, JSON, no protected header, no aad: built built, with a deprecation InvalidArgumentException
the same: decrypted decrypted, with a deprecation not decrypted, the reason goes to the error callback
an A*GCMKW encrypted key presented as such a JWE its CEK returned, with a deprecation not decrypted
dir with a protected header (every compact JWE), or with an aad member, even empty unchanged unchanged
any other key management mode unchanged unchanged

Proposed text for the 5.0 upgrade guide:

"dir" JWEs need a protected header. A dir JWE must have a protected header or an aad member: put alg and enc in the shared protected header (withSharedProtectedHeader()). The builder refuses the other layouts, and the tokens built that way by 4.x are not decrypted anymore. 4.3 reported both with a deprecation notice. Compact JWEs always have a protected header and are not affected.

Branch layout

5.0.x does not contain #796 yet: the merge-up of 4.3.x into 5.0.x is prepared but not pushed. Merging the 4.3.x commit of #796 here would also have brought the 37 commits of 4.3.x (19 without the merges) between the last merge-up (7dfbe15f) and its base, with their conflicts. This branch therefore cherry-picks it (f4b6da51, from 1411e4a7), then adds the refusal (54617b07). The diff of the second commit is what 5.0 changes compared to 4.3.

Once the merge-up is pushed, merge 5.0.x into this branch. Simulated with the prepared merge-up completed with the current 4.3.x:

Verification

  • The new test class has 38 cases. 26 of them fail on 5.0.x (b81a608b) and on the cherry-picked deprecation (f4b6da51): all the refusal cases. The 12 that pass there are the layouts that stay accepted.
    • The encrypted key of an A128GCMKW, A192GCMKW or A256GCMKW token, presented as the content of a flattened or general dir JWE, is not decrypted, and the reason is the refusal (6 cases).
    • The builder refuses the layout for the six content encryption algorithms, with alg and enc in the shared unprotected header, with an explicitly empty shared protected header, and with aad set back to null (9 cases).
    • Hand-made tokens without protected header and without aad are not decrypted, for the six content encryption algorithms and both JSON serializations (6 cases). An empty protected member and a shared unprotected header only are refused; e30, a protected header with a parameter, an empty aad member and an aad member are not (7 cases).
    • Every key of a key set reports the refusal, the right key included, and the loader chains it as the previous exception (2 cases).
    • Still built and decrypted: a protected header (compact and JSON), enc protected with alg in the recipient header, an empty or a non-empty aad (6 cases); A128KW, and A128GCMKW with two recipients and an empty shared protected header (2 cases).
  • BC, against 5.0.x (b81a608b):
    • the public and protected API of src/ (443 types), dumped by reflection, is identical;
    • the probe of fix(encryption): deprecate "dir" JWEs without protected header and without "aad" #796 (a corpus of 96 tokens, each decrypted with a key and with a key set, and 16 builder configurations: 208 cases) changes 59 cases, all from accepted to refused: 48 decryptions of dir tokens of this layout, the 6 forged A*GCMKW tokens, which no longer return the CEK, and 5 builds. The 13 other cases of this layout use dir keys longer than the CEK, which 5.0.x already refuses. No other case changes.
  • Whole suite with phpunit-11 in phpqa:8.4: 2,291 tests green (base: 2,253, the difference is the new class), with the same 3 skipped and 2 incomplete. ECS, Rector, PHPStan, Deptrac, the license check, the lint and composer validate --strict are clean.
  • Mutation testing on the diff: 22 mutants, all killed in a local run with a 60-second timeout (MSI 100%). With the 3-second timeout of infection.json.dist, the CI run of this PR kills 4 and skips 18 as "required more time than configured".

Refs #760, #796, #717

Found by the Anthropic OSS Scanner (ANT-2026-35BMRM17).

…thout "aad"

AES-GCM key wrapping encrypts the CEK with AES-GCM and an empty AAD. A
JSON "dir" JWE that has neither a protected header nor an "aad" member
encrypts its content the same way. Under a key without "alg" that a
decrypter accepts for "dir" and for an A*GCMKW algorithm, the encrypted
key of an A*GCMKW token, presented as the content of such a JWE, is
decrypted to the CEK of that token. Conversely, the content of such a
JWE is a valid A*GCMKW encrypted key of its payload.

Refusing this shape would break tokens that the JSON serializations
allow, so 4.3.0 deprecates it on both sides. The decrypter triggers a
deprecation when it decrypts such a JWE, for the key that decrypted it
only, and the builder when it builds one. Both still work; they will be
refused in 5.0.0. Compact JWEs always have a protected header and are
not concerned.

Reported by Anthropic's OSS Scanner (ANT-2026-35BMRM17).

(cherry picked from commit 1411e4a)
…ut "aad"

4.3.0 deprecates building and decrypting a JSON "dir" JWE that has
neither a protected header nor an "aad" member: its content is encrypted
with an empty AAD, as AES-GCM key wrapping encrypts a CEK, so the
encrypted key of an A*GCMKW token passes for such a content under the
same key, and the other way round.

5.0.0 refuses it. The builder throws before encrypting the content. The
decrypter refuses the token with every key, like a filled encrypted key
in direct mode: the decryption fails and the reason goes to the error
callback, and the loader keeps it as the previous exception.

Reported by Anthropic's OSS Scanner (ANT-2026-35BMRM17).
@Spomky Spomky added this to the 5.0.0 milestone Nov 8, 2026
5.0.x now holds #796 (1d88a1a) through the merge-up of 4.3.x, so 5.0.x
is merged into this branch as its description plans. The cherry-pick of
#796 (f4b6da5) gives way to the code of 5.0.x, and the refusal
(54617b0) is kept:

- JWEBuilder and JWEDecrypter keep checkDirectEncryptionAADIsNotEmpty(),
  and the call to deprecateDirectEncryptionWithEmptyAAD() goes; the
  deprecation of a "dir" key without "decrypt" (#797) stays, #806 turns
  it into a refusal;
- DirectEncryptionAdditionalAuthenticatedDataTest is the version of this
  branch;
- KeyOperationsTest::aKeyWrappingKeyThatDecryptsDirectContentIsReported
  (#797) forges the token of #760 and expected its CEK: the token is now
  refused, so the test, renamed aKeyWrappingKeyCannotDecryptDirectContent
  as in #806, expects a failed decryption without deprecation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant