Report malformed JSON keyset fields as IOException instead of an uncaught exception#70
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.
|
@morambro could you take a look? Parsing a malformed JSON keyset currently throws an uncaught runtime exception instead of a clean IOException, so callers can't catch it the way the API implies. The change reports it as IOException. Small and covered by a test — let me know if you'd prefer a different exception type. |
tholenst
left a comment
There was a problem hiding this comment.
Can you add a test which would have failed before this patch?
Cover the read() and readEncrypted() paths where a field that is expected to be a JSON primitive is instead an object, which makes gson's getAsString() raise UnsupportedOperationException. Both tests fail against the previous code (the exception escaped as UnsupportedOperationException) and pass now that it is wrapped in an IOException.
|
@tholenst added in 30002a3. Two tests — one on the read() path ( |
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.