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

bpo-34164: Fix handling of incorrect padding in base64.b32decode(). #8351

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 4 additions & 12 deletions Lib/base64.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,23 +231,15 @@ def b32decode(s, casefold=False, map01=None):
raise binascii.Error('Non-base32 digit found') from None
decoded += acc.to_bytes(5, 'big')
# Process the last, partial quanta
if padchars:
if l % 8 or padchars not in {0, 1, 3, 4, 6}:
raise binascii.Error('Incorrect padding')
if padchars and decoded:
acc <<= 5 * padchars
last = acc.to_bytes(5, 'big')
if padchars == 1:
decoded[-5:] = last[:-1]
elif padchars == 3:
decoded[-5:] = last[:-2]
elif padchars == 4:
decoded[-5:] = last[:-3]
elif padchars == 6:
decoded[-5:] = last[:-4]
else:
raise binascii.Error('Incorrect padding')
decoded[-5:] = last[:-((padchars * 5 + 4) // 8)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to put the complex operation a a separated line. Example:

n = ((padchars * 5 + 4) // 8)
decoded[-5:] = last[:-n]

Oh, GitHub lost my comment. I had to write it again.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leftover = (43 - 5 * padchars) // 8  # 1: 4, 3: 3, 4: 2, 6: 1

or

leftover = (5, 4, 4, 3, 2, 2, 1, 1, 0)[padchars]

or

leftover = (..., 4, ..., 3, 2, ..., 1)[padchars]

What is better?

return bytes(decoded)



# RFC 3548, Base 16 Alphabet specifies uppercase, but hexlify() returns
# lowercase. The RFC also recommends against accepting input case
# insensitively.
Expand Down
19 changes: 14 additions & 5 deletions Lib/test/test_base64.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,11 +343,20 @@ def test_b32decode_casefold(self):
self.assertRaises(binascii.Error, base64.b32decode, data_str)

def test_b32decode_error(self):
for data in [b'abc', b'ABCDEF==', b'==ABCDEF']:
with self.assertRaises(binascii.Error):
base64.b32decode(data)
with self.assertRaises(binascii.Error):
base64.b32decode(data.decode('ascii'))
tests = [b'abc', b'ABCDEF==', b'==ABCDEF']
prefixes = [b'M', b'ME', b'MFRA', b'MFRGG', b'MFRGGZA', b'MFRGGZDF']
for i in range(0, 17):
if i:
tests.append(b'='*i)
for prefix in prefixes:
if len(prefix) + i != 8:
tests.append(prefix + b'='*i)
for data in tests:
with self.subTest(data=data):
with self.assertRaises(binascii.Error):
base64.b32decode(data)
with self.assertRaises(binascii.Error):
base64.b32decode(data.decode('ascii'))

def test_b16encode(self):
eq = self.assertEqual
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:func:`base64.b32decode` could raise UnboundLocalError or OverflowError for
incorrect padding. Now it always raises :exc:`base64.Error` in these cases.