Skip to content

Commit 6229545

Browse files
committed
trustProxy: keep the Host header port
With trustProxy enabled and no X-Forwarded-Host header, the port of the Host header was dropped unless the host was "localhost", and the default port followed the scheme of the connection instead of X-Forwarded-Proto. Behind a TLS-terminating proxy a request for https://myhost:90/ reported https://myhost/ as its URL.
1 parent 89370b8 commit 6229545

3 files changed

Lines changed: 89 additions & 6 deletions

File tree

‎jooby/src/main/java/io/jooby/internal/ProxyPeerAddress.java‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ public static ProxyPeerAddress parse(Context ctx) {
5656
String forwardedProto = ctx.header(X_FORWARDED_PROTO).toOptional().orElseGet(ctx::getScheme);
5757
result.scheme = mostRecent(forwardedProto);
5858

59-
String forwardedHost = ctx.header(X_FORWARDED_HOST).toOptional().orElseGet(ctx::getHost);
59+
String forwardedHost =
60+
ctx.header(X_FORWARDED_HOST).toOptional().orElseGet(() -> hostAndPort(ctx));
6061
String forwardedPort = ctx.header(X_FORWARDED_PORT).valueOrNull();
6162

6263
String value = mostRecent(forwardedHost);
@@ -88,22 +89,30 @@ public static ProxyPeerAddress parse(Context ctx) {
8889
try {
8990
result.port = Integer.parseInt(mostRecent(forwardedPort));
9091
} catch (NumberFormatException ignore) {
91-
result.port = defaultPort(ctx, hostHeader);
92+
result.port = defaultPort(ctx, hostHeader, result.scheme);
9293
}
9394
} else {
94-
result.port = defaultPort(ctx, hostHeader);
95+
result.port = defaultPort(ctx, hostHeader, result.scheme);
9596
}
9697

9798
result.host = hostHeader;
9899

99100
return result;
100101
}
101102

102-
private static int defaultPort(Context ctx, String host) {
103+
private static String hostAndPort(Context ctx) {
104+
// getHostAndPort() drops the brackets of an IPv6 address without a port, so use it only when
105+
// it is the host followed by a port
106+
String host = ctx.getHost();
107+
String value = ctx.getHostAndPort();
108+
return value.startsWith(host + ":") ? value : host;
109+
}
110+
111+
private static int defaultPort(Context ctx, String host, String scheme) {
103112
if ("localhost".equals(host)) {
104113
return ctx.getServerPort();
105114
}
106-
return ctx.isSecure() ? 443 : 80;
115+
return "https".equals(scheme) ? 443 : 80;
107116
}
108117

109118
private static String mostRecent(String header) {

‎jooby/src/test/java/io/jooby/internal/ProxyPeerAddressTest.java‎

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ private Context setupBaseContext() {
3737
when(ctx.getRemoteAddress()).thenReturn("127.0.0.1");
3838
when(ctx.getScheme()).thenReturn("http");
3939
when(ctx.getHost()).thenReturn("localhost");
40+
when(ctx.getHostAndPort()).thenReturn("localhost");
4041
when(ctx.getServerPort()).thenReturn(8080);
4142
when(ctx.isSecure()).thenReturn(false);
4243

@@ -90,6 +91,72 @@ void testParseIPv4WithPort() {
9091
assertEquals(9090, peer.getPort());
9192
}
9293

94+
@Test
95+
@DisplayName("Test Host header port is kept when X-Forwarded-Host is missing")
96+
void testParseHostHeaderWithPort() {
97+
Context ctx = setupBaseContext();
98+
when(ctx.getHost()).thenReturn("api.jooby.io");
99+
when(ctx.getHostAndPort()).thenReturn("api.jooby.io:9090");
100+
mockHeader(ctx, "X-Forwarded-Proto", "https");
101+
102+
ProxyPeerAddress peer = ProxyPeerAddress.parse(ctx);
103+
104+
assertEquals("api.jooby.io", peer.getHost());
105+
assertEquals(9090, peer.getPort());
106+
}
107+
108+
@Test
109+
@DisplayName("Test localhost Host header port wins over the server port")
110+
void testParseLocalhostHostHeaderWithPort() {
111+
Context ctx = setupBaseContext();
112+
when(ctx.getHostAndPort()).thenReturn("localhost:9090");
113+
114+
ProxyPeerAddress peer = ProxyPeerAddress.parse(ctx);
115+
116+
assertEquals("localhost", peer.getHost());
117+
assertEquals(9090, peer.getPort());
118+
}
119+
120+
@Test
121+
@DisplayName("Test IPv6 Host header port is kept when X-Forwarded-Host is missing")
122+
void testParseIPv6HostHeaderWithPort() {
123+
Context ctx = setupBaseContext();
124+
when(ctx.getHost()).thenReturn("[2001:db8::1]");
125+
when(ctx.getHostAndPort()).thenReturn("[2001:db8::1]:9090");
126+
127+
ProxyPeerAddress peer = ProxyPeerAddress.parse(ctx);
128+
129+
assertEquals("[2001:db8::1]", peer.getHost());
130+
assertEquals(9090, peer.getPort());
131+
}
132+
133+
@Test
134+
@DisplayName("Test IPv6 Host header without port when X-Forwarded-Host is missing")
135+
void testParseIPv6HostHeaderNoPort() {
136+
Context ctx = setupBaseContext();
137+
// getHostAndPort() drops the brackets of an IPv6 address that has no port
138+
when(ctx.getHost()).thenReturn("[2001:db8::1]");
139+
when(ctx.getHostAndPort()).thenReturn("2001:db8::1");
140+
141+
ProxyPeerAddress peer = ProxyPeerAddress.parse(ctx);
142+
143+
assertEquals("[2001:db8::1]", peer.getHost());
144+
assertEquals(80, peer.getPort());
145+
}
146+
147+
@Test
148+
@DisplayName("Test default port follows X-Forwarded-Proto")
149+
void testDefaultPortFollowsForwardedProto() {
150+
Context ctx = setupBaseContext();
151+
mockHeader(ctx, "X-Forwarded-Proto", "https");
152+
mockHeader(ctx, "X-Forwarded-Host", "jooby.io");
153+
154+
ProxyPeerAddress peer = ProxyPeerAddress.parse(ctx);
155+
156+
assertEquals("https", peer.getScheme());
157+
assertEquals(443, peer.getPort());
158+
}
159+
93160
@Test
94161
@DisplayName("Test X-Forwarded-Port taking precedence over Host port")
95162
void testPortPrecedence() {
@@ -108,7 +175,7 @@ void testPortPrecedence() {
108175
void testParseIPv6NoPort() {
109176
Context ctx = setupBaseContext();
110177
mockHeader(ctx, "X-Forwarded-Host", "[2001:db8::1]");
111-
when(ctx.isSecure()).thenReturn(true);
178+
when(ctx.getScheme()).thenReturn("https");
112179

113180
ProxyPeerAddress peer = ProxyPeerAddress.parse(ctx);
114181

‎tests/src/test/java/io/jooby/test/FeaturedTest.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3833,6 +3833,13 @@ public void requestUrl(ServerTestRunner runner) {
38333833
assertEquals(
38343834
"http://localhost:" + client.getPort() + "/somepath", rsp.body().string());
38353835
});
3836+
client.header("Host", "myhost:90");
3837+
client.header("X-Forwarded-Proto", "https");
3838+
client.get(
3839+
"/somepath",
3840+
rsp -> {
3841+
assertEquals("https://myhost:90/somepath", rsp.body().string());
3842+
});
38363843
client.header("X-Forwarded-Host", "myhost");
38373844
client.get(
38383845
"/somepath",

0 commit comments

Comments
 (0)