spring-cloud-gateway: ProxyExchange fails with "IOException: insufficient data written" with boot 3.2

The ProxyExchange type in spring-cloud-gateway-mvc copies over the headers from the incoming request, unless they’re marked as sensitive. In addition, it also uses the HTTP message converters to unmarshal the payload of the incoming request and marshal it again when sending. What we found is that in the header copying, it also includes the Content-Length header of the incoming request. However, b/o the unmarshalling and then marshalling the content length of the proxied request can be different: especially in the case of JSON where whitespace is removed, it can become less.

This causes problems with some downstream servers, as they keep waiting for additional request contents based on the erroneous Content-Length header. When not using smth like Apache Component HTTP client but using the default Java client instead, this even leads to errors in the client itself (showcased in attached sample).

This behavior was not there in previous versions: actually, with Spring Boot 3.1.6 and the underlying Spring web version this works. When you check what’s being sent then you can see that a Content-Length header is always set, with the correct value. It looks like the latest Spring web code (Jackson HTTP message converter?) no longer sets its own Content-Length header when marshalling to JSON. Therefore, spring-cloud-gateway-mvc should never copy over this header from the incoming request, but should rather leave it up to the RestTemplate to either leave it out or set this to the correct value for the outgoing request.

BTW, although this isn’t strictly related to this issue, I believe that the Host header of the incoming request shouldn’t be copied either by default: this can also lead to issues.

Sample I’ve attached a project with two integration tests that showcase the problem. By configuring the content-length to be a sensitive header you can work around the issue, but users of the framework should not be required to do this. gateway-mvc-contentlength.zip

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 4
  • Comments: 20 (15 by maintainers)

Commits related to this issue

Most upvoted comments

Hey, it’s been 5 five months since reporting this and four weeks since providing a suggested fix: any chance of getting a review on the PR? Right now both ProxyExchange modules are broken as shipped in the latest releases…

OK, I’ve created https://github.com/spring-cloud/spring-cloud-gateway/pull/3313 to address the issue for both the MVC and Webflux version.

It’ll be in the May release

narrator: it wasn’t

I thought the idea was that a PR could be considered for this week’s release, but I see that the release was already cut without any feedback on the PR that I created last week… Any chance to get this into the next release, at least? Or any feedback on what might still be needed for that to happen?

@spencergibb hello!

Can you download updated artifact (v 4.1.5) in mvnrepo please?

It’ll be in the May release

Missed yet another release, I see. I’m going to give up on chasing this: it’s very puzzling to me how an official Spring project simply ignores shipping two broken modules for half a year already when it’s clear what the problem is and there’s a proposed solution.

Yes, PRs welcome. We have an upcoming release in a week

Posting for those encountering, the workaround is to set

spring.cloud.gateway.proxy.sensitive=content-length