Skip to content

Commit 46cf4f9

Browse files
committed
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
1 parent 2b930e3 commit 46cf4f9

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,12 @@ public class ForwardedHeaderFilter extends OncePerRequestFilter {
6969
}
7070

7171

72-
private final UrlPathHelper pathHelper = new UrlPathHelper();
72+
private final UrlPathHelper pathHelper;
73+
74+
public ForwardedHeaderFilter() {
75+
this.pathHelper = new UrlPathHelper();
76+
this.pathHelper.setUrlDecode(false);
77+
}
7378

7479

7580
@Override

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,18 @@ public void requestUriWithTrailingSlash() throws Exception {
108108
assertEquals("", actual.getContextPath());
109109
assertEquals("/path/", actual.getRequestURI());
110110
}
111+
112+
@Test
113+
public void requestUriPreserveEncoding() throws Exception {
114+
this.request.setContextPath("/app");
115+
this.request.setRequestURI("/app/path%20with%20spaces/");
116+
HttpServletRequest actual = filterAndGetWrappedRequest();
117+
118+
assertEquals("/app", actual.getContextPath());
119+
assertEquals("/app/path%20with%20spaces/", actual.getRequestURI());
120+
assertEquals("http://localhost/app/path%20with%20spaces/", actual.getRequestURL().toString());
121+
}
122+
111123
@Test
112124
public void requestUriEqualsContextPath() throws Exception {
113125
this.request.addHeader(X_FORWARDED_PREFIX, "/");

0 commit comments

Comments
 (0)