From 0567cd540151757763c0acf72ed68a08a339cfe9 Mon Sep 17 00:00:00 2001 From: Srinivas Chippagiri Date: Fri, 13 Nov 2026 19:11:51 -0800 Subject: [PATCH 1/2] Ignore JWKs with unsupported key types when parsing JWT bundles The bundle spec says a JWK with a key type the client doesn't understand must be ignored, but JwtBundle rejected the whole bundle as soon as it saw one (an Ed25519 OKP key, for example). That also made the Workload API client drop the JWT bundle for that trust domain. Skip keys that aren't EC or RSA and keep the rest. Update the tests that relied on the old rejection and add a fixture with an OKP key next to an EC key. Fixes #512 Signed-off-by: Srinivas Chippagiri --- CHANGELOG.md | 1 + .../io/spiffe/bundle/jwtbundle/JwtBundle.java | 11 +++++ .../bundle/jwtbundle/JwtBundleTest.java | 40 +++++++++++-------- .../workloadapi/GrpcConversionUtilsTest.java | 14 +++---- .../testdata/jwtbundle/jwks_EC_and_OKP.json | 18 +++++++++ 5 files changed, 61 insertions(+), 23 deletions(-) create mode 100644 java-spiffe-core/src/test/resources/testdata/jwtbundle/jwks_EC_and_OKP.json diff --git a/CHANGELOG.md b/CHANGELOG.md index e2245f17..3854f7c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ ### Fixed * Reject X.509-SVID leaf certificates with a root SPIFFE ID path in `X509SvidValidator.verifyChain`, which `SpiffeTrustManager` uses for peer certificates (#510) +* Ignore JWKs with an unsupported key type when parsing a JWT bundle instead of rejecting the whole bundle, per the SPIFFE bundle spec (#512) ### Build diff --git a/java-spiffe-core/src/main/java/io/spiffe/bundle/jwtbundle/JwtBundle.java b/java-spiffe-core/src/main/java/io/spiffe/bundle/jwtbundle/JwtBundle.java index 007fd17f..42ec2d54 100644 --- a/java-spiffe-core/src/main/java/io/spiffe/bundle/jwtbundle/JwtBundle.java +++ b/java-spiffe-core/src/main/java/io/spiffe/bundle/jwtbundle/JwtBundle.java @@ -5,6 +5,7 @@ import com.nimbusds.jose.jwk.ECKey; import com.nimbusds.jose.jwk.JWK; import com.nimbusds.jose.jwk.JWKSet; +import com.nimbusds.jose.jwk.KeyType; import com.nimbusds.jose.jwk.RSAKey; import io.spiffe.internal.JwtSignatureAlgorithm; import io.spiffe.bundle.BundleSource; @@ -238,6 +239,11 @@ private static JwtBundle toJwtBundle(TrustDomain trustDomain, final Map authorities = new ConcurrentHashMap<>(); for (JWK jwk : jwkSet.getKeys()) { + // The SPIFFE bundle spec says JWKs with a key type the client doesn't + // understand must be ignored, so they shouldn't fail the whole bundle. + if (!isSupportedKeyType(jwk)) { + continue; + } String keyId = getKeyId(jwk); PublicKey publicKey = getPublicKey(jwk); authorities.put(keyId, publicKey); @@ -245,6 +251,11 @@ private static JwtBundle toJwtBundle(TrustDomain trustDomain, return new JwtBundle(trustDomain, authorities); } + private static boolean isSupportedKeyType(final JWK jwk) { + KeyType keyType = jwk.getKeyType(); + return KeyType.EC.equals(keyType) || KeyType.RSA.equals(keyType); + } + private static String getKeyId(final JWK jwk) throws JwtBundleException { Objects.requireNonNull(jwk, "jwk must not be null"); String keyId = jwk.getKeyID(); diff --git a/java-spiffe-core/src/test/java/io/spiffe/bundle/jwtbundle/JwtBundleTest.java b/java-spiffe-core/src/test/java/io/spiffe/bundle/jwtbundle/JwtBundleTest.java index 4d525453..49e0b019 100644 --- a/java-spiffe-core/src/test/java/io/spiffe/bundle/jwtbundle/JwtBundleTest.java +++ b/java-spiffe-core/src/test/java/io/spiffe/bundle/jwtbundle/JwtBundleTest.java @@ -171,16 +171,26 @@ void testLoadFile_MissingKid_ThrowsJwtBundleException() throws URISyntaxExceptio } @Test - void testLoadFile_InvalidKeyType_ThrowsKeyException() throws URISyntaxException, KeyException { + void testLoadFile_UnsupportedKeyType_IsIgnored() throws URISyntaxException, JwtBundleException { Path path = Paths.get(toUri("testdata/jwtbundle/jwks_invalid_keytype.json")); TrustDomain trustDomain = TrustDomain.parse("domain.test"); - try { - JwtBundle.load(trustDomain, path); - fail("should have thrown exception"); - } catch (JwtBundleException e) { - assertEquals("Unsupported JWT family algorithm: OKP", e.getCause().getMessage()); - } + JwtBundle jwtBundle = JwtBundle.load(trustDomain, path); + + assertEquals(trustDomain, jwtBundle.getTrustDomain()); + assertTrue(jwtBundle.getJwtAuthorities().isEmpty()); + } + + @Test + void testLoadFile_UnsupportedKeyTypeNextToECKey_KeepsECKey() throws URISyntaxException, JwtBundleException { + Path path = Paths.get(toUri("testdata/jwtbundle/jwks_EC_and_OKP.json")); + TrustDomain trustDomain = TrustDomain.parse("domain.test"); + + JwtBundle jwtBundle = JwtBundle.load(trustDomain, path); + + assertEquals(1, jwtBundle.getJwtAuthorities().size()); + assertNotNull(jwtBundle.getJwtAuthorities().get("C6vs25welZOx6WksNYfbMfiw9l96pMnD")); + assertFalse(jwtBundle.hasJwtAuthority("okp-key")); } @Test @@ -267,18 +277,16 @@ void testParse_MissingKid_Fails() throws URISyntaxException, IOException { } @Test - void testParse_InvalidKeyType_ThrowsJwtBundleException() throws URISyntaxException, IOException { - Path path = Paths.get(toUri("testdata/jwtbundle/jwks_invalid_keytype.json")); + void testParse_UnsupportedKeyTypeNextToECKey_KeepsECKey() throws URISyntaxException, IOException, JwtBundleException { + Path path = Paths.get(toUri("testdata/jwtbundle/jwks_EC_and_OKP.json")); byte[] bundleBytes = Files.readAllBytes(path); TrustDomain trustDomain = TrustDomain.parse("domain.test"); - try { - JwtBundle.parse(trustDomain, bundleBytes); - fail("should have thrown exception"); - } catch (JwtBundleException e) { - assertEquals("Could not parse bundle from bytes", e.getMessage()); - assertEquals("Unsupported JWT family algorithm: OKP", e.getCause().getMessage()); - } + JwtBundle jwtBundle = JwtBundle.parse(trustDomain, bundleBytes); + + assertEquals(1, jwtBundle.getJwtAuthorities().size()); + assertTrue(jwtBundle.hasJwtAuthority("C6vs25welZOx6WksNYfbMfiw9l96pMnD")); + assertFalse(jwtBundle.hasJwtAuthority("okp-key")); } @Test diff --git a/java-spiffe-core/src/test/java/io/spiffe/workloadapi/GrpcConversionUtilsTest.java b/java-spiffe-core/src/test/java/io/spiffe/workloadapi/GrpcConversionUtilsTest.java index cd52f44d..5bfd475e 100644 --- a/java-spiffe-core/src/test/java/io/spiffe/workloadapi/GrpcConversionUtilsTest.java +++ b/java-spiffe-core/src/test/java/io/spiffe/workloadapi/GrpcConversionUtilsTest.java @@ -1,6 +1,8 @@ package io.spiffe.workloadapi; import com.google.protobuf.ByteString; +import io.spiffe.bundle.jwtbundle.JwtBundle; +import io.spiffe.bundle.jwtbundle.JwtBundleSet; import io.spiffe.bundle.x509bundle.X509Bundle; import io.spiffe.bundle.x509bundle.X509BundleSet; import io.spiffe.exception.BundleNotFoundException; @@ -26,6 +28,7 @@ import static io.spiffe.utils.TestUtils.toUri; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; class GrpcConversionUtilsTest { @@ -54,19 +57,16 @@ void test_toJwtBundleSet_emtpyResponse() { } @Test - void test_toJwtBundleSet_unsupportedKeyType() throws URISyntaxException, IOException { + void test_toJwtBundleSet_unsupportedKeyType() throws Exception { byte[] bundleBytes = Files.readAllBytes(Paths.get(toUri("testdata/jwtbundle/jwks_invalid_keytype.json"))); Workload.JWTBundlesResponse response = Workload.JWTBundlesResponse .newBuilder() .putBundles("example.org", ByteString.copyFrom(bundleBytes)) .build(); - try { - GrpcConversionUtils.toJwtBundleSet(response); - fail("should have thrown exception"); - } catch (JwtBundleException e) { - assertEquals("Could not parse bundle from bytes", e.getMessage()); - } + JwtBundleSet bundleSet = GrpcConversionUtils.toJwtBundleSet(response); + JwtBundle bundle = bundleSet.getBundleForTrustDomain(TrustDomain.parse("example.org")); + assertTrue(bundle.getJwtAuthorities().isEmpty()); } @Test diff --git a/java-spiffe-core/src/test/resources/testdata/jwtbundle/jwks_EC_and_OKP.json b/java-spiffe-core/src/test/resources/testdata/jwtbundle/jwks_EC_and_OKP.json new file mode 100644 index 00000000..a2b1353f --- /dev/null +++ b/java-spiffe-core/src/test/resources/testdata/jwtbundle/jwks_EC_and_OKP.json @@ -0,0 +1,18 @@ +{ + "keys": [ + { + "kty": "OKP", + "crv": "Ed25519", + "kid": "okp-key", + "x": "c2Rmc2Zk", + "alg": "EdDSA" + }, + { + "kty": "EC", + "kid": "C6vs25welZOx6WksNYfbMfiw9l96pMnD", + "crv": "P-256", + "x": "ngLYQnlfF6GsojUwqtcEE3WgTNG2RUlsGhK73RNEl5k", + "y": "tKbiDSUSsQ3F1P7wteeHNXIcU-cx6CgSbroeQrQHTLM" + } + ] +} From 807f9de0cf21c171192d0de25708ee6573640d8f Mon Sep 17 00:00:00 2001 From: Srinivas Chippagiri Date: Sat, 14 Nov 2026 11:55:18 -0800 Subject: [PATCH 2/2] Declare KeyException in the new JwtBundle load tests Signed-off-by: Srinivas Chippagiri --- .../test/java/io/spiffe/bundle/jwtbundle/JwtBundleTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/java-spiffe-core/src/test/java/io/spiffe/bundle/jwtbundle/JwtBundleTest.java b/java-spiffe-core/src/test/java/io/spiffe/bundle/jwtbundle/JwtBundleTest.java index 49e0b019..d7bb2be3 100644 --- a/java-spiffe-core/src/test/java/io/spiffe/bundle/jwtbundle/JwtBundleTest.java +++ b/java-spiffe-core/src/test/java/io/spiffe/bundle/jwtbundle/JwtBundleTest.java @@ -171,7 +171,7 @@ void testLoadFile_MissingKid_ThrowsJwtBundleException() throws URISyntaxExceptio } @Test - void testLoadFile_UnsupportedKeyType_IsIgnored() throws URISyntaxException, JwtBundleException { + void testLoadFile_UnsupportedKeyType_IsIgnored() throws URISyntaxException, JwtBundleException, KeyException { Path path = Paths.get(toUri("testdata/jwtbundle/jwks_invalid_keytype.json")); TrustDomain trustDomain = TrustDomain.parse("domain.test"); @@ -182,7 +182,7 @@ void testLoadFile_UnsupportedKeyType_IsIgnored() throws URISyntaxException, JwtB } @Test - void testLoadFile_UnsupportedKeyTypeNextToECKey_KeepsECKey() throws URISyntaxException, JwtBundleException { + void testLoadFile_UnsupportedKeyTypeNextToECKey_KeepsECKey() throws URISyntaxException, JwtBundleException, KeyException { Path path = Paths.get(toUri("testdata/jwtbundle/jwks_EC_and_OKP.json")); TrustDomain trustDomain = TrustDomain.parse("domain.test");