Skip to content

fix(json): bound the JSON documents decoded outside of a token - #819

Merged
Spomky merged 1 commit into
4.3.xfrom
fix/json-decoding-bounds
Nov 10, 2026
Merged

Spomky merged 1 commit into
4.3.xfrom
fix/json-decoding-bounds

Conversation

@Spomky

@Spomky Spomky commented Nov 9, 2026

Copy link
Copy Markdown
Member

Follow-up of #816 and #817, whose "Not covered" sections listed this.

The defect

  • JsonConverter::decode() counts no value. A document of a few megabytes full of colliding member names costs seconds of CPU, and a document of nested brackets decodes to a hundred times its size. That was the case of the tokens before GHSA-2xq2, which BoundedJsonDecoder fixed for them only. JsonConverter::decode() decodes:
    • the key sets that JKUFactory and X5UFactory fetch from a URL, with no limit on their size;
    • the payload of the JWTs of an SD-JWT;
    • the input of the console commands.
  • Its message was Unsupported input. whatever the error: syntax, malformed UTF-8 or depth.
  • JWK::createFromJson(), JWKSet::createFromJson() and JWKFactory::fromJsonObject() call json_decode() directly. They have no bound either, and let its JsonException through. Its trace records the JSON among the arguments of json_decode() and of the method, that is, the private key whenever the document is malformed.

The fix

The bound

JsonConverter::decode(), JWK::createFromJson(), JWKSet::createFromJson() and JWKFactory::fromJsonObject() read a second argument. As in #800, it is read with func_num_args()/func_get_arg(1) until 5.0.0 declares it. The values are counted as BoundedJsonDecoder counts them, before decoding.

