Repository navigation
fix(sd-jwt): refuse the unparsable unprotected headers as the other claims - #815
Merged
Merged
Conversation
…laims SDJWTIssuer::issueJson() checks its SD-JWT on a placeholder that the Holder and the Verifier would parse. SDJWT::create() rejected the placeholder with an InvalidSerializationException when an unprotected header was nested too deeply, held more than 4096 JSON values or was a list, and that exception escaped, while the Issuer refuses everything else with an InvalidArgumentException. Near the bound, an unprotected header of 4090 values was refused with a message that blamed the Disclosures, even without any. The JSON of the placeholder is now decoded first, as the Holder and the Verifier decode it, and every failure to parse it is refused with an InvalidArgumentException. In the JWS JSON serialization, the budget message names the unprotected headers with the Disclosures.
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 #809 and #813.
The defect
Before it signs,
SDJWTIssuer::issueJson()checks the SD-JWT on a placeholder that the Holder and the Verifier would parse (#809). The protected headers and the signatures of the placeholder are placeholders, so its only JSON values are its structure, the unprotected headers and the Disclosures.Three kinds of unprotected headers escaped the refusals of the Issuer, because
SDJWT::create()rejected the placeholder with anInvalidSerializationException:issueJson()The JWS JSON serialization is nested too deeply.The JWS JSON serialization holds more JSON values than the 4096 that are decoded.The "header" member must be a JSON object.The Issuer refuses everything else it cannot issue with an
InvalidArgumentException. AnInvalidSerializationExceptionis the exception for a string that is not a token.There was a second defect just below the bound on values. An unprotected header of 4,090 or 4,091 integers passed
SDJWT::create(), but notSDJWT::fromString(), which also reads the "disclosures" parameter. The Issuer refused it withThe Disclosures of the SD-JWT hold more JSON values than the 4096 the Holder and the Verifier decode., although the SD-JWT had no Disclosure.The fix
placeholderJsonSdJwt()decodes the JSON of the placeholder with a newBoundedJsonDecoderbefore parsing it, asDisclosure::serialize()does for a Disclosure (fix(sd-jwt): create only the Disclosures that can be parsed #810).json_encode()wrote that JSON, so the decoder can only reject it for the values it holds or for its depth. The Issuer says which:The Disclosures and the unprotected headers of the SD-JWT hold more JSON values than the 4096 the Holder and the Verifier decode.The unprotected headers of the SD-JWT are nested more deeply than the Holder and the Verifier decode.SDJWT::create()rejects the placeholder for another reason, such as a list as unprotected header, the Issuer throws anInvalidArgumentException. It keeps the message and chains the rejection.checkBounds()therefore gives the budget message that names them both. The compact serialization keepsThe Disclosures of the SD-JWT hold more JSON values than the 4096 the Holder and the Verifier decode.An unprotected header nested even more deeply, from 511 levels in the flattened serialization, is still refused by
json_encode()withInvalid content.. That is anInvalidArgumentException, as for a claim nested too deeply.Backward compatibility
The SD-JWT component has not been released yet.
API. Unchanged: three private constants are added, and the private methods
placeholderJsonSdJwt()andcheckBounds()change.Behaviour. A probe ran the same 431 cases on 4.3.x (da615c9) and on this branch. In the base run, the 4.3.x version of
SDJWTIssuerwas loaded before Composer's autoloader. The cases are:Each SD-JWT that was issued was parsed back. The results:
InvalidSerializationExceptiontoInvalidArgumentException: 72 for the values and 12 for the depth, with the messages above, and 12 for a list, with the same message as before.Verification
IssuerBoundsTest:anUnprotectedHeaderTheHolderAndTheVerifierRejectIsRefused(7 cases): a list, a header nested too deeply and a header with too many values, in the flattened and the general serializations, and a header of 4,090 integers. The test checks the exact class, becauseInvalidSerializationExceptionextendsInvalidArgumentException;anSdJwtWithUnprotectedHeadersIsIssuedExactlyWhenItCanBeParsed(2 cases): unprotected headers from 505 to 512 levels and from 4,080 to 4,100 integers, in both JSON serializations. The Issuer must issue an SD-JWT exactly when it can be parsed back, and refuse the others with anInvalidArgumentException;disclosuresBeyondTheBudgetAreRefused: in the JWS JSON serialization, the test now expects the message that names the unprotected headers.disclosuresBeyondTheBudgetAreRefusedpasses, since that case does not change. With this PR, all 11 pass.phpunit-11inphpqa:8.4: 2,854 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, all 13 time out. The tests that cover these lines includeaJsonSdJwtIsIssuedExactlyWhenItCanBeParsed(SD-JWT: refuse at issuance the SD-JWTs the Holder and the Verifier reject #809), which takes 4 to 5 seconds per data set here and runs before the new tests. The CI job sets no minimum MSI.