Fix silent login loop caused by strict cookie parsing
The admin UI accepted the password, issued a valid session cookie, then
bounced straight back to the login page with no error shown.
http.cookies.SimpleCookie.load() silently discards the remainder of a Cookie
header the moment it meets a value it considers illegal, rather than raising
or skipping just that entry. A neighbouring cookie with a JSON-ish value such
as prefs={"a":1} is enough. Everything after it — including our session —
becomes invisible, so a perfectly good login looked like a failed one.
curl never showed it because curl only sends the one cookie. A browser sends
every cookie on the domain, and tube.jihakuz.xyz previously served
TubeArchivist alongside several sibling services on jihakuz.xyz.
Replaced with a tolerant hand-rolled parser and covered it with regression
tests for nine hostile neighbour values.
Also log successful logins at INFO. Only failures were logged, which made
"password rejected" and "session did not stick" indistinguishable from the
journal and sent the diagnosis down the wrong path.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
18bb2e420b
commit
96f8d89442
@@ -2,6 +2,8 @@
|
||||
|
||||
import time
|
||||
|
||||
import pytest
|
||||
|
||||
from youtube_automate.web import auth
|
||||
|
||||
|
||||
@@ -91,6 +93,61 @@ class TestCookie:
|
||||
assert "Max-Age=0" in auth.clear_cookie_header()
|
||||
|
||||
|
||||
class TestCookieParsing:
|
||||
"""Regression tests for a real failure: http.cookies.SimpleCookie silently
|
||||
drops everything after a value it dislikes, which made valid sessions
|
||||
invisible and bounced users back to the login page with no error."""
|
||||
|
||||
def test_finds_our_cookie_alone(self):
|
||||
assert auth.cookie_value("yta_session=abc") == "abc"
|
||||
|
||||
def test_finds_it_after_a_neighbour(self):
|
||||
assert auth.cookie_value("sessionid=xyz; yta_session=abc") == "abc"
|
||||
|
||||
def test_finds_it_before_a_neighbour(self):
|
||||
assert auth.cookie_value("yta_session=abc; sessionid=xyz") == "abc"
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"neighbour",
|
||||
[
|
||||
'prefs={"a":1}', # JSON value — what actually broke it
|
||||
"junk=[1,2,3]",
|
||||
"weird=a b c",
|
||||
"empty=",
|
||||
"novalue",
|
||||
"quoted=\"has spaces\"",
|
||||
"path=/a/b/c",
|
||||
"colons=a:b:c",
|
||||
"comma=a,b",
|
||||
],
|
||||
)
|
||||
def test_survives_hostile_neighbours(self, neighbour):
|
||||
assert auth.cookie_value(f"{neighbour}; yta_session=abc") == "abc"
|
||||
assert auth.cookie_value(f"yta_session=abc; {neighbour}") == "abc"
|
||||
|
||||
def test_strips_surrounding_quotes(self):
|
||||
assert auth.cookie_value('yta_session="abc"') == "abc"
|
||||
|
||||
def test_tolerates_whitespace(self):
|
||||
assert auth.cookie_value(" yta_session = abc ") == "abc"
|
||||
|
||||
def test_absent_cookie_returns_empty(self):
|
||||
assert auth.cookie_value("sessionid=xyz") == ""
|
||||
|
||||
def test_empty_header_returns_empty(self):
|
||||
assert auth.cookie_value("") == ""
|
||||
assert auth.cookie_value(None) == ""
|
||||
|
||||
def test_does_not_match_a_name_that_merely_contains_ours(self):
|
||||
assert auth.cookie_value("not_yta_session=nope") == ""
|
||||
|
||||
def test_real_token_round_trips_through_the_header(self):
|
||||
secret = auth.new_secret()
|
||||
token = auth.issue_session(secret)
|
||||
header = f'prefs={{"theme":"dark"}}; yta_session={token}; other=1'
|
||||
assert auth.verify_session(secret, auth.cookie_value(header))
|
||||
|
||||
|
||||
class TestCsrf:
|
||||
def test_token_verifies(self):
|
||||
secret, session = auth.new_secret(), auth.issue_session(auth.new_secret())
|
||||
|
||||
Reference in New Issue
Block a user