Repository navigation
What are possible reasons for CSRFTokenMismatchError?
#4389
Replies: 2 comments
|
There are several common causes for 1. Cookie domain / secure flag mismatch Blitz sets the CSRF token in a cookie. If your production setup has any of these, the cookie might not be sent back:
2. CDN or load balancer caching If a CDN (CloudFront, Vercel Edge, etc.) caches HTML pages that contain the CSRF token, multiple users could end up with the same embedded token but different session cookies. Check that your HTML responses have 3. Multiple tabs / stale tokens If a user has your app open in multiple tabs and one tab triggers a session refresh, the CSRF token in the other tab becomes stale. This is especially common if your sessions have short expiry times. 4. Browser extensions or privacy settings Extensions like uBlock Origin, Privacy Badger, or strict Safari ITP can block or partition cookies, preventing the CSRF cookie from being sent with requests. This is "intended" from the browser's perspective but causes CSRF mismatches. 5. Cross-origin requests If your frontend and API are on different subdomains (e.g., To debug in production: // Add logging to see what's happening
import { middleware } from "blitz"
export default middleware(async (req, res, next) => {
console.log("CSRF cookie present:", !!req.cookies["sAntiCrfToken"])
console.log("CSRF header present:", !!req.headers["anti-csrf"])
console.log("Origin:", req.headers.origin)
await next()
})In most production cases I've seen, it's either #1 (reverse proxy eating cookies) or #4 (privacy-focused browsers). Check your proxy config first - make sure it forwards |
|
Yes — this can absolutely be "intended" in the sense that Blitz is rejecting a request whose CSRF token and CSRF cookie no longer match. The common production-only causes are usually one of these:
What I would log in production for failing requests:
In my experience, the two biggest culprits are proxy/cookie config and cached HTML. I would start there first. |
Uh oh!
There was an error while loading. Please reload this page.
We are currently experiencing a consistent number of
CSRFTokenMismatchErrormessages. We cannot reproduce this issue locally, which is why I wanted to ask: What are possible reasons forCSRFTokenMismatchError? Is it possible that this is intended?All reactions