-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Description
Summary
Intermittent errors related to invalid csrf tokens when using a CsrfCookieTokenRepository
Actual Behavior
We're seeing intermittent messages in our logs for an internal application
Invalid CSRF Token '3c49b924-5b26-4413-b456-7a44f5ac4751' was found on the request parameter '_csrf' or header 'X-XSRF-TOKEN'.
Expected Behavior
Tokens generated by spring-security shouldn't be rejected. I may be misunderstanding this, but the documentation seems to suggest that the issue isn't related to timeouts, as tokens from the CsrfCookieTokenRepository do not expire.... in the way that tokens from a HttpSessionCsrfTokenRepository would when the session times out.
Configuration
Http security is configured as follows
.csrf()
.csrfTokenRepository(new CookieCsrfTokenRepository().withHttpOnlyFalse())
....
The token is actually being injected into the HTML as a metatag via a thymeleaf template at the moment.
<meta name="_csrf" th:content="${_csrf != null ? _csrf.token : ''}"/>
The token is being passed in a header via a json/ajax call.
const csrfHeaderValue = this.getMetaContent("_csrf");
options.headers = new Headers({
"X-Requested-With": "XMLHttpRequest",
"Content-Type": "application/json",
"X-XSRF-TOKEN": csrfHeaderValue,
...options.headers
});
fetch(getBaseUrl() + path, options)
As can be seen in the error message above, a non-null token value is being passed in with requests, but is rejected.
Version
Spring boot starter security - 2.0.2.RELEASE