Second argument A document of more than 4096 values
none, or null decoded as before, with a deprecation: 5.0.0 refuses it by default
true decoded, without notice
false refused with a JsonBoundExceededException (#817)
anything else InvalidArgumentException

The deprecation says how to choose:

Decoding a JSON document of more than 4096 values is deprecated and "Jose\Component\Core\Util\JsonConverter::decode()" will refuse it by default in 5.0.0. Pass true as the second argument to keep decoding it, or false to refuse it now.

JKUFactory and X5UFactory read the same policy as a third constructor argument, after the redirect policy of #800. The bundle gets a jose.jku_factory.allow_large_key_sets option, which is passed to both factories only when it is set. When only that option is set, the redirect argument is null, which keeps the default. Their deprecation names the factory and the option:

Loading a key set of more than 4096 JSON values is deprecated and "Jose\Component\KeyManagement\JKUFactory" will refuse it by default in 5.0.0. Pass true as the third argument of the constructor to keep loading it, or false to refuse it now. With the bundle, set the "jose.jku_factory.allow_large_key_sets" option.

UrlKeySetFactory, which is deprecated, decodes nothing itself: its subclasses decode the key sets they fetch, so the third argument does not apply to it.

The callers of this library

  • Console commands (13 calls) and the jwk/jwkset environment variable processor of the bundle pass true: they decode the operator's own documents.
  • BoundedJsonDecoder passes true: it has counted the values already.
  • SD-JWT:
    • The Verifier bounds the payload of the Key Binding JWT (false). The Holder signs it, and it holds a few claims.
    • The payload of the Issuer-signed JWT is not bounded (true), by the Holder or by the Verifier. It holds a digest for each Disclosure: the 2,048 Disclosures that SD-JWT: refuse at issuance the SD-JWTs the Holder and the Verifier reject #809 issues and parses already make it more than 4096 values. It is signed by the Issuer, and the Verifier decodes it after verifying that signature.

The messages

JsonConverter::decode() now names the error of json_decode(), and keeps its code and the chained copy of its JsonException:

Error Before Now
invalid JSON (syntax, malformed UTF-8, control character…) InvalidArgumentException: Unsupported input. InvalidArgumentException: The JSON document is not valid: Syntax error. (the message of json_decode())
nested too deeply InvalidArgumentException: Unsupported input. JsonBoundExceededException, a subclass: The JSON document is nested too deeply.

The tokens keep their messages. BoundedJsonDecoder still answers Unsupported input. for invalid JSON, so the serializers and the SD-JWT parser do not change.

The three key factories keep throwing a JsonException, with the same message and code. It is now a copy created in the method, whose $json parameter is #[SensitiveParameter], so the document no longer appears in its trace.

Backward compatibility

  • API. No signature changes. The second (or third) arguments are undeclared, and PHPStan treats the methods that read func_get_arg() as variadic. BoundedJsonDecoder, which is @internal, gains checkDocument() and exceedsMaxValues(). The bundle gets one optional option. The PHPStan baseline counts of the configuration tree of JKUSource are raised, as feat(key-mgmt): let JKUFactory and X5UFactory refuse HTTP redirects #800 did for follow_redirects.

  • Behaviour. A probe ran the same 253 cases on 4.3.x (e39db7e) and on this branch. In the base run, the 4.3.x versions of the twelve changed classes were loaded before Composer's autoloader: JsonConverter, BoundedJsonDecoder, JWK, JWKSet, JWKFactory, JKUFactory, X5UFactory, UrlKeySetFactory and its trait, SDJWT, SDJWTVerifier and SDJWTIssuer. The cases cover:

    • documents from a few values to 20,000, 511 to 600 levels deep, invalid JSON of several kinds, and keys holding a secret;
    • every second (or third) argument;
    • JKU and X5U key sets that are small, large, invalid and deep, under six policies;
    • JWS serializers and manager, Disclosures;
    • SD-JWTs with small and large payloads and Key Binding JWTs.

    The results:

    • 122 cases are identical, among them every token, every SD-JWT except the two below, and every accepted document within the bound.
    • 16 cases keep their outcome and gain one deprecation: documents of more than 4096 values decoded without a second argument.
    • 42 refusals name the error in their message. The 29 refusals for invalid JSON keep their class and code. The 13 depth refusals keep their code and become JsonBoundExceededExceptions. They include the JKU and X5U key sets that are not valid JSON (two tests of UrlKeySetFactoryTest assert the new message).
    • 16 refusals throw the same JsonException, with the same message and code, but no longer record the secret of the key in their trace.
    • 55 cases pass the new argument: false refuses beyond the bound (11), and a value that is not a boolean is refused (44). Before, the extra argument was ignored. As for feat(key-mgmt): let JKUFactory and X5UFactory refuse HTTP redirects #800, refusing a value that is not a boolean was the only choice left open.
    • 2 SD-JWT presentations whose Key Binding JWT holds more than 4096 values are now refused. The SD-JWT component has not been released yet.
    • There is no other difference.

Verification

  • New and updated tests (87 cases):
    • JsonConverterTest: the named errors and their codes and chained exception, the depth, and the second argument (4096 and 4097 values, with no argument, null, true, false, and invalid values);
    • KeyJsonDocumentTest (JWK, JWKSet) and LargeJsonDocumentTest:
      • the same matrix for the three key factories;
      • the JsonException kept, without the key in its trace;
      • the JKU and X5U policies, for large and small key sets;
    • JKUFactoryLargeKeySetsTest (bundle): the option, true, false and not set, and the redirect policy kept in its place;
    • LargeJsonInputTest (console): eight commands decode a key of more than 4096 values, without deprecation;
    • the depth of the three key factories, 511 levels accepted and 512 refused with the JsonException of json_decode(), and the deprecated JWKFactory::createFromJsonObject(), which passes the second argument on;
    • SDJWTVerifierTest::aKeyBindingJwtOfMoreThan4096ValuesIsRejected;
    • JsonValueBoundTest::anIssuerSignedJwtOfMoreThan4096ValuesIsProcessedAndVerified and anObjectOfMoreThan4096ValuesIsIssuedInClear, guards for the decision above;
    • UrlKeySetFactoryTest: the two tests of a key set that is not JSON expect the named error.
  • On 4.3.x, 42 of these cases fail and 45 pass. The 45 that pass are the guards: documents within the bound, true beyond it, the depths, small key sets, the console commands, and the payloads that the Issuer signs.
  • Full suite with phpunit-11 in phpqa:8.4: 3,043 tests, no failure. The 4 skipped and 2 incomplete tests are the PKCS#12 and ML-DSA platform tests.
  • ECS, Rector, PHPStan (baseline raised as described above) and Deptrac pass with phpqa:8.4. The changed files pass php -l with PHP 8.2.
  • Mutations on the diff: 110 mutants, all detected with a 60-second timeout (88 killed, 22 timed out). Locally, with the 3-second timeout of infection.json.dist, 66 of them time out. Infection counts them as detected. A first run left 19 mutants alive; the tests above were added to kill them, among them those of the console commands, the depths and the deprecated static factory.
  • This branch merges without conflict with 4.3.x and with fix(serializers): chain the reason of the serializer a token belongs to #818, which also touches BoundedJsonDecoder.

Not covered

  • 5.0.0: declare the argument with false as default for these methods, and the third argument of the factories. That belongs to the 5.0.x branch.
  • Documentation: the allow_large_key_sets option and the deprecations are not documented yet.

JsonConverter::decode() counted no value: a document of a few megabytes
full of colliding member names costs seconds of CPU, as for the tokens
before GHSA-2xq2. It decodes the key sets that JKUFactory and
X5UFactory fetch from a URL, without any limit on their size. Its
message was "Unsupported input." whatever the error. JWK::createFromJson(),
JWKSet::createFromJson() and JWKFactory::fromJsonObject() called
json_decode() directly, without bound either, and their JsonException
recorded the JSON of the key among the arguments of its trace.

These four methods now read a second argument, with func_get_arg(1)
until 5.0.0 declares it: true decodes a document of any number of
values, false refuses one of more than 4096 with a
JsonBoundExceededException, and without it, or with null, such a
document is decoded with a deprecation, as 5.0.0 refuses it by default.
JKUFactory and X5UFactory read the same policy as a third constructor
argument, and the bundle gets a "jose.jku_factory.allow_large_key_sets"
option.

JsonConverter::decode() names the error of json_decode(), and refuses a
document nested too deeply with a JsonBoundExceededException; the codes
and the chained JsonException are kept. The three key factories keep
their JsonException, thrown again as a copy without the key in its
trace. The tokens keep "Unsupported input.".

The console commands and the environment variable processor decode the
operator's own documents without bound. The SD-JWT Verifier bounds the
payload of the Key Binding JWT, signed by the Holder; that of the
Issuer-signed JWT, signed by the Issuer and holding a digest for each
Disclosure, is decoded without bound.
@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 41c097d into 4.3.x Nov 10, 2026
18 checks passed
@Spomky
Spomky deleted the fix/json-decoding-bounds branch November 10, 2026 15:44
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