diff --git a/src/main/java/com/google/crypto/tink/JsonKeysetReader.java b/src/main/java/com/google/crypto/tink/JsonKeysetReader.java index 2bd68988b..3baa18085 100644 --- a/src/main/java/com/google/crypto/tink/JsonKeysetReader.java +++ b/src/main/java/com/google/crypto/tink/JsonKeysetReader.java @@ -163,7 +163,7 @@ public Keyset read() throws IOException { try { return keysetFromJson( JsonParser.parse(new String(Util.readAll(inputStream), UTF_8)).getAsJsonObject()); - } catch (JsonParseException | IllegalStateException e) { + } catch (JsonParseException | IllegalStateException | UnsupportedOperationException e) { throw new IOException(e); } finally { if (inputStream != null) { @@ -177,7 +177,7 @@ public EncryptedKeyset readEncrypted() throws IOException { try { return encryptedKeysetFromJson( JsonParser.parse(new String(Util.readAll(inputStream), UTF_8)).getAsJsonObject()); - } catch (JsonParseException | IllegalStateException e) { + } catch (JsonParseException | IllegalStateException | UnsupportedOperationException e) { throw new IOException(e); } finally { if (inputStream != null) { diff --git a/src/test/java/com/google/crypto/tink/JsonKeysetReaderTest.java b/src/test/java/com/google/crypto/tink/JsonKeysetReaderTest.java index 11e894a89..de44eae5b 100644 --- a/src/test/java/com/google/crypto/tink/JsonKeysetReaderTest.java +++ b/src/test/java/com/google/crypto/tink/JsonKeysetReaderTest.java @@ -468,6 +468,25 @@ public void testReadKeyset_keyIdWithComment_throws() throws Exception { assertThrows(IOException.class, () -> JsonKeysetReader.withString(jsonKeysetString).read()); } + @Test + public void readKeyset_nonPrimitiveStatusField_throwsIOException() throws Exception { + // "status" is a JSON object, so gson's getAsString() raises UnsupportedOperationException while + // parsing the key. read() must surface that as an IOException rather than let it escape + // uncaught to the caller. + String jsonKeysetString = + "{\"key\":[{\"keyData\":{},\"status\":{},\"keyId\":1,\"outputPrefixType\":\"TINK\"}]}"; + assertThrows(IOException.class, () -> JsonKeysetReader.withString(jsonKeysetString).read()); + } + + @Test + public void readEncrypted_nonPrimitiveEncryptedKeysetField_throwsIOException() throws Exception { + // "encryptedKeyset" is a JSON object, so getAsString() raises UnsupportedOperationException. + // readEncrypted() must surface that as an IOException rather than let it escape uncaught. + String jsonEncryptedKeyset = "{\"encryptedKeyset\":{}}"; + assertThrows( + IOException.class, () -> JsonKeysetReader.withString(jsonEncryptedKeyset).readEncrypted()); + } + @Test public void testReadKeyset_withDuplicatedMapKey_throws() throws Exception { String jsonKeysetString = "{"