Skip to content

Commit b060f28

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

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,10 @@ public static Function<ServerRequest, ServerRequest> prefixPath(String prefix) {
193193
Map<String, Object> uriVariables = MvcUtils.getUriTemplateVariables(request);
194194
URI uri = uriTemplate.expand(uriVariables);
195195

196-
String newPath = uri.getRawPath() + request.uri().getRawPath();
196+
String newPath = uri.getPath() + request.uri().getPath();
197197

198-
URI prefixedUri = UriComponentsBuilder.fromUri(request.uri()).replacePath(newPath).build().toUri();
198+
URI prefixedUri = UriComponentsBuilder.fromUri(request.uri())
199+
.replacePath(newPath).encode().build().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

+28
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)