Skip to content

Commit 6619e60

Browse files
committed
Fix prefixPath double encoding
Fixes gh-3769 Signed-off-by: Sebastien <[email protected]>
1 parent ec07cb8 commit 6619e60

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/BeforeFilterFunctions.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,8 @@ public static Function<ServerRequest, ServerRequest> prefixPath(String prefix) {
195195

196196
String newPath = uri.getRawPath() + request.uri().getRawPath();
197197

198-
URI prefixedUri = UriComponentsBuilder.fromUri(request.uri()).replacePath(newPath).build().toUri();
198+
URI prefixedUri = UriComponentsBuilder.fromUri(request.uri())
199+
.replacePath(newPath).build(true).toUri();
199200
return ServerRequest.from(request).uri(prefixedUri).build();
200201
};
201202
}

spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/filter/BeforeFilterFunctionsTests.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,32 @@ void rewritePathWithEnDashAndSpace() {
245245
.isEqualTo("/modified/path/with%E2%80%93en%E2%80%93dashes%20and%20spaces");
246246
}
247247

248+
@Test
249+
void prefixPath() {
250+
MockHttpServletRequest servletRequest = MockMvcRequestBuilders
251+
.get("http://localhost/get").buildRequest(null);
252+
253+
ServerRequest request = ServerRequest.create(servletRequest,
254+
Collections.emptyList());
255+
256+
ServerRequest modified = BeforeFilterFunctions.prefixPath("/prefix")
257+
.apply(request);
258+
259+
assertThat(modified.uri().getRawPath()).isEqualTo("/prefix/get");
260+
}
261+
262+
@Test
263+
void prefixEncodedPath() {
264+
MockHttpServletRequest servletRequest = MockMvcRequestBuilders
265+
.get("http://localhost/get/é").buildRequest(null);
266+
267+
ServerRequest request = ServerRequest.create(servletRequest,
268+
Collections.emptyList());
269+
270+
ServerRequest modified = BeforeFilterFunctions.prefixPath("/pre fix")
271+
.apply(request);
272+
273+
assertThat(modified.uri().getRawPath()).isEqualTo("/pre%20fix/get/%C3%A9");
274+
}
275+
248276
}

0 commit comments

Comments
 (0)