|
| 1 | +/* |
| 2 | + * Copyright 2025 Red Hat, Inc. and/or its affiliates |
| 3 | + * and other contributors as indicated by the @author tags. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.keycloak.tests.admin; |
| 19 | + |
| 20 | +import org.apache.http.HttpResponse; |
| 21 | +import org.apache.http.client.HttpClient; |
| 22 | +import org.apache.http.client.methods.HttpGet; |
| 23 | +import org.apache.http.util.EntityUtils; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.junit.jupiter.params.ParameterizedTest; |
| 26 | +import org.junit.jupiter.params.provider.ValueSource; |
| 27 | +import org.keycloak.testframework.annotations.InjectHttpClient; |
| 28 | +import org.keycloak.testframework.annotations.KeycloakIntegrationTest; |
| 29 | +import org.keycloak.testframework.server.KeycloakServerConfig; |
| 30 | +import org.keycloak.testframework.server.KeycloakServerConfigBuilder; |
| 31 | + |
| 32 | +import java.util.Arrays; |
| 33 | +import java.util.Map; |
| 34 | + |
| 35 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 36 | +import static org.hamcrest.Matchers.containsString; |
| 37 | +import static org.hamcrest.Matchers.not; |
| 38 | +import static org.hamcrest.Matchers.startsWith; |
| 39 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 40 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 41 | + |
| 42 | +/** |
| 43 | + * @author Vaclav Muzikar <[email protected]> |
| 44 | + */ |
| 45 | +@KeycloakIntegrationTest(config = AdminRootTest.AdminUrlConfig.class) |
| 46 | +public class AdminRootTest { |
| 47 | + // This might not be robust enough. If something made KC on a different port, this would fail. |
| 48 | + private static final String HOSTNAME = "http://127.0.0.1.nip.io:8080"; |
| 49 | + private static final String HOSTNAME_ADMIN = "http://admin.127.0.0.1.nip.io:8080"; |
| 50 | + private static final String HOSTNAME_LOCAL_ADMIN = "http://localhost:8080"; |
| 51 | + |
| 52 | + @InjectHttpClient(followRedirects = false) |
| 53 | + private HttpClient client; |
| 54 | + |
| 55 | + @ParameterizedTest |
| 56 | + @ValueSource(strings = {HOSTNAME_ADMIN, HOSTNAME_LOCAL_ADMIN}) |
| 57 | + public void testRedirect(String hostname) throws Exception { |
| 58 | + HttpResponse response = client.execute(new HttpGet(hostname + "/admin")); |
| 59 | + |
| 60 | + assertEquals(302, response.getStatusLine().getStatusCode()); |
| 61 | + assertThat(response.getFirstHeader("Location").getValue(), startsWith(HOSTNAME_ADMIN + "/admin/master/console")); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testNoRedirectWithFrontendUrl() throws Exception { |
| 66 | + HttpResponse response = client.execute(new HttpGet(HOSTNAME + "/admin")); |
| 67 | + |
| 68 | + assertEquals(404, response.getStatusLine().getStatusCode()); |
| 69 | + |
| 70 | + // Check for leaks of hostname-admin in headers and body |
| 71 | + assertFalse(Arrays.stream(response.getAllHeaders()).anyMatch(header -> header.getValue().contains(HOSTNAME_ADMIN))); |
| 72 | + assertThat(EntityUtils.toString(response.getEntity()), not(containsString(HOSTNAME_ADMIN))); // just in case of a JS redirect |
| 73 | + } |
| 74 | + |
| 75 | + public static class AdminUrlConfig implements KeycloakServerConfig { |
| 76 | + @Override |
| 77 | + public KeycloakServerConfigBuilder configure(KeycloakServerConfigBuilder config) { |
| 78 | + return config.options(Map.of( |
| 79 | + "hostname", HOSTNAME, |
| 80 | + "hostname-admin", HOSTNAME_ADMIN |
| 81 | + )); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments