Repository navigation
fix(serializers): chain the bound a token goes beyond in the serializer managers - #817
Merged
Merged
Conversation
…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.
This was referenced Nov 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up of #816, whose "Not covered" section listed this.
The defect
When no serializer can convert a token,
JWSSerializerManager::unserializeToken()andJWESerializerManager::unserializeToken()throwUnsupported 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:
Unsupported input..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
BoundedJsonDecodernow throws a new exception for its two bounds:Jose\Component\Core\Exception\JsonBoundExceededException, which extendsJose\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 ofJsonConverter::decode().InvalidSerializationException). When there is none, they chain the exception of the last serializer, as before.InvalidSerializationException,Unsupported input., code 0.Backward compatibility
JsonBoundExceededException, registered inJoseExceptionTestwith its SPL parent,InvalidArgumentException. No signature changes.JsonBoundExceededExceptions instead ofJose\Component\Core\Exception\InvalidArgumentExceptions. Because it is a subclass, acatchofInvalidArgumentException, Jose or SPL, still catches them. The messages, codes and chains are unchanged, and every other exception keeps its class.BoundedJsonDecoderand 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.JsonBoundExceededException.Unsupported inputin 69 of them andUnsupported input.in 52.Verification
BoundedJsonDecoderTest:aDocumentBeyondABoundIsRejectedWithAJsonBoundExceededException(2 cases);anInvalidDocumentKeepsTheErrorOfTheJsonConverternow also checks that invalid JSON is not such an exception;Signature\JsonValueBoundTestandEncryption\JsonValueBoundTest:theSerializerManagerChainsTheExceptionOfTheBound(7 JWS and 7 JWE cases). The cases are:Unsupported inputorUnsupported 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.phpunit-11inphpqa:8.4: 2,889 tests, no failure. The 4 skipped and 2 incomplete tests are the PKCS#12 and ML-DSA platform tests.phpqa:8.4. The changed files passphp -lwith PHP 8.2.infection.json.dist, 17 of them time out. Infection counts them as detected. The first run left 4 mutants of the code0of the exceptions of the managers alive, so the tests now check that code.Not covered
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.