Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop double-encoding path in various filter functions #3437

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public static Function<ServerRequest, ServerRequest> prefixPath(String prefix) {
Map<String, Object> uriVariables = MvcUtils.getUriTemplateVariables(request);
URI uri = uriTemplate.expand(uriVariables);

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

URI prefixedUri = UriComponentsBuilder.fromUri(request.uri()).replacePath(newPath).build().toUri();
return ServerRequest.from(request).uri(prefixedUri).build();
Expand All @@ -217,7 +217,7 @@ public static Function<ServerRequest, ServerRequest> removeRequestParameter(Stri
// remove from uri
URI newUri = UriComponentsBuilder.fromUri(request.uri())
.replaceQueryParams(unmodifiableMultiValueMap(queryParams))
.build()
.build(true)
.toUri();

// remove resolved params from request
Expand Down Expand Up @@ -324,7 +324,7 @@ public static Function<ServerRequest, ServerRequest> rewritePath(String regexp,
Pattern pattern = Pattern.compile(regexp);
return request -> {
// TODO: original request url
String path = request.uri().getRawPath();
String path = request.uri().getPath();
String newPath = pattern.matcher(path).replaceAll(normalizedReplacement);

URI rewrittenUri = UriComponentsBuilder.fromUri(request.uri()).replacePath(newPath).build().toUri();
Expand All @@ -350,7 +350,7 @@ public static Function<ServerRequest, ServerRequest> setPath(String path) {
return request -> {
Map<String, Object> uriVariables = MvcUtils.getUriTemplateVariables(request);
URI uri = uriTemplate.expand(uriVariables);
String newPath = uri.getRawPath();
String newPath = uri.getPath();

URI prefixedUri = UriComponentsBuilder.fromUri(request.uri()).replacePath(newPath).build().toUri();
return ServerRequest.from(request).uri(prefixedUri).build();
Expand Down Expand Up @@ -385,7 +385,7 @@ public static Function<ServerRequest, ServerRequest> stripPrefix() {
public static Function<ServerRequest, ServerRequest> stripPrefix(int parts) {
return request -> {
// TODO: gateway url attributes
String path = request.uri().getRawPath();
String path = request.uri().getPath();
// TODO: begin duplicate code from StripPrefixGatewayFilterFactory
String[] originalParts = StringUtils.tokenizeToStringArray(path, "/");

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 @@ -625,6 +626,15 @@ public void redirectToWorks() {
.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 @@ -1087,6 +1097,19 @@ public RouterFunction<ServerResponse> gatewayRouterFunctionsStripPrefix() {
// @formatter:on
}

@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctionsTestEncoding() {
// @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