Skip to content

Do not look up the client when viewing a realm role - #55227

Open
Inf1mum wants to merge 2 commits into
keycloak:mainfrom
Inf1mum:fix/55225-realm-role-view-clients-403
Open

Inf1mum wants to merge 2 commits into
keycloak:mainfrom
Inf1mum:fix/55225-realm-role-view-clients-403

Conversation

@Inf1mum

@Inf1mum Inf1mum commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Fixes #55225

What

Guard the client lookup in RealmRoleTabs so it only runs for client roles.

Why

The useFetch callback was guarded by clientId, but the fetch itself was not. For a realm role, clientId is undefined and the {id} URI template of adminClient.clients.findOne({ id: clientId }) collapses to the clients list endpoint (GET /admin/realms/{realm}/clients/). That endpoint returns 403 for users who may manage realm roles without view-clients/query-clients permission, and useFetch forwards the error to the error boundary, which makes the whole realm-role page unusable. Regression introduced in #32249.

How

Return undefined instead of calling the admin client when there is no clientId, exactly as suggested in the issue. A new render test covers both paths:

  • a realm role must not trigger the client lookup (and must not hit the error boundary),
  • a client role still resolves the client and propagates its manage access flag.

Verification

  • pnpm exec vitest run in js/apps/admin-ui: 15 test files, 87 tests pass, including the 2 new ones; the new realm-role test fails without the fix (red-check).
  • tsc --noEmit, eslint and prettier --check pass on the changed files.

What:
Only run the client lookup in RealmRoleTabs when a clientId is present.

Why:
The useFetch callback was guarded by clientId, but the fetch itself was
not. For a realm role, clientId is undefined and the {id} URI template
of the client lookup collapses to the clients list endpoint, which
requires view-clients permission. Users who may manage realm roles
without that permission got a 403 that useFetch forwards to the error
boundary, leaving the whole realm-role page unusable. Regression
introduced in keycloak#32249.

How:
Return undefined instead of calling the admin client when there is no
clientId, and add render tests covering both paths: a realm role must
not trigger the client lookup, while a client role still resolves the
client and propagates its manage access flag.

Signed-off-by: Inf1mum <vadimlazuk5@gmail.com>
@Inf1mum
Inf1mum requested review from a team as code owners November 16, 2026 15:00
Copilot AI balanced review requested due to automatic review settings November 16, 2026 15:00

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 error-boundary test can pass without verifying boundary behavior because mocks.showBoundary is disconnected and the header assertion does not observe the provider’s error state.

1 open issue
What changed in this PR

Fixes #55225 by avoiding unnecessary client lookups when viewing realm roles in the Admin Console, allowing role managers without client-view permissions to access the page.

Changes:

  • Guard the client lookup with clientId, preserving lookup behavior for client roles.
  • Add render tests for skipped realm-role lookups and propagation of client-role manage access.
File Description
js/​apps/​admin-ui/​src/​realm-roles/​RealmRoleTabs.tsx Skip the client request when no client ID is present.
js/​apps/​admin-ui/​src/​realm-roles/​__tests__/​RealmRoleTabs.test.tsx Add regression tests for realm-role and client-role paths.

Review effort: Balanced

@edewit edewit 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.

Instead of a unit test it would be better to use playwright

What:
Replace the RealmRoleTabs unit test with a Playwright spec under
test/realm-roles/client-lookup.spec.ts.

Why:
Requested in the PR review: cover the regression with a Playwright test
instead of a mocked unit test. The mock harness could not demonstrate
the real failure mode where a role manager without view-clients
permission receives a 403 from the clients collection endpoint that
trips the error boundary and makes the realm role page unusable
(keycloak#55225).

How:
The spec creates a realm with a role manager granted only view-realm,
view-users and manage-realm, plus a client with a client role. The
clients collection endpoint is intercepted to respond 403 as the server
does for a user without view-clients. Opening and editing the realm
role must render the role page and never send that request, while a
client role must still resolve its client.

Signed-off-by: Inf1mum <vadimlazuk5@gmail.com>
Copilot AI balanced review requested due to automatic review settings November 17, 2026 11:18
@Inf1mum

Inf1mum commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

Reworked in e6e55c1: the mocked unit test is removed and replaced by a Playwright spec at js/apps/admin-ui/test/realm-roles/client-lookup.spec.ts, mirroring the existing patterns in test/realm-roles/main.spec.ts and test/events/list.spec.ts (dedicated realm via the admin client, login() helpers, table/sidebar utils).

It creates a realm with a role manager holding only view-realm, view-users and manage-realm, plus a client with a client role. The clients collection endpoint is intercepted to answer 403 — what the server returns to a user without view-clients — so opening and editing the realm role must render the page and never send that request. A second test asserts a client role still resolves its client (GET /clients/{id} responds 200).

Verification: eslint (type-aware), a scoped tsc --noEmit on the spec and the remaining vitest suite pass locally. Running Playwright locally needs a built console and a Keycloak server, which are not available on this machine, so the E2E run is left to the Admin UI E2E CI job on this PR.

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 regression test cannot reach the role page because its realm is disabled by default and its user lacks required profile fields, both of which block login.

2 open issues
1 resolved since last review

Review effort: Balanced

let clientObjectId: string;

test.beforeAll(async () => {
await adminClient.createRealm(realmName);
const { id } = await adminClient.createUser({
realm: realmName,
username: restrictedUser.username,
enabled: true,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

User without view-clients cannot edit a realm role

3 participants