Skip to content

Commit 8779484

Browse files
committed
context: handle IPv6 addresses in getHost() and getPort()- #4030
fixes #4030 - handle `::1` or `::` IPv6 - add more tests
1 parent 94d2244 commit 8779484

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

‎jooby/src/main/java/io/jooby/DefaultContext.java‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,14 @@ default String getHostAndPort() {
394394
header.orElseGet(
395395
() ->
396396
ofNullable(header("Host").valueOrNull())
397-
.orElseGet(() -> getServerHost() + ":" + getServerPort()));
397+
.orElseGet(
398+
() -> {
399+
String sh = getServerHost();
400+
if (sh.indexOf(':') != -1 && !sh.startsWith("[")) {
401+
return "[" + sh + "]:" + getServerPort();
402+
}
403+
return sh + ":" + getServerPort();
404+
}));
398405
int i = value.indexOf(',');
399406
String host = i > 0 ? value.substring(0, i).trim() : value;
400407
if (host.startsWith("[") && host.endsWith("]")) {

‎jooby/src/test/java/io/jooby/DefaultContextTest.java‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,50 @@ void requestURLIPv6() {
582582
assertEquals("http://[::1]:8080/path?q=1", ctx.getRequestURL());
583583
}
584584

585+
@Test
586+
void hostAndPortIPv6ServerFallback() {
587+
ServerOptions options = new ServerOptions().setPort(9090).setHost("::1");
588+
doReturn(options).when(ctx).require(ServerOptions.class);
589+
doReturn(false).when(ctx).isSecure();
590+
591+
Value mockHostHeader = mock(Value.class);
592+
when(mockHostHeader.valueOrNull()).thenReturn(null);
593+
doReturn(mockHostHeader).when(ctx).header("Host");
594+
595+
// Without brackets in the fallback, this would fail and return "[::1:9090]" / 80
596+
assertEquals("[::1]:9090", ctx.getHostAndPort());
597+
assertEquals("[::1]", ctx.getHost());
598+
assertEquals(9090, ctx.getPort());
599+
}
600+
601+
@Test
602+
void hostAndPortBareIPv6() {
603+
doReturn(false).when(ctx).isSecure();
604+
605+
Value mockHostHeader = mock(Value.class);
606+
when(mockHostHeader.valueOrNull()).thenReturn("2001:db8::1");
607+
doReturn(mockHostHeader).when(ctx).header("Host");
608+
609+
// Ensures getHost() successfully detects and wraps unbracketed IPv6 headers
610+
assertEquals("2001:db8::1", ctx.getHostAndPort());
611+
assertEquals("[2001:db8::1]", ctx.getHost());
612+
assertEquals(80, ctx.getPort());
613+
}
614+
615+
@Test
616+
void hostAndPortIPv6WithProxy() {
617+
when(router.getRouterOptions()).thenReturn(new RouterOptions().setTrustProxy(true));
618+
Value mockProxyHeader = mock(Value.class);
619+
620+
// Multiple proxy hops, first one is IPv6 with a port
621+
when(mockProxyHeader.toOptional()).thenReturn(Optional.of("[2001:db8::1]:8443, [::1]"));
622+
doReturn(mockProxyHeader).when(ctx).header("X-Forwarded-Host");
623+
624+
assertEquals("[2001:db8::1]:8443", ctx.getHostAndPort());
625+
assertEquals("[2001:db8::1]", ctx.getHost());
626+
assertEquals(8443, ctx.getPort());
627+
}
628+
585629
@Test
586630
void securePortFallback() {
587631
ServerOptions options = new ServerOptions().setPort(8080); // securePort is null

0 commit comments

Comments
 (0)