Skip to content

fix(sd-jwt): tell the decoding bounds apart from invalid JSON - #813

Merged
Spomky merged 1 commit into
4.3.xfrom
fix/sd-jwt-parse-budget-message
Nov 9, 2026
Merged

Spomky merged 1 commit into
4.3.xfrom
fix/sd-jwt-parse-budget-message

Conversation

@Spomky

@Spomky Spomky commented Nov 9, 2026

Copy link
Copy Markdown
Member

Follow-up of #810, whose "Not covered" section listed this.

The defect

When the BoundedJsonDecoder rejected a document, the SD-JWT parser always gave the message of invalid JSON:

  • Disclosure::parse(), used by Disclosure::fromString() and SDJWT::fromString(), said The Disclosure is not valid JSON.;
  • SDJWT::readJson(), used by SDJWT::fromString() and SDJWT::create(), said The JWS JSON serialization must be a JSON object..

Three causes ended in these messages: invalid JSON, more than 4096 JSON values (the decoder counts them before decoding), and a document nested too deeply for json_decode(). In the last two, the JSON is valid:

  • a Disclosure ["salt", [1, …, 4095]] written by another library, which json_decode() reads, was "not valid JSON";
  • a JWS JSON serialization whose unprotected header holds 600 nested arrays "must be a JSON object";
  • the Disclosures of an SD-JWT share one budget, so the Disclosure blamed was the one on which the budget ran out. In a compact SD-JWT of 2,049 Disclosures ["s", 1], the 2,049th, a few bytes long, was "not valid JSON".

The fix

  • BoundedJsonDecoder::hasExceededMaxValues() tells whether the documents given to the decoder went beyond MAX_VALUES. The class is @internal and decode() is unchanged, so the JWS and JWE serializers that use it behave as before.
  • Disclosure::parse() and SDJWT::readJson() name the cause. The depth is read from the exception code: JsonConverter::decode() gives its exception the code of the JSON error, here JSON_ERROR_DEPTH.
  • SDJWT::fromString() rejects an SD-JWT whose Disclosures go beyond the budget as a whole, whichever Disclosure goes beyond it, and chains the exception of that Disclosure.
Cause Disclosure::fromString() A Disclosure, in SDJWT::fromString() The JWS JSON serialization, in SDJWT::fromString() and SDJWT::create()
More than 4096 JSON values With this Disclosure, there are more than 4096 JSON values to decode. The SD-JWT holds more JSON values than the 4096 that are decoded. The JWS JSON serialization holds more JSON values than the 4096 that are decoded.
Nested too deeply The Disclosure is nested too deeply. The Disclosure is nested too deeply. The JWS JSON serialization is nested too deeply.
Invalid JSON The Disclosure is not valid JSON. (unchanged) The Disclosure is not valid JSON. (unchanged) The JWS JSON serialization must be a JSON object. (unchanged)
  • The message of Disclosure::parse() for the budget holds wherever it is read: alone, the Disclosure holds more than 4096 values; in an SD-JWT, it takes the values decoded so far beyond 4096.
  • The JWS JSON serialization is the first document its decoder is given, so its own message can state the bound.
  • The values are counted before decoding, so a document beyond both bounds is reported for its values: 5,000 nested arrays hold 5,000 values.

Backward compatibility

