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

Most upvoted comments

@amuttsch and @resmo, thanks for confirming that the latest snapshots work! 👍

I’ll likely backport to 6.0.x and 5.3.x tomorrow.

Hi everybody,

Thanks a lot for all of the prompt feedback!

In the end, we decided to revert the Content-Type caching in ContentCachingResponseWrapper.

So, hopefully that will address all remaining issues. 🤞

See cca440eb8e46c14d19df22df837ed4b90dd38673 for details.

The latest fix is now available in 6.1.5-SNAPSHOT builds.

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.x and 5.3.x.

Cheers,

Sam

I can check my production code tomorrow. But I think it will most likely work too.

That would be much appreciated.

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-SNAPSHOT builds and will be available in upcoming 6.0.18-SNAPSHOT and 5.3.33-SNAPSHOT builds.

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 the Content-Type header 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.x and 5.3.x and will be available in upcoming 6.0.18-SNAPSHOT and 5.3.33-SNAPSHOT builds.

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!

@yikuo123, @amuttsch, @tjalling-ran, and anybody else interested in helping out…

The proposed fix is now available in 6.1.5-SNAPSHOT builds and will be available in upcoming 6.0.18-SNAPSHOT and 5.3.33-SNAPSHOT builds.

As mentioned in #32317 (comment), 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.

I tried the 6.1.5-SNAPSHOT but it did not work, the Content-Type header is still missing. To rule out any configuration error on my side: I added the repository

repositories {
  maven { url = uri("https://repo.spring.io/snapshot") }
}

and overwrote the version using ext["spring-framework.version"] = "6.1.5-SNAPSHOT". At least IntelliJ shows me that the ShallowEtagHeaderFilter was loaded from spring-web-6.1.5-SNAPSHOT and I synced, cleared and rebuild my gradle project.

I’ve pushed a potential fix to 6.1.x and main (see commit 5680d20).

@yikuo123, @amuttsch, @tjalling-ran, and anybody else interested in helping out…

We would be grateful if you could try out one of the upcoming 6.1.5-SNAPSHOT builds and let us know if the proposed fix addresses the regressions you’ve encountered!

If you confirm the fix works, we’ll then backport to 6.0.x and 5.3.x.

Cheers,

Sam

p.s. you can find information on accessing snapshot builds here.

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, when ConditionalContentCachingResponseWrapper.content.size() is 0, no copying is done.

So then the question is: how can ConditionalContentCachingResponseWrapper.content.size() be 0? The answer is that ConditionalContentCachingResponseWrapper.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, ConditionalContentCachingResponseWrapper will 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.

@jhoeller We set them through the @GetMapping(produces = [MediaType.APPLICATION_JSON_VALUE]) annotation. We return a ResponseEntity.ok() and also tried to set the content type using .contentType(MediaType.APPLICATION_JSON_VALUE), which did not work either.