Repository navigation
fix: relaxes the admin root redirect check #39095
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,6 @@ | |
| import org.eclipse.microprofile.openapi.annotations.Operation; | ||
| import org.jboss.logging.Logger; | ||
| import org.keycloak.http.HttpRequest; | ||
| import org.keycloak.http.HttpResponse; | ||
| import jakarta.ws.rs.NotFoundException; | ||
| import jakarta.ws.rs.NotAuthorizedException; | ||
| import org.keycloak.common.Profile; | ||
|
|
@@ -35,6 +34,7 @@ | |
| import org.keycloak.services.managers.AppAuthManager; | ||
| import org.keycloak.services.managers.AuthenticationManager; | ||
| import org.keycloak.services.managers.RealmManager; | ||
| import org.keycloak.services.resources.WelcomeResource; | ||
| import org.keycloak.services.resources.admin.info.ServerInfoAdminResource; | ||
| import org.keycloak.services.resources.admin.permissions.AdminPermissions; | ||
| import org.keycloak.theme.Theme; | ||
|
|
@@ -94,19 +94,31 @@ public static UriBuilder adminBaseUrl(UriBuilder base) { | |
| @GET | ||
| @Operation(hidden = true) | ||
| public Response masterRealmAdminConsoleRedirect() { | ||
| String requestUrl = session.getContext().getUri().getRequestUri().toString(); | ||
| KeycloakUriInfo adminUriInfo = session.getContext().getUri(UrlType.ADMIN); | ||
| if (shouldRedirect(adminUriInfo)) { | ||
| RealmModel master = new RealmManager(session).getKeycloakAdminstrationRealm(); | ||
| return Response.status(302).location( | ||
| adminUriInfo.getBaseUriBuilder().path(AdminRoot.class).path(AdminRoot.class, "getAdminConsole").path("/").build(master.getName()) | ||
| ).build(); | ||
| } | ||
| return Response.status(Response.Status.NOT_FOUND).build(); | ||
| } | ||
|
|
||
| boolean shouldRedirect(KeycloakUriInfo adminUriInfo) { | ||
| if (!isAdminConsoleEnabled()) { | ||
| return false; | ||
| } | ||
| KeycloakUriInfo frontEndUriInfo = session.getContext().getUri(); | ||
| String frontEndUrl = frontEndUriInfo.getBaseUri().toString(); | ||
| String adminUrl = adminUriInfo.getBaseUri().toString(); | ||
| String localAdminUrl = session.getContext().getUri(UrlType.LOCAL_ADMIN).getBaseUri().toString(); | ||
|
|
||
| if (!isAdminConsoleEnabled() || (!requestUrl.startsWith(adminUrl) && !requestUrl.startsWith(localAdminUrl))) { | ||
| return Response.status(Response.Status.NOT_FOUND).build(); | ||
| if (adminUrl.equals(frontEndUrl)) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This makes sense to me - the issue I reported would enter here, my frontend and admin are the same. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I built a new In another window I curled the LGTM! |
||
| return true; // admin is the same as front-end, we're not leaking information | ||
| } | ||
| String requestUrl = frontEndUriInfo.getRequestUri().toString(); | ||
|
|
||
| RealmModel master = new RealmManager(session).getKeycloakAdminstrationRealm(); | ||
| return Response.status(302).location( | ||
| adminUriInfo.getBaseUriBuilder().path(AdminRoot.class).path(AdminRoot.class, "getAdminConsole").path("/").build(master.getName()) | ||
| ).build(); | ||
| // if we're using the admin url or are local, it's also safe to redirect | ||
| return requestUrl.startsWith(adminUrl) || WelcomeResource.isLocal(session); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -282,10 +294,6 @@ public Object getServerInfo() { | |
| return new ServerInfoAdminResource(session); | ||
| } | ||
|
|
||
| private HttpResponse getHttpResponse() { | ||
| return session.getContext().getHttpResponse(); | ||
| } | ||
|
|
||
| private HttpRequest getHttpRequest() { | ||
| return session.getContext().getHttpRequest(); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* | ||
| * Copyright 2025 Red Hat, Inc. and/or its affiliates | ||
| * and other contributors as indicated by the @author tags. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.keycloak.tests.admin; | ||
|
|
||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.hamcrest.Matchers.startsWith; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import org.apache.http.HttpResponse; | ||
| import org.apache.http.client.HttpClient; | ||
| import org.apache.http.client.methods.HttpGet; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.ValueSource; | ||
| import org.keycloak.testframework.annotations.InjectHttpClient; | ||
| import org.keycloak.testframework.annotations.KeycloakIntegrationTest; | ||
| import org.keycloak.testframework.server.KeycloakServerConfig; | ||
| import org.keycloak.testframework.server.KeycloakServerConfigBuilder; | ||
|
|
||
| /** | ||
| * @author Vaclav Muzikar <vmuzikar@redhat.com> | ||
| */ | ||
| @KeycloakIntegrationTest(config = AdminRootEdgeTest.AdminUrlConfig.class) | ||
| public class AdminRootEdgeTest { | ||
| // full url with https, with default hostname-admin | ||
| private static final String HOSTNAME = "https://127.0.0.1.nip.io:8080"; | ||
|
|
||
| @InjectHttpClient(followRedirects = false) | ||
| private HttpClient client; | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource(strings = {"http://127.0.0.1:8080", "http://127.0.0.1.nip.io:8080"}) | ||
| public void testRedirect(String hostname) throws Exception { | ||
| HttpResponse response = client.execute(new HttpGet(hostname + "/admin")); | ||
|
|
||
| assertEquals(302, response.getStatusLine().getStatusCode()); | ||
| assertThat(response.getFirstHeader("Location").getValue(), startsWith(HOSTNAME + "/admin/master/console")); | ||
| } | ||
|
|
||
| public static class AdminUrlConfig implements KeycloakServerConfig { | ||
| @Override | ||
| public KeycloakServerConfigBuilder configure(KeycloakServerConfigBuilder config) { | ||
| return config.options(Map.of( | ||
| "hostname", HOSTNAME | ||
| )); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The heading should provide more context, for example:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's both the enum and the template variable that are being deprecated.