Skip to content

Commit a244675

Browse files
committed
Merge pull request #23305' from AndreasKl/fowarded-prefix-causes-invalid-path into 5.1.x
2 parents 2b94205 + 153ac82 commit a244675

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

spring-web/src/main/java/org/springframework/web/server/adapter/ForwardedHeaderTransformer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -96,7 +96,7 @@ public ServerHttpRequest apply(ServerHttpRequest request) {
9696
builder.uri(uri);
9797
String prefix = getForwardedPrefix(request);
9898
if (prefix != null) {
99-
builder.path(prefix + uri.getPath());
99+
builder.path(prefix + uri.getRawPath());
100100
builder.contextPath(prefix);
101101
}
102102
}

spring-web/src/test/java/org/springframework/web/server/adapter/ForwardedHeaderTransformerTests.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -90,6 +90,22 @@ public void xForwardedPrefix() throws Exception {
9090
assertForwardedHeadersRemoved(request);
9191
}
9292

93+
@Test // gh-23305
94+
public void xForwardedPrefixShouldNotLeadToDecodedPath() throws Exception {
95+
HttpHeaders headers = new HttpHeaders();
96+
headers.add("X-Forwarded-Prefix", "/prefix");
97+
ServerHttpRequest request = MockServerHttpRequest
98+
.method(HttpMethod.GET, new URI("https://example.com/a%20b?q=a%2Bb"))
99+
.headers(headers)
100+
.build();
101+
102+
request = this.requestMutator.apply(request);
103+
104+
assertEquals(new URI("https://example.com/prefix/a%20b?q=a%2Bb"), request.getURI());
105+
assertEquals("/prefix/a%20b", request.getPath().value());
106+
assertForwardedHeadersRemoved(request);
107+
}
108+
93109
@Test
94110
public void xForwardedPrefixTrailingSlash() throws Exception {
95111
HttpHeaders headers = new HttpHeaders();

0 commit comments

Comments
 (0)