8000 gh-118314: Fix padding edge case in binascii.a2b_base64 strict mode by zhangyoufu · Pull Request #118320 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-118314: Fix padding edge case in binascii.a2b_base64 strict mode #118320

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 6 commits into from
May 7, 2024
Next Next commit
gh-118314: Fix padding edge case in binascii.a2b_base64 strict mode
  • Loading branch information
zhangyoufu committed Apr 26, 2024
commit 84b8420a41b5816e7c5c4fd434bb4416bc5747c5
6 changes: 6 additions & 0 deletions Lib/test/test_binascii.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ def assertLeadingPadding(data, non_strict_mode_expected_result: bytes):
def assertDiscontinuousPadding(data, non_strict_mode_expected_result: bytes):
_assertRegexTemplate(r'(?i)Discontinuous padding', data, non_strict_mode_expected_result)

def assertIncorrectPadding(data, non_strict_mode_expected_result: bytes):
_assertRegexTemplate(r'(?i)Incorrect padding', data, non_strict_mode_expected_result)

# Test excess data exceptions
assertExcessData(b'ab==a', b'i')
assertExcessData(b'ab===', b'i')
Expand All @@ -159,6 +162,9 @@ def assertDiscontinuousPadding(data, non_strict_mode_expected_result: bytes):
assertLeadingPadding(b'===', b'')
assertDiscontinuousPadding(b'ab=c=', b'i\xb7')
assertDiscontinuousPadding(b'ab=ab==', b'i\xb6\x9b')
assertIncorrectPadding(b'AAAA=', b'\x00\x00\x00')
assertIncorrectPadding(b'AAAA==', b'\x00\x00\x00')
assertIncorrectPadding(b'AAAA===', b'\x00\x00\x00')


def test_base64errors(self):
Expand Down
7 changes: 7 additions & 0 deletions Modules/binascii.c
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,13 @@ binascii_a2b_base64_impl(PyObject *module, Py_buffer *data, int strict_mode)
if (this_ch == BASE64_PAD) {
padding_started = 1;

if (strict_mode && quad_pos == 0) {
state = get_binascii_state(module);
if (state) {
PyErr_SetString(state->Error, "Incorrect padding");
}
goto error_end;
}
if (quad_pos >= 2 && quad_pos + ++pads >= 4) {
/* A pad sequence means we should not parse more input.
** We've already interpreted the data from the quad at this point.
Expand Down
0