Add explicit binascii.Error catch and bad-padding test

binascii.Error is already a subclass of ValueError, but listing it
explicitly makes the intent clear. Added test for incorrect base64
padding (e.g. "Basic abc").
This commit is contained in:
J. Nick Koston
2026-02-08 07:18:29 -06:00
parent 806a86a6ad
commit 82d9616f1b
2 changed files with 9 additions and 1 deletions
+1 -1
View File
@@ -123,7 +123,7 @@ def is_authenticated(handler: BaseHandler) -> bool:
try:
auth_decoded = base64.b64decode(auth_header[6:]).decode()
username, password = auth_decoded.split(":", 1)
except (ValueError, UnicodeDecodeError):
except (binascii.Error, ValueError, UnicodeDecodeError):
return False
return settings.check_password(username, password)
return handler.get_secure_cookie(AUTH_COOKIE_NAME) == COOKIE_AUTHENTICATED_YES
+8
View File
@@ -1707,6 +1707,14 @@ def test_is_authenticated_malformed_base64(
assert web_server.is_authenticated(handler) is False
def test_is_authenticated_bad_base64_padding(
mock_auth_settings: MagicMock,
) -> None:
"""Test that incorrect base64 padding (binascii.Error) returns False."""
handler = _make_auth_handler("Basic abc")
assert web_server.is_authenticated(handler) is False
def test_is_authenticated_invalid_utf8(
mock_auth_settings: MagicMock,
) -> None: