Browse Source

Checks path for encoding as well as query.

fixes gh-233
pull/243/head
Spencer Gibb 7 years ago
parent
commit
85461d41b2
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 4
      spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/RouteToRequestUrlFilter.java
  2. 4
      spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/HystrixGatewayFilterFactory.java
  3. 9
      spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/support/ServerWebExchangeUtils.java
  4. 22
      spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/RouteToRequestUrlFilterTests.java
  5. 4
      spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/AddRequestParameterGatewayFilterFactoryTests.java

4
spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/RouteToRequestUrlFilter.java

@ -30,7 +30,7 @@ import org.springframework.web.util.UriComponentsBuilder;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR; import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR; import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_SCHEME_PREFIX_ATTR; import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_SCHEME_PREFIX_ATTR;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.containsEncodedQuery; import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.containsEncodedParts;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
@ -58,7 +58,7 @@ public class RouteToRequestUrlFilter implements GlobalFilter, Ordered {
} }
log.trace("RouteToRequestUrlFilter start"); log.trace("RouteToRequestUrlFilter start");
URI uri = exchange.getRequest().getURI(); URI uri = exchange.getRequest().getURI();
boolean encoded = containsEncodedQuery(uri); boolean encoded = containsEncodedParts(uri);
URI routeUri = route.getUri(); URI routeUri = route.getUri();
if (hasAnotherScheme(routeUri)) { if (hasAnotherScheme(routeUri)) {

4
spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/HystrixGatewayFilterFactory.java

@ -38,7 +38,7 @@ import com.netflix.hystrix.exception.HystrixRuntimeException;
import static com.netflix.hystrix.exception.HystrixRuntimeException.FailureType.TIMEOUT; import static com.netflix.hystrix.exception.HystrixRuntimeException.FailureType.TIMEOUT;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR; import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.containsEncodedQuery; import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.containsEncodedParts;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.setResponseStatus; import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.setResponseStatus;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
@ -123,7 +123,7 @@ public class HystrixGatewayFilterFactory extends AbstractGatewayFilterFactory<Hy
//TODO: copied from RouteToRequestUrlFilter //TODO: copied from RouteToRequestUrlFilter
URI uri = exchange.getRequest().getURI(); URI uri = exchange.getRequest().getURI();
//TODO: assume always? //TODO: assume always?
boolean encoded = containsEncodedQuery(uri); boolean encoded = containsEncodedParts(uri);
URI requestUrl = UriComponentsBuilder.fromUri(uri) URI requestUrl = UriComponentsBuilder.fromUri(uri)
.host(null) .host(null)
.port(null) .port(null)

9
spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/support/ServerWebExchangeUtils.java

@ -68,11 +68,10 @@ public class ServerWebExchangeUtils {
return response; return response;
} }
public static boolean containsEncodedQuery(URI uri) { public static boolean containsEncodedParts(URI uri) {
if (uri.getRawQuery() == null) { boolean encoded = (uri.getRawQuery() != null && uri.getRawQuery().contains("%"))
return false; || (uri.getPath() != null && uri.getRawPath().contains("%"));
} return encoded;
return uri.getRawQuery().contains("%");
} }
public static HttpStatus parse(String statusString) { public static HttpStatus parse(String statusString) {

22
spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/RouteToRequestUrlFilterTests.java

@ -102,6 +102,28 @@ public class RouteToRequestUrlFilterTests {
assertThat(uri.getRawQuery()).isEqualTo("a=b&c=d%5B%5D"); assertThat(uri.getRawQuery()).isEqualTo("a=b&c=d%5B%5D");
} }
@Test
public void encodedUrl() {
URI url = UriComponentsBuilder.fromUriString("http://localhost/abc def/get").buildAndExpand().encode().toUri();
// prove that it is encoded
assertThat(url.getRawPath()).isEqualTo("/abc%20def/get");
assertThat(url).hasPath("/abc def/get");
MockServerHttpRequest request = MockServerHttpRequest
.method(HttpMethod.GET, url)
.build();
ServerWebExchange webExchange = testFilter(request, "http://myhost");
URI uri = webExchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
assertThat(uri).hasScheme("http").hasHost("myhost")
.hasPath("/abc def/get");
// prove that it is not double encoded
assertThat(uri.getRawPath()).isEqualTo("/abc%20def/get");
}
@Test @Test
public void unencodedParameters() { public void unencodedParameters() {
URI url = URI.create("http://localhost/get?a=b&c=d[]"); URI url = URI.create("http://localhost/get?a=b&c=d[]");

4
spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/AddRequestParameterGatewayFilterFactoryTests.java

@ -40,7 +40,7 @@ import org.springframework.web.util.UriComponentsBuilder;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.containsEncodedQuery; import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.containsEncodedParts;
import static org.springframework.cloud.gateway.test.TestUtils.getMap; import static org.springframework.cloud.gateway.test.TestUtils.getMap;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@ -81,7 +81,7 @@ public class AddRequestParameterGatewayFilterFactoryTests extends BaseWebClientT
query = ""; query = "";
} }
URI uri = UriComponentsBuilder.fromUriString(this.baseUri+"/get" + query).build(true).toUri(); URI uri = UriComponentsBuilder.fromUriString(this.baseUri+"/get" + query).build(true).toUri();
boolean checkForEncodedValue = containsEncodedQuery(uri); boolean checkForEncodedValue = containsEncodedParts(uri);
testClient.get() testClient.get()
.uri(uri) .uri(uri)
.header("Host", host) .header("Host", host)

Loading…
Cancel
Save