Skip to content

fix(sd-jwt): create only the Disclosures that can be parsed - #810

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

Spomky merged 1 commit into
4.3.xfrom
fix/sd-jwt-disclosure-factory-bounds

Conversation

@Spomky

@Spomky Spomky commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

Fixes #808.

The defect

Disclosure::forObjectProperty() and Disclosure::forArrayElement() wrote the JSON of any value, while Disclosure::fromString() decodes it with a BoundedJsonDecoder: at most 4,096 values, and at most 511 levels of arrays and objects, the array of the Disclosure included (with their default depth of 512, json_encode() writes 512 levels but json_decode() reads only 511). The factories created Disclosures that this library then rejected with The Disclosure is not valid JSON.:

  • a value of 511 nested arrays or more;
  • an object whose JSON has 511 levels. Since fix(sd-jwt): serialize the objects of the Disclosures as the payload #804, an object goes through json_encode() and json_decode() on its own, so the levels above it were not counted either: 300 arrays around an object of 300 levels passed;
  • a list of 4,095 integers (4,094 with forObjectProperty()), which json_decode() reads.

Through SDJWTIssuer, a Disclosable of 600 nested arrays was issued, then SDJWT::fromString() rejected the SD-JWT.

The fix

Disclosure::serialize(), which both factories use, now decodes the JSON it wrote with a new BoundedJsonDecoder, as Disclosure::fromString() does, before the Disclosure is returned. When the decoder refuses it, the factory throws an InvalidArgumentException:

The value is nested too deeply or holds too many JSON values: its Disclosure could not be parsed.

The previous exception tells which bound was hit: Unsupported input. alone for the budget, followed by Maximum stack depth exceeded for the depth. The bounds stay in the class that enforces them, and the levels are counted from the top of the Disclosure, for arrays and objects alike.

The cost is one more decoding per Disclosure. The decoder counts the values before decoding, so a Disclosure over the budget is never decoded.

SDJWTIssuer is unchanged. It now refuses such a Disclosable at issuance with an InvalidArgumentException, the class of its other refusals, which is also what the same claim in clear gets (Invalid content.).

Backward compatibility

The SD-JWT component has not been released yet: no tag contains src/SelectiveDisclosure, and the split web-token/jwt-selective-disclosure has no tag.

  • API: unchanged. Only the body of the private Disclosure::serialize() and three docblocks change. By reflection, the public methods (parameters, types and defaults), the constants and the modifiers of the class are identical. No deprecation, no new dependency: BoundedJsonDecoder is in web-token/jwt-library, which the split already requires.
  • Behaviour: a probe ran the same 617 cases on 4.3.x (2d24d85) and on this branch, with the base version of Disclosure loaded by an autoloader placed before Composer's. The cases are 146 values through the two factories, with ordinary and unusual claim names and salts (584 cases), and 13 sets of claims issued in both serializations (26 cases). The values cover scalars, strings full of JSON syntax, enums, DateTimeImmutable, ArrayObject, JsonSerializable, self references, nested arrays and objects and their combinations around 510, 511 and 512 levels, and lists, maps and objects around 4,093 to 4,096 values.
    • 427 cases are identical: 347 created (or issued) and parsed, 74 refused with the same exception and message, 3 issued but not parsed (see "Not covered"), plus the API.
    • The 190 others (180 through the factories, 10 through the Issuer) are all of one kind: a Disclosure that was created, or an SD-JWT that was issued, but that Disclosure::fromString() or SDJWT::fromString() rejected, is now refused at creation with the message above.
    • No value that could be parsed is refused, and nothing that was refused is accepted. An object whose JSON has 512 levels keeps The value cannot be serialized as JSON.: json_decode() refuses it while it is converted, before this check.

