Skip to content

bpo-34736: improve error message for invalid length b64decode inputs #9563

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

Merged
merged 1 commit into from
Sep 28, 2018
Merged
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
6 changes: 5 additions & 1 deletion Lib/test/test_binascii.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import unittest
import binascii
import array
import re

# Note: "*_hex" functions are aliases for "(un)hexlify"
b2a_functions = ['b2a_base64', 'b2a_hex', 'b2a_hqx', 'b2a_qp', 'b2a_uu',
Expand Down Expand Up @@ -127,7 +128,10 @@ def assertIncorrectPadding(data):

# Test base64 with invalid number of valid characters (1 mod 4)
def assertInvalidLength(data):
with self.assertRaisesRegex(binascii.Error, r'(?i)invalid.+length'):
n_data_chars = len(re.sub(br'[^A-Za-z0-9/+]', br'', data))
expected_errmsg_re = \
r'(?i)Invalid.+number of data characters.+' + str(n_data_chars)
with self.assertRaisesRegex(binascii.Error, expected_errmsg_re):
binascii.a2b_base64(self.type2test(data))

assertInvalidLength(b'a')
Expand Down
10 changes: 7 additions & 3 deletions Modules/binascii.c
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ binascii_a2b_base64_impl(PyObject *module, Py_buffer *data)
{
const unsigned char *ascii_data;
unsigned char *bin_data;
unsigned char *bin_data_start;
int leftbits = 0;
unsigned char this_ch;
unsigned int leftchar = 0;
Expand All @@ -461,6 +462,7 @@ binascii_a2b_base64_impl(PyObject *module, Py_buffer *data)
bin_data = _PyBytesWriter_Alloc(&writer, bin_len);
if (bin_data == NULL)
return NULL;
bin_data_start = bin_data;

for( ; ascii_len > 0; ascii_len--, ascii_data++) {
this_ch = *ascii_data;
Expand Down Expand Up @@ -516,9 +518,11 @@ binascii_a2b_base64_impl(PyObject *module, Py_buffer *data)
** This is an invalid length, as there is no possible input that
** could encoded into such a base64 string.
*/
PyErr_SetString(Error,
"Invalid base64-encoded string: "
"length cannot be 1 more than a multiple of 4");
PyErr_Format(Error,
"Invalid base64-encoded string: "
"number of data characters (%d) cannot be 1 more "
"than a multiple of 4",
(bin_data - bin_data_start) / 3 * 4 + 1);
} else {
PyErr_SetString(Error, "Incorrect padding");
}
Expand Down