From 944ce6354fe1db9f199756460893142c70af5f55 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Thu, 2 Jun 2022 14:17:26 -0500 Subject: [PATCH] Fixing #7975 - ForwardedRequestCustomizer should clear old MethodHandles when renaming headers. (#8102) * Adding test case to prove report * Fixing updateHandles() to clear the stored handles list. Signed-off-by: Joakim Erdfelt --- .../server/ForwardedRequestCustomizer.java | 2 + .../ForwardedRequestCustomizerTest.java | 51 +++++++++++++++++++ 2 files changed, 53 insertions(+) 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 b7d6f343ea0..6f2538e4070 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 @@ -593,6 +593,8 @@ public class ForwardedRequestCustomizer implements Customizer private void updateHandles() { + _handles.clear(); + MethodHandles.Lookup lookup = MethodHandles.lookup(); try { 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 d73ee634360..ac7939601bd 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 @@ -1112,6 +1112,57 @@ public class ForwardedRequestCustomizerTest assertThat("status", response.getStatus(), is(400)); } + public static Stream customHeaderNameRequestCases() + { + return Stream.of( + Arguments.of(new Request("Old name then new name") + .headers( + "GET / HTTP/1.1", + "Host: myhost", + "X-Forwarded-For: 1.1.1.1", + "X-Custom-For: 2.2.2.2" + ) + .configureCustomizer((forwardedRequestCustomizer) -> + forwardedRequestCustomizer.setForwardedForHeader("X-Custom-For")), + new Expectations() + .scheme("http").serverName("myhost").serverPort(80) + .secure(false) + .requestURL("http://myhost/") + .remoteAddr("2.2.2.2").remotePort(0) + ), + Arguments.of(new Request("New name then old name") + .headers( + "GET / HTTP/1.1", + "Host: myhost", + "X-Custom-For: 2.2.2.2", + "X-Forwarded-For: 1.1.1.1" + ) + .configureCustomizer((forwardedRequestCustomizer) -> + forwardedRequestCustomizer.setForwardedForHeader("X-Custom-For")), + new Expectations() + .scheme("http").serverName("myhost").serverPort(80) + .secure(false) + .requestURL("http://myhost/") + .remoteAddr("2.2.2.2").remotePort(0) + ) + ); + } + + @ParameterizedTest + @MethodSource("customHeaderNameRequestCases") + public void testCustomHeaderName(Request request, Expectations expectations) throws Exception + { + request.configure(customizer); + + String rawRequest = request.getRawRequest((header) -> header); + // System.out.println(rawRequest); + + HttpTester.Response response = HttpTester.parseResponse(connector.getResponse(rawRequest)); + assertThat("status", response.getStatus(), is(200)); + + expectations.accept(actual); + } + private static class Request { String description;