Commit Graph

13 Commits

Author SHA1 Message Date
bikeborb
a2a4cccb51 Update backend/handler/auth/middleware/csrf_middleware.py
Co-authored-by: Georges-Antoine Assi <3247106+gantoine@users.noreply.github.com>
2026-08-03 22:49:59 -04:00
Dakota Brown
b53c7cf98a fix(auth): accept a stale user-bound CSRF token on anonymous requests
Logging in failed once with a CSRF error and succeeded on retry.

`_csrf_tokens_match` validates the user id embedded in the token against
the current request's user. A browser holding a token bound to user N,
whose server-side session is gone (container restart, session expiry,
Redis flush), makes an anonymous login POST. N != None, so the request is
rejected. The rejection response then rotates the cookie, which is why the
second attempt works.

Keep the double-submit check and the cookie/header user agreement
unconditionally, and enforce the caller binding only when there is a
caller. An anonymous request cannot be forged this way regardless: an
attacker cannot read the cookie cross-origin, so it cannot produce the
matching header.

This is the mirror of #3420, which fixed the authenticated direction by
rotating the cookie on safe requests. That path is untouched here, and
test_authenticated_user_binding_still_enforced pins it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-03 12:41:12 -04:00
Georges-Antoine Assi
be476cb7dc Only set CSRF cookie on http.response.start
ASGI spec only allows headers on the http.response.start message;
appending Set-Cookie to body messages is out-of-spec and may break on
some servers. Early-return for non-start messages.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-24 15:46:50 -04:00
copilot-swe-agent[bot]
f94206aa53 Refresh CSRF cookie when auth user changes
Co-authored-by: gantoine <3247106+gantoine@users.noreply.github.com>
2026-05-24 14:46:31 +00:00
Georges-Antoine Assi
ac43b0aa94 changes from bot review 2025-11-22 11:22:45 -05:00
Georges-Antoine Assi
ec6bb24662 Add new redis-backed session middleware 2025-11-22 10:47:59 -05:00
Georges-Antoine Assi
4f6442a6ad catch typeerror in csrf token and return false 2025-11-18 16:56:10 -05:00
Georges-Antoine Assi
156d31b62a Fix CSRF failure on first admin signup 2025-11-18 14:04:47 -05:00
Georges-Antoine Assi
91ad9f7b7f fix trunk check issues 2025-11-18 10:12:58 -05:00
Georges-Antoine Assi
ee39fe1aba changes from bot review 2025-11-18 10:09:00 -05:00
Georges-Antoine Assi
d1824bf894 manually fix tests 2025-11-18 00:00:49 -05:00
Georges-Antoine Assi
6a1a344ba2 add tests for middlewares 2025-11-17 23:40:00 -05:00
Georges-Antoine Assi
551ff72a8a implement csrf middleware directly in repo 2025-11-17 21:12:29 -05:00