Repository navigation
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Revoked audiences can still appear in tokens from client-credentials and refresh-token grants, and authorization-time cleanup can roll back when later checks fail.
2 open issues
What changed in this PR
This PR addresses stale CIMD resource-audience mappers described in #55131 by reconciling them against the current allowlist during authorization requests.
Changes:
- Removes stale mappers even when
resourceis omitted or a still-allowed resource is requested. - Removes CIMD-owned audience mappers when Resource Indicators are disabled.
- Adds regression tests for omitted-resource and alternate-resource authorization requests.
| File | Description |
|---|---|
tests/base/src/test/java/org/keycloak/tests/client/policies/ClientIdMetadataDocumentTest.java |
Tests mapper cleanup after allowlist tightening. |
services/src/main/java/org/keycloak/protocol/oauth2/cimd/clientpolicy/executor/AbstractClientIdMetadataDocumentExecutor.java |
Reconciles CIMD audience mappers during authorization. |
Review effort: Balanced
7f15d4a to
6894fe2
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Revoked audiences can still be issued in several token flows, and the condition change can bypass authenticator enforcement in existing policies.
5 open issues
Preserve ABSTAIN for nonmatching clients in combined policies · New Reconcile the target client’s mappers during token exchange · New Reconcile the allowlist before implicit and hybrid token issuance · New Refresh the current client view after removing stale mappers · New Enforce resource allowlist regardless of audience mapper source · New
2 resolved since last review
Review effort: Balanced
6894fe2 to
582d775
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Critical issues remain in policy scoping and allowlist enforcement, including revoked audiences surviving token issuance or introspection and a rejected authorization leaving a redeemable code.
7 open issues
Matching an audience runs target executors on the requester · New Nonmatching clients can inherit a policy's CIMD allowlist · New Policy conditions can bypass CIMD cleanup at token issuance · New Introspection can expose revoked CIMD audiences · New Token exchange applies one policy's allowlist to both clients · New Rejected authorization response can retain a redeemable code · New Null JWS payload causes an uncaught refresh-token exception · New
5 resolved since last review
Enforce resource allowlist regardless of audience mapper source Refresh the current client view after removing stale mappers Reconcile the allowlist before implicit and hybrid token issuance Reconcile the target client’s mappers during token exchange Preserve ABSTAIN for nonmatching clients in combined policies
Review effort: Balanced
582d775 to
113dba7
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Revoked resource audiences can still be issued because token-time restrictions are not enforced and cleanup can miss clients or retain mappers for inapplicable policies.
8 open issues
Reconcile all clients regardless of administrator visibility filters · New Combine applicable CIMD restrictions instead of unioning allowed resources · New Evaluate client conditions before retaining CIMD mappers · New Enforce current CIMD restrictions during token issuance · New Avoid scanning every client on unrelated policy updates · New Include global CIMD policies in the enabled-policy stream · New Reconcile CIMD mappers only after realm import is complete · New Assert active introspection and test mapper changes with a fresh token · New
7 resolved since last review
Rejected authorization response can retain a redeemable code Token exchange applies one policy's allowlist to both clients Introspection can expose revoked CIMD audiences Policy conditions can bypass CIMD cleanup at token issuance Nonmatching clients can inherit a policy's CIMD allowlist Matching an audience runs target executors on the requester Null JWS payload causes an uncaught refresh-token exception
Review effort: Balanced
113dba7 to
0e65c32
Compare
There was a problem hiding this comment.
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.
Copilot review overview
3 open issues
8 resolved since last review
Enforce current CIMD restrictions during token issuance Evaluate client conditions before retaining CIMD mappers Combine applicable CIMD restrictions instead of unioning allowed resources Reconcile all clients regardless of administrator visibility filters Assert active introspection and test mapper changes with a fresh token Reconcile CIMD mappers only after realm import is complete Include global CIMD policies in the enabled-policy stream Avoid scanning every client on unrelated policy updates
Review effort: Lite
0e65c32 to
092116b
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Critical cleanup gaps can leave revoked audiences in client-credentials tokens, while mapper removal can also roll back policy updates.
6 open issues
Track CIMD mapper ownership independently of client ID · New Invalidate client cache when removing a mapper · New Reconcile mappers when global security profiles change · New Initialize user storage before registering the prepare callback · New Serialize concurrent profile and policy reconciliation · New Avoid scanning every URL client on each policy update · New
3 resolved since last review
Review effort: Balanced
…ning closes keycloak#55131 Signed-off-by: Takashi Norimatsu <takashi.norimatsu.ws@hitachi.com>
092116b to
e830168
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Client renames can fail, while migration, import, and scan gaps can leave revoked audiences active in client-credentials tokens.
6 open issues
Use stable traversal to avoid skipping clients during scans · New Reconcile imported clients independently of the startup migration · New Include previously renamed CIMD clients in the migration scan · New Reconcile renamed client mappers only after the update commits · New Process bounded client pages without retaining all hydrated models · New Skip strict policies that abstain at pre-authorization · New
6 resolved since last review
Serialize concurrent profile and policy reconciliation Initialize user storage before registering the prepare callback Reconcile mappers when global security profiles change Invalidate client cache when removing a mapper Track CIMD mapper ownership independently of client ID Avoid scanning every URL client on each policy update
Review effort: Balanced
| Map<String, String> attributes = Map.of(RESOURCE_INDICATOR_AUDIENCE_MAPPERS_ATTRIBUTE, Boolean.TRUE.toString()); | ||
| List<String> ids = new ArrayList<>(); | ||
| while (true) { | ||
| List<String> page = session.clients().searchClientsByAttributes(realm, attributes, ids.size(), RESOURCE_INDICATOR_AUDIENCE_MAPPERS_BATCH_SIZE) |
| return realm == null || realm.getAttribute(RESOURCE_INDICATOR_AUDIENCE_MAPPERS_MARKED_REALM_ATTRIBUTE) != null; | ||
| }); | ||
| if (marked) { | ||
| return; |
| session.getContext().setRealm(realm); | ||
| return AdminPermissionsSchema.runWithoutAuthorization(session, () -> { | ||
| List<ClientModel> clients = session.clients() | ||
| .searchClientsByClientIdStream(realm, CIMD_CLIENT_ID_SEARCH, firstResult, RESOURCE_INDICATOR_AUDIENCE_MAPPERS_BATCH_SIZE) |
| AbstractClientIdMetadataDocumentExecutor.removeStaleResourceIndicatorAudienceMappers(session, client.getRealm(), client, | ||
| executorProviderId, create(session).getExecutorConfigurationClass()); |
| } | ||
| session.getContext().setRealm(realm); | ||
| return AdminPermissionsSchema.runWithoutAuthorization(session, () -> getClientIdsWithResourceIndicatorAudienceMappers(session, realm)); |
| List<EnabledPolicy> enabledPolicies = Stream.concat(nullSafe(policies.getGlobalPolicies()), nullSafe(policies.getPolicies())) | ||
| .filter(p -> Boolean.TRUE.equals(p.isEnabled())) | ||
| // a client policy without conditions is never applied. | ||
| .filter(p -> p.getConditions() != null && !p.getConditions().isEmpty()) | ||
| .map(p -> new EnabledPolicy(nullSafe(p.getProfiles()).toList(), readClientIdUriConditions(realm, p))) |
closes #55131