Description
Describe the bug
Prior to Spring Security 6, the CsrfTokenRepository used the response.addCookie in the saveToken method to add the XSRF-TOKEN cookie. In Spring-Security 6 it was changed to call response.addHeader which bypasses the Tomcat CookieProcessor.
To Reproduce
I forked the Spring-Security-Samples project and used hello-security-explicit
https://github.com/burghduffkc/spring-security-samples/commits/main/servlet/spring-boot/java/hello-security-explicit, below are the modifications and steps to reproduce.
- Add the following to the HttpSecurity in the SecurityFilterChain bean of the SecurityConfiguration class
.csrf((httpSecurityCsrfConfigurer -> {
//START Bug change
httpSecurityCsrfConfigurer
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.ignoringRequestMatchers(request -> Pattern.compile("^(GET)$").matcher(request.getMethod()).matches());
}))
- Add the following Bean to the SecurityConfiguration class
@Bean
WebServerFactoryCustomizer<TomcatServletWebServerFactory> servletWebServerFactoryWebServerFactoryCustomizer(){
return container -> container.addContextCustomizers(context -> {
Rfc6265CookieProcessor rfc = new Rfc6265CookieProcessor();
rfc.setSameSiteCookies("STRICT");
context.setCookieProcessor(rfc);
});
}
- run the spring boot application
- Open Chrome and the Chrome Developer tools and access
http://locahost:8080
- Look at the cookies under Application -> Storage -> Cookies. You will see that the
JSESSIONID
cookie has the sameSite set to Strict, but theXSRF-TOKEN
does not have sameSite set. This is because theCsrfTokenRepository#saveToken
does not use addCookie, bypassing the Tomcat CookieProcessor.
Expected behavior
A clear and concise description of what you expected to happen.
The XSRF-TOKEN cookie should have sameSite set to Strict.
Sample
Change to hello-security-explicit sample
burghduffkc/spring-security-samples@fb8971a
Full code for Spring-Security Example
https://github.com/burghduffkc/spring-security-samples/commits/main/servlet/spring-boot/java/hello-security-explicit
A link to a GitHub repository with a minimal, reproducible sample.
Reports that include a sample will take priority over reports that do not.
At times, we may require a sample, so it is good to try and include a sample up front.