Repository navigation
Description
Updated Description
Spring Security should only write cache related headers (i.e. Cache-Control, Expires, Pragma) if the application has not already provided any of the headers.
Original Description
Mike Noordermeer (Migrated from SEC-2728) said:
The documentation states about the cache-control header support:
bq. If you actually want to cache specific responses, your application can selectively invoke HttpServletResponse.setHeader(String,String) to override the header set by Spring Security. This is useful to ensure things like CSS, JavaScript, and images are properly cached.
Judging from our own experiments and the code, this is not possible. The Pragma: no-cache header will always be set, and is impossible to override, since there is no defined 'cache' value, and no way in the Servlet API to remove a header once set.
This is at least a documentation bug, but it would be nice if the Pragma header is only set when there are no cache-control headers set by the application (but that seems to be hard given the current architecture, since headers are set before the request is passed on in the filter chain).
Workaround
Many users might consider "ignoring security" for the requests to work around the issue. This is not advised as many other important security features will be disabled for any request that is matched. This is important even for static resources.
Other users might disable only headers support or only cache control. This will cause security vulnerabilities for the rest of the application.
This can be worked around with using a configuration like this:
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
RequestMatcher notResourcesMatcher = new NegatedRequestMatcher(new AntPathRequestMatcher("/resources/**"));
HeaderWriter notResourcesHeaderWriter = new DelegatingRequestMatcherHeaderWriter(notResourcesMatcher , new CacheControlHeadersWriter());
http
.headers()
.cacheControl().disable()
.addHeaderWriter(notResourcesHeaderWriter);
}
}
Activity
spring-projects-issues commentedon Dec 3, 2014
Stephen Duncan Jr said:
I don't think just a documentation fix would be sufficient. This basically makes it impossible to set the cache-control security settings while also allowing caching for desired resources. I agree that it seems hard in the current architecture; I think the minimum that could be done is to allow configuration of Spring-Security headers on a URL instead of just globally, like:
http
// ...
.antMatchers("/resources/**")
.headers().contentTypeOptions().frameOptions().httpStrictTransportSecurity().xssProtection();
Obviously, it would also be nice to have a syntax for just disabling the cache control instead of having to specify everything but that header set, but that's just a nice-to-have.
Make OnCommittedResponseWrapper public
rwinch commentedon Mar 15, 2016
This is related to SPR-14005
[-]SEC-2728: Cache-control overriding not possible[/-][+]SEC-2728: Only write cache related headers if no other cache headers found[/+]Merge pull request #3759 from rwinch/gh-2953
eballetbaz commentedon Oct 3, 2016
This feature is quite incompatible with JSON responses generated with standard the Spring AbstractJackson2View.
Indeed by default the class AbstractJackson2View is adding header "Cache-Control: no-store" (and only this one). So when using Jackson Views the tag is doing nothing (and so headers Pragma and Expires are not defined)
vhadzhipopov commentedon Jan 6, 2017
Hey Happy New Year @rwinch !
I'm not sure this should be closed, here is a simple REST endpoint I have:
Using a logging aspect I get:
....Example.cacheTest() with result = <200 OK,Hello World!,{Cache-Control=[max-age=86400]}>But in the browser it's still default Spring Security header:
Cache-Control:no-cache, no-store, max-age=0, must-revalidateWhen I debugged it, it looked like at HttpEntityMethodProcessor.java:183
The Cache-Control header already exists and can't be overriden.
I'm running Spring Boot 1.4.0.RELEASE and Spring Security 4.2.1.RELEASE
If you need anything else don't hesitate to ask. :)
nealeu commentedon Oct 10, 2017
Anyone tearing their hair out at the prospect of having to migrate to JavaConfig for a legacy project (as I was), there does seem to be a way around it.
Ah... by digging around, I think I've avoided the need to do this, you can use the header writer bean with the
<header>tag which would appear to be only static headers, but can actually take a ref to aHeaderWriter.I now have:
and