You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Keep Admin CLI v2 requests and their bearer token on the configured server - #55797
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.
…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.
Closeskeycloak#55552
Signed-off-by: Yasir Alnuwasir <68956108+yasser-rafid@users.noreply.github.com>
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.
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.
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.
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.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
executeRequestchecks 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%3bpath parameters, and trailing control characters. Otherwise the command fails without sending anything.Authorizationheader to theLocationhost, 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.doRequestkeeps its existing behaviour for other callers through a new overload.config credentialsorconfig openapiruns again.Tests:
KcAdmV2RequestTargetTest: allowed and rejected URLs, including userinfo and host-suffix injection, port changes, context-path escapes, plain and encoded dot segments (the variantsRedirectUtilsTestcovers), 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 defaultdoRequeststill follows redirects for v1.KcAdmV2RequestTargetCliTest: runs the realkcadm --v2command 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 testpasses (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