fix(ui): preserve bracketed IPv6 hosts in sidecar redirect URLs (#5538)

Preserve the bracketed IPv6 Host literal when replacing the request
port with the published UI sidecar port. Cover the public status method
and both HTTP status routes without starting a Docker sidecar.

Signed-off-by: Liu Jie <maxazure@gmail.com>
This commit is contained in:
Liu Jie authored and GitHub committed 2026-10-13 23:15:09 -05:00
1 parent 255162654f
commit 5a90be1049
3 files changed
+138

No files matched your search

@@ -63,6 +63,13 @@ public class UiController {
private static String redirectUrl(String host, int port) {
String hostname = (host == null || host.isBlank()) ? "localhost" : host;
int colon = hostname.indexOf(':');
if (hostname.startsWith("[")) {
int closingBracket = hostname.indexOf(']');
if (closingBracket == hostname.length() - 1
|| (closingBracket >= 0 && hostname.charAt(closingBracket + 1) == ':')) {
colon = hostname.indexOf(':', closingBracket + 1);
}
}
if (colon >= 0) {
hostname = hostname.substring(0, colon);
}
@@ -0,0 +1,85 @@
package io.github.hectorvent.floci.lifecycle;
import io.github.hectorvent.floci.lifecycle.UiController.StatusResponse;
import io.github.hectorvent.floci.services.floci.ui.FlociUiManager;
import io.github.hectorvent.floci.services.floci.ui.FlociUiManager.UiStatus;
import io.github.hectorvent.floci.services.floci.ui.UiPages;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.ValueSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class UiControllerTest {
private FlociUiManager uiManager;
private UiController controller;
@BeforeEach
void setUp() {
uiManager = mock(FlociUiManager.class);
controller = new UiController(uiManager, mock(UiPages.class));
}
@ParameterizedTest
@CsvSource({
"[::1]:4566, http://[::1]:4500/",
"[::1], http://[::1]:4500/",
"[2001:db8::1]:4566, http://[2001:db8::1]:4500/",
"[2001:db8::1], http://[2001:db8::1]:4500/",
"floci.example:4566, http://floci.example:4500/",
"floci.example, http://floci.example:4500/",
"127.0.0.1:4566, http://127.0.0.1:4500/",
"127.0.0.1, http://127.0.0.1:4500/"
})
void readyStatusReplacesOnlyTheHostPort(String host, String url) {
when(uiManager.status()).thenReturn(new UiStatus(true, true, 4500, null));
assertEquals(new StatusResponse(true, url, null), status(host));
}
@ParameterizedTest
@CsvSource({
"[::1]@evil.example:4566, http://[:4500/",
"[::1, http://[:4500/",
"::1, http://:4500/"
})
void malformedHostsKeepTheExistingBehavior(String host, String url) {
when(uiManager.status()).thenReturn(new UiStatus(true, true, 4500, null));
assertEquals(new StatusResponse(true, url, null), status(host));
}
@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = " ")
void missingHostDefaultsToLocalhost(String host) {
when(uiManager.status()).thenReturn(new UiStatus(true, true, 4500, null));
assertEquals(new StatusResponse(true, "http://localhost:4500/", null), status(host));
}
@ParameterizedTest
@CsvSource({"false, 4500", "true, 0", "true, -1"})
void statusWithoutAReadyPublishedPortHasNoUrl(boolean ready, int port) {
when(uiManager.status()).thenReturn(new UiStatus(true, ready, port, null));
assertEquals(new StatusResponse(false, null, null), status("[::1]:4566"));
}
@Test
void statusErrorTakesPrecedenceOverReadiness() {
when(uiManager.status()).thenReturn(new UiStatus(true, true, 4500, "sidecar failed"));
assertEquals(new StatusResponse(false, null, "sidecar failed"), status("[::1]:4566"));
}
private StatusResponse status(String host) {
return (StatusResponse) controller.status(host).getEntity();
}
}
@@ -1,11 +1,20 @@
package io.github.hectorvent.floci.services.floci.ui;
import io.github.hectorvent.floci.services.floci.ui.FlociUiManager.UiStatus;
import io.quarkus.test.InjectMock;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import java.util.List;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.mockito.Mockito.when;
/**
* Verifies the browser landing page content-negotiation on {@code /} and the
@@ -15,6 +24,43 @@ import static org.hamcrest.Matchers.is;
@QuarkusTest
class UiLandingIntegrationTest {
@InjectMock
FlociUiManager uiManager;
@BeforeEach
void sidecarHasNotStarted() {
when(uiManager.status()).thenReturn(new UiStatus(false, false, 0, null));
}
@ParameterizedTest
@CsvSource({
"[::1]:4566, http://[::1]:4500/",
"[::1], http://[::1]:4500/",
"[2001:db8::1]:4566, http://[2001:db8::1]:4500/",
"[2001:db8::1], http://[2001:db8::1]:4500/",
"floci.example:4566, http://floci.example:4500/",
"floci.example, http://floci.example:4500/",
"127.0.0.1:4566, http://127.0.0.1:4500/",
"127.0.0.1, http://127.0.0.1:4500/"
})
void readyStatusUsesTheBrowserHostAndPublishedPort(String host, String url) {
when(uiManager.status()).thenReturn(new UiStatus(true, true, 4500, null));
for (String prefix : List.of("_floci", "_localstack")) {
given()
.header("Host", host)
.accept("application/json")
.when()
.get("/" + prefix + "/ui/status")
.then()
.statusCode(200)
.body("ready", is(true))
.body("url", is(url))
.body("error", nullValue());
}
}
@Test
void browserAcceptHtmlGetsLandingPage() {
given()