"error": "csrf validation error" #26
-
I have cloned the project, tested it and it works as I would like to implement it. However I have a problem, because when I query the token and pass it to my header/body it is done correctly. is there any way to store the csrf that is set in the cookie? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
What do you mean the token changes? Why should it change? It should only change if you're generating a new one yourself, this is completely within your control. The logic on when to generate a token is 100% up to you, so if you're generating a new token when you don't want to be, then don't? If you're facing this issue when using the application across multiple tabs or something, this is a side effect of the double submit cookie pattern, and there are various approaches to work around that. For example, you could generate one token per user session instead of per request, you could design your frontend so that a token is requested and used right before submission as opposed to page load.
Header or body? By default, it's expected to be in the header. If you're putting it in the body, you'll need to customise
It's already stored inside the cookie. The cookie is httpOnly, so you can't access that through the frontend - this is required as that is how the double submit pattern works. Alternatively you would use csrf-sync which implements the token synchroniser pattern instead. Can't really give much more advice without seeing your implementation, if you have a code sandbox or a repo that reproduces the problem, that would help a lot. |
Beta Was this translation helpful? Give feedback.
Hey @sycod-maker I pulled down your code and took a look.
The problem is your cors configuration. In the code you have shared, you have this:
What this does is, it means you're not accepting any http only cookies on requests from the configured origins. This means the cookie gets ignored. If you change this value to true, it works as expected, there is no longer a csrf error.
If you don't want to accept http only cookies, then it doesn't make sense. You only need CSRF protection if you're using http only cookies (typically sessions) for authentication. If you are not using http only cookies for authentication, then you aren't susceptible to CSRF attacks.
https://de…