Skip to content

CookieCsrfTokenRepository does not set the XSRF cookie on login response with Spring Security 6 RC1 #12094

@Artur-

Description

@Artur-

Describe the bug
In Spring Security 5.x and 6.x before RC1 includes the new XSRF token in the login response so it is immediately available in the browser. The code in CsrfAuthenticationStrategy.onAuthentication is

this.csrfTokenRepository.saveToken(null, request, response);
CsrfToken newToken = this.csrfTokenRepository.generateToken(request);
this.csrfTokenRepository.saveToken(newToken, request, response);

In 6 RC1 the code only deletes the old token and does not generate nor save a new one

this.tokenRepository.saveToken(null, request, response);

If the login request is a standard browser POST, then it is typically followed by a page reload which loads the new token. However when the login request is an XHR (fetch), the cookie should be included in the login response as it will not be followed by a new request. This does not happen with RC1.

To Reproduce / Sample

    @Bean(name = "MySecurityFilterChainBean")
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http.authorizeHttpRequests().requestMatchers(new AntPathRequestMatcher("**")).authenticated();
        http.formLogin();
        http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());

        return http.build();
    }

Login and check the POST response in the browser. There is only

Set-Cookie: XSRF-TOKEN=; Max-Age=0; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/

Expected behavior
There should also be the new token included so the following fetch/xhr works correctly.

Metadata

Metadata

Assignees

Labels

for: stackoverflowA question that's better suited to stackoverflow.com

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions