Skip to content

fix(serializers): chain the bound a token goes beyond in the serializer managers - #817

Merged
Spomky merged 1 commit into
4.3.xfrom
fix/jose-serializer-managers-bound-reason
Nov 9, 2026
Merged

Spomky merged 1 commit into
4.3.xfrom
fix/jose-serializer-managers-bound-reason

Conversation

@Spomky

@Spomky Spomky commented Nov 9, 2026

Copy link
Copy Markdown
Member

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

The defect

When no serializer can convert a token, JWSSerializerManager::unserializeToken() and JWESerializerManager::unserializeToken() throw Unsupported input.. They chain the exception of the last serializer they tried, "so that the actual reason of the failure remains available to the caller". The loaders chain that exception in turn: Unable to load and verify the token. → Unsupported input. → the exception of a serializer.

Only the serializer of the format of the token decodes it far enough to reach a bound of the JSON decoder. So whenever another serializer came after that one, the bound was lost from the chain:

  • Compact token whose protected header goes beyond a bound. The JSON serializers, tried after the compact one, reject it as invalid JSON, and the chain says Unsupported input..
  • Flattened token whose protected header goes beyond a bound. The general serializer, tried after the flattened one, rejects it for its missing members.
  • Any JSON token beyond a bound, with the compact serializer registered last. The chain says Unsupported input.

#816 said that a JSON token kept the reason in the chain. That holds only for a bound in the JSON document itself, which both JSON serializers decode first, and only when the compact serializer is not registered last.

The probe below measured it: in the 243 manager cases where the serializer of the format of the token hit a bound, the chain kept that bound in only 122. The result depended on the order of the serializers and on where the bound was hit.

The fix

  • BoundedJsonDecoder now throws a new exception for its two bounds: Jose\Component\Core\Exception\JsonBoundExceededException, which extends Jose\Component\Core\Exception\InvalidArgumentException. The messages, codes and chained exceptions are those of fix(serializers): name the bound a token goes beyond when it is decoded #816. Invalid JSON keeps the exception of JsonConverter::decode().
  • The managers now chain the last exception that goes back to such an exception, either directly or through the exceptions it chains (the compact serializers wrap it in their InvalidSerializationException). When there is none, they chain the exception of the last serializer, as before.
  • The exception that the managers throw is unchanged: InvalidSerializationException, Unsupported input., code 0.

Backward compatibility

  • API. One new class, JsonBoundExceededException, registered in JoseExceptionTest with its SPL parent, InvalidArgumentException. No signature changes.
  • Exception classes. The JSON serializers let the exception of the decoder through. Their two bound refusals are therefore now JsonBoundExceededExceptions instead of Jose\Component\Core\Exception\InvalidArgumentExceptions. Because it is a subclass, a catch of InvalidArgumentException, Jose or SPL, still catches them. The messages, codes and chains are unchanged, and every other exception keeps its class.
  • Managers. The exception they throw is unchanged. The exception they chain changes only when a serializer hit a bound and another serializer came after it.
  • Behaviour. A probe ran the same 819 cases on 4.3.x (80bbda4) and on this branch. In the base run, the 4.3.x versions of BoundedJsonDecoder and of the two managers were loaded before Composer's autoloader. The cases are the 277 cases of fix(serializers): name the bound a token goes beyond when it is decoded #816 (the six serializers, the SD-JWT parser, JWK and JWKSet), plus every JWS and JWE token of that corpus through the two managers, with the serializers in three orders.
    • 489 cases are identical: the 322 accepted inputs and 167 refusals.
    • 69 refusals of the JSON serializers change their class only, to JsonBoundExceededException.
    • 140 refusals change only in their chain: they keep their class, message and code, and only the class of the bound exception in their chain changes. They are refusals of the compact serializers, of the SD-JWT parser, and of the managers that already chained the bound.
    • 121 manager refusals now chain the exception of the bound. They keep their own exception. The chain held Unsupported input in 69 of them and Unsupported input. in 52.
    • There is no other difference. In all 243 manager cases where the serializer of the format of the token hit a bound, the chain now holds that bound (122 on 4.3.x).

Verification

  • New tests (17 cases):
    • BoundedJsonDecoderTest:
      • aDocumentBeyondABoundIsRejectedWithAJsonBoundExceededException (2 cases);
      • anInvalidDocumentKeepsTheErrorOfTheJsonConverter now also checks that invalid JSON is not such an exception;
    • Signature\JsonValueBoundTest and Encryption\JsonValueBoundTest: theSerializerManagerChainsTheExceptionOfTheBound (7 JWS and 7 JWE cases). The cases are:
      • compact tokens beyond each bound, with the compact serializer registered first and last;
      • flattened tokens whose protected header goes beyond each bound;
      • a JSON document beyond the values, with the compact serializer registered last;
      • a string that is not a token, which keeps the exception of the last serializer.
  • On 4.3.x, 13 of these 17 cases fail: 3 errors, because the class does not exist, and 10 failures, where the manager chains Unsupported input or Unsupported input.. The 4 others pass before and after: the two cases where the compact serializer is registered last, and the two strings that are not a token.
  • Full suite with phpunit-11 in phpqa:8.4: 2,889 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: 29 mutants, all killed with a 60-second timeout. Locally, with the 3-second timeout of infection.json.dist, 17 of them time out. Infection counts them as detected. The first run left 4 mutants of the code 0 of the exceptions of the managers alive, so the tests now check that code.

Not covered

  • Other reasons for rejecting a compact token. Take a protected header that is not a JSON object (Bad protected header.). That reason is still lost when a JSON serializer comes after the compact one, because nothing marks it apart from the rejection of a token in another format.
  • JsonConverter::decode(). It is unchanged, and not bounded.

…er managers

When no serializer could convert a token, JWSSerializerManager and
JWESerializerManager chained the exception of the last serializer they
tried. Only the serializer of the format of the token decodes it far
enough to reach a bound of the JSON decoder, so the reason was lost
whenever another serializer came after that one. A compact token was
then rejected as invalid JSON by a JSON serializer, and a flattened
token whose protected header went beyond a bound was rejected by the
general serializer for its missing members.

The decoder now throws a JsonBoundExceededException, a subclass of
InvalidArgumentException, for its two bounds. The managers chain the
last exception that goes back to one, and the exception of the last
serializer otherwise. Their own exception is 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 69e9709 into 4.3.x Nov 9, 2026
18 checks passed
@Spomky
Spomky deleted the fix/jose-serializer-managers-bound-reason branch November 9, 2026 09:03
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