Skip to content

Commit

Permalink
Fix handling of invalid base64 values (#2718)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidism authored Jun 2, 2023
2 parents d5b454a + c52fa37 commit 4afaf51
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Version 2.3.5
Unreleased

- Python 3.12 compatibility. :issue:`2704`
- Fix handling of invalid base64 values in ``Authorization.from_header``. :issue:`2717`


Version 2.3.4
Expand Down
3 changes: 2 additions & 1 deletion src/werkzeug/datastructures/auth.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import base64
import binascii
import typing as t
import warnings
from functools import wraps
Expand Down Expand Up @@ -107,7 +108,7 @@ def from_header(cls, value: str | None) -> te.Self | None:
if scheme == "basic":
try:
username, _, password = base64.b64decode(rest).decode().partition(":")
except UnicodeError:
except (binascii.Error, UnicodeError):
return None

return cls(scheme, {"username": username, "password": password})
Expand Down
3 changes: 3 additions & 0 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ def test_authorization_token_padding(self):
assert a.type == "token"
assert a.token == token

def test_authorization_basic_incorrect_padding(self):
assert Authorization.from_header("Basic foo") is None

def test_bad_authorization_header_encoding(self):
"""If the base64 encoded bytes can't be decoded as UTF-8"""
content = base64.b64encode(b"\xffser:pass").decode()
Expand Down

0 comments on commit 4afaf51

Please sign in to comment.