-
Notifications
You must be signed in to change notification settings - Fork 6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add the ability to set the SameSite policy to the CRSF Cookie #12109
Conversation
Add a new public method to the class org.springframework.security.web.csrf.CookieCsrfTokenRepository that allows setting 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. Removes unnecessary default no-parameters empty constructor. Fix gh-12086
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, @kumo829, for the PR! I've left some feedback inline.
web/src/main/java/org/springframework/security/web/csrf/CookieCsrfTokenRepository.java
Outdated
Show resolved
Hide resolved
web/src/main/java/org/springframework/security/web/csrf/CookieCsrfTokenRepository.java
Outdated
Show resolved
Hide resolved
web/src/main/java/org/springframework/security/web/csrf/CookieCsrfTokenRepository.java
Outdated
Show resolved
Hide resolved
- Mark setCookieHttpOnly, setCookieDomain, setCookieMaxAge and setSecure as deprecated. - Add the method addCookieInitializer which allows to set properties to the ResponseCookieBuilder without having to add new setter methods. - Apply same changes to CookieServerCsrfTokenRepository. - Add unit tests to validate the use of same site policy.
Hi, @jzheaux; thanks for the comments. I made the requested changes. Please let me know if you consider any other changes required. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks again, @kumo829! I've left some additional feedback inline.
web/src/main/java/org/springframework/security/web/csrf/CookieCsrfTokenRepository.java
Outdated
Show resolved
Hide resolved
web/src/main/java/org/springframework/security/web/csrf/CookieCsrfTokenRepository.java
Outdated
Show resolved
Hide resolved
web/src/test/java/org/springframework/security/web/csrf/CookieCsrfTokenRepositoryTests.java
Show resolved
Hide resolved
web/src/test/java/org/springframework/security/web/csrf/CookieCsrfTokenRepositoryTests.java
Outdated
Show resolved
Hide resolved
.../java/org/springframework/security/web/server/csrf/CookieServerCsrfTokenRepositoryTests.java
Show resolved
Hide resolved
- Mark setCookieHttpOnly, setCookieDomain, setCookieMaxAge and setSecure as deprecated. - Add the method setCookieCustomizer which allows to set properties to the ResponseCookieBuilder without having to add new setter methods. - Apply same changes to CookieServerCsrfTokenRepository. - Add unit tests to validate the use of same site policy.
- Mark setCookieHttpOnly, setCookieDomain, setCookieMaxAge and setSecure as deprecated. - Add the method setCookieCustomizer which allows to set properties to the ResponseCookieBuilder without having to add new setter methods. - Apply same changes to CookieServerCsrfTokenRepository. - Add unit tests to validate the use of same site policy.
- Changes to use assertJ semantic methods as isTrue and isNull
Hi @jzheaux, thanks for the comments and suggestions. I addressed them. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, @kumo829! I left two minor items for you inline.
In preparation for merging, will you please squash your commits into two; one for the feature and one for the Junit cleanup? In the feature commit, will you please format it to include the ticket like so:
Your Commit Title
Any additional description
Closes gh-12086
Doing it this way makes so that when the PR is merged, the ticket is automatically closed.
.httpOnly(this.cookieHttpOnly) | ||
.domain(this.cookieDomain); | ||
|
||
this.cookieCustomizer.accept(cookieBuilder); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The indentation looks off here. Would you mind adjusting it, please?
* @since 6.1 | ||
*/ | ||
public void setCookieCustomizer(Consumer<ResponseCookie.ResponseCookieBuilder> cookieCustomizer) { | ||
this.cookieCustomizer = cookieCustomizer; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please check for null. You can use Assert.notNull(cookieCustomizer, "cookieCustomizer must not be null")
.
…enRepository - Mark setCookieHttpOnly, setCookieDomain, setCookieMaxAge and setSecure as deprecated. - Add the method setCookieCustomizer which allows to set properties to the ResponseCookieBuilder without having to add new setter methods. Closes gh-12086
Add a new public method to the class org.springframework.security.web.csrf.CookieCsrfTokenRepository that allows setting 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.
Removes unnecessary default no-parameters empty constructor.
This could fix gh-12086