Repository navigation
fix(serializers): name the bound a token goes beyond when it is decoded - #816
Merged
Merged
Conversation
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.
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 #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 whenjson_decode()rejects a document nested too deeply. It also rejects invalid JSON. Whatever the cause, the serializers answered with the same message:JSONFlattenedSerializerandJSONGeneralSerializer(JWS and JWE)Unsupported input.Unsupported input.Unsupported input.CompactSerializer(JWS and JWE)Unsupported inputUnsupported inputUnsupported inputThe JSON serializers let the exception of the decoder through, so their messages are those of
BoundedJsonDecoderandJsonConverter::decode(). The compact serializers replace any exception withUnsupported 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 codeJSON_ERROR_DEPTHand the chainedJsonExceptionofjson_decode(), as before.For the other errors of
json_decode(), the decoder throws the exception ofJsonConverter::decode(),Unsupported input., unchanged. The new methodhasExceededMaxDepth()tells whether a document was rejected for its depth, ashasExceededMaxValues()(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.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.Unsupported input(unchanged)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
InvalidArgumentExceptionof the decoder, and the compact ones anInvalidSerializationException. The code is 0 for the values andJSON_ERROR_DEPTH(1) for the depth, as before.Chained exceptions. Same shape: a depth refusal still chains the
JsonExceptionofjson_decode().API.
BoundedJsonDecoder, which is@internal, gainshasExceededMaxDepth(): bool. Nothing else changes, andJsonConverteris 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
BoundedJsonDecoderand of the two compact serializers were loaded before Composer's autoloader. The cases cover:Disclosure::fromString()andSDJWT::fromString();JWK::createFromJson()andJWKSet::createFromJson().The results:
JsonException(1): Maximum stack depth exceededfor the depth).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).Verification
BoundedJsonDecoderTest:aDocumentNestedTooDeeplyIsRejectedForItsDepthchecks the message, the code, the chainedJsonExceptionandhasExceededMaxDepth();anInvalidDocumentKeepsTheErrorOfTheJsonConverterchecks that invalid JSON keepsUnsupported input.and sets neither flag.On 4.3.x: 3 errors, because the method does not exist, and 4 failures.
Signature\JsonValueBoundTestandEncryption\JsonValueBoundTest: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 exactlyUnsupported inputorUnsupported input.. These 6 cases pass before and after.phpunit-11inphpqa:8.4: 2,863 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, 5 of them time out. Infection counts them as detected.Not covered
The serializer managers.
JWSSerializerManagerandJWESerializerManager, which the loaders use, still throwUnsupported input.and chain the exception of the last serializer they tried.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 keepsUnsupported input.for every error.The documentation. It does not quote these messages.