Repository navigation
fix(sd-jwt): tell the decoding bounds apart from invalid JSON - #813
Merged
Merged
Conversation
Disclosure::fromString() and SDJWT::fromString() reported every Disclosure that the BoundedJsonDecoder rejected as "not valid JSON", and SDJWT::fromString() and SDJWT::create() reported every JWS JSON serialization it rejected as "must be a JSON object", even when the JSON was valid but held more than 4096 values or was nested too deeply. As the Disclosures of an SD-JWT share the budget, the Disclosure blamed was the one on which the budget ran out, however small. BoundedJsonDecoder::hasExceededMaxValues() tells whether the budget was exceeded. A Disclosure or a JWS JSON serialization rejected for its values or for its depth now says so, and an SD-JWT whose Disclosures go beyond the budget is rejected as a whole. The exception classes and codes are unchanged, and invalid JSON keeps its message.
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 #810, whose "Not covered" section listed this.
The defect
When the
BoundedJsonDecoderrejected a document, the SD-JWT parser always gave the message of invalid JSON:Disclosure::parse(), used byDisclosure::fromString()andSDJWT::fromString(), saidThe Disclosure is not valid JSON.;SDJWT::readJson(), used bySDJWT::fromString()andSDJWT::create(), saidThe JWS JSON serialization must be a JSON object..Three causes ended in these messages: invalid JSON, more than 4096 JSON values (the decoder counts them before decoding), and a document nested too deeply for
json_decode(). In the last two, the JSON is valid:["salt", [1, …, 4095]]written by another library, whichjson_decode()reads, was "not valid JSON";["s", 1], the 2,049th, a few bytes long, was "not valid JSON".The fix
BoundedJsonDecoder::hasExceededMaxValues()tells whether the documents given to the decoder went beyondMAX_VALUES. The class is@internalanddecode()is unchanged, so the JWS and JWE serializers that use it behave as before.Disclosure::parse()andSDJWT::readJson()name the cause. The depth is read from the exception code:JsonConverter::decode()gives its exception the code of the JSON error, hereJSON_ERROR_DEPTH.SDJWT::fromString()rejects an SD-JWT whose Disclosures go beyond the budget as a whole, whichever Disclosure goes beyond it, and chains the exception of that Disclosure.Disclosure::fromString()SDJWT::fromString()SDJWT::fromString()andSDJWT::create()With this Disclosure, there are more than 4096 JSON values to decode.The SD-JWT holds more JSON values than the 4096 that are decoded.The JWS JSON serialization holds more JSON values than the 4096 that are decoded.The Disclosure is nested too deeply.The Disclosure is nested too deeply.The JWS JSON serialization is nested too deeply.The Disclosure is not valid JSON.(unchanged)The Disclosure is not valid JSON.(unchanged)The JWS JSON serialization must be a JSON object.(unchanged)Disclosure::parse()for the budget holds wherever it is read: alone, the Disclosure holds more than 4096 values; in an SD-JWT, it takes the values decoded so far beyond 4096.Backward compatibility
The SD-JWT component has not been released yet: no tag contains
src/SelectiveDisclosure.API. By reflection, the public methods (parameters, types and defaults), the constants and the modifiers of
DisclosureandSDJWTare identical.BoundedJsonDecoder, which is@internal, gainshasExceededMaxValues(): bool.web-token/jwt-selective-disclosurerequiresweb-token/jwt-library: ^4.3, so the method is always there.Behaviour. A probe ran the same 155 cases on 4.3.x (85d5df0) and on this branch. In the base run, the 4.3.x versions of
BoundedJsonDecoder,DisclosureandSDJWTwere loaded by an autoloader placed before Composer's. The cases cover:SDJWT::create(), the factories, the Issuer in both serializations, and the JWS and JWE serializers.The results:
0). In each case, the chain of the 4.3.x exception shows the bound that was hit:Unsupported input.alone for the 36 budget cases, followed byJsonException(1): Maximum stack depth exceededfor the 21 depth cases. The new message names that bound.SDJWTIssuer::checkBounds()(SD-JWT: refuse at issuance the SD-JWTs the Holder and the Verifier reject #809). Only the exception it chains changes, fromThe Disclosure is not valid JSON.to the SD-JWT message.Verification
BoundedJsonDecoderTest:hasExceededMaxValues()is false at the bound (4096 values) and true once a document is rejected for its values. It stays false whenjson_decode()rejects a document for its syntax or its depth;DisclosureTest::aDisclosureBeyondTheBoundsOfTheDecoderIsRejected: a Disclosure of 4,097 values, and one of 512 levels;JsonValueBoundTest:theDisclosuresOfAnSdJwtShareTheBoundnow checks the message, in both serializations;SDJWT::fromString()andSDJWT::create().The Disclosure is not valid JSON.orThe JWS JSON serialization must be a JSON object.. With this PR, they all pass. The existing tests check that invalid JSON keeps its message (DisclosureTest"not JSON",JsonSerializationTest"invalid JSON"); they pass before and after.phpunit-11inphpqa:8.4: 2,840 tests, no failure. The 4 skipped and 2 incomplete tests are the PKCS#12 and ML-DSA platform tests.phpqa:8.4.infection.json.dist, 20 of them time out. Infection counts them as detected, and the MSI stays at 100%.Not covered
Unsupported inputfor all three causes. Their messages are unchanged.The value is nested too deeply or holds too many JSON values: its Disclosure could not be parsed.). They could now tell the two bounds apart as well.SDJWTIssuer::issueJson()fails on theInvalidSerializationExceptionofSDJWT::create(), while its other refusals areInvalidArgumentExceptions. This is not new; with this PR, the message isThe JWS JSON serialization is nested too deeply.instead ofmust be a JSON object.