Skip to content

SEC-2728: Only write cache related headers if no other cache headers found #2953

Closed

Description

@spring-projects-issues

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

  1. spring-projects-issues commented on Dec 3, 2014

    @spring-projects-issues
    Author

    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.

  2. added
    in: web
    type: bug
    type: jira
    on Feb 5, 2016
  3. modified the milestones: 4.1.0 RC1, 4.0 Backlog on Mar 15, 2016
  4. self-assigned this
    on Mar 15, 2016
  5. added 2 commits that reference this issue on Mar 15, 2016
  6. rwinch commented on Mar 15, 2016

    @rwinch
    Member

    This is related to SPR-14005

  7. changed the title [-]SEC-2728: Cache-control overriding not possible[/-] [+]SEC-2728: Only write cache related headers if no other cache headers found[/+] on Mar 15, 2016
  8. added a commit that references this issue on Mar 15, 2016
  9. eballetbaz commented on Oct 3, 2016

    @eballetbaz

    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)

  10. vhadzhipopov commented on Jan 6, 2017

    @vhadzhipopov

    Hey Happy New Year @rwinch !

    I'm not sure this should be closed, here is a simple REST endpoint I have:

    @RequestMapping(value = "/hi", method = RequestMethod.GET)
    public ResponseEntity cacheTest() {
        return ResponseEntity.ok()
                             .cacheControl(CacheControl.maxAge(1, TimeUnit.DAYS))
                             .body("Hello World!");
    }

    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-revalidate

    When I debugged it, it looked like at HttpEntityMethodProcessor.java:183

    if (!entityHeaders.isEmpty()) {
        for (Map.Entry<String, List<String>> entry : entityHeaders.entrySet()) {
            if (!outputHeaders.containsKey(entry.getKey())) {
                outputHeaders.put(entry.getKey(), entry.getValue());
            }
        }
    }

    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. :)

  11. nealeu commented on Oct 10, 2017

    @nealeu

    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 a HeaderWriter.

    I now have:

        @Bean
        public HeaderWriter selectiveCacheControlHeaderWriter() {
    
            RequestMatcher paths = new OrRequestMatcher(
                    new AntPathRequestMatcher("/resources/**"),
                    new AntPathRequestMatcher("/api/**")
            );
    
            return new DelegatingRequestMatcherHeaderWriter(
                    new NegatedRequestMatcher(paths),
                    new CacheControlHeadersWriter());
        }
    

    and

        <headers>
            <cache-control disabled="true"/>
            <header ref="selectiveCacheControlHeaderWriter"/>
            <frame-options />
            <xss-protection />
        </headers>
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: webtype: bugtype: jira

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions