spring-framework: ContentCachingResponseWrapper no longer honors Content-Type and Content-Length
As of Spring Framework 6.1.4, ContentCachingResponseWrapper returns null for Content-Type and Content-Length via getHeader("Content-Type"), getContentType(), etc.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 5
- Comments: 28 (13 by maintainers)
Commits related to this issue
- Honor Content-[Type|Length] headers from wrapped response again Commit 375e0e6827 introduced a regression in ContentCachingResponseWrapper (CCRW). Specifically, CCRW no longer honors Content-Type and... — committed to sbrannen/spring-framework by sbrannen 3 years ago
- Honor Content-[Type|Length] headers from wrapped response again Commit 375e0e6827 introduced a regression in ContentCachingResponseWrapper (CCRW). Specifically, CCRW no longer honors Content-Type and... — committed to spring-projects/spring-framework by sbrannen 3 years ago
- Return unique set from ContentCachingResponseWrapper.getHeaderNames() Prior to this commit, getHeaderNames() returned duplicates for the Content-Type and Content-Length headers if they were set in th... — committed to spring-projects/spring-framework by sbrannen 3 years ago
- Honor Content-[Type|Length] headers from wrapped response again Commit 375e0e6827 introduced a regression in ContentCachingResponseWrapper (CCRW). Specifically, CCRW no longer honors Content-Type and... — committed to spring-projects/spring-framework by sbrannen 3 years ago
- Honor Content-[Type|Length] headers from wrapped response again Commit 375e0e6827 introduced a regression in ContentCachingResponseWrapper (CCRW). Specifically, CCRW no longer honors Content-Type and... — committed to spring-projects/spring-framework by sbrannen 3 years ago
- Do not cache Content-Type in ContentCachingResponseWrapper Based on feedback from several members of the community, we have decided to revert the caching of the Content-Type header that was introduce... — committed to spring-projects/spring-framework by sbrannen 3 years ago
- Do not cache Content-Type in ContentCachingResponseWrapper Based on feedback from several members of the community, we have decided to revert the caching of the Content-Type header that was introduce... — committed to spring-projects/spring-framework by sbrannen 3 years ago
- Do not cache Content-Type in ContentCachingResponseWrapper Based on feedback from several members of the community, we have decided to revert the caching of the Content-Type header that was introduce... — committed to spring-projects/spring-framework by sbrannen 3 years ago
@amuttsch and @resmo, thanks for confirming that the latest snapshots work! 👍
I’ll likely backport to
6.0.xand5.3.xtomorrow.Hi everybody,
Thanks a lot for all of the prompt feedback!
In the end, we decided to revert the
Content-Typecaching inContentCachingResponseWrapper.So, hopefully that will address all remaining issues. 🤞
See cca440eb8e46c14d19df22df837ed4b90dd38673 for details.
The latest fix is now available in
6.1.5-SNAPSHOTbuilds.So, if you’re using
6.1.x, please try out the snapshot and let us know if the latest fix addresses the regressions you’ve encountered.Once the fix is confirmed by a number of you, we’ll then backport to
6.0.xand5.3.x.Cheers,
Sam
My production code works as well 👍
Thanks for the quick response. My demo code works now, I can check my production code tomorrow. But I think it will most likely work too.
@yikuo123, @amuttsch, @tjalling-ran, and anybody else interested in helping out…
The proposed fix is now available in
6.1.5-SNAPSHOTbuilds and will be available in upcoming6.0.18-SNAPSHOTand5.3.33-SNAPSHOTbuilds.As mentioned in https://github.com/spring-projects/spring-framework/issues/32317#issuecomment-1962732906, we’d be grateful if you can test against the snapshots and report back here to let us know if the fixes work for you.
Thanks!
Sam
p.s. In the interim, we are leaving this issue “open” until we receive confirmations that the fixes work.
spring-framework 5.3.32 may also be affected.
After upgrading to it (because of CVE-2024-22243), we no longer had content type headers in responses. So I downgraded back to 5.3.29. (luckily, we don’t think we have any endpoints that are vulnerable to the CVE)
We have the problem that our rest controller, which sits behind a
ShallowEtagHeaderFilter, does not return theContent-Typeheader anymore. I guess this is also related to this change.Just tested with
5.3.33-SNAPSHOT, works for us too!Glad to hear it, @tjalling-ran!
And thanks for letting us know.
The fix has now been backported to
6.0.xand5.3.xand will be available in upcoming6.0.18-SNAPSHOTand5.3.33-SNAPSHOTbuilds.If you can test your applications against those snapshots and report back here, that would be great.
Thanks,
Sam
Just for the record: as mentioned in https://github.com/spring-projects/spring-boot/issues/39745#issuecomment-1964865263: spring-core-6.1.5-20240226.173247-18.jar fixes my issue, umlauts are shown correctly (content-length size is as it was in 6.1.3)
Thanks!
I created a reproducible setup here: https://github.com/amuttsch/spring-boot-etag-content-header
I tried the 6.1.5-SNAPSHOT but it did not work, the
Content-Typeheader is still missing. To rule out any configuration error on my side: I added the repositoryand overwrote the version using
ext["spring-framework.version"] = "6.1.5-SNAPSHOT". At least IntelliJ shows me that theShallowEtagHeaderFilterwas loaded fromspring-web-6.1.5-SNAPSHOTand I synced, cleared and rebuild my gradle project.Thank you very much!
I tried the 6.1.5-SNAPSHOT build and the fix works.
I may have found the issue. (Though I’m not familiar enough with the code to be fully confident that I didn’t miss anything.)
As you know,
ConditionalContentCachingResponseWrapper.setContentType()recently started setting its own ContentType instead of delegating to the wrapped Response.As long as
ConditionalContentCachingResponseWrapper.copyBodyToResponse()does indeed do copying, this is fine, as it will copy its ContentType to the wrapped response. However, whenConditionalContentCachingResponseWrapper.content.size()is0, no copying is done.So then the question is: how can
ConditionalContentCachingResponseWrapper.content.size()be0? The answer is thatConditionalContentCachingResponseWrapper.getOutputStream()only returns its own output stream if content caching is enabled. If content caching is disabled, it will delegate to the wrapped response’s output stream. Thus,ConditionalContentCachingResponseWrapperwill not have any content.It will not delegate to the wrapped response’s
setContentType(), however (as stated above). So now we have a situation where the content is stored in the wrapped response, but the content type is stored in the wrapper.And because a wrapper without content doesn’t copy any of its own properties to the wrapped response, the wrapped response’s content type (and possibly other properties) will remain
null.Current work on this issue can be viewed in the following feature branch.
https://github.com/spring-projects/spring-framework/compare/main...sbrannen:spring-framework:issues/gh-32317-ContentCachingResponseWrapper-and-preset-headers
@jhoeller We set them through the
@GetMapping(produces = [MediaType.APPLICATION_JSON_VALUE])annotation. We return aResponseEntity.ok()and also tried to set the content type using.contentType(MediaType.APPLICATION_JSON_VALUE), which did not work either.It may be caused by this commit https://github.com/spring-projects/spring-framework/commit/375e0e6827216ceb38e9bcf0b97b46bb79d79de6