Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
benba opened this issue Mar 28, 2024 · 1 comment

Comments

@benba
Copy link
Contributor

benba commented Mar 28, 2024

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.

@spencergibb
Copy link
Member

PRs welcome

benba added a commit to benba/spring-cloud-gateway that referenced this issue Apr 22, 2024
This avoids to have a "Transfer-Encoding: chunked" with an empty body if downstream exchanges in HTTP 1.1

Fixes spring-cloudgh-3325
benba added a commit to benba/spring-cloud-gateway that referenced this issue Apr 22, 2024
This avoids to have a "Transfer-Encoding: chunked" with an empty body if downstream exchanges are negociated in HTTP 1.1

Fixes spring-cloudgh-3325
benba added a commit to benba/spring-cloud-gateway that referenced this issue Apr 22, 2024
This avoids to have a "Transfer-Encoding: chunked" with an empty body if downstream exchanges are negotiated in HTTP 1.1

Fixes spring-cloudgh-3325
benba added a commit to benba/spring-cloud-gateway that referenced this issue Apr 23, 2024
This avoids to have a "Transfer-Encoding: chunked" with an empty body if downstream exchanges are negotiated in HTTP 1.1

Fixes spring-cloudgh-3325
@spencergibb spencergibb moved this to In Progress in 2023.0.4 Sep 26, 2024
@spencergibb spencergibb moved this to In Progress in 2024.0.0-M2 Sep 26, 2024
@spencergibb spencergibb added this to the 4.1.6 milestone Sep 26, 2024
@github-project-automation github-project-automation bot moved this from In Progress to Done in 2023.0.4 Sep 26, 2024
@github-project-automation github-project-automation bot moved this from In Progress to Done in 2024.0.0-M2 Sep 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants