Browse Source

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
pull/1384/head
Rob Winch 8 years ago
parent
commit
84db06ec0b
  1. 7
      spring-web/src/main/java/org/springframework/web/filter/ForwardedHeaderFilter.java
  2. 12
      spring-web/src/test/java/org/springframework/web/filter/ForwardedHeaderFilterTests.java

7
spring-web/src/main/java/org/springframework/web/filter/ForwardedHeaderFilter.java

@ -69,7 +69,12 @@ public class ForwardedHeaderFilter extends OncePerRequestFilter { @@ -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

12
spring-web/src/test/java/org/springframework/web/filter/ForwardedHeaderFilterTests.java

@ -106,6 +106,18 @@ public class ForwardedHeaderFilterTests { @@ -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, "/");

Loading…
Cancel
Save