Repository navigation
Conversation
…on Grant Closes keycloak#55747 Signed-off-by: kanywst <45947799+kanywst@users.noreply.github.com>
There was a problem hiding this comment.
🟡 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
There was a problem hiding this comment.
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>
|
@jamesconwaydev thanks. Switched to |
|
Follow-up, read only, didn't run it. a944469 looks right: One thing outside this PR: 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>
|
@jamesconwaydev added it here in e69d412, the three imports went with it. |

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. AddedtestInvalidAudienceDoesNotConsumeJti, 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.