diff --git a/src/test/java/com/google/crypto/tink/jwt/JwkSetConverterTest.java b/src/test/java/com/google/crypto/tink/jwt/JwkSetConverterTest.java index a66eb8f89..d7199d737 100644 --- a/src/test/java/com/google/crypto/tink/jwt/JwkSetConverterTest.java +++ b/src/test/java/com/google/crypto/tink/jwt/JwkSetConverterTest.java @@ -900,6 +900,38 @@ public void ecdsaWithUnknownField_toPublicKeysetHandleSuccess() throws Exception Object unused = JwkSetConverter.toPublicKeysetHandle(jwksString); } + @Test + public void withoutKeysField_toPublicKeysetHandleFailsWithGeneralSecurityException() + throws Exception { + // Well-formed JSON, but missing the top-level "keys" array entirely. Before the fix, this + // threw an uncaught NullPointerException instead of the documented GeneralSecurityException, + // since jsonKeyset.get("keys") returns null for a Gson JsonObject that has no such member. + String jwksString = "{}"; + assertThrows( + GeneralSecurityException.class, () -> JwkSetConverter.toPublicKeysetHandle(jwksString)); + } + + @Test + public void keysFieldIsNotAnArray_toPublicKeysetHandleFailsWithGeneralSecurityException() + throws Exception { + // Before the fix, this threw an uncaught IllegalStateException (from + // JsonElement.getAsJsonArray()) instead of the documented GeneralSecurityException, because + // the "keys" value is read and converted outside of the method's only try/catch block. + String jwksString = "{\"keys\":\"not an array\"}"; + assertThrows( + GeneralSecurityException.class, () -> JwkSetConverter.toPublicKeysetHandle(jwksString)); + } + + @Test + public void keysArrayContainsNonObjectElement_toPublicKeysetHandleFailsWithGeneralSecurityException() + throws Exception { + // Before the fix, this threw an uncaught IllegalStateException (from + // JsonElement.getAsJsonObject()) instead of the documented GeneralSecurityException. + String jwksString = "{\"keys\":[\"not an object\"]}"; + assertThrows( + GeneralSecurityException.class, () -> JwkSetConverter.toPublicKeysetHandle(jwksString)); + } + @Test public void ecdsaWithoutAlg_toPublicKeysetHandleFails() throws Exception { String jwksString = diff --git a/src/test/java/com/google/crypto/tink/signature/SignatureJwkSetConverterTest.java b/src/test/java/com/google/crypto/tink/signature/SignatureJwkSetConverterTest.java index 273d48f2f..156f86f8d 100644 --- a/src/test/java/com/google/crypto/tink/signature/SignatureJwkSetConverterTest.java +++ b/src/test/java/com/google/crypto/tink/signature/SignatureJwkSetConverterTest.java @@ -140,6 +140,41 @@ public void testExportTinkVariantThrows() throws Exception { GeneralSecurityException.class, () -> SignatureJwkSetConverter.fromPublicKeysetHandle(publicHandle)); } + @Test + public void withoutKeysField_toPublicKeysetHandleFailsWithGeneralSecurityException() + throws Exception { + // Well-formed JSON, but missing the top-level "keys" array entirely. Before the fix, this + // threw an uncaught NullPointerException instead of the documented GeneralSecurityException, + // since jsonKeyset.get("keys") returns null for a Gson JsonObject that has no such member. + String jwksString = "{}"; + assertThrows( + GeneralSecurityException.class, + () -> SignatureJwkSetConverter.toPublicKeysetHandle(jwksString)); + } + + @Test + public void keysFieldIsNotAnArray_toPublicKeysetHandleFailsWithGeneralSecurityException() + throws Exception { + // Before the fix, this threw an uncaught IllegalStateException (from + // JsonElement.getAsJsonArray()) instead of the documented GeneralSecurityException, because + // the "keys" value is read and converted outside of the method's only try/catch block. + String jwksString = "{\"keys\":\"not an array\"}"; + assertThrows( + GeneralSecurityException.class, + () -> SignatureJwkSetConverter.toPublicKeysetHandle(jwksString)); + } + + @Test + public void keysArrayContainsNonObjectElement_toPublicKeysetHandleFailsWithGeneralSecurityException() + throws Exception { + // Before the fix, this threw an uncaught IllegalStateException (from + // JsonElement.getAsJsonObject()) instead of the documented GeneralSecurityException. + String jwksString = "{\"keys\":[\"not an object\"]}"; + assertThrows( + GeneralSecurityException.class, + () -> SignatureJwkSetConverter.toPublicKeysetHandle(jwksString)); + } + @Test public void testExportEcdsaDerEncodingThrows() throws Exception { // PredefinedSignatureParameters.ECDSA_P256 is TINK variant, but even if we make it RAW,