Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace all base64 decoding errors with BadData. #27

Closed
wants to merge 3 commits into from
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
17 changes: 16 additions & 1 deletion itsdangerous.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
:license: BSD, see LICENSE for more details.
"""

import re
import sys
import hmac
import zlib
Expand All @@ -26,12 +27,15 @@
text_type = unicode
int_to_byte = chr
number_types = (int, long, float)
import string
_maketrans = string.maketrans
else:
from functools import reduce
izip = zip
text_type = str
int_to_byte = operator.methodcaller('to_bytes', 1, 'big')
number_types = (int, float)
_maketrans = bytes.maketrans


try:
Expand All @@ -57,6 +61,10 @@ def dumps(self, obj):
# 2011/01/01 in UTC
EPOCH = 1293840000

_urlsafe_decode_translation = _maketrans(b'-_', b'+/')
_b64_strip_re = re.compile(b'[^\+\/0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
b'abcdefghijklmnopqrstuvwxyz]')


def want_bytes(s, encoding='utf-8', errors='strict'):
if isinstance(s, text_type):
Expand Down Expand Up @@ -213,7 +221,14 @@ def base64_decode(string):
The result is also a bytestring.
"""
string = want_bytes(string, encoding='ascii', errors='ignore')
return base64.urlsafe_b64decode(string + b'=' * (-len(string) % 4))
string = string.translate(_urlsafe_decode_translation)
string = _b64_strip_re.sub(b'', string)
string = string + b'=' * (-len(string) % 4)

try:
return base64.standard_b64decode(string)
except (TypeError, ValueError):
raise BadData('Invalid base64-encoded data')


def int_to_bytes(num):
Expand Down
10 changes: 9 additions & 1 deletion tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from datetime import datetime

import itsdangerous as idmod
from itsdangerous import want_bytes, text_type, PY2
from itsdangerous import want_bytes, text_type, PY2, base64_decode


# Helper function for some unsafe string manipulation on encoded
Expand All @@ -26,6 +26,14 @@ def test_want_bytes(self):
self.assertEqual(want_bytes(b"foobar"), b"foobar")
self.assertEqual(want_bytes(u"foobar"), b"foobar")

def test_base64_decode(self):
self.assertRaises(idmod.BadData, base64_decode, b'A')

self.assertEqual(base64_decode(b'AA'), b'\x00')
self.assertEqual(base64_decode(b'AA=='), b'\x00')
self.assertEqual(base64_decode(b'AA\\\\'), b'\x00')
self.assertEqual(base64_decode(b'A!A'), b'\x00')


class SerializerTestCase(unittest.TestCase):
serializer_class = idmod.Serializer
Expand Down