Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/itsdangerous/encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ def base64_encode(string: str | bytes) -> bytes:


def base64_decode(string: str | bytes) -> bytes:
"""Base64 decode a URL-safe string of bytes or text. The result is
bytes.
"""Base64 decode a URL-safe string of bytes or ASCII text. The
result is bytes. Non-ASCII text is invalid and will raise
:exc:`.BadData`.
"""
string = want_bytes(string, encoding="ascii", errors="ignore")
string += b"=" * (-len(string) % 4)

try:
string = want_bytes(string, encoding="ascii")
string += b"=" * (-len(string) % 4)
return base64.urlsafe_b64decode(string)
Comment on lines +34 to 36
except (TypeError, ValueError) as e:
except (TypeError, ValueError, UnicodeError) as e:
raise BadData("Invalid base64-encoded data") from e


Expand Down
5 changes: 5 additions & 0 deletions tests/test_itsdangerous/test_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ def test_base64_bad():
base64_decode("12345")


def test_base64_non_ascii():
with pytest.raises(BadData):
base64_decode("abc😀")


Comment on lines +33 to +34
@pytest.mark.parametrize(
("value", "expect"), ((0, b""), (192, b"\xc0"), (18446744073709551615, b"\xff" * 8))
)
Expand Down
Loading