feat(auth): Add CSRF debug logging for login flow#107153
Merged
Conversation
Add debug logging to help diagnose CSRF token mismatches affecting staff/superusers on POST /auth/2fa/. Changes: - Log CSRF cookie hash and session key hash on all /auth/* requests - Enhanced csrf_failure logging with cookie hash, POST token hash, origin, user-agent, and path This allows comparing token hashes between form render (GET) and submit (POST) to verify the multi-tab cookie overwrite hypothesis. Only first 8 chars of SHA256 hashes are logged to avoid leaking sensitive token values.
dcramer
approved these changes
Jan 28, 2026
priscilawebdev
pushed a commit
that referenced
this pull request
Feb 2, 2026
## Summary Add debug logging to help diagnose CSRF token mismatches affecting staff/superusers on `POST /auth/2fa/` with reason "CSRF token from POST incorrect". ## Changes **`src/sentry/web/frontend/base.py`** - Added `csrf.auth_request` logging in `dispatch()` for all `/auth/*` paths - Logs: `path`, `method`, `csrf_cookie_hash`, `session_key_hash`, `user_id` **`src/sentry/web/frontend/csrf_failure.py`** - Enhanced `csrf_failure` logging with: - `csrf_cookie_hash` and `csrf_post_token_hash` (for comparison) - `path`, `origin`, `user_agent` - Preserved existing fields (`reason`, `referer`, `user_id`, etc.) ## How to Debug When a CSRF failure occurs, compare the logs: 1. **`csrf.auth_request`** (on GET /auth/2fa/) - Shows `csrf_cookie_hash` at form render time 2. **`csrf.auth_request`** (on POST /auth/2fa/) - Shows `csrf_cookie_hash` at submit time 3. **`csrf_failure`** - Shows both `csrf_cookie_hash` and `csrf_post_token_hash` | Scenario | Indicates | |----------|-----------| | `csrf_cookie_hash` differs between GET and POST | Cookie was overwritten (multi-tab hypothesis) | | Hashes match but `csrf_post_token_hash` differs from `csrf_cookie_hash` | Stale form token | ## Security Only first 8 chars of SHA256 hashes are logged to avoid leaking sensitive token values. ## Test Plan - [x] Ran `pytest tests/sentry/web/frontend/test_twofactor.py` - 6 passed - [x] Ran `pytest tests/sentry/web/frontend/test_auth_login.py` - 45 passed - [x] Pre-commit hooks pass
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add debug logging to help diagnose CSRF token mismatches affecting staff/superusers on
POST /auth/2fa/with reason "CSRF token from POST incorrect".Changes
src/sentry/web/frontend/base.pycsrf.auth_requestlogging indispatch()for all/auth/*pathspath,method,csrf_cookie_hash,session_key_hash,user_idsrc/sentry/web/frontend/csrf_failure.pycsrf_failurelogging with:csrf_cookie_hashandcsrf_post_token_hash(for comparison)path,origin,user_agentreason,referer,user_id, etc.)How to Debug
When a CSRF failure occurs, compare the logs:
csrf.auth_request(on GET /auth/2fa/) - Showscsrf_cookie_hashat form render timecsrf.auth_request(on POST /auth/2fa/) - Showscsrf_cookie_hashat submit timecsrf_failure- Shows bothcsrf_cookie_hashandcsrf_post_token_hashcsrf_cookie_hashdiffers between GET and POSTcsrf_post_token_hashdiffers fromcsrf_cookie_hashSecurity
Only first 8 chars of SHA256 hashes are logged to avoid leaking sensitive token values.
Test Plan
pytest tests/sentry/web/frontend/test_twofactor.py- 6 passedpytest tests/sentry/web/frontend/test_auth_login.py- 45 passed