File tree Expand file tree Collapse file tree 2 files changed +26
-1
lines changed
main/java/org/springframework/web/server/adapter
test/java/org/springframework/web/server/adapter Expand file tree Collapse file tree 2 files changed +26
-1
lines changed Original file line number Diff line number Diff line change 22
22
import java .util .Set ;
23
23
import java .util .function .Function ;
24
24
25
+ import org .springframework .context .ApplicationContext ;
25
26
import org .springframework .http .HttpHeaders ;
26
27
import org .springframework .http .server .reactive .ServerHttpRequest ;
27
28
import org .springframework .lang .Nullable ;
36
37
* <p>Alternatively if {@link #setRemoveOnly removeOnly} is set to "true",
37
38
* then "Forwarded" and "X-Forwarded-*" headers are only removed, and not used.
38
39
*
40
+ * <p>An instance of this class is typically declared as a bean with the name
41
+ * "forwardedHeaderTransformer" and detected by
42
+ * {@link WebHttpHandlerBuilder#applicationContext(ApplicationContext)}, or it
43
+ * can also be registered directly via
44
+ * {@link WebHttpHandlerBuilder#forwardedHeaderTransformer(ForwardedHeaderTransformer)}.
45
+ *
39
46
* @author Rossen Stoyanchev
40
47
* @since 5.1
41
48
* @see <a href="https://tools.ietf.org/html/rfc7239">https://tools.ietf.org/html/rfc7239</a>
@@ -85,7 +92,7 @@ public ServerHttpRequest apply(ServerHttpRequest request) {
85
92
if (hasForwardedHeaders (request )) {
86
93
ServerHttpRequest .Builder builder = request .mutate ();
87
94
if (!this .removeOnly ) {
88
- URI uri = UriComponentsBuilder .fromHttpRequest (request ).build ().toUri ();
95
+ URI uri = UriComponentsBuilder .fromHttpRequest (request ).build (true ).toUri ();
89
96
builder .uri (uri );
90
97
String prefix = getForwardedPrefix (request );
91
98
if (prefix != null ) {
Original file line number Diff line number Diff line change 21
21
import org .junit .Test ;
22
22
23
23
import org .springframework .http .HttpHeaders ;
24
+ import org .springframework .http .HttpMethod ;
24
25
import org .springframework .http .server .reactive .ServerHttpRequest ;
25
26
import org .springframework .mock .http .server .reactive .test .MockServerHttpRequest ;
26
27
@@ -100,6 +101,23 @@ public void xForwardedPrefixTrailingSlash() throws Exception {
100
101
assertForwardedHeadersRemoved (request );
101
102
}
102
103
104
+ @ Test // SPR-17525
105
+ public void shouldNotDoubleEncode () throws Exception {
106
+ HttpHeaders headers = new HttpHeaders ();
107
+ headers .add ("Forwarded" , "host=84.198.58.199;proto=https" );
108
+
109
+ ServerHttpRequest request = MockServerHttpRequest
110
+ .method (HttpMethod .GET , new URI ("http://example.com/a%20b?q=a%2Bb" ))
111
+ .headers (headers )
112
+ .build ();
113
+
114
+ request = this .requestMutator .apply (request );
115
+
116
+ assertEquals (new URI ("https://84.198.58.199/a%20b?q=a%2Bb" ), request .getURI ());
117
+ assertForwardedHeadersRemoved (request );
118
+ }
119
+
120
+
103
121
private MockServerHttpRequest getRequest (HttpHeaders headers ) {
104
122
return MockServerHttpRequest .get (BASE_URL ).headers (headers ).build ();
105
123
}
You can’t perform that action at this time.
0 commit comments