GLM-5.3 · Heavy

add-client-resource-type-and-scopes-to-authorization-schema-keycloak

Keycloak · Java · L · 300–799 lines · 5 files

The generalist plus 9 specialist lens agents in parallel, then the same verify and dedup steps. Judge: Claude Haiku 5.5.

Gold bugs found
4 of 8
Comments published
8
Comments matching a bug
4
Comments not matching
4
Cost of this PR
$4.083

Published comments (8)

highbugserver-spi-private/src/main/java/org/keycloak/authorization/AdminPermissionsSchema.java:67-73

Existing admin-permissions servers never get the `Clients` type or new scopes — Creating any client permission fails — `getScope` throws, `getOrCreateResource` throws `ModelException` — and every client permission evaluation NPEs via the null type resource. Because `init()` returns early when `getAdminPermissionsClient()` exists, add a migration creating the missing type resource and scopes.

highbugservices/src/main/java/org/keycloak/services/resources/admin/permissions/ClientPermissionsV2.java:138-142

getClientsWithPermission adds type-level 'Clients' name, not per-client IDs — Per-client resources from `getOrCreateResource` carry no type, so `findByType` matches only the type-level resource and `granted` collects the literal `Clients` — `AvailableRoleMappingResource.getRoleIdsWithPermissions` then NPEs calling `realm.getClientById`. Set the resource type at creation, as V1's `ClientPermissions` does, or resolve per-client resources by name.

highbugservices/src/main/java/org/keycloak/services/resources/admin/permissions/ClientPermissionsV2.java:217-221

NPE when Clients type resource is missing in hasPermission(ClientModel, String) — When the type resource is absent, `getResourceTypeResource` returns null and `policyStore.findByResource` throws, turning every `canManage`/`canView`/`canConfigure`/`canMapRoles` client check into a 500 instead of a denial. Add a null check returning false, as the `hasPermission(String)` overload does.

highbugservices/src/main/java/org/keycloak/services/resources/admin/permissions/AdminPermissions.java:89-91

Client removal listener calls setPermissionsEnabled that throws under AUTHZ_V2 — With `ADMIN_FINE_GRAINED_AUTHZ_V2` enabled, the `ClientRemovedEvent` handler reaches `ClientPermissionsV2.setPermissionsEnabled`, which throws `UnsupportedOperationException` — every client deletion fails. Make the listener V2-aware: skip the call or perform V2-appropriate cleanup when that flag is on. **Also found in:** - `services/src/main/java/org/keycloak/services/resources/admin/permissions/ClientPermissionsV2.java:192-195` - `services/src/main/java/org/keycloak/services/resources/admin/permissions/ClientPermissionsV2.java:193-195`

highbugservices/src/main/java/org/keycloak/services/resources/admin/permissions/ClientPermissionsV2.java:148-150

V2 canExchangeTo throws UnsupportedOperationException, crashing token exchange — `AbstractTokenExchangeProvider` calls `canExchangeTo` for every non-self audience client, so with V2 enabled cross-client token exchange returns a 500 rather than evaluating or denying. Implement the evaluation — the now-dead `getEvaluationContext` shows the intent — or return false.

mediumperformanceservices/src/main/java/org/keycloak/services/resources/admin/permissions/ClientPermissionsV2.java:214-224

Type-level all-clients fallback re-evaluated for every client and scope check — In the `ClientsResource.getClients` listing, each `canView` repeats the identical type-level fallback up to three times — thousands of full policy evaluations per request in large realms. Compute the fallback once per request or collapse scopes into a single `hasPermission` call.

mediumbugservices/src/main/java/org/keycloak/services/resources/admin/permissions/ClientPermissionsV2.java:214-222

All-clients permission fallback skipped once a per-client resource exists — Granting `view` on one client then `manage` for all clients leaves `canManage` returning false and a 403 despite the realm-wide grant, contradicting the `ClientPermissionEvaluator` javadoc. In `hasPermission(ClientModel, String)`, evaluate the all-clients resource whenever the per-client evaluation fails to grant the requested scope.

mediumbugtests/base/src/test/java/org/keycloak/tests/admin/authz/fgap/PermissionClientTest.java:326-365

testMapRolesAndCompositesOnlyOneClient doesn't verify per-client scoping — Every operation in `testMapRolesAndCompositesOnlyOneClient` targets `myclient`, so a regression where one client's permission authorizes all clients would still pass. Add a negative check against a second client, as `testManageOnlyOneClient` does, and grant scopes separately. **Also found in:** - `tests/base/src/test/java/org/keycloak/tests/admin/authz/fgap/PermissionClientTest.java:191-247`

Gold bugs found (4)

highgetClientsWithPermission in ClientPermissionsV2 only finds the type-level 'Clients' resource and returns its name, never actual client ids, so callers that resolve the returned values as client ids get wrong results or null clients.
mediumClaims getResourceTypeResource can return null in ClientPermissionsV2.hasPermission, causing a NullPointerException in findByResource.
highIn ClientPermissionsV2.hasPermission, an admin granted a permission on all clients is denied on any client that also has its own per-client permission resource, because only the per-client resource is evaluated.
highClientPermissionsV2.canExchangeTo throws UnsupportedOperationException, but AbstractTokenExchangeProvider still calls clients().canExchangeTo for client-to-client exchange, so with token exchange and FGAP V2 enabled the request fails with a server error instead of a permission decision.

Gold bugs missed (4)

mediumClaims the removal-cleanup listener is guarded by the V1 fine-grained flag, so client/role/group permissions are orphaned when V1 is disabled or only V2 is enabled.
mediumClientPermissionsV2.canView() ignores a global MANAGE permission, so admins with only global manage take the unpaginated, per-client evaluated listing path.
highcanView(client) short-circuits on the global all-clients VIEW permission, so a client-specific permission denying an admin is never evaluated and the client is still viewable.
mediumThe negative check for removing a default client scope expects ForbiddenException, but it can come from the preceding getDefaultClientScopes() call, so the test passes even without a permission check on removal.