-
Notifications
You must be signed in to change notification settings - Fork 20
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
Tokens in the header and cookie are rarely different #81
Comments
The first thing that comes to mind is, is the CSRF cookie expired? What could be happening is, you're including an expired CSRF token in the header, when the request first comes in, there's no CSRF cookie with the request, it generates a new one for the session and sets the You can provide If you're using sessions with server side state, I'd generally recommend |
Thanks! 🤩 The cookie has "session" expiration, it should be ok, but I don't know exactly that is the cookie's lifespan in case of closing browser/closing laptop, etc. I guess there may be some corner cases. It looks like the most reliable solution is to have dedicated /csrf-token endpoint for serving a fresh token together with up-to-date cookie. Something as is in csrf-csrf's examples. Maybe an extra backend call is worth. What do you think? |
We ran into a similar issue. Your issue seems slightly different from ours. But I thought I'd mention our solution in case you find it helpful. Turns out setting sameSite="strict" can cause problems with this. This is because if the request originates from an external source the request is considered "cross-site" and the cookie is not included in the initial request to your server. So if people are following an external link to come to your site you can run into issues where the cookie is missing from their request. Anyway, changing sameSite to "lax" fixed our issue. |
Hi,
First of all... thanks for this library! It's great and easy to use. In my project, I'm trying to solve a mystery when CSRF validation failed because the csrf token in the header and in the cookie didn't match (I'm logging this exact case). It's a rare issue, happening independently on OS/Platform. But I want the system to be reliable. And it seems like I have run out of ideas what could be the reason.
Could it be something like long running sessions, when react SPA is loaded a couple days ago (I store CSRF token to DOM) and then an API call refreshed the cookie?
Thanks so much!
I have a react, SSR, Vite app in this setup:
RequestContext
is something passing data from the backend to the react appcsrf-csrf setup:
And wired like this in express:
In a form, I'm using Axios with this configuration for passing CSRF token back:
The text was updated successfully, but these errors were encountered: