Repository navigation
fix(json): bound the JSON documents decoded outside of a token - #819
Merged
Merged
Conversation
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.
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 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, whichBoundedJsonDecoderfixed for them only.JsonConverter::decode()decodes:JKUFactoryandX5UFactoryfetch from a URL, with no limit on their size;Unsupported input.whatever the error: syntax, malformed UTF-8 or depth.JWK::createFromJson(),JWKSet::createFromJson()andJWKFactory::fromJsonObject()calljson_decode()directly. They have no bound either, and let itsJsonExceptionthrough. Its trace records the JSON among the arguments ofjson_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()andJWKFactory::fromJsonObject()read a second argument. As in #800, it is read withfunc_num_args()/func_get_arg(1)until 5.0.0 declares it. The values are counted asBoundedJsonDecodercounts them, before decoding.nulltruefalseJsonBoundExceededException(#817)InvalidArgumentExceptionThe deprecation says how to choose:
JKUFactoryandX5UFactoryread the same policy as a third constructor argument, after the redirect policy of #800. The bundle gets ajose.jku_factory.allow_large_key_setsoption, which is passed to both factories only when it is set. When only that option is set, the redirect argument isnull, which keeps the default. Their deprecation names the factory and the 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
jwk/jwksetenvironment variable processor of the bundle passtrue: they decode the operator's own documents.BoundedJsonDecoderpassestrue: it has counted the values already.false). The Holder signs it, and it holds a few claims.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 ofjson_decode(), and keeps its code and the chained copy of itsJsonException:InvalidArgumentException:Unsupported input.InvalidArgumentException:The JSON document is not valid: Syntax error.(the message ofjson_decode())InvalidArgumentException:Unsupported input.JsonBoundExceededException, a subclass:The JSON document is nested too deeply.The tokens keep their messages.
BoundedJsonDecoderstill answersUnsupported 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$jsonparameter 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, gainscheckDocument()andexceedsMaxValues(). The bundle gets one optional option. The PHPStan baseline counts of the configuration tree ofJKUSourceare raised, as feat(key-mgmt): let JKUFactory and X5UFactory refuse HTTP redirects #800 did forfollow_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,UrlKeySetFactoryand its trait,SDJWT,SDJWTVerifierandSDJWTIssuer. The cases cover:The results:
JsonBoundExceededExceptions. They include the JKU and X5U key sets that are not valid JSON (two tests ofUrlKeySetFactoryTestassert the new message).JsonException, with the same message and code, but no longer record the secret of the key in their trace.falserefuses 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.Verification
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) andLargeJsonDocumentTest:JsonExceptionkept, without the key in its trace;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;JsonExceptionofjson_decode(), and the deprecatedJWKFactory::createFromJsonObject(), which passes the second argument on;SDJWTVerifierTest::aKeyBindingJwtOfMoreThan4096ValuesIsRejected;JsonValueBoundTest::anIssuerSignedJwtOfMoreThan4096ValuesIsProcessedAndVerifiedandanObjectOfMoreThan4096ValuesIsIssuedInClear, guards for the decision above;UrlKeySetFactoryTest: the two tests of a key set that is not JSON expect the named error.truebeyond it, the depths, small key sets, the console commands, and the payloads that the Issuer signs.phpunit-11inphpqa:8.4: 3,043 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, 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.BoundedJsonDecoder.Not covered
falseas default for these methods, and the third argument of the factories. That belongs to the 5.0.x branch.allow_large_key_setsoption and the deprecations are not documented yet.