Skip to content

Commit

Permalink
Stop double-encoding path in various filters
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Worsnop committed Jun 25, 2024
1 parent 920b3a8 commit c6083a6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public static Function<ServerRequest, ServerRequest> prefixPath(String prefix) {

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

URI prefixedUri = UriComponentsBuilder.fromUri(request.uri()).replacePath(newPath).build().toUri();
URI prefixedUri = UriComponentsBuilder.fromUri(request.uri()).replacePath(newPath).build(true).toUri();
return ServerRequest.from(request).uri(prefixedUri).build();
};
}
Expand All @@ -211,7 +211,7 @@ public static Function<ServerRequest, ServerRequest> removeRequestParameter(Stri

// remove from uri
URI newUri = UriComponentsBuilder.fromUri(request.uri())
.replaceQueryParams(unmodifiableMultiValueMap(queryParams)).build().toUri();
.replaceQueryParams(unmodifiableMultiValueMap(queryParams)).build(true).toUri();

// remove resolved params from request
return ServerRequest.from(request).params(params -> params.remove(name)).uri(newUri).build();
Expand Down Expand Up @@ -320,7 +320,7 @@ public static Function<ServerRequest, ServerRequest> rewritePath(String regexp,
String path = request.uri().getRawPath();
String newPath = pattern.matcher(path).replaceAll(normalizedReplacement);

URI rewrittenUri = UriComponentsBuilder.fromUri(request.uri()).replacePath(newPath).build().toUri();
URI rewrittenUri = UriComponentsBuilder.fromUri(request.uri()).replacePath(newPath).build(true).toUri();

ServerRequest modified = ServerRequest.from(request).uri(rewrittenUri).build();

Expand All @@ -345,7 +345,7 @@ public static Function<ServerRequest, ServerRequest> setPath(String path) {
URI uri = uriTemplate.expand(uriVariables);
String newPath = uri.getRawPath();

URI prefixedUri = UriComponentsBuilder.fromUri(request.uri()).replacePath(newPath).build().toUri();
URI prefixedUri = UriComponentsBuilder.fromUri(request.uri()).replacePath(newPath).build(true).toUri();
return ServerRequest.from(request).uri(prefixedUri).build();
};
}
Expand Down Expand Up @@ -398,7 +398,7 @@ public static Function<ServerRequest, ServerRequest> stripPrefix(int parts) {
}
// TODO: end duplicate code from StripPrefixGatewayFilterFactory

URI prefixedUri = UriComponentsBuilder.fromUri(request.uri()).replacePath(newPath.toString()).build()
URI prefixedUri = UriComponentsBuilder.fromUri(request.uri()).replacePath(newPath.toString()).build(true)
.toUri();
return ServerRequest.from(request).uri(prefixedUri).build();
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.cloud.gateway.server.mvc.common.MvcUtils;
import org.springframework.cloud.gateway.server.mvc.filter.FilterFunctions;
import org.springframework.cloud.gateway.server.mvc.filter.FormFilter;
import org.springframework.cloud.gateway.server.mvc.filter.ForwardedRequestHeadersFilter;
import org.springframework.cloud.gateway.server.mvc.filter.XForwardedRequestHeadersFilter;
Expand Down Expand Up @@ -481,6 +482,15 @@ public void redirectToWorks() {
.expectHeader().valueEquals(HttpHeaders.LOCATION, "https://exampleredirect.com");
}

@Test
public void filtersWorksWithEncoding() {
restClient.get().uri("/test-encoding").exchange().expectStatus().isOk()
.expectBody(Map.class).consumeWith(res -> {
Map<String, Object> map = res.getResponseBody();
assertThat(map.get("url")).asString().endsWith("anything%20goes");
});
}

private MultiValueMap<String, HttpEntity<?>> createMultipartData() {
ClassPathResource part = new ClassPathResource("test/1x1.png");
MultipartBodyBuilder builder = new MultipartBodyBuilder();
Expand Down Expand Up @@ -815,6 +825,19 @@ public RouterFunction<ServerResponse> gatewayRouterFunctionsStripPrefix() {
// @formatter:on
}

@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctionsStripPrefixWithEncoding() {
// @formatter:off
return route(GET("/test-encoding"), http())
.filter(new HttpbinUriResolver())
.filter(FilterFunctions.removeRequestParameter("foo"))
.filter(stripPrefix(4))
.filter(prefixPath("prefix"))
.filter(rewritePath("long", "longer"))
.filter(setPath("/long/path/to/anything/anything goes"));
// @formatter:on
}

@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctionsStripPrefixPost() {
// @formatter:off
Expand Down

0 comments on commit c6083a6

Please sign in to comment.