fix(auth): avoid SameSite CookieError on Python < 3.8 (login 500)#32
Merged
Conversation
On Python 3.6/3.7 the XSRF cookie's 'samesite' attribute raises
http.cookies.CookieError ("Invalid attribute 'samesite'") — samesite was only
added to http.cookies.Morsel in Python 3.8 — which 500s the login page.
Only include samesite='Lax' in xsrf_cookie_kwargs when the interpreter supports
it (detected via http.cookies.Morsel._reserved). On older Python the cookie is
set without SameSite (the double-submit XSRF token protection still applies);
on 3.8+ behaviour is unchanged. Extracted as build_xsrf_cookie_kwargs() with
unit tests for both branches.
Note: the project's documented minimum is Python 3.9+, but this avoids a hard
500 on older interpreters.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Symptom
On the user's server the login page fails with:
Root cause
The XSRF cookie is configured with
samesite: 'Lax'(inxsrf_cookie_kwargs). Thesamesiteattribute was only added tohttp.cookies.Morselin Python 3.8. On Python 3.6/3.7, Tornado'sset_cookie→morsel['samesite']raisesCookieError, which 500s every response that sets the XSRF cookie — including login.(The server in question runs Python 3.6; the project's documented minimum is 3.9+, but a hard 500 is a poor failure mode.)
Fix
Only include
samesite='Lax'when the interpreter supports it, detected viahttp.cookies.Morsel._reserved. On older Python the XSRF cookie is set without SameSite — the double-submit XSRF token protection still applies, so CSRF protection is preserved; the SameSite defense-in-depth is simply dropped where the runtime can't express it. On 3.8+ behaviour is unchanged.Extracted as
build_xsrf_cookie_kwargs(cookie_secure, samesite_supported=None)so both branches are unit-tested.Testing
pytest src/tests/— 1748 passed (4 new tests covering supported/unsupported/securepass-through/auto-detection).CookieError; SameSite still applied on 3.8+ (no regression).🤖 Generated with Claude Code