Skip to content

fix(auth): avoid SameSite CookieError on Python < 3.8 (login 500)#32

Merged
knep merged 1 commit into
masterfrom
fix/samesite-py36
Jun 25, 2026
Merged

fix(auth): avoid SameSite CookieError on Python < 3.8 (login 500)#32
knep merged 1 commit into
masterfrom
fix/samesite-py36

Conversation

@knep

@knep knep commented Jun 25, 2026

Copy link
Copy Markdown
Owner

Symptom

On the user's server the login page fails with:

http.cookies.CookieError: Invalid attribute 'samesite'

Root cause

The XSRF cookie is configured with samesite: 'Lax' (in xsrf_cookie_kwargs). The samesite attribute was only added to http.cookies.Morsel in Python 3.8. On Python 3.6/3.7, Tornado's set_cookiemorsel['samesite'] raises CookieError, 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 via http.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/secure pass-through/auto-detection).
  • Local htpasswd server (Python 3.14): login.html → 200, no CookieError; SameSite still applied on 3.8+ (no regression).

🤖 Generated with Claude Code

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-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@knep knep merged commit 5cbd1a0 into master Jun 25, 2026
8 checks passed
@knep knep deleted the fix/samesite-py36 branch June 25, 2026 14:31
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