Browse Source

Merge pull request #467 from wangzw/issue462

Handle partial encoded URI. #462
pull/824/head
Ryan Baxter 6 years ago committed by GitHub
parent
commit
2646d6b345
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/support/ServerWebExchangeUtils.java
  2. 26
      spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/RouteToRequestUrlFilterTests.java

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

@ -24,6 +24,7 @@ import java.util.function.Predicate; @@ -24,6 +24,7 @@ import java.util.function.Predicate;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.util.UriComponentsBuilder;
import reactor.core.publisher.Mono;
import org.springframework.cloud.gateway.handler.AsyncPredicate;
@ -176,7 +177,19 @@ public final class ServerWebExchangeUtils { @@ -176,7 +177,19 @@ public final class ServerWebExchangeUtils {
public static boolean containsEncodedParts(URI uri) {
boolean encoded = (uri.getRawQuery() != null && uri.getRawQuery().contains("%"))
|| (uri.getPath() != null && uri.getRawPath().contains("%"));
|| (uri.getRawPath() != null && uri.getRawPath().contains("%"));
// Verify if it is really fully encoded. Treat partial encoded as uncoded.
if (encoded) {
try {
UriComponentsBuilder.fromUri(uri).build(true);
return true;
} catch (IllegalArgumentException ignore) {
}
return false;
}
return encoded;
}

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

@ -114,6 +114,32 @@ public class RouteToRequestUrlFilterTests { @@ -114,6 +114,32 @@ public class RouteToRequestUrlFilterTests {
assertThat(uri.getRawQuery()).isEqualTo("a=b&c=d%5B%5D");
}
@Test
public void partialEncodedParameters() {
URI url = UriComponentsBuilder.fromUriString(
"http://localhost/get?key[]=test= key&start=1533108081")
.build().toUri();
// prove that it is partial encoded
assertThat(url.getRawQuery()).isEqualTo("key[]=test=%20key&start=1533108081");
assertThat(url).hasParameter("key[]", "test= key");
assertThat(url).hasParameter("start", "1533108081");
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")
.hasParameter("key[]", "test= key")
.hasParameter("start", "1533108081");
// prove that it is double encoded since partial encoded uri is treated as uncoded.
assertThat(uri.getRawQuery()).isEqualTo("key[]=test=%2520key&start=1533108081");
}
@Test
public void encodedUrl() {
URI url = UriComponentsBuilder.fromUriString("http://localhost/abc def/get")

Loading…
Cancel
Save