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

Fix NPE parsing an EC JWK with a missing x or y coordinate - #52297

Open
amdadulbari wants to merge 1 commit into
keycloak:mainfrom
amdadulbari:fix/jwk-ec-missing-coordinate-npe
Open

amdadulbari wants to merge 1 commit into
keycloak:mainfrom
amdadulbari:fix/jwk-ec-missing-coordinate-npe

Conversation

@amdadulbari
Copy link
Copy Markdown
Contributor

Closes #52294

JWKParser#createECPublicKey read the x/y fields 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 JWK missing a coordinate threw NullPointerException before the null-guard - which explicitly lists crv, x and y as nullable - could run.

Read x/y with path() too, so a malformed externally-supplied EC JWK fails with the intended Fail to retrieve ... message instead of an NPE.

Added JWKTest#toPublicKeyEcMissingCoordinate, verified in the crypto modules (DefaultCryptoJWKTest) fails-before / passes-after.

Copilot AI balanced review requested due to automatic review settings September 1, 2026 18:08
@amdadulbari
amdadulbari requested a review from a team as a code owner September 1, 2026 18:08
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Pull request overview

Fixes a NullPointerException when parsing malformed EC JWKs missing x or y by retrieving coordinates via path() (returning a non-null MissingNode) rather than get() (returning null), and adds a regression test to ensure the error is surfaced as the intended “Fail to retrieve …” message.

Changes:

  • Switch EC JWK coordinate retrieval from get() to path() in JWKParser#createECPublicKey.
  • Add a regression test ensuring missing EC coordinates fail with a descriptive error (not an NPE).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
core/src/main/java/org/keycloak/jose/jwk/JWKParser.java Uses path() for EC x/y retrieval to avoid NPE and hit the intended null-guard.
core/src/test/java/org/keycloak/jose/jwk/JWKTest.java Adds a regression test for malformed EC JWK missing a coordinate, asserting non-NPE behavior and error messaging.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Copilot AI balanced review requested due to automatic review settings October 5, 2026 13:37
@mposolda mposolda self-assigned this Oct 5, 2026
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🔵 Needs a closer look

The missing-y path remains untested despite being part of the fix.

Review effort: Balanced
Findings: 1 Medium severity · 1 Low severity

Open (2)
Resolved since last review (1)
Previously missed (1)

In code that hasn't changed since last review

Low severity Add regression coverage for missing y coordinate

core/​src/​main/​java/​org/​keycloak/​jose/​jwk/​JWKParser.java:97

The regression test only omits x, so it would still pass if this y lookup remained get(...); the missing-y half of the reported failure is therefore uncovered. Please exercise both omitted-coordinate cases (for example, with a small loop or parameterized test).

@amdadulbari
amdadulbari force-pushed the fix/jwk-ec-missing-coordinate-npe branch from 19fa731 to daa902b Compare November 28, 2026 05:40
Copilot AI balanced review requested due to automatic review settings November 28, 2026 05:40
@amdadulbari
Copy link
Copy Markdown
Contributor Author

@mposolda Thanks for the follow-up commit. I've addressed the remaining review points and squashed everything into a single commit:

  • Missing y is now covered. The test exercises both a missing x and a missing y. Previously only x was omitted, so the test still passed if the y lookup stayed on get(). I verified it now fails with the NPE in that case.
  • assertThrows instead of try/catch. The test asserts the thrown exception is not a NullPointerException and checks the exact message, Fail to retrieve ECPublicJWK.CRV, ECPublicJWK.X or ECPublicJWK.Y field.
  • This replaces the message.toLowerCase().contains("x") check from the autofix commit, which always passed because the message itself contains ECPublicJWK.X.

DefaultCryptoJWKTest and ElytronCryptoJWKTest pass.

Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the change improving error messages when EC JWK coordinates are missing.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

@keycloak-github-bot keycloak-github-bot Bot left a comment
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unreported flaky test detected, please review

@keycloak-github-bot
Copy link
Copy Markdown

Unreported flaky test detected

If the flaky tests below are affected by the changes, please review and update the changes accordingly. Otherwise, a maintainer should report the flaky tests prior to merging the PR.

org.keycloak.testsuite.actions.TermsAndConditionsTest#termsAccepted

Keycloak CI - Forms IT (firefox)

java.lang.RuntimeException: Could not create statement
	at org.jboss.arquillian.junit.Arquillian.methodBlock(Arquillian.java:307)
	at org.jboss.arquillian.junit.Arquillian$2.evaluate(Arquillian.java:160)
	at org.jboss.arquillian.junit.Arquillian.multiExecute(Arquillian.java:344)
	at org.jboss.arquillian.junit.Arquillian.access$200(Arquillian.java:48)
...

Report flaky test

JWKParser.createECPublicKey read the "crv" field with path() (which
returns a non-null MissingNode) but read "x" and "y" with get() (which
returns null for an absent key). The subsequent guard explicitly treats
crv, x and y as nullable and throws a descriptive RuntimeException, but
for a missing x or y it was never reached: get(...) returned null and
.asText(null) dereferenced it, throwing NullPointerException instead.

Read x and y with path() as well, mirroring crv and the RSA path in
createRSAPublicKey, so a malformed externally-supplied EC JWK fails with
the intended "Fail to retrieve ..." message rather than an NPE.

Add a regression test covering both a missing x and a missing y
coordinate, asserting the descriptive error is thrown rather than a
NullPointerException.

Closes keycloak#52294

Signed-off-by: Md. Amdadul Bari Imad <amdadulbari@gmail.com>
@amdadulbari
amdadulbari force-pushed the fix/jwk-ec-missing-coordinate-npe branch from daa902b to d6e1b7e Compare November 30, 2026 02:07
@amdadulbari
Copy link
Copy Markdown
Contributor Author

Rebased onto latest main to re-trigger CI. The previous failures were unrelated to this change: Maven could not resolve tools.jackson:jackson-bom:pom:3.2.4 while processing the POMs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NullPointerException parsing an EC JWK missing the "x" or "y" coordinate

4 participants

0