Skip to content

Commit

Permalink
Merge #2761 into 1.1.6
Browse files Browse the repository at this point in the history
  • Loading branch information
pderop committed Apr 4, 2023
2 parents 60be3a0 + 77621ce commit 5fc9ab9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,25 +98,31 @@ private ConnectionInfo parseXForwardedInfo(ConnectionInfo connectionInfo, HttpRe
}
String hostHeader = request.headers().get(X_FORWARDED_HOST_HEADER);
if (hostHeader != null) {
String scheme = connectionInfo.getScheme();
int port = scheme.equalsIgnoreCase("https") || scheme.equalsIgnoreCase("wss") ?
DEFAULT_HTTPS_PORT : DEFAULT_HTTP_PORT;
connectionInfo = connectionInfo.withHostAddress(
AddressUtils.parseAddress(hostHeader.split(",", 2)[0].trim(), port, DEFAULT_FORWARDED_HEADER_VALIDATION));
String portHeader = request.headers().get(X_FORWARDED_PORT_HEADER);
if (portHeader != null && !portHeader.isEmpty()) {
String portStr = portHeader.split(",", 2)[0].trim();
if (portStr.chars().allMatch(Character::isDigit)) {
port = Integer.parseInt(portStr);
}
else if (DEFAULT_FORWARDED_HEADER_VALIDATION) {
throw new IllegalArgumentException("Failed to parse a port from " + portHeader);
}
connectionInfo = connectionInfo.withHostAddress(
AddressUtils.createUnresolved(connectionInfo.getHostAddress().getHostString(), port));
AddressUtils.parseAddress(hostHeader.split(",", 2)[0].trim(),
getDefaultHostPort(connectionInfo), DEFAULT_FORWARDED_HEADER_VALIDATION));
}

String portHeader = request.headers().get(X_FORWARDED_PORT_HEADER);
if (portHeader != null && !portHeader.isEmpty()) {
String portStr = portHeader.split(",", 2)[0].trim();
if (portStr.chars().allMatch(Character::isDigit)) {
int port = Integer.parseInt(portStr);
connectionInfo = new ConnectionInfo(
AddressUtils.createUnresolved(connectionInfo.getHostAddress().getHostString(), port),
connectionInfo.getHostName(), port, connectionInfo.getRemoteAddress(), connectionInfo.getScheme());
}
else if (DEFAULT_FORWARDED_HEADER_VALIDATION) {
throw new IllegalArgumentException("Failed to parse a port from " + portHeader);
}
}
return connectionInfo;
}

private int getDefaultHostPort(ConnectionInfo connectionInfo) {
String scheme = connectionInfo.getScheme();
return scheme.equalsIgnoreCase("https") || scheme.equalsIgnoreCase("wss") ?
DEFAULT_HTTPS_PORT : DEFAULT_HTTP_PORT;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,32 @@ void xForwardedForMultipleHostAndPortAndProto() {
});
}

@Test
void xForwardedForAndHostOnly() throws SSLException {
SslContext clientSslContext = SslContextBuilder.forClient()
.trustManager(InsecureTrustManagerFactory.INSTANCE).build();
SslContext serverSslContext = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();

testClientRequest(
clientRequestHeaders -> {
clientRequestHeaders.add("Host", "a.example.com");
clientRequestHeaders.add("X-Forwarded-For", "192.168.0.1");
clientRequestHeaders.add("X-Forwarded-Port", "8443");
clientRequestHeaders.add("X-Forwarded-Proto", "https");
},
serverRequest -> {
Assertions.assertThat(serverRequest.remoteAddress().getHostString()).isEqualTo("192.168.0.1");
Assertions.assertThat(serverRequest.hostAddress().getHostString())
.containsPattern("^0:0:0:0:0:0:0:1(%\\w*)?|127.0.0.1$");
Assertions.assertThat(serverRequest.hostPort()).isEqualTo(8443);
Assertions.assertThat(serverRequest.hostName()).isEqualTo("a.example.com");
Assertions.assertThat(serverRequest.scheme()).isEqualTo("https");
},
httpClient -> httpClient.secure(ssl -> ssl.sslContext(clientSslContext)),
httpServer -> httpServer.secure(ssl -> ssl.sslContext(serverSslContext)),
true);
}

@Test
void customForwardedHandlerForMultipleHost() {
testClientRequest(
Expand Down

0 comments on commit 5fc9ab9

Please sign in to comment.