Skip to content

samesite set by Tomcat CookieProcessor ignored when creating XSRF-TOKEN cookie in CsrfTokenRepository #14131

Closed
@burghduffkc

Description

@burghduffkc

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.

  1. 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());
}))
  1. 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);
		});
	}
  1. run the spring boot application
  2. Open Chrome and the Chrome Developer tools and access http://locahost:8080
  3. Look at the cookies under Application -> Storage -> Cookies. You will see that the JSESSIONID cookie has the sameSite set to Strict, but the XSRF-TOKEN does not have sameSite set. This is because the CsrfTokenRepository#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.

Metadata

Metadata

Labels

in: webAn issue in web modules (web, webmvc)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions