-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
Improve CSRF documentation for Javascript usage in multi-page apps #13009
Comments
@ckalan thanks for this input in improving the CSRF documentation! I have updated the main CSRF chapter of the reference documentation for the 6.1 release. You can preview it here: https://docs.spring.io/spring-security/reference/6.1-SNAPSHOT/servlet/exploits/csrf.html You may note that the old docs actually did have an example of using I did not incorporate your suggestion of using a global variable, as I felt that was a bit too specific for our reference docs, as the existing example (organized and elaborated more thoroughly) should hopefully lead users in the right direction. I could be wrong of course, and if you still feel the docs need improvement, let me know. In the meantime, I'll close this as a duplicate of gh-13089, but this was valuable for the docs refresh, thanks! |
@sjohnr Thanks for the changes. I think that's perfect now. Global variable was just an example. I think it is much better to have the examples with the meta tags as you've done. |
Unfortunately this documentation is not applied to the 6.0.x version. I was able to apply the directions from 6.1.x on the 6.0.x based spring-security. Not everyone can upgrade to 6.1.x yet, for example due to the use of spring-cloud. |
Understood @JurrianFahner, sorry you didn't find the documentation in the version you needed. We generally apply enhancements (including documentation) on our |
@sjohnr The problem is in version 6.0 that there breaking changes were introduced in version 6.0 (because of semantic versioning) and that's fine. For people that are implementing stuff based on this version would read documentation for version 6.0. It would be convenient if improvement of documentation in the later version is retrofit in the earlier versions (when applicable and e.g. only for one minor patch version before). I created an issue #13910 to raise attention to this (small) problem. The reason that I commented on a closed issue, is to help the community to mention the applicability of 6.1 on the 6.0 delivery on this subject. |
Thanks for opening an issue on this. The CSRF docs improvements are now backported to 6.0. |
Expected Behavior
Documentation can state that if you have a multi-page app like one that mounts React components where the API requests are handled by the JavaScript, you can keep deferred loading and breach protection via
HttpSessionCsrfTokenRepository
and using X-CSRF-TOKEN (not X-XSRF-TOKEN) header. You don't need to useCookieCsrfTokenRepository
in those cases and you can have the default CSRF configuration without any customization.The only thing you need to do is to put the CSRF token inside the page in a global JS variable or a meta tag and configure your HTTP library to send it with the X-CSRF-TOKEN header which is already recognized by the
HttpSessionCsrfTokenRepository
;Current Behavior
Documentation is not clear for such scenarios and I had to check the source code to find my way out of it.
Context
This applies to applications that send CSRF tokens to REST APIs via JavaScript and want to get all the benefits of deferred loading and BREACH protection.
Example code:
Spring security config:
JavaScript API with AXIOS
Thyemeleaf layout
The text was updated successfully, but these errors were encountered: