Skip to content

fix(sd-jwt): refuse the unparsable unprotected headers as the other claims - #815

Merged
Spomky merged 1 commit into
4.3.xfrom
fix/sd-jwt-issuer-header-bounds
Nov 9, 2026
Merged

Spomky merged 1 commit into
4.3.xfrom
fix/sd-jwt-issuer-header-bounds

Conversation

@Spomky

@Spomky Spomky commented Nov 9, 2026

Copy link
Copy Markdown
Member

Follow-up of #809 and #813.

The defect

Before it signs, SDJWTIssuer::issueJson() checks the SD-JWT on a placeholder that the Holder and the Verifier would parse (#809). The protected headers and the signatures of the placeholder are placeholders, so its only JSON values are its structure, the unprotected headers and the Disclosures.

Three kinds of unprotected headers escaped the refusals of the Issuer, because SDJWT::create() rejected the placeholder with an InvalidSerializationException:

Unprotected header Exception that escaped issueJson()
Nested too deeply: 510 levels in the flattened serialization, 508 in the second signature of the general one The JWS JSON serialization is nested too deeply.
More than 4096 JSON values: from 4,092 integers in the flattened serialization, from 4,088 in the general one The JWS JSON serialization holds more JSON values than the 4096 that are decoded.
A list instead of an object The "header" member must be a JSON object.

The Issuer refuses everything else it cannot issue with an InvalidArgumentException. An InvalidSerializationException is the exception for a string that is not a token.

There was a second defect just below the bound on values. An unprotected header of 4,090 or 4,091 integers passed SDJWT::create(), but not SDJWT::fromString(), which also reads the "disclosures" parameter. The Issuer refused it with The Disclosures of the SD-JWT hold more JSON values than the 4096 the Holder and the Verifier decode., although the SD-JWT had no Disclosure.

The fix

  • placeholderJsonSdJwt() decodes the JSON of the placeholder with a new BoundedJsonDecoder before parsing it, as Disclosure::serialize() does for a Disclosure (fix(sd-jwt): create only the Disclosures that can be parsed #810). json_encode() wrote that JSON, so the decoder can only reject it for the values it holds or for its depth. The Issuer says which:
    • The Disclosures and the unprotected headers of the SD-JWT hold more JSON values than the 4096 the Holder and the Verifier decode.
    • The unprotected headers of the SD-JWT are nested more deeply than the Holder and the Verifier decode.
  • When SDJWT::create() rejects the placeholder for another reason, such as a list as unprotected header, the Issuer throws an InvalidArgumentException. It keeps the message and chains the rejection.
  • In the JWS JSON serialization, both the unprotected headers and the Disclosures count against the budget. checkBounds() therefore gives the budget message that names them both. The compact serialization keeps The Disclosures of the SD-JWT hold more JSON values than the 4096 the Holder and the Verifier decode.

An unprotected header nested even more deeply, from 511 levels in the flattened serialization, is still refused by json_encode() with Invalid content.. That is an InvalidArgumentException, as for a claim nested too deeply.

Backward compatibility

The SD-JWT component has not been released yet.

  • API. Unchanged: three private constants are added, and the private methods placeholderJsonSdJwt() and checkBounds() change.

  • Behaviour. A probe ran the same 431 cases on 4.3.x (da615c9) and on this branch. In the base run, the 4.3.x version of SDJWTIssuer was loaded before Composer's autoloader. The cases are:

    • 11 sets of claims, issued in the compact serialization;
    • 7 of them, also issued in the flattened and the general serializations with 30 unprotected headers: none, usual ones, a list, numeric keys, the reserved parameters, 300 to 600 levels, and 2,000 to 20,000 integers.

    Each SD-JWT that was issued was parsed back. The results:

    • 252 cases are identical: the 85 SD-JWTs issued and parsed, and 167 refusals, among them every compact case.
    • 96 refusals change from InvalidSerializationException to InvalidArgumentException: 72 for the values and 12 for the depth, with the messages above, and 12 for a list, with the same message as before.
    • 83 refusals in the JWS JSON serialization keep their class and code, and get the budget message that names the unprotected headers.
    • No SD-JWT is issued that cannot be parsed, before or after. Nothing that was issued is now refused, and nothing that was refused is now issued.

Verification

  • New and updated tests in IssuerBoundsTest:
    • anUnprotectedHeaderTheHolderAndTheVerifierRejectIsRefused (7 cases): a list, a header nested too deeply and a header with too many values, in the flattened and the general serializations, and a header of 4,090 integers. The test checks the exact class, because InvalidSerializationException extends InvalidArgumentException;
    • anSdJwtWithUnprotectedHeadersIsIssuedExactlyWhenItCanBeParsed (2 cases): unprotected headers from 505 to 512 levels and from 4,080 to 4,100 integers, in both JSON serializations. The Issuer must issue an SD-JWT exactly when it can be parsed back, and refuse the others with an InvalidArgumentException;
    • disclosuresBeyondTheBudgetAreRefused: in the JWS JSON serialization, the test now expects the message that names the unprotected headers.
  • On 4.3.x, 10 of these 11 cases fail. Only the compact case of disclosuresBeyondTheBudgetAreRefused passes, since that case does not change. With this PR, all 11 pass.
  • Full suite with phpunit-11 in phpqa:8.4: 2,854 tests, no failure. The 4 skipped and 2 incomplete tests are the PKCS#12 and ML-DSA platform tests.
  • ECS, Rector, PHPStan (baseline unchanged) and Deptrac pass with phpqa:8.4. The changed files pass php -l with PHP 8.2.
  • Mutations on the diff: 13 mutants, all killed with a 60-second timeout. Locally, with the 3-second timeout of infection.json.dist, all 13 time out. The tests that cover these lines include aJsonSdJwtIsIssuedExactlyWhenItCanBeParsed (SD-JWT: refuse at issuance the SD-JWTs the Holder and the Verifier reject #809), which takes 4 to 5 seconds per data set here and runs before the new tests. The CI job sets no minimum MSI.

…laims

SDJWTIssuer::issueJson() checks its SD-JWT on a placeholder that the
Holder and the Verifier would parse. SDJWT::create() rejected the
placeholder with an InvalidSerializationException when an unprotected
header was nested too deeply, held more than 4096 JSON values or was a
list, and that exception escaped, while the Issuer refuses everything
else with an InvalidArgumentException. Near the bound, an unprotected
header of 4090 values was refused with a message that blamed the
Disclosures, even without any.

The JSON of the placeholder is now decoded first, as the Holder and the
Verifier decode it, and every failure to parse it is refused with an
InvalidArgumentException. In the JWS JSON serialization, the budget
message names the unprotected headers with the Disclosures.
@Spomky Spomky added this to the 4.3.0 milestone Nov 9, 2026
@Spomky Spomky added the bug label Nov 9, 2026
@Spomky
Spomky merged commit 51eee52 into 4.3.x Nov 9, 2026
18 checks passed
@Spomky
Spomky deleted the fix/sd-jwt-issuer-header-bounds branch November 9, 2026 08:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant