Skip to content

Commit 25a6216

Browse files
committed
Merge #2712 into 1.1.4
2 parents abb9ccb + 228b499 commit 25a6216

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

reactor-netty-http/src/main/java/reactor/netty/http/server/DefaultHttpForwardedHeaderHandler.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020-2021 VMware, Inc. or its affiliates, All Rights Reserved.
2+
* Copyright (c) 2020-2023 VMware, Inc. or its affiliates, All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -89,8 +89,10 @@ private ConnectionInfo parseXForwardedInfo(ConnectionInfo connectionInfo, HttpRe
8989
}
9090
String hostHeader = request.headers().get(X_FORWARDED_HOST_HEADER);
9191
if (hostHeader != null) {
92-
String portHeader = request.headers().get(X_FORWARDED_PORT_HEADER);
9392
int port = connectionInfo.getHostAddress().getPort();
93+
connectionInfo = connectionInfo.withHostAddress(
94+
AddressUtils.parseAddress(hostHeader.split(",", 2)[0].trim(), port, DEFAULT_FORWARDED_HEADER_VALIDATION));
95+
String portHeader = request.headers().get(X_FORWARDED_PORT_HEADER);
9496
if (portHeader != null && !portHeader.isEmpty()) {
9597
String portStr = portHeader.split(",", 2)[0].trim();
9698
if (portStr.chars().allMatch(Character::isDigit)) {
@@ -99,9 +101,9 @@ private ConnectionInfo parseXForwardedInfo(ConnectionInfo connectionInfo, HttpRe
99101
else if (DEFAULT_FORWARDED_HEADER_VALIDATION) {
100102
throw new IllegalArgumentException("Failed to parse a port from " + portHeader);
101103
}
104+
connectionInfo = connectionInfo.withHostAddress(
105+
AddressUtils.createUnresolved(connectionInfo.getHostAddress().getHostString(), port));
102106
}
103-
connectionInfo = connectionInfo.withHostAddress(
104-
AddressUtils.createUnresolved(hostHeader.split(",", 2)[0].trim(), port));
105107
}
106108
String protoHeader = request.headers().get(X_FORWARDED_PROTO_HEADER);
107109
if (protoHeader != null) {

reactor-netty-http/src/test/java/reactor/netty/http/server/ConnectionInfoTests.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018-2021 VMware, Inc. or its affiliates, All Rights Reserved.
2+
* Copyright (c) 2018-2023 VMware, Inc. or its affiliates, All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -126,6 +126,17 @@ void xForwardedHost() {
126126
});
127127
}
128128

129+
@Test
130+
void xForwardedHostPortIncluded() {
131+
testClientRequest(
132+
clientRequestHeaders -> clientRequestHeaders.add("X-Forwarded-Host",
133+
"[1abc:2abc:3abc::5ABC:6abc]:9090, 192.168.0.1"),
134+
serverRequest -> {
135+
Assertions.assertThat(serverRequest.hostAddress().getHostString()).isEqualTo("1abc:2abc:3abc:0:0:0:5abc:6abc");
136+
Assertions.assertThat(serverRequest.hostAddress().getPort()).isEqualTo(9090);
137+
});
138+
}
139+
129140
@Test
130141
void xForwardedHostAndPort() {
131142
testClientRequest(
@@ -139,6 +150,19 @@ void xForwardedHostAndPort() {
139150
});
140151
}
141152

153+
@Test
154+
void xForwardedHostPortIncludedAndXForwardedPort() {
155+
testClientRequest(
156+
clientRequestHeaders -> {
157+
clientRequestHeaders.add("X-Forwarded-Host", "192.168.0.1:9090");
158+
clientRequestHeaders.add("X-Forwarded-Port", "8080");
159+
},
160+
serverRequest -> {
161+
Assertions.assertThat(serverRequest.hostAddress().getHostString()).isEqualTo("192.168.0.1");
162+
Assertions.assertThat(serverRequest.hostAddress().getPort()).isEqualTo(8080);
163+
});
164+
}
165+
142166
@Test
143167
void xForwardedMultipleHeaders() {
144168
testClientRequest(

0 commit comments

Comments
 (0)