diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/ForwardedRequestCustomizer.java b/jetty-server/src/main/java/org/eclipse/jetty/server/ForwardedRequestCustomizer.java index 93a69857792..4d224dfe3cb 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/ForwardedRequestCustomizer.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/ForwardedRequestCustomizer.java @@ -589,13 +589,14 @@ public class ForwardedRequestCustomizer implements Customizer } } + @SuppressWarnings("unused") public void handleHost(HttpField field) { if (getForwardedPortAsAuthority() && !StringUtil.isEmpty(getForwardedPortHeader())) { if (_host == null) _host = new PossiblyPartialHostPort(getLeftMost(field.getValue())); - else if (_for instanceof PortSetHostPort) + else if (_host instanceof PortSetHostPort) _host = new HostPort(HostPort.normalizeHost(getLeftMost(field.getValue())), _host.getPort()); } else if (_host == null) diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/ForwardedRequestCustomizerTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/ForwardedRequestCustomizerTest.java index 9a60e7f20d6..a6dfa9f58a2 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/ForwardedRequestCustomizerTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/ForwardedRequestCustomizerTest.java @@ -397,6 +397,34 @@ public class ForwardedRequestCustomizerTest .requestURL("http://myhost:4444/") .remoteAddr("192.168.1.200").remotePort(0) ), + Arguments.of(new Request("X-Forwarded-* (all headers except server)") + .headers( + "GET / HTTP/1.1", + "Host: myhost", + "X-Forwarded-Proto: https", + "X-Forwarded-Host: www.example.com", + "X-Forwarded-Port: 4333", + "X-Forwarded-For: 8.5.4.3:2222" + ), + new Expectations() + .scheme("https").serverName("www.example.com").serverPort(4333) + .requestURL("https://www.example.com:4333/") + .remoteAddr("8.5.4.3").remotePort(2222) + ), + Arguments.of(new Request("X-Forwarded-* (all headers except server, port first)") + .headers( + "GET / HTTP/1.1", + "Host: myhost", + "X-Forwarded-Proto: https", + "X-Forwarded-Port: 4333", + "X-Forwarded-Host: www.example.com", + "X-Forwarded-For: 8.5.4.3:2222" + ), + new Expectations() + .scheme("https").serverName("www.example.com").serverPort(4333) + .requestURL("https://www.example.com:4333/") + .remoteAddr("8.5.4.3").remotePort(2222) + ), Arguments.of(new Request("X-Forwarded-* (all headers)") .headers( "GET / HTTP/1.1", @@ -427,6 +455,21 @@ public class ForwardedRequestCustomizerTest .requestURL("https://www.example.com:4333/") .remoteAddr("8.5.4.3").remotePort(2222) ), + Arguments.of(new Request("X-Forwarded-* (all headers reversed)") + .headers( + "GET / HTTP/1.1", + "Host: myhost", + "X-Forwarded-Server: fw.example.com", + "X-Forwarded-For: 8.5.4.3:2222", + "X-Forwarded-Port: 4333", + "X-Forwarded-Host: www.example.com", + "X-Forwarded-Proto: https" + ), + new Expectations() + .scheme("https").serverName("www.example.com").serverPort(4333) + .requestURL("https://www.example.com:4333/") + .remoteAddr("8.5.4.3").remotePort(2222) + ), Arguments.of(new Request("X-Forwarded-* (Server and Port)") .headers( "GET / HTTP/1.1", @@ -440,6 +483,19 @@ public class ForwardedRequestCustomizerTest .requestURL("http://fw.example.com:4333/") .remoteAddr("8.5.4.3").remotePort(2222) ), + Arguments.of(new Request("X-Forwarded-* (Port and Server)") + .headers( + "GET / HTTP/1.1", + "Host: myhost", + "X-Forwarded-Port: 4333", + "X-Forwarded-For: 8.5.4.3:2222", + "X-Forwarded-Server: fw.example.com" + ), + new Expectations() + .scheme("http").serverName("fw.example.com").serverPort(4333) + .requestURL("http://fw.example.com:4333/") + .remoteAddr("8.5.4.3").remotePort(2222) + ), // ================================================================= // Mixed Behavior