spring-cloud-gateway: java.lang.IllegalArgumentException: Invalid character '[' for QUERY_PARAM in "match[]"

2018-08-01 08:21:20.226 ERROR 1 --- [-server-epoll-7] .a.w.r.e.DefaultErrorWebExceptionHandler : Failed to handle request [GET http://xxx.com/grafana/api/datasources/proxy/1/api/v1/series?match[]=jvm_classes_loaded%7Binstance%3D%22config%3A8888%22%7D&start=1533108081&end=1533111681]

java.lang.IllegalArgumentException: Invalid character '[' for QUERY_PARAM in "match[]"
	at org.springframework.web.util.HierarchicalUriComponents.verifyUriComponent(HierarchicalUriComponents.java:417) ~[spring-web-5.0.8.RELEASE.jar!/:5.0.8.RELEASE]
	at org.springframework.web.util.HierarchicalUriComponents.lambda$verify$4(HierarchicalUriComponents.java:383) ~[spring-web-5.0.8.RELEASE.jar!/:5.0.8.RELEASE]
	at java.util.Map.forEach(Map.java:630) ~[na:1.8.0_111]
	at org.springframework.web.util.HierarchicalUriComponents.verify(HierarchicalUriComponents.java:382) ~[spring-web-5.0.8.RELEASE.jar!/:5.0.8.RELEASE]
	at org.springframework.web.util.HierarchicalUriComponents.<init>(HierarchicalUriComponents.java:152) ~[spring-web-5.0.8.RELEASE.jar!/:5.0.8.RELEASE]
	at org.springframework.web.util.UriComponentsBuilder.build(UriComponentsBuilder.java:394) ~[spring-web-5.0.8.RELEASE.jar!/:5.0.8.RELEASE]
	at org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter.filter(RouteToRequestUrlFilter.java:73) ~[spring-cloud-gateway-core-2.0.1.RELEASE.jar!/:2.0.1.RELEASE]
	at org.springframework.cloud.gateway.handler.FilteringWebHandler$GatewayFilterAdapter.filter(FilteringWebHandler.java:133) ~[spring-cloud-gateway-core-2.0.1.RELEASE.jar!/:2.0.1.RELEASE]
	at org.springframework.cloud.gateway.filter.OrderedGatewayFilter.filter(OrderedGatewayFilter.java:44) ~[spring-cloud-gateway-core-2.0.1.RELEASE.jar!/:2.0.1.RELEASE]
	at org.springframework.cloud.gateway.handler.FilteringWebHandler$DefaultGatewayFilterChain.lambda$filter$0(FilteringWebHandler.java:115) ~[spring-cloud-gateway-core-2.0.1.RELEASE.jar!/:2.0.1.RELEASE]

Use gateway to forward grafana request get above error.

Seems the request http://xxx.com/grafana/api/datasources/proxy/1/api/v1/series?match[]=jvm_classes_loaded%7Binstance%3D%22config%3A8888%22%7D&start=1533108081&end=1533111681 not encoded well. But not sure if it is a gateway issue.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 18 (8 by maintainers)

Commits related to this issue

Most upvoted comments

I have the same issue, just like described above. In my case the query params always contain an URL with “=” in it. The target service alone can handle these requests without any problem, the gateway unfortunately not. It’d be nice, if you could push a fix for that or provide an official workaround. 😃

Hi, I’ve come across with the same problem, where Spring’s URL handling can handle the request but gateway fails with an exception.

Example URI: http://localhost:8080/test?key=test= key

Gateway Scenario:

Caused by: java.lang.IllegalArgumentException: Invalid character '=' for QUERY_PARAM in "test=%20key"
	at org.springframework.web.util.HierarchicalUriComponents.verifyUriComponent(HierarchicalUriComponents.java:417)
	at org.springframework.web.util.HierarchicalUriComponents.lambda$verify$4(HierarchicalUriComponents.java:385)
	at java.util.Map.forEach(Map.java:630)
	at org.springframework.web.util.HierarchicalUriComponents.verify(HierarchicalUriComponents.java:382)
	at org.springframework.web.util.HierarchicalUriComponents.<init>(HierarchicalUriComponents.java:152)
	at org.springframework.web.util.UriComponentsBuilder.build(UriComponentsBuilder.java:394)
	at org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter.filter(RouteToRequestUrlFilter.java:73)
	at org.springframework.cloud.gateway.handler.FilteringWebHandler$GatewayFilterAdapter.filter(FilteringWebHandler.java:133)
	at org.springframework.cloud.gateway.filter.OrderedGatewayFilter.filter(OrderedGatewayFilter.java:44)
	at org.springframework.cloud.gateway.handler.FilteringWebHandler$DefaultGatewayFilterChain.lambda$filter$0(FilteringWebHandler.java:115)
	at reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:45)
	... 153 more

WebFlux raw controller scenario:

   @GetMapping("/test")
    @ResponseStatus(HttpStatus.OK)
    public Mono<String> ping(ServerWebExchange exchange){
        System.out.println(exchange.getRequest().getQueryParams());
        return Mono.just("works");
    }

results with {key=[test= key]}

In summary, raw controller handles the partial encoding by the order of "="s. However, RouteToRequestUrlFilter passes the query params to HierarchicalUriComponents as encoded because of ‘%’ in the sample URI. Since, HierarchicalUriComponents expects fully encoded parameters this.encodeState = encoded ? EncodeState.FULLY_ENCODED : EncodeState.RAW; It fails.

I think gateway should support these kind of cases to provide compatibility with Webflux, and this could happen in many situations.

For example, chrome encodes the uri http://localhost:8080/test?key=test= key as http://localhost:8080/test?key=test=%20key

@spencergibb Would you please consider fixing this behaviour which causes inconsistencies between the gateway and the webflux ecosystem?

#467 to fix this issue, and this patch works well for me.