8000 Keep Admin CLI v2 requests and their bearer token on the configured server by yasser-rafid · Pull Request #55797 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

Keep Admin CLI v2 requests and their bearer token on the configured server - #55797

Open
yasser-rafid wants to merge 4 commits into
keycloak:mainfrom
yasser-rafid:fix-55552-admin-cli-v2-origin
Open

yasser-rafid wants to merge 4 commits into
keycloak:mainfrom
yasser-rafid:fix-55552-admin-cli-v2-origin

Conversation

@yasser-rafid
@yasser-rafid yasser-rafid commented Dec 3, 2026 •
Copy link
Copy Markdown

The v2 Admin CLI builds each request URL by appending the command descriptor's path to the configured server URL. A descriptor can come from a separately configured OpenAPI endpoint or from the on-disk cache, so its path decides where the admin bearer token is sent. For example, a path of @evil.example/... or .evil.example/... changes the host.

Changes:

  • Host check: executeRequest checks the final request URL before attaching the token. It must start with the configured server URL followed by /, and it must keep the same authority and stay under the server's context path. Any path segment that a server or proxy could read as . or .. is rejected. This includes percent-encoded and double-encoded dots, encoded / and \ separators, ; and %3b path parameters, and trailing control characters. Otherwise the command fails without sending anything.
  • Redirects: v2 requests no longer follow redirects. Apache HttpClient copies the Authorization header to the Location host, which could send the token elsewhere after the check. A 3xx response now gives the same "server responded with a redirect" error the v1 CLI already shows. HttpUtil.doRequest keeps its existing behaviour for other callers through a new overload.
  • Per-server cache: cached descriptors are stored per server, so two servers that report the same version no longer share and overwrite one file. Each file is named by the version plus a SHA-256 key of the server URL. Old version-only cache files are no longer read. The CLI falls back to the bundled descriptor until config credentials or config openapi runs again.
  • Unchanged: fetching the OpenAPI document stays unauthenticated.

Tests:

  • KcAdmV2RequestTargetTest: allowed and rejected URLs, including userinfo and host-suffix injection, port changes, context-path escapes, plain and encoded dot segments (the variants RedirectUtilsTest covers), and IPv6 and underscore hostnames.
  • KcAdmV2DescriptorCacheTest: updated for per-server files, and adds a check that one server's descriptor is never loaded for another.
  • HttpUtilRedirectTest: loopback servers verify that 301/302/303/307/308 redirects are not followed when disabled, that the token never reaches the second host, and that the default doRequest still follows redirects for v1.
  • KcAdmV2RequestTargetCliTest: runs the real kcadm --v2 command against loopback servers. A normal request still carries the token, while a crafted descriptor path and a redirect are both refused before the token leaves the configured server.

./mvnw -pl integration/client-cli/admin-cli test passes (205 tests).

I used an AI coding assistant (Claude Code) to help investigate and write this change, per the project's guidelines on generative AI. I have reviewed and tested the change and can revise it myself.

Closes #55552

…erver

The v2 Admin CLI appends the command descriptor's path to the configured
server URL. A descriptor fetched from a separate OpenAPI endpoint or read
from the cache could therefore change the host the admin token is sent to,
for example with a path starting with "@other.host/" or ".other.host/".

- Check the final request URL in executeRequest, before the token is
  attached: it must start with the configured server URL, keep the same
  authority and stay under the server's context path once dot segments
  (including %2e and ;-parameters) are resolved.
- Do not follow redirects for v2 requests, since the HTTP client copies
  the Authorization header to the redirect target; report them instead.
- Store cached descriptors per server, so servers that report the same
  version no longer share one descriptor file.

Closes keycloak#55552

Signed-off-by: Yasir Alnuwasir <68956108+yasser-rafid@users.noreply.github.com>
@yasser-rafid
yasser-rafid requested review from a team as code owners December 3, 2026 21:13
Copilot AI balanced review requested due to automatic review settings December 3, 2026 21:13
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 path check permits encoded traversal that can escape the configured server path and expose the bearer token to another application on the same origin.

