-
Notifications
You must be signed in to change notification settings - Fork 38.7k
Description
Affects: 5.2.5
If a query has brackets in its values, then it is not completely encoded, but accepted by the URI implementation.
Problem is with build(boolean)
method, which works in an inconsistent way.
if parameter is true
, then it validates if URI is completely encoded and fails when there are an unencoded []
when parameter is false
, then it selectively encodes the URI, specifically it doesn't encode []
, producing again invalid URI.
Look at this test:
@Test
public void encoding() throws URISyntaxException {
URI uri = new URI("http://example.com/some/path?query=[from%20to]");
try {
UriComponentsBuilder.fromUri(uri).build(true);
fail("It wasn't completely encoded URI");
} catch (IllegalArgumentException e) {
//good
}
//ok, then encode it
uri = UriComponentsBuilder.fromUri(uri).build(false).toUri();
//now it is double encoded http://example.com/some/path?query=[from%2520to]
//so is it encoded now?
UriComponentsBuilder.fromUri(uri).build(true); //fail, no it is not, square brackets are there
}
Whatever you do, it is not possible to use UriComponentsBuilder
on the above uri and get the valid or at least the same uri as a result.
I am not the only one confused: https://github.com/spring-cloud/spring-cloud-gateway/blob/master/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/RouteToRequestUrlFilter.java#L63