From 84db06ec0bbb6dcae37366af8aa8f7464d89acee Mon Sep 17 00:00:00 2001 From: Rob Winch Date: Mon, 10 Apr 2017 08:41:07 -0500 Subject: [PATCH] ForwardedHeaderFilter requestURI preserve encoding Previously ForwardedHeaderFilter would override the requestURI with a URL decoded value. This would cause problems when using a URL encoded requestURI since downstream Filters would not see the URL encoded value as they should. This commit resolves this issue by ensuring that the requestURI is properly encoded. Issues SPR-15422 --- .../web/filter/ForwardedHeaderFilter.java | 7 ++++++- .../web/filter/ForwardedHeaderFilterTests.java | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/spring-web/src/main/java/org/springframework/web/filter/ForwardedHeaderFilter.java b/spring-web/src/main/java/org/springframework/web/filter/ForwardedHeaderFilter.java index 2a4777fe22..77743aa9a8 100644 --- a/spring-web/src/main/java/org/springframework/web/filter/ForwardedHeaderFilter.java +++ b/spring-web/src/main/java/org/springframework/web/filter/ForwardedHeaderFilter.java @@ -69,7 +69,12 @@ public class ForwardedHeaderFilter extends OncePerRequestFilter { } - private final UrlPathHelper pathHelper = new UrlPathHelper(); + private final UrlPathHelper pathHelper; + + public ForwardedHeaderFilter() { + this.pathHelper = new UrlPathHelper(); + this.pathHelper.setUrlDecode(false); + } @Override diff --git a/spring-web/src/test/java/org/springframework/web/filter/ForwardedHeaderFilterTests.java b/spring-web/src/test/java/org/springframework/web/filter/ForwardedHeaderFilterTests.java index 3e46124149..26d55b596c 100644 --- a/spring-web/src/test/java/org/springframework/web/filter/ForwardedHeaderFilterTests.java +++ b/spring-web/src/test/java/org/springframework/web/filter/ForwardedHeaderFilterTests.java @@ -106,6 +106,18 @@ public class ForwardedHeaderFilterTests { assertEquals("", actual.getContextPath()); assertEquals("/path/", actual.getRequestURI()); } + + @Test + public void requestUriPreserveEncoding() throws Exception { + this.request.setContextPath("/app"); + this.request.setRequestURI("/app/path%20with%20spaces/"); + HttpServletRequest actual = filterAndGetWrappedRequest(); + + assertEquals("/app", actual.getContextPath()); + assertEquals("/app/path%20with%20spaces/", actual.getRequestURI()); + assertEquals("http://localhost/app/path%20with%20spaces/", actual.getRequestURL().toString()); + } + @Test public void requestUriEqualsContextPath() throws Exception { this.request.addHeader(X_FORWARDED_PREFIX, "/");