Skip to content

fix: handle stale CSRF tokens gracefully#54

Merged
occult merged 1 commit into
occult:mainfrom
VitorFOG:fix/csrf-stale-cookie-handler
Mar 18, 2026
Merged

fix: handle stale CSRF tokens gracefully#54
occult merged 1 commit into
occult:mainfrom
VitorFOG:fix/csrf-stale-cookie-handler

Conversation

@VitorFOG

Copy link
Copy Markdown
Contributor

Summary

  • When CSRF validation fails (stale cookies from environment switches, redeployments, or reverse proxy setups), the middleware now clears the invalid cookie and redirects back instead of returning a 403 error
  • The redirect triggers a fresh CSRF token to be set automatically, so the user can retry seamlessly

Context

When deploying behind reverse proxies (e.g., Cloudflare → Traefik → App) or switching between local dev and production, browsers may hold stale XSRF-TOKEN cookies that don't match the server's expected token. This causes a 403 Forbidden error on any POST request (login, form submissions, etc.) with no clear way for the user to recover other than manually clearing cookies.

Test plan

  • Deploy behind a reverse proxy, verify login works
  • Clear cookies manually, verify fresh token is set on redirect
  • Switch between environments (local → production), verify no 403 errors

🤖 Generated with Claude Code

…ecting

When deploying behind reverse proxies or switching between environments,
stale CSRF cookies cause 403 errors. Instead of showing an error, clear
the invalid cookie and redirect back so a fresh token is set automatically.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@occult occult merged commit 9d82fa0 into occult:main Mar 18, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants