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-44678: Separate error message for discontinuous padding in binascii.a2b_base64 strict mode #27249

Merged
merged 5 commits into from
Jul 19, 2021
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
15 changes: 9 additions & 6 deletions Lib/test/test_binascii.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,12 @@ def assertExcessData(data, non_strict_mode_expected_result: bytes):
def assertNonBase64Data(data, non_strict_mode_expected_result: bytes):
_assertRegexTemplate(r'(?i)Only base64 data', data, non_strict_mode_expected_result)

def assertMalformedPadding(data, non_strict_mode_expected_result: bytes):
def assertLeadingPadding(data, non_strict_mode_expected_result: bytes):
_assertRegexTemplate(r'(?i)Leading padding', data, non_strict_mode_expected_result)

def assertDiscontinuousPadding(data, non_strict_mode_expected_result: bytes):
_assertRegexTemplate(r'(?i)Discontinuous padding', data, non_strict_mode_expected_result)

# Test excess data exceptions
assertExcessData(b'ab==a', b'i')
assertExcessData(b'ab===', b'i')
Expand All @@ -148,11 +151,11 @@ def assertMalformedPadding(data, non_strict_mode_expected_result: bytes):
assertNonBase64Data(b'a\x00b==', b'i')

# Test malformed padding
assertMalformedPadding(b'=', b'')
assertMalformedPadding(b'==', b'')
assertMalformedPadding(b'===', b'')
assertMalformedPadding(b'ab=c=', b'i\xb7')
assertMalformedPadding(b'ab=ab==', b'i\xb6\x9b')
assertLeadingPadding(b'=', b'')
assertLeadingPadding(b'==', b'')
assertLeadingPadding(b'===', b'')
assertDiscontinuousPadding(b'ab=c=', b'i\xb7')
assertDiscontinuousPadding(b'ab=ab==', b'i\xb6\x9b')


def test_base64errors(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added a separate error message for discontinuous padding in *binascii.a2b_base64* strict mode.
7 changes: 5 additions & 2 deletions Modules/binascii.c
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,6 @@ binascii_a2b_base64_impl(PyObject *module, Py_buffer *data, int strict_mode)
unsigned char *bin_data_start = bin_data;

if (strict_mode && ascii_len > 0 && ascii_data[0] == '=') {
malformed_padding:
state = get_binascii_state(module);
if (state) {
PyErr_SetString(state->Error, "Leading padding not allowed");
Expand Down Expand Up @@ -516,7 +515,11 @@ binascii_a2b_base64_impl(PyObject *module, Py_buffer *data, int strict_mode)

// Characters that are not '=', in the middle of the padding, are not allowed
if (strict_mode && padding_started) {
goto malformed_padding;
state = get_binascii_state(module);
if (state) {
PyErr_SetString(state->Error, "Discontinuous padding not allowed");
}
goto error_end;
}
pads = 0;

Expand Down