Skip to content

Ignore request-supplied kc.* attributes in AuthZen evaluation - #55186

Open
JBoogieman wants to merge 1 commit into
keycloak:mainfrom
JBoogieman:53325-authzen-kc-claims
Open

JBoogieman wants to merge 1 commit into
keycloak:mainfrom
JBoogieman:53325-authzen-kc-claims

Conversation

@JBoogieman

@JBoogieman JBoogieman commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Closes #53325

Following the approach agreed with @vramik on the issue: strip and set, not strip-only. Stripping alone would leave kc.client.id absent, because DefaultEvaluationContext.getBaseAttributes() only sets it for a KeycloakIdentity, and AuthZen evaluates with UserModelIdentity/ClientModelIdentity, so client policies would never match.

The filter runs on the raw request maps in evaluateSingle(), so it covers both the single and batch endpoints. It filters a copy because mergeDefaults() shares the top-level context map across batch items. kc.client.id is set from token.getIssuedFor() after the request values are merged. For a CLIENT subject this is the same value, since the subject id must already equal the token's azp.

The whole kc. prefix is reserved rather than only the keys the server currently overwrites. This also closes the kc.client.user_agent case when the request has no User-Agent header, and covers any kc.* attribute added later. Matching is exact and case-sensitive, consistent with how Attributes looks up names.

subject.properties is left unchanged. It feeds the subject's identity attributes rather than the evaluation context, and is meant to carry caller-supplied attributes.

Tests: seven new cases in AuthZenEvaluationTest. Five cover the issue's three reproduction steps, the batch endpoint, and a positive case showing a client policy now matches the real caller; with the whole fix removed, four of them fail. Because the server also sets kc.client.id after filtering, those alone don't exercise the filter, so two more send kc.test.reserved (a reserved key the server never sets) through context and resource.properties, checked by a test-only ReservedAttributePolicyProvider in authzen/tests/providers. With only the filter disabled, exactly those two fail. All 123 AuthZen tests pass with the fix, and Spotless is clean.

Docs: the "Context and Properties" section of the AuthZen guide now notes that kc.* keys are reserved and that kc.client.id is always the authenticated client.

AI disclosure: AI was used in the testing of this fix and ensuring there was no regression.

@JBoogieman
JBoogieman requested a review from a team as a code owner November 14, 2026 12:46
Copilot AI balanced review requested due to automatic review settings November 14, 2026 12:46
@JBoogieman
JBoogieman force-pushed the 53325-authzen-kc-claims branch from 580b934 to 2c91d8f Compare November 14, 2026 12:48

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 new tests cover only the overwritten kc.client.id key, leaving broader kc.* reservation enforcement unprotected against regressions through context and resource.properties.

1 open issue
What changed in this PR

This PR addresses #53325 by preventing request-supplied kc.* attributes from influencing AuthZen evaluation context while ensuring client policies use the authenticated caller.

Changes:

  • Filter reserved attributes from copies of request context and resource properties for single and batch evaluations.
  • Set kc.client.id from the authenticated token and add five client-policy regression tests.
  • Document the reserved namespace and caller-based client identity.
File Description
docs/​guides/​securing-apps/​authzen-authorization.adoc Documents reserved attributes and authenticated-client behavior.
authzen/​tests/​base/​src/​test/​java/​org/​keycloak/​tests/​authzen/​AuthZenEvaluationTest.java Adds client-policy fixtures and single/batch spoofing tests.
authzen/​services/​src/​main/​java/​org/​keycloak/​authorization/​authzen/​AuthZenResource.java Filters request-supplied kc.* keys and sets the caller’s client ID.

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 14, 2026 12:48

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.

🔵 Needs a closer look

The new tests pass even without reserved-attribute filtering because kc.client.id is always overwritten, leaving non-overwritten kc.* keys untested through both context and resource.properties.

1 open issue

Review effort: Balanced

AuthZen evaluation merged context and resource.properties into the policy evaluation context as-is, so a caller could send kc.client.id and impersonate another client towards client policies. Keys in the reserved kc. namespace are now dropped from both maps, and kc.client.id is set from the authenticated token's azp, since DefaultEvaluationContext only sets it for a KeycloakIdentity.

Closes keycloak#53325

Signed-off-by: Justin Brueggemann <167224505+JBoogieman@users.noreply.github.com>
@JBoogieman
JBoogieman force-pushed the 53325-authzen-kc-claims branch from 2c91d8f to 8e68468 Compare November 14, 2026 13:40
Copilot AI balanced review requested due to automatic review settings November 14, 2026 13:40

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 recorded for the changes preventing reserved AuthZen attribute injection and enforcing authenticated-caller client identity.

1 open issue

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.

AuthZen evaluation: request-supplied kc.client.id overrides the caller's client identity and flips client-based policies

2 participants