Verification

  • New tests:
  • On 4.3.x, these 19 cases give 10 failures, one for each refusal case (Failed asserting that exception of type "Jose\Component\Core\Exception\InvalidArgumentException" is thrown.). The 9 cases within the bounds pass before and after: they check that the fix refuses nothing that can be parsed. With this PR, all of them pass.
  • Full suite with phpunit-11 in phpqa:8.4: 2,806 tests, no failure. The 4 skipped and 2 incomplete tests are the PKCS#12 and ML-DSA platform tests. tests/SelectiveDisclosure also passes with PHP 8.5. PHP 8.2 is left to the CI matrix, because the local vendor requires 8.4.
  • ECS, Rector, PHPStan (baseline unchanged) and Deptrac pass with phpqa:8.4.
  • Mutations on the diff: 4 mutants (removal of the decoding, removal of the throw, and two changes of the exception code), all killed with a 60-second timeout. Locally, the 3-second timeout of infection.json.dist skips one of them.

Interaction with #809

#809 (fix of #805) touches other files: the two branches merge without conflict, and tests/SelectiveDisclosure passes on the merge (626 tests). Together, the two checks split the work. This PR refuses a Disclosure that cannot be parsed on its own, before #809 runs. #809 refuses an SD-JWT whose Disclosures share a budget that is too small, or whose processed payload has more than 64 levels.

Without this PR, #809 also refuses a Disclosure that is too deep, but reports it with its budget message, The Disclosures of the SD-JWT hold more JSON values than the 4096 the Holder and the Verifier decode.. On the merge, the probe gives that message only for the SD-JWTs over the shared budget.

Not covered

  • This only guarantees that each Disclosure can be parsed on its own. SDJWT::fromString() shares one budget between all the Disclosures of an SD-JWT, and DisclosureProcessor bounds the processed payload at 64 levels: those are the bounds of a whole SD-JWT, covered by SD-JWT: SDJWTIssuer issues SD-JWTs beyond the depth and size bounds of the Holder and Verifier #805. The probe found 3 such SD-JWTs, issued but not parsed both before and after this PR: one Disclosure of 4,093 integers in the JWS JSON serialization, whose JSON document adds values to the budget, and 2,049 Disclosures of integers, in both serializations.
  • Disclosure::fromString() still rejects a Disclosure beyond the bounds, received from elsewhere, with The Disclosure is not valid JSON., even when json_decode() can read it.
  • The documentation of the component does not describe the bounds of the Disclosures, so no page becomes wrong.

Disclosure::forObjectProperty() and Disclosure::forArrayElement() wrote
the JSON of any value, while Disclosure::fromString() decodes it with a
BoundedJsonDecoder: at most 4096 values, and at most 511 levels of
arrays and objects, the array of the Disclosure included. A value of 511
nested arrays, an object whose JSON has 511 levels, 300 arrays around an
object of 300 levels or a list of 4095 integers gave a Disclosure that
this library then rejected as "not valid JSON", and SDJWTIssuer issued
SD-JWTs that the Holder and the Verifier could not parse.

Both factories now decode the JSON of the Disclosure with a new
BoundedJsonDecoder before returning it, and refuse the value with an
InvalidArgumentException when the Disclosure cannot be parsed. The
levels are thus counted from the array of the Disclosure, for the arrays
and the objects of the value alike, and the bounds are those of the
parser.

Fixes #808
@Spomky Spomky added this to the 4.3.0 milestone Nov 8, 2026
@Spomky Spomky added the bug label Nov 8, 2026
@Spomky
Spomky merged commit be17de8 into 4.3.x Nov 9, 2026
18 checks passed
@Spomky
Spomky deleted the fix/sd-jwt-disclosure-factory-bounds branch November 9, 2026 07:00
Spomky added a commit to Spomky-Labs/docs that referenced this pull request Nov 9, 2026
…s created

Disclosure::forObjectProperty(), Disclosure::forArrayElement() and the
Issuer now refuse a value whose Disclosure Disclosure::fromString()
would reject: more than 510 levels of arrays and objects in the value,
or more than 4096 JSON values in the Disclosure
(web-token/jwt-framework#810). The page gives both bounds and the
longest lists of integers each factory accepts, and the list of what
the Issuer refuses points to them.
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.

SD-JWT: Disclosure creates Disclosures that Disclosure::fromString() rejects

1 participant