Report malformed JSON keyset fields as IOException instead of an uncaught exception#70
Open
adilburaksen wants to merge 1 commit into
Conversation
…ught exception JsonKeysetReader.read()/readEncrypted() declare `throws IOException`, and the public TinkJsonProtoKeysetFormat.parseKeyset* helpers wrap that into `throws GeneralSecurityException`. When a JSON keyset sets a string-typed field (e.g. "status", "typeUrl", "keyMaterialType", "value", "encryptedKeyset") to a JSON object or array, gson's JsonElement.getAsString() throws UnsupportedOperationException, a RuntimeException not covered by the existing `catch (JsonParseException | IllegalStateException)`. It then propagates uncaught, past the documented IOException / GeneralSecurityException contract, so a caller parsing an untrusted (e.g. public) keyset that only handles the declared exceptions crashes. Add UnsupportedOperationException to the catch clauses so malformed input is reported as IOException, consistent with other parse errors.
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
JsonKeysetReader.read()andreadEncrypted()declarethrows IOException, and the publicTinkJsonProtoKeysetFormat.parseKeyset*helpers wrap that intothrows GeneralSecurityException. Theirtryblocks catchJsonParseException | IllegalStateExceptionand rethrow asIOException.However, when a JSON keyset sets a string-typed field — e.g.
status,typeUrl,keyMaterialType,value,encryptedKeyset— to a JSON object or array, gson'sJsonElement.getAsString()throwsUnsupportedOperationException. That is aRuntimeExceptionand is not covered by the existing catch, so it propagates uncaught, past the documentedIOException/GeneralSecurityExceptioncontract.A caller that parses an untrusted keyset (for example a public keyset via
parseKeysetWithoutSecret) and handles only the declared exceptions will therefore see an unexpectedUnsupportedOperationExceptioninstead of a clean parse error.Minimal reproducer (the field is a
{}instead of a string):{"primaryKeyId":1,"key":[{"keyData":{"typeUrl":{},"value":"","keyMaterialType":"SYMMETRIC"},"status":"ENABLED","keyId":1,"outputPrefixType":"TINK"}]}getAsString()on the object-valuedtypeUrlthrowsUnsupportedOperationException. (The existingtestRead_outputPrefixTypeIsNotAStringtest uses a number, which gson coerces to a string, so the object/array case is not currently exercised.)Fix
Add
UnsupportedOperationExceptionto the catch clauses inread()andreadEncrypted()so malformed input is reported asIOException, consistent with the other parse errors.Happy to add a test covering object/array-valued string fields if preferred.