The SD-JWT component has not been released yet: no tag contains src/SelectiveDisclosure.

  • API. By reflection, the public methods (parameters, types and defaults), the constants and the modifiers of Disclosure and SDJWT are identical. BoundedJsonDecoder, which is @internal, gains hasExceededMaxValues(): bool. web-token/jwt-selective-disclosure requires web-token/jwt-library: ^4.3, so the method is always there.

  • Behaviour. A probe ran the same 155 cases on 4.3.x (85d5df0) and on this branch. In the base run, the 4.3.x versions of BoundedJsonDecoder, Disclosure and SDJWT were loaded by an autoloader placed before Composer's. The cases cover:

    • Disclosures around 510 to 513 levels, around 4,094 to 4,097 values, and far beyond;
    • invalid JSON of several kinds;
    • compact and JSON SD-JWTs with shared budgets, Key Binding JWTs and general serializations;
    • SDJWT::create(), the factories, the Issuer in both serializations, and the JWS and JWE serializers.

    The results:

    • 94 cases are identical. They include every accepted input, every refusal for a syntax or a structure error, and every case of the JWS and JWE serializers.
    • 57 cases change their message, and nothing else: same exception class, same code (0). In each case, the chain of the 4.3.x exception shows the bound that was hit: Unsupported input. alone for the 36 budget cases, followed by JsonException(1): Maximum stack depth exceeded for the 21 depth cases. The new message names that bound.
    • 4 issuances keep the message of SDJWTIssuer::checkBounds() (SD-JWT: refuse at issuance the SD-JWTs the Holder and the Verifier reject #809). Only the exception it chains changes, from The Disclosure is not valid JSON. to the SD-JWT message.
    • There is no other difference.

Verification

  • New tests (13 cases):
    • BoundedJsonDecoderTest: hasExceededMaxValues() is false at the bound (4096 values) and true once a document is rejected for its values. It stays false when json_decode() rejects a document for its syntax or its depth;
    • DisclosureTest::aDisclosureBeyondTheBoundsOfTheDecoderIsRejected: a Disclosure of 4,097 values, and one of 512 levels;
    • JsonValueBoundTest:
      • theDisclosuresOfAnSdJwtShareTheBound now checks the message, in both serializations;
      • a single Disclosure in an SD-JWT, beyond the budget or nested too deeply;
      • a JWS JSON serialization beyond the budget or nested too deeply, through SDJWT::fromString() and SDJWT::create().
  • On 4.3.x, the 13 cases fail. There are 3 errors, because the method does not exist, and 10 failures on the message: The Disclosure is not valid JSON. or The JWS JSON serialization must be a JSON object.. With this PR, they all pass. The existing tests check that invalid JSON keeps its message (DisclosureTest "not JSON", JsonSerializationTest "invalid JSON"); they pass before and after.
  • Full suite with phpunit-11 in phpqa:8.4: 2,840 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.
  • Mutations on the diff: 29 mutants, all killed with a 60-second timeout. Locally, with the 3-second timeout of infection.json.dist, 20 of them time out. Infection counts them as detected, and the MSI stays at 100%.

Not covered

  • The JWS and JWE serializers still answer Unsupported input for all three causes. Their messages are unchanged.
  • The factories of fix(sd-jwt): create only the Disclosures that can be parsed #810 still give one message for both bounds (The value is nested too deeply or holds too many JSON values: its Disclosure could not be parsed.). They could now tell the two bounds apart as well.
  • Given an unprotected header of 510 nested arrays, SDJWTIssuer::issueJson() fails on the InvalidSerializationException of SDJWT::create(), while its other refusals are InvalidArgumentExceptions. This is not new; with this PR, the message is The JWS JSON serialization is nested too deeply. instead of must be a JSON object.
  • The documentation does not quote these messages.

Disclosure::fromString() and SDJWT::fromString() reported every
Disclosure that the BoundedJsonDecoder rejected as "not valid JSON", and
SDJWT::fromString() and SDJWT::create() reported every JWS JSON
serialization it rejected as "must be a JSON object", even when the JSON
was valid but held more than 4096 values or was nested too deeply. As
the Disclosures of an SD-JWT share the budget, the Disclosure blamed was
the one on which the budget ran out, however small.

BoundedJsonDecoder::hasExceededMaxValues() tells whether the budget was
exceeded. A Disclosure or a JWS JSON serialization rejected for its
values or for its depth now says so, and an SD-JWT whose Disclosures go
beyond the budget is rejected as a whole. The exception classes and
codes are unchanged, and invalid JSON keeps its message.
@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 da615c9 into 4.3.x Nov 9, 2026
18 checks passed
@Spomky
Spomky deleted the fix/sd-jwt-parse-budget-message branch November 9, 2026 07:44
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