8000 Validate the audience before consuming the jti in the JWT Authorization Grant by kanywst · Pull Request #55751 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

Validate the audience before consuming the jti in the JWT Authorization Grant - #55751

Open
kanywst wants to merge 3 commits into
keycloak:mainfrom
kanywst:jwt-grant-audience-before-jti
Open

kanywst wants to merge 3 commits into
keycloak:mainfrom
kanywst:jwt-grant-audience-before-jti

Conversation

@kanywst
@kanywst kanywst commented Dec 2, 2026
Copy link
Copy Markdown

Closes #55747

The audience check now runs before validateTokenActive, so an assertion rejected for its audience is no longer written to the single-use cache. Added testInvalidAudienceDoesNotConsumeJti, which sends the same assertion first with a disallowed audience and then after the audience is allowed.

The cache key is left as is (no realm in it) because changing its format opens a replay window during rolling upgrades, see #50193.

An AI agent was used to write this change; I have reviewed and tested it.

…on Grant

Closes keycloak#55747

Signed-off-by: kanywst <45947799+kanywst@users.noreply.github.com>
@kanywst
kanywst requested a review from a team as a code owner December 2, 2026 15:47
Copilot AI balanced review requested due to automatic review settings December 2, 2026 15:47
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.

🟡 Changes recommended

The untracked ManagedClient prevents cleanup of the custom audience, leaking configuration into subsequent tests and causing Invalid token audience failures.

1 open issue
What changed in this PR

Moves JWT authorization-grant audience validation before assertion consumption, addressing #55747 while preserving the cache-key format for rolling-upgrade compatibility.

Changes:

  • Reject invalid audiences before recording the assertion’s jti.
  • Add regression coverage for rejection, subsequent acceptance, and replay prevention.
File Description
tests/​base/​src/​test/​java/​org/​keycloak/​tests/​oauth/​AbstractJWTAuthorizationGrantTest.java Tests that audience rejection does not consume an assertion.
services/​src/​main/​java/​org/​keycloak/​protocol/​oidc/​grants/​JWTAuthorizationGrantType.java Moves audience validation before token activity and reuse checks.

Review effort: Balanced

@jamesconwaydev jamesconwaydev 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.

Read only, didn't run it. The reorder looks right to me.

validateTokenAudience in AbstractBaseJWTValidator only reads the token, and nothing on main sets a flag that the client policy step could have used to skip it, so the old "if not validated previously by client policies" comment was already stale. Moving the check up only changes which error wins when an assertion has more than one problem (a wrong audience now reports before "User not found", consent and client policy errors). "Multiple audiences not allowed" also moves ahead of the cache write, which is the same fix for the same reason.

On the realm part of #55747: with the reorder alone, the cross-realm repro there is covered too. When the client has no audience map for the IdP, JWTAuthorizationGrantIdentityProvider#getAllowedAudienceForJWTGrant returns this realm's issuer and token endpoint, so an assertion for realm b now fails the audience check in realm c before validateTokenReuse writes the jti. What's left for the realm-less key is the case where the same audience is valid in two realms: the same value in oauth2.jwt.authorization.grant.audience on clients in both, or an OIDC IdP with "allow client ID as audience" and the same client ID in both. Might be worth saying so on the issue, so it's clear what #50193 would still need to cover.

+1 to Copilot's note on the test: realm.updateClientWithCleanup("test-app", ...), as testConsentRequiredIsRejected does, registers the restore with the realm's cleanup; the hand-built ManagedClient just marks itself dirty.

Written with help from an AI assistant (Claude Code); each reference was checked against the source.

Signed-off-by: kanywst <45947799+kanywst@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings December 3, 2026 10:21
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 identified for the JWT assertion audience-validation change.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@kanywst
kanywst commented Dec 3, 2026
Copy link
Copy Markdown
Author

@jamesconwaydev thanks. Switched to realm.updateClientWithCleanup in a944469, and added the same-audience case to #55747.

@jamesconwaydev
Copy link
Copy Markdown

Follow-up, read only, didn't run it.

a944469 looks right: realm.updateClientWithCleanup registers the restore with the realm (ManagedRealm.java:147), so the custom audience no longer outlives the test.

One thing outside this PR: testCustomAudiences (line 389 of the same file at a944469) still builds the ManagedClient by hand and calls updateWithCleanup, which only calls dirty() on an object the framework isn't tracking, so it has the same leak Copilot flagged here. It was like that on main before this PR, so it may be better as a separate one-line change, but it'd fit here too if you'd rather. That's also the last user of the ClientResource, ManagedClient and AdminApiUtil imports, so they'd go with it.

Written with help from an AI assistant (Claude Code); each reference was checked against the source.

Signed-off-by: kanywst <45947799+kanywst@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings December 5, 2026 10:49
@kanywst
kanywst commented Dec 5, 2026
Copy link
Copy Markdown
Author

@jamesconwaydev added it here in e69d412, the three imports went with it.

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 remain for the change preventing invalid-audience assertions from consuming single-use identifiers.

No open issues 🎉

Review effort: Balanced

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.

JWT Authorization Grant: assertion is consumed before the audience check

3 participants

0