1 open issue
What changed in this PR

This PR addresses #55552 by restricting Admin CLI v2 authenticated requests to the configured server and isolating cached command descriptors.

Changes:

  • Validates request targets and disables redirects for v2 while preserving other callers’ behavior.
  • Keys descriptor caches by server URL and version.
  • Adds request-target, redirect, cache-isolation, and CLI regression tests.
File Description
integration/​client-cli/​admin-cli/​src/​test/​java/​org/​keycloak/​client/​cli/​util/​HttpUtilRedirectTest.java Tests redirect controls and existing defaults.
integration/​client-cli/​admin-cli/​src/​test/​java/​org/​keycloak/​client/​admin/​cli/​v2/​KcAdmV2RequestTargetTest.java Tests allowed and rejected request URLs.
integration/​client-cli/​admin-cli/​src/​test/​java/​org/​keycloak/​client/​admin/​cli/​commands/​v2/​KcAdmV2RequestTargetCliTest.java Exercises token handling through CLI commands.
integration/​client-cli/​admin-cli/​src/​test/​java/​org/​keycloak/​client/​admin/​cli/​commands/​v2/​KcAdmV2DescriptorCacheTest.java Tests server isolation and cache compatibility.
integration/​client-cli/​admin-cli/​src/​main/​java/​org/​keycloak/​client/​cli/​util/​HttpUtil.java Adds per-request redirect control.
integration/​client-cli/​admin-cli/​src/​main/​java/​org/​keycloak/​client/​admin/​cli/​v2/​KcAdmV2RequestExecutor.java Validates targets before attaching tokens and rejects redirects.
integration/​client-cli/​admin-cli/​src/​main/​java/​org/​keycloak/​client/​admin/​cli/​v2/​KcAdmV2DescriptorCache.java Stores descriptors under server-specific cache keys.

Review effort: Balanced

Signed-off-by: Yasir Alnuwasir <68956108+yasser-rafid@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings December 3, 2026 21:22
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 five-pass decoding limit allows deeply encoded path traversal to bypass the guard, while unclosed redirect response bodies can exhaust the shared connection pool.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

Medium severity Close redirect response bodies before throwing

integration/​client-cli/​admin-cli/​src/​main/​java/​org/​keycloak/​client/​admin/​cli/​v2/​KcAdmV2RequestExecutor.java:229

If a redirect includes a response body, this throw leaves its stream unread and unclosed; unlike checkSuccess(), it never releases the pooled connection. Close the body before throwing so repeated CLI executions in the same JVM cannot exhaust the shared client's connection pool.

1 resolved since last review

Review effort: Balanced

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Yasir Alnuwasir <68956108+yasser-rafid@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings December 3, 2026 21:28
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

Unbounded repeated path decoding can stall the CLI, and mismatched loopback bindings cause tests to fail when IPv6 is preferred.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

Medium severity Bind the test server to IPv4 to match request URLs

integration/​client-cli/​admin-cli/​src/​test/​java/​org/​keycloak/​client/​cli/​util/​HttpUtilRedirectTest.java:31

With -Djava.net.preferIPv6Addresses=true, this binds the servers to ::1, but the request URLs use 127.0.0.1, so the tests fail with connection refused instead of exercising redirect handling. Bind explicitly to IPv4 to match those URLs; the same change is needed in integration/client-cli/admin-cli/src/test/java/org/keycloak/client/admin/cli/commands/v2/KcAdmV2RequestTargetCliTest.java:66.

1 resolved since last review

Review effort: Balanced

… to IPv4

Signed-off-by: Yasir Alnuwasir <68956108+yasser-rafid@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings December 3, 2026 21: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.

🔵 Needs a closer look

Human review is needed to assess the upgrade-documentation impact of the changed redirect and descriptor-cache behavior noted in an unpublished finding.

No open issues 🎉

1 resolved since last review

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.

Admin CLI v2 can send authenticated requests outside the configured Keycloak server origin

2 participants

0