You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Spring adding 'chunked' transfer encoding even if this header already exists [SPR-16985] #21523
In our Middleware project we forward rest response from another service as it is to angular UI. The response we are getting to the Middleware already having transfer-encoding →chunked header. while returning the same response from our middleware to angular UI spring adding transfer-encoding again. see below response
content-type →application/json;charset=UTF-8
date →Fri, 29 Jun 2018 07:02:06 GMT
transfer-encoding →chunked, chunked
Because of this F5 not able to send the response properly. Spring should not add transfer-encoding If this header already present in the response.
From a quick review, I can't find any place where we explicitly set the "Transfer-Encoding" header in Spring's processing, in particular not to "chunked". Does this maybe come through the HTTP server or some HTTP client library here?
Brian Clozel, assigning this to you for further consideration...
I have added a filter and tested the same, this issue is not because of the Httpserver. I am not sure how to figure it out whether the issue is with HTTP client or Spring. Could you please provide me any suggestion on how to proceed with this issue.
We stumbled upon this just yesterday and opened an issue against traefik because of this (traefik/traefik#8060). Basically what we do is using a Spring Boot application as a proxy, as we don't want to deal with CORS and Same-Origin-Policy and all that stuff. Thankfully, our test case for this is rather simple:
In the meantime, we received out-of-band feedback on issues similar to yours. I'll try to summarize our findings in this comment.
As far as I understand, golang introduced this restriction in the http layer to prevent HTTP smuggling attacks - but this can only apply to requests received by servers/proxies, not responses sent by servers. So while this change is enforcing a new restriction, it's not meant to protect you here. This change not only applies to traefik, but to many proxies/servers using golang.
We've only found that problem in Spring applications with the exact use case you're describing: a controller uses an HTTP client to retrieve a response and returns it as the response of the controller handler method without any modification. We consider this use case as problematic for several reasons:
This is not specific to Spring, as you could do the same thing with the HTTP Servlet response directly - in that case, Spring would have no way of enforcing rules or cleaning the response.
Applications are not in a position to set the Transfer-Encoding response header because it has not the necessary visibility to set that correctly, nor the ability to then send a correctly formatted chunked body.
So while sending duplicate chunked values are illegal anyway, we don't think that Spring is in a position to fix that. It's up to servers to decide what they want to do with this.
Now we've had similar discussions with developers using the same usage pattern (controllers proxying requests). The chunked header value is a symptom of a much bigger problem: copying headers in and out can cause many HTTP and security issues. HTTP headers can carry information about the state of the connection, security restrictions or options negotiated between client and server. This implementation is a typical example of possible HTTP smuggling vulnerabilities and I'd encourage you to consider this in your application.
At a minimum, headers should be properly filtered (in and out). I understand that we're probably looking at a very restricted use case, but implementing a general purpose proxy/gateway is not easy and there are many more problems with that. Maybe Spring Cloud Gateway can be helpful.
Reacted by Gary Sheppard, Marcelo Miyake, Felix Medam and code4curiosity
@anwarmd this issue is about getting duplicate Transfer-Encoding: chucked response headers. If you're seeing this problem, this is very likely due to your application setting those headers directly in a ResponseEntity returned by a controller: this should not be done by the application, you'll need to fix the problem in your codebase.
@bclozel Is a received ResponseEntity not meant to be directly returned again in a Controller?
For most usecases creating a new ResponseEntity should solve the issue (when original Headers are not required at all): return ResponseEntity.status(responseEntity.getStatusCode()).body(responseEntity.getBody())
@Hollerweger Proxies do more than just copying HTTP responses. There are many things to consider, like compression, security headers, caching, content-neogtiation, Forward headers and more. So in general, I don't think that they're meant to be returned directly from a Controller.
i found prepareResponse() function in tomcat-embed-core package, Http11Processor.java file,
use headers.addValue(Constants.TRANSFERENCODING).setString(Constants.CHUNKED) to add duplicate chunked, not checking if already exists, if it is reasonable ?
@xunzhaoderen Tomcat is responsible for setting that header because it knows how the body is being written. As said in my previous comment, application components are not in a position to set this header. Guarding against a possible duplicate in Tomcat would not help - maybe the response is not chunked at all and writing this header is invalid anyway?
If your application is adding this header, this should be fixed in your application directly.
In Spring-based microservice applications, when using RestTemplate from a Controller annotated class and returning the result of RestTemplate.exchange in a method that returns a ResponseEntity. Similar to situation 1, this will cause a problem if the microservice to which you are talking returns a transfer-encoding header because RestTemplate.exchange will copy all of the response headers from the client response into the ResponseEntity it creates, including the transfer-encoding header.
For most usecases creating a new ResponseEntity should solve the issue (when original Headers are not required at all): return ResponseEntity.status(responseEntity.getStatusCode()).body(responseEntity.getBody())
Reacted by npschoo, Chris Willmore and Rameez ul haq
I am also affected by the "Trigger number 3" mentioned above.
This issue seems to be much worse with HTTP/2 headers.
The :status pseudo-header (among others) is duplicated, which causes issues in Chrome. (ERR_HTTP2_PROTOCOL_ERROR)
Hoping for another solution rather than refactoring tons of Controller methods.
Perhaps there's some way to prevent the duplicate headers app-wide with a single Filter?
Activity
spring-projects-issues commentedon Jun 29, 2018
Juergen Hoeller commented
From a quick review, I can't find any place where we explicitly set the "Transfer-Encoding" header in Spring's processing, in particular not to "chunked". Does this maybe come through the HTTP server or some HTTP client library here?
Brian Clozel, assigning this to you for further consideration...
spring-projects-issues commentedon Jun 29, 2018
Pradeep Gadi commented
I have added a filter and tested the same, this issue is not because of the Httpserver. I am not sure how to figure it out whether the issue is with HTTP client or Spring. Could you please provide me any suggestion on how to proceed with this issue.
spring-projects-issues commentedon Aug 23, 2018
Brian Clozel commented
Hi Pradeep Gadi,
Could you provide us with a sample application? I can't seem to reproduce this at the moment.
BSchwetzel commentedon Apr 14, 2021
We stumbled upon this just yesterday and opened an issue against traefik because of this (traefik/traefik#8060). Basically what we do is using a Spring Boot application as a proxy, as we don't want to deal with CORS and Same-Origin-Policy and all that stuff. Thankfully, our test case for this is rather simple:
Now if we curl that controller:
As you can see we have Transfer-Encoding present twice, which breaks traefik (by breaking golang's net/http it seems: https://github.com/golang/go/blob/59bfc18e3441d9cd0b1b2f302935403bbf52ac8b/src/net/http/transfer.go#L634-L637)
We are using Spring Boot 2.4.4 / Spring 5.3.5
bclozel commentedon Apr 14, 2021
Hi @BSchwetzel
In the meantime, we received out-of-band feedback on issues similar to yours. I'll try to summarize our findings in this comment.
As far as I understand, golang introduced this restriction in the http layer to prevent HTTP smuggling attacks - but this can only apply to requests received by servers/proxies, not responses sent by servers. So while this change is enforcing a new restriction, it's not meant to protect you here. This change not only applies to traefik, but to many proxies/servers using golang.
We've only found that problem in Spring applications with the exact use case you're describing: a controller uses an HTTP client to retrieve a response and returns it as the response of the controller handler method without any modification. We consider this use case as problematic for several reasons:
Spring provides a way to set response headers using
ResponseEntity, but does not enforce HTTP validation here, since it's the job of the web server. Some server implementations are more lenient than others and might reject or ignore combinations. In this case, servers like Tomcat or Netty might choose to enforce stricter rules as well in the future (see Refuse adding invalid HTTP 2.0 headers apache/tomcat#277 and https://bz.apache.org/bugzilla/show_bug.cgi?id=64144, for example). Some Servlet containers are already "fixing" the response for you if it contains bothTransfer-EncodingandContent-Length.This is not specific to Spring, as you could do the same thing with the HTTP Servlet response directly - in that case, Spring would have no way of enforcing rules or cleaning the response.
Applications are not in a position to set the
Transfer-Encodingresponse header because it has not the necessary visibility to set that correctly, nor the ability to then send a correctly formatted chunked body.So while sending duplicate
chunkedvalues are illegal anyway, we don't think that Spring is in a position to fix that. It's up to servers to decide what they want to do with this.Now we've had similar discussions with developers using the same usage pattern (controllers proxying requests). The chunked header value is a symptom of a much bigger problem: copying headers in and out can cause many HTTP and security issues. HTTP headers can carry information about the state of the connection, security restrictions or options negotiated between client and server. This implementation is a typical example of possible HTTP smuggling vulnerabilities and I'd encourage you to consider this in your application.
At a minimum, headers should be properly filtered (in and out). I understand that we're probably looking at a very restricted use case, but implementing a general purpose proxy/gateway is not easy and there are many more problems with that. Maybe Spring Cloud Gateway can be helpful.
anwarmd commentedon Oct 22, 2021
We are also seeing same issue, "Transfer-Encoding: chunked" in response and using following SPring boot version 2.5.2.
bclozel commentedon Oct 22, 2021
@anwarmd this issue is about getting duplicate
Transfer-Encoding: chuckedresponse headers. If you're seeing this problem, this is very likely due to your application setting those headers directly in aResponseEntityreturned by a controller: this should not be done by the application, you'll need to fix the problem in your codebase.anwarmd commentedon Oct 25, 2021
@bclozel Can you please suggest me on
Hollerweger commentedon Aug 31, 2022
@bclozel Is a received
ResponseEntitynot meant to be directly returned again in a Controller?For most usecases creating a new ResponseEntity should solve the issue (when original Headers are not required at all):
return ResponseEntity.status(responseEntity.getStatusCode()).body(responseEntity.getBody())bclozel commentedon Aug 31, 2022
@Hollerweger Proxies do more than just copying HTTP responses. There are many things to consider, like compression, security headers, caching, content-neogtiation, Forward headers and more. So in general, I don't think that they're meant to be returned directly from a Controller.
NameFILIP commentedon Aug 30, 2023
Should this issue be reopened? It's still happening for us with Spring 2.7.2
bclozel commentedon Aug 30, 2023
@NameFILIP we have assessed that this is not a bug in Spring. See #21523 (comment)
If this does not work for you, please create a new issue with a minimal sample application showing the problem.
xunzhaoderen commentedon Jan 31, 2024
i found prepareResponse() function in tomcat-embed-core package, Http11Processor.java file,
use headers.addValue(Constants.TRANSFERENCODING).setString(Constants.CHUNKED) to add duplicate chunked, not checking if already exists, if it is reasonable ?
bclozel commentedon Jan 31, 2024
@xunzhaoderen Tomcat is responsible for setting that header because it knows how the body is being written. As said in my previous comment, application components are not in a position to set this header. Guarding against a possible duplicate in Tomcat would not help - maybe the response is not chunked at all and writing this header is invalid anyway?
If your application is adding this header, this should be fixed in your application directly.
xunzhaoderen commentedon Feb 1, 2024
the response chucked header is passed from another http interface, can I directly filter transfer-encoding header ?
VariabileAleatoria commentedon Feb 5, 2024
completely wrong
snowymike commentedon Jun 24, 2024
posting this in a couple spring issues on this topic in the hopes it helps others. the best writeup of this problem i've encountered is https://knowledge.broadcom.com/external/article/298108/too-many-transfer-encodings-502-chunked.html. Trigger number 3 in that article was our problem
@Hollerweger 's comment above in #21523 (comment) provides an alternative that avoided this problem for us.
npschoo commentedon Jun 24, 2024
I am also affected by the "Trigger number 3" mentioned above.
This issue seems to be much worse with HTTP/2 headers.
The
:statuspseudo-header (among others) is duplicated, which causes issues in Chrome. (ERR_HTTP2_PROTOCOL_ERROR)Hoping for another solution rather than refactoring tons of Controller methods.
Perhaps there's some way to prevent the duplicate headers app-wide with a single Filter?
srinidhi-costrategix commentedon Jan 15, 2025