8000 Fix NPE parsing an EC JWK with a missing x or y coordinate by amdadulbari · Pull Request #52297 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/src/main/java/org/keycloak/jose/jwk/JWKParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ private static PublicKey createECPublicKey(RawJsonValue jwk) {

/* Try retrieving the necessary fields */
String crv = jwk.path(ECPublicJWK.CRV).asText(null);
String xStr = jwk.get(ECPublicJWK.X).asText(null);
String yStr = jwk.get(ECPublicJWK.Y).asText(null);
String xStr = jwk.path(ECPublicJWK.X).asText(null);
String yStr = jwk.path(ECPublicJWK.Y).asText(null);

/* Check if the retrieving of necessary fields success */
if (crv == null || xStr == null || yStr == null) {
Expand Down
27 changes: 27 additions & 0 deletions core/src/test/java/org/keycloak/jose/jwk/JWKTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

/**
Expand Down Expand Up @@ -281,6 +283,31 @@ public void toPublicKey_EC() {
assertEquals("X.509", pub.getFormat());
}

@Test
public void toPublicKeyEcMissingCoordinate() {
// An EC JWK missing the "x" or "y" coordinate must fail with the explicit
// "Fail to retrieve ..." error, not a NullPointerException. Before the fix the
// x/y fields were read with get() (which returns null for a missing key) instead
// of path() (a non-null MissingNode, as crv and the RSA path already use), so a
// missing coordinate dereferenced null and threw NPE before the null-guard - which
// explicitly lists X and Y as nullable - could ever run.
String x = "\"x\": \"usWxHK2PmfnHKwXPS54m0kTcGJ90UiglWiGahtagnv8\"";
String y = "\"y\": \"IBOL-C3BttVivg-lSreASjpkttcsz-1rb7btKLv8EX4\"";

assertMissingEcCoordinateFails(y); // missing "x"
assertMissingEcCoordinateFails(x); // missing "y"
}

private void assertMissingEcCoordinateFails(String presentCoordinate) {
String jwkJson = "{ \"kty\": \"EC\", \"crv\": \"P-256\", " + presentCoordinate + " }";
JWKParser sut = JWKParser.create().parse(jwkJson);

RuntimeException e = assertThrows(RuntimeException.class, sut::toPublicKey);
assertFalse("Missing EC coordinate must not surface as NullPointerException: " + e,
e instanceof NullPointerException);
assertEquals("Fail to retrieve ECPublicJWK.CRV, ECPublicJWK.X or ECPublicJWK.Y field.", e.getMessage());
}

private byte[] sign(byte[] data, String javaAlgorithm, PrivateKey key) throws Exception {
Signature signature = Signature.getInstance(javaAlgorithm);
signature.initSign(key);
Expand Down
Loading
0