Skip to content

Add an option to set the SameSite policy in the CookieCsrfTokenRepository #12086

Closed
@kumo829

Description

@kumo829

Expected Behavior

The class org.springframework.security.web.csrf.CookieCsrfTokenRepository should allow settingset the SameSite policy as an optional parameter; with this, when sending the CSRF token in a cookie instead of a header, no warning or error should be displayed in the browser's console.

Current Behavior

With the current implementation of org.springframework.security.web.csrf.CookieCsrfTokenRepository used to send the CSRF token in a cookie instead of a header (widely used when working with, for example JWT), some browsers have started to report a warning stating that:

Cookie “XSRF-TOKEN” does not have a proper “SameSite” attribute value. Soon, cookies without the “SameSite” attribute or with an invalid value will be treated as “Lax”. This means that the cookie will no longer be sent in third-party contexts. If your application depends on this cookie being available in such contexts, please add the “SameSite=None“ attribute to it. To know more about the “SameSite“ attribute, read https://developer.mozilla.org/docs/Web/HTTP/Headers/Set-Cookie/SameSite

This same behavior happens for each response that includes the cookies (potentially every single request other than the login).

Currently, there is o way no set the SameSite policy in the class org.springframework.security.web.csrf.CookieCsrfTokenRepository as internally it uses a jakarta.servlet.http.Cookie which doesn't allows it either.

Context

I've started to see the previous message in the browser's console.
I don't know any workaround other than implementing my own version of org.springframework.security.web.csrf.CookieCsrfTokenRepository with the option of adding the SameSite policy, but I guess that this "problem" is affecting others the same way that is affecting me.

Metadata

Metadata

Assignees

Labels

in: webAn issue in web modules (web, webmvc)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions