Skip to content

Commit d0d7a88

Browse files
xylomanrwinch
authored andcommitted
Fix ForwardedHeaderFilter getRequestURL()
Previously ForwardedHeaderFilter would return the same StringBuffer for every invocation. This meant that users that modified the StringBuffer changed the state of the HttpServletRequest. This commit ensures that a new StringBuffer is always returned for ForwardedHeaderFilter. Issue: SPR-15423
1 parent f7548a8 commit d0d7a88

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ private static class ForwardedHeaderRequestWrapper extends HttpServletRequestWra
118118

119119
private final String requestUri;
120120

121-
private final StringBuffer requestUrl;
121+
private final String requestUrl;
122122

123123
private final Map<String, List<String>> headers;
124124

@@ -137,8 +137,8 @@ public ForwardedHeaderRequestWrapper(HttpServletRequest request, UrlPathHelper p
137137
String prefix = getForwardedPrefix(request);
138138
this.contextPath = (prefix != null ? prefix : request.getContextPath());
139139
this.requestUri = this.contextPath + pathHelper.getPathWithinApplication(request);
140-
this.requestUrl = new StringBuffer(this.scheme + "://" + this.host +
141-
(port == -1 ? "" : ":" + port) + this.requestUri);
140+
this.requestUrl = this.scheme + "://" + this.host +
141+
(port == -1 ? "" : ":" + port) + this.requestUri;
142142
this.headers = initHeaders(request);
143143
}
144144

@@ -206,7 +206,7 @@ public String getRequestURI() {
206206

207207
@Override
208208
public StringBuffer getRequestURL() {
209-
return this.requestUrl;
209+
return new StringBuffer(this.requestUrl);
210210
}
211211

212212
// Override header accessors to not expose forwarded headers

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,16 @@ public void requestUriWithForwardedPrefixTrailingSlash() throws Exception {
208208
HttpServletRequest actual = filterAndGetWrappedRequest();
209209
assertEquals("http://localhost/prefix/mvc-showcase", actual.getRequestURL().toString());
210210
}
211+
212+
@Test
213+
public void requestURLNewStringBuffer() throws Exception {
214+
this.request.addHeader(X_FORWARDED_PREFIX, "/prefix/");
215+
this.request.setRequestURI("/mvc-showcase");
216+
217+
HttpServletRequest actual = filterAndGetWrappedRequest();
218+
actual.getRequestURL().append("?key=value");
219+
assertEquals("http://localhost/prefix/mvc-showcase", actual.getRequestURL().toString());
220+
}
211221

212222
@Test
213223
public void contextPathWithForwardedPrefix() throws Exception {

0 commit comments

Comments
 (0)