Checks path for encoding as well as query.
fixes gh-233
This commit is contained in:
@@ -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_ROUTE_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;
|
||||
|
||||
@@ -58,7 +58,7 @@ public class RouteToRequestUrlFilter implements GlobalFilter, Ordered {
|
||||
}
|
||||
log.trace("RouteToRequestUrlFilter start");
|
||||
URI uri = exchange.getRequest().getURI();
|
||||
boolean encoded = containsEncodedQuery(uri);
|
||||
boolean encoded = containsEncodedParts(uri);
|
||||
URI routeUri = route.getUri();
|
||||
|
||||
if (hasAnotherScheme(routeUri)) {
|
||||
|
||||
@@ -38,7 +38,7 @@ import com.netflix.hystrix.exception.HystrixRuntimeException;
|
||||
|
||||
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.containsEncodedQuery;
|
||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.containsEncodedParts;
|
||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.setResponseStatus;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
@@ -123,7 +123,7 @@ public class HystrixGatewayFilterFactory extends AbstractGatewayFilterFactory<Hy
|
||||
//TODO: copied from RouteToRequestUrlFilter
|
||||
URI uri = exchange.getRequest().getURI();
|
||||
//TODO: assume always?
|
||||
boolean encoded = containsEncodedQuery(uri);
|
||||
boolean encoded = containsEncodedParts(uri);
|
||||
URI requestUrl = UriComponentsBuilder.fromUri(uri)
|
||||
.host(null)
|
||||
.port(null)
|
||||
|
||||
@@ -68,11 +68,10 @@ public class ServerWebExchangeUtils {
|
||||
return response;
|
||||
}
|
||||
|
||||
public static boolean containsEncodedQuery(URI uri) {
|
||||
if (uri.getRawQuery() == null) {
|
||||
return false;
|
||||
}
|
||||
return uri.getRawQuery().contains("%");
|
||||
public static boolean containsEncodedParts(URI uri) {
|
||||
boolean encoded = (uri.getRawQuery() != null && uri.getRawQuery().contains("%"))
|
||||
|| (uri.getPath() != null && uri.getRawPath().contains("%"));
|
||||
return encoded;
|
||||
}
|
||||
|
||||
public static HttpStatus parse(String statusString) {
|
||||
|
||||
@@ -102,6 +102,28 @@ public class RouteToRequestUrlFilterTests {
|
||||
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
|
||||
public void unencodedParameters() {
|
||||
URI url = URI.create("http://localhost/get?a=b&c=d[]");
|
||||
|
||||
@@ -40,7 +40,7 @@ import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
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;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@@ -81,7 +81,7 @@ public class AddRequestParameterGatewayFilterFactoryTests extends BaseWebClientT
|
||||
query = "";
|
||||
}
|
||||
URI uri = UriComponentsBuilder.fromUriString(this.baseUri+"/get" + query).build(true).toUri();
|
||||
boolean checkForEncodedValue = containsEncodedQuery(uri);
|
||||
boolean checkForEncodedValue = containsEncodedParts(uri);
|
||||
testClient.get()
|
||||
.uri(uri)
|
||||
.header("Host", host)
|
||||
|
||||
Reference in New Issue
Block a user