Note: I have read the answer to this question, but found it unsatisfactory, probably because I don't understand enough about security.
I'm referring to a frontend server-side rendered SPA + a backend API, which needs to use cookies, and needs persistent login. Since I need cookies for server-side authentication, I want to eliminate any usage of LocalStorage for authentication (to (further) mitigate XSS)
Upon successful login, an httpOnly cookie is set by backend,
auth_token
, and is sent back to the client's browser. This contains the authentication token. It will be sent automatically with each request (as is every cookie).Another cookie is set by backend,
csrftoken
, which is a long random string.The frontend application (a JS framework) does NOT refer, store, or save
auth_token
anywhere in its code.The frontend application code checks to see if there's a
csrftoken
cookie present, and if there is, attaches it as a custom header,X-CSRFToken
, with the same value ascsrftoken
.The server authenticates a user successfully if:
5.1
auth_token
present and valid (database token)5.2
csrftoken
cookie +X-CSRFToken
header are present and not empty5.3
csrftoken
cookie +X-CSRFToken
are equal
Here's my question: What's the difference between having csrftoken
(and therefore the X-CSRFToken
header) as a very long, random string, and something like 1
? It is clearly stated in multiple places that a CSRF attacker can't create custom headers..If the difference is "brute-forcing" the CSRF token, please explain what that would entail, and how the X-CSRFToken
header be set?