spring-cloud-gateway icon indicating copy to clipboard operation
spring-cloud-gateway copied to clipboard

[Gateway MVC] Transfer-Encoding added to GET/HEAD requests, causing some WAF errors

Open benba opened this issue 1 year ago • 1 comments

Using the Jdk HttpClient, when no body is present in a GET or HEAD request, and that the downstream server only supports HTTP 1.1:
a Transfer-Encoding: chunked will be added to the downstream request, causing some WAF to reject it (see "Body in GET or HEAD requests" in BIG-IP ASM HTTP protocol compliance for example).

The reason seems to be a combination of the issue describe in GH-3308 that causes HTTP 1.1 exchange to be always performed in chunked mode, and the fact that the body is always set in RestClientProxyExchange.

This will cause this JdkClientHttpRequest check to think that there is a body (since body is not null and upstream Content-Length: 0 was removed if present) and will cause the addition of the Transfer-Encoding: chunked header later.

As a workaround I've implemented this fix

@Override
public ServerResponse exchange(Request request) {
	var requestSpec = restClient.method(request.getMethod()).uri(request.getUri())
			.headers(httpHeaders -> httpHeaders.putAll(request.getHeaders()));
	if (isBodyPresent(request)) {
		requestSpec.body(outputStream -> copyBody(request, outputStream));
	}
	return requestSpec.exchange((clientRequest, clientResponse) -> doExchange(request, clientResponse), false);
}

isBodyPresent(request) is simply checking that request.getServerRequest().servletRequest().getInputStream().available() > 0 (using upstream Content-Length or Transfer-Encoding would not work if server.http2.enabled=true).

I can submit a PR if you feel that the fix is relevant.

benba avatar Mar 28 '24 15:03 benba

PRs welcome

spencergibb avatar Apr 05 '24 15:04 spencergibb