Skip to content

Handle CSRF token failures gracefully on login page#8331

Merged
iHiD merged 2 commits intomainfrom
fix-csrf-login-error
Feb 8, 2026
Merged

Handle CSRF token failures gracefully on login page#8331
iHiD merged 2 commits intomainfrom
fix-csrf-login-error

Conversation

@iHiD
Copy link
Member

@iHiD iHiD commented Feb 7, 2026

Summary

  • Rescue ActionController::InvalidAuthenticityToken in Auth::SessionsController#create and redirect back to the login page with a flash message instead of showing a 422 error
  • Handles common cases: stale sessions, browser back button, multiple tabs, bot traffic
  • Follows the identical pattern already used for BCrypt::Errors::InvalidHash in the same method

Test plan

  • Verify the new test passes: bundle exec rails test test/controllers/auth/sessions_controller_test.rb
  • Confirm login still works normally with valid CSRF tokens
  • Confirm that submitting a stale login form now redirects to login with "Your login session expired. Please try again." flash

🤖 Generated with Claude Code

iHiD and others added 2 commits February 7, 2026 17:39
Rescue ActionController::InvalidAuthenticityToken in the sessions
controller and redirect back to the login page with a friendly flash
message instead of showing a 422 error. This handles common cases like
stale sessions, browser back button, and multiple tabs.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The inline rescue in the create method body never triggers because CSRF
verification runs as a before_action. Use rescue_from to properly catch
the exception. Fix test to enable real forgery protection instead of
stubbing a private method (blocked by Mocha config).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@iHiD iHiD merged commit 3a2c9c3 into main Feb 8, 2026
40 of 41 checks passed
@iHiD iHiD deleted the fix-csrf-login-error branch February 8, 2026 11:05
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.

1 participant