Skip to content

fix(serializers): name the bound a token goes beyond when it is decoded - #816

Merged
Spomky merged 1 commit into
4.3.xfrom
fix/jose-serializers-decoding-messages
Nov 9, 2026
Merged

Spomky merged 1 commit into
4.3.xfrom
fix/jose-serializers-decoding-messages

Conversation

@Spomky

@Spomky Spomky commented Nov 9, 2026

Copy link
Copy Markdown
Member

Follow-up of #813, which named these bounds for the SD-JWT.

The defect

The JWS and JWE serializers decode their JSON with the BoundedJsonDecoder. Its decoder rejects a token in two cases: when the JSON documents of the token hold more than 4096 values, and when json_decode() rejects a document nested too deeply. It also rejects invalid JSON. Whatever the cause, the serializers answered with the same message:

Serializers Invalid JSON More than 4096 JSON values Nested too deeply
JSONFlattenedSerializer and JSONGeneralSerializer (JWS and JWE) Unsupported input. Unsupported input. Unsupported input.
CompactSerializer (JWS and JWE) Unsupported input Unsupported input Unsupported input

The JSON serializers let the exception of the decoder through, so their messages are those of BoundedJsonDecoder and JsonConverter::decode(). The compact serializers replace any exception with Unsupported input, and keep its code.

The fix

  • BoundedJsonDecoder::decode() now names the two bounds:

    • The JSON documents of the token hold more JSON values than the 4096 that are decoded., with code 0 and no previous exception, as before;
    • A JSON document of the token is nested too deeply., with the code JSON_ERROR_DEPTH and the chained JsonException of json_decode(), as before.

    For the other errors of json_decode(), the decoder throws the exception of JsonConverter::decode(), Unsupported input., unchanged. The new method hasExceededMaxDepth() tells whether a document was rejected for its depth, as hasExceededMaxValues() (fix(sd-jwt): tell the decoding bounds apart from invalid JSON #813) does for the values.

  • The JSON serializers do not change: they still let the exception of the decoder through.

  • The compact serializers keep the message of the decoder when it rejected the protected header for one of the two bounds. Otherwise, they still answer Unsupported input.

Serializers Invalid JSON More than 4096 JSON values Nested too deeply
JSON (JWS and JWE) Unsupported input. (unchanged) The JSON documents of the token hold more JSON values than the 4096 that are decoded. A JSON document of the token is nested too deeply.
Compact (JWS and JWE) Unsupported input (unchanged) the same message the same message

Backward compatibility

These serializers have been released. Only the messages of the two bound refusals change, which is the purpose of this PR.

  • Classes and codes. Unchanged. The JSON serializers throw the InvalidArgumentException of the decoder, and the compact ones an InvalidSerializationException. The code is 0 for the values and JSON_ERROR_DEPTH (1) for the depth, as before.

  • Chained exceptions. Same shape: a depth refusal still chains the JsonException of json_decode().

  • API. BoundedJsonDecoder, which is @internal, gains hasExceededMaxDepth(): bool. Nothing else changes, and JsonConverter is not touched.

  • Behaviour. A probe ran the same 277 cases on 4.3.x (da615c9) and on this branch. In the base run, the 4.3.x versions of BoundedJsonDecoder and of the two compact serializers were loaded before Composer's autoloader. The cases cover:

    • the six serializers, with protected headers, unprotected headers and documents of 100 to 100,000 values and of 505 to 5,000 levels;
    • invalid JSON, strings full of JSON syntax, malformed parts and missing members;
    • the two serializer managers;
    • Disclosure::fromString() and SDJWT::fromString();
    • JWK::createFromJson() and JWKSet::createFromJson().

    The results:

    • 164 cases are identical: the 95 accepted inputs, the 34 refusals for invalid JSON and the 35 refusals for their structure. They include every JWK and JWKSet case.
    • 81 refusals change their message, and nothing else (same class, same code): 41 for their values and 40 for their depth. In each case, the chain of the 4.3.x exception shows that bound (JsonException(1): Maximum stack depth exceeded for the depth).
    • 32 refusals keep their message; only the exception they chain changes. They are those of the serializer managers (Unsupported input.) and of the SD-JWT parser, which gives its own messages (fix(sd-jwt): tell the decoding bounds apart from invalid JSON #813).
    • There is no other difference.

Verification

  • Tests: 31 cases fail on 4.3.x, and all of them pass here.
    • BoundedJsonDecoderTest:

      • the four tests of the values expect the new message;
      • aDocumentNestedTooDeeplyIsRejectedForItsDepth checks the message, the code, the chained JsonException and hasExceededMaxDepth();
      • anInvalidDocumentKeepsTheErrorOfTheJsonConverter checks that invalid JSON keeps Unsupported input. and sets neither flag.

      On 4.3.x: 3 errors, because the method does not exist, and 4 failures.

    • Signature\JsonValueBoundTest and Encryption\JsonValueBoundTest:

      • the tests of the values now expect the new message: 8 JWS cases and 6 JWE cases;
      • aTokenNestedTooDeeplyIsRejected (5 JWS and 5 JWE cases) covers the protected and unprotected headers, in the compact, flattened and general serializations;
      • aTokenWhoseJsonIsInvalidIsAnUnsupportedInput (3 JWS and 3 JWE cases) checks that invalid JSON keeps exactly Unsupported input or Unsupported input.. These 6 cases pass before and after.
  • Full suite with phpunit-11 in phpqa:8.4: 2,863 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: 23 mutants, all killed with a 60-second timeout. Locally, with the 3-second timeout of infection.json.dist, 5 of them time out. Infection counts them as detected.
  • This branch merges without conflict with fix(sd-jwt): name the bound a Disclosure factory hits #814 and fix(sd-jwt): refuse the unparsable unprotected headers as the other claims #815, which touch other files.

Not covered

  • The serializer managers. JWSSerializerManager and JWESerializerManager, which the loaders use, still throw Unsupported input. and chain the exception of the last serializer they tried.

    • For a JSON token beyond a bound, the last serializer is a JSON serializer that hit the bound, so the reason is in the chain.
    • For a compact token, the last serializer is a JSON serializer that rejected the token as invalid JSON, so the reason given by the compact serializer is lost.

    To fix this, the managers would need to know which serializer recognized the token, for instance from a mark on the exceptions of the bounds.

  • JsonConverter::decode(). It decodes the JWKs, the key sets and the claims. It is not bounded, and keeps Unsupported input. for every error.

  • The documentation. It does not quote these messages.

The JWS and JWE serializers rejected every token whose JSON the
BoundedJsonDecoder refused with "Unsupported input": invalid JSON, more
than 4096 values, or a document nested too deeply. The decoder now
names the bound, with "The JSON documents of the token hold more JSON
values than the 4096 that are decoded." or "A JSON document of the token
is nested too deeply.", and BoundedJsonDecoder::hasExceededMaxDepth()
tells the second case apart.

The JSON serializers let the exception of the decoder through, as
before. The compact serializers now keep its message for these two
bounds. Invalid JSON keeps "Unsupported input", and the exception
classes and codes are unchanged.
@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 80bbda4 into 4.3.x Nov 9, 2026
18 checks passed
@Spomky
Spomky deleted the fix/jose-serializers-decoding-messages branch November 9, 2026 08:23
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