Validate the top-level "keys" field in JWK Set import - #76
Open
rishuranjanofficial wants to merge 1 commit into
Open
Validate the top-level "keys" field in JWK Set import#76rishuranjanofficial wants to merge 1 commit into
rishuranjanofficial wants to merge 1 commit into
Conversation
JwkSetConverter.toPublicKeysetHandle and SignatureJwkSetConverter.toPublicKeysetHandle
read jsonKeyset.get("keys") and iterate it without checking that the field exists,
is an array, or that its elements are JSON objects. A malformed JWK Set (missing
"keys", "keys" as a non-array, or a non-object element) throws an uncaught
NullPointerException or IllegalStateException instead of the documented
GeneralSecurityException/IOException.
This mirrors the same defensive has()/isJsonArray()/isJsonObject() pattern already
used elsewhere in both files (e.g. validateKeyOpsIsVerify), and the same bug class
fixed in JsonKeysetReader.java (commit d9552c8).
Author
|
I've submitted this pull request and would appreciate a review when you have some bandwidth. Please let me know if any changes or additional context are needed. Thank you for your time and consideration. |
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.
Problem
JwkSetConverter.toPublicKeysetHandle(String jwkSet) and
SignatureJwkSetConverter.toPublicKeysetHandle(String jwkSet) both wrap only
the initial JSON parse in a try/catch block:
Immediately after, both methods read the top-level "keys" field and
iterate its contents with no further validation, outside of that
try/catch:
A well-formed but unexpected JSON input triggers an uncaught
RuntimeException instead of the documented GeneralSecurityException in
three cases:
null, and calling .getAsJsonArray() on it throws
NullPointerException.
JsonElement.getAsJsonArray() throws IllegalStateException.
{"keys":["not an object"]}): JsonElement.getAsJsonObject() throws
IllegalStateException.
Both methods declare a checked-exception contract (GeneralSecurityException,
plus IOException on the JWT variant for backward compatibility), so any
caller written against that contract will not catch these exceptions.
Root cause
Both files already apply a defensive has() / isJsonArray() / isJsonObject()
check before extracting every other field from untrusted JSON, for
example getStringItem() and validateKeyOpsIsVerify() in both files. The
one place this pattern is missing is the very first thing each method
does with the parsed JSON: reading the top-level "keys" array.
This is the same bug class fixed in JsonKeysetReader.java in commit
d9552c8 ("Report malformed JSON keyset fields as IOException instead of
an uncaught exception"), which documents the identical mechanism for a
different accessor (getAsString() rather than getAsJsonArray() /
getAsJsonObject()). That fix did not extend to these two JWK Set
converters.
Fix
Add the missing validation, mirroring the existing
has()/isJsonArray()/isJsonObject() pattern already used elsewhere in both
files, before the "keys" field is read or iterated in each converter.
Testing
Added three regression tests to each of JwkSetConverterTest.java and
SignatureJwkSetConverterTest.java, covering a missing "keys" field, a
non-array "keys" field, and a non-object element inside "keys". All three
previously threw an uncaught RuntimeException and now correctly throw
GeneralSecurityException.