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
…ning closes keycloak#55131 Signed-off-by: Takashi Norimatsu <takashi.norimatsu.ws@hitachi.com>
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
| List<ClientModel> clients = session.clients().getClientsStream(realm) | ||
| .filter(c -> getResourceIndicatorAudienceMappers(c).findAny().isPresent()) | ||
| .toList(); |
| .flatMap(p -> nullSafe(p.getProfiles())) | ||
| .map(allowListsByProfile::get) | ||
| .filter(Objects::nonNull) | ||
| .flatMap(List::stream) |
| if (!ClientIdUriSchemeConditionFactory.PROVIDER_ID.equals(condition.getConditionProviderId())) { | ||
| // other conditions depend on a request, so they are regarded as possibly satisfied. | ||
| continue; |
| KeycloakSession session = attributeUpdateEvent.getKeycloakSession(); | ||
| AbstractClientIdMetadataDocumentExecutor.removeStaleResourceIndicatorAudienceMappers(session, | ||
| attributeUpdateEvent.getRealm(), getId(), create(session).getExecutorConfigurationClass()); |
| List<ClientModel> clients = session.clients().getClientsStream(realm) | ||
| .filter(c -> getResourceIndicatorAudienceMappers(c).findAny().isPresent()) | ||
| .toList(); |
| policies = session.clientPolicy().getClientPolicies(realm, false); | ||
| } catch (ClientPolicyException e) { | ||
| RESOURCE_INDICATOR_LOGGER.warnv("client profiles or policies not loaded, so stale resource indicator audience mappers are not removed: realm = {0}, error = {1}", | ||
| realm.getName(), e.getErrorDetail()); | ||
| return null; | ||
| } | ||
|
|
||
| Map<String, List<String>> allowListsByProfile = new HashMap<>(); | ||
| Stream.concat(nullSafe(profiles.getProfiles()), nullSafe(profiles.getGlobalProfiles())).forEach(profile -> { | ||
| List<String> allowList = nullSafe(profile.getExecutors()) | ||
| .filter(e -> executorProviderId.equals(e.getExecutorProviderId())) | ||
| .map(e -> (Configuration) JsonSerialization.mapper.convertValue(RawJsonValue.unwrap(JsonNode.class, e.getConfiguration()), configurationClass)) | ||
| .filter(Objects::nonNull) | ||
| .flatMap(c -> nullSafe(c.getResourceIndicatorAllowList())) | ||
| .toList(); | ||
| allowListsByProfile.merge(profile.getName(), allowList, (a, b) -> Stream.concat(a.stream(), b.stream()).toList()); | ||
| }); | ||
|
|
||
| List<ClientPolicyRepresentation> enabledPolicies = nullSafe(policies.getPolicies()) |
| if (event instanceof RealmModel.RealmAttributeUpdateEvent attributeUpdateEvent | ||
| && (Constants.CLIENT_PROFILES.equals(attributeUpdateEvent.getAttributeName()) | ||
| || Constants.CLIENT_POLICIES.equals(attributeUpdateEvent.getAttributeName()))) { | ||
| KeycloakSession session = attributeUpdateEvent.getKeycloakSession(); | ||
| AbstractClientIdMetadataDocumentExecutor.removeStaleResourceIndicatorAudienceMappers(session, | ||
| attributeUpdateEvent.getRealm(), getId(), create(session).getExecutorConfigurationClass()); |
| IntrospectionResponse introspectionResponse = oauth.client("test-app", "test-secret").doIntrospectionAccessTokenRequest(tokenResponse.getAccessToken()); | ||
| Assertions.assertEquals(200, introspectionResponse.getStatusCode()); | ||
| JsonNode introspectedAudience = introspectionResponse.asJsonNode().get(JsonWebToken.AUD); | ||
| Assertions.assertFalse(introspectedAudience != null && introspectedAudience.toString().contains(RESOURCE), "aud = " + introspectedAudience); |
closes #55131