Skip to content
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
Prev Previous commit
Next Next commit
Address commit review.
  • Loading branch information
serhiy-storchaka committed Feb 28, 2026
commit 2676401771de27d370b81fc249cbd1f472f8c51c
8 changes: 4 additions & 4 deletions Lib/test/test_binascii.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,17 +240,17 @@ def assertNonBase64Data(data, expected, ignorechars):

def test_base64_excess_data(self):
# Test excess data exceptions
def assertExcessData(data, non_strict_expected):
def assertExcessData(data, expected):
assert_regex = r'(?i)Excess data'
data = self.type2test(data)
with self.assertRaisesRegex(binascii.Error, assert_regex):
binascii.a2b_base64(data, strict_mode=True)
self.assertEqual(binascii.a2b_base64(data, strict_mode=False),
non_strict_expected)
expected)
self.assertEqual(binascii.a2b_base64(data, strict_mode=True,
ignorechars=b'='),
non_strict_expected)
self.assertEqual(binascii.a2b_base64(data), non_strict_expected)
expected)
self.assertEqual(binascii.a2b_base64(data), expected)

assertExcessData(b'ab==c=', b'i\xb7')
assertExcessData(b'ab==cd', b'i\xb7\x1d')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Base64 decoder (see :func:`binascii.a2b_base64`, :func:`base64.b64decode`, etc) no
longer ignores excess data after the first padded quad in non-strict
(default) mode. Instead, in conformance with :rfc:`4648`, it ignores
(default) mode. Instead, in conformance with :rfc:`4648`, section 3.3, it now ignores
the pad character, "=", if it is present before the end of the encoded data.
8 changes: 7 additions & 1 deletion Modules/binascii.c
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,10 @@ binascii_a2b_base64_impl(PyObject *module, Py_buffer *data, int strict_mode,
if (quad_pos >= 2 && quad_pos + pads <= 4) {
continue;
}
// See RFC 4648, section-3.3: "specifications MAY ignore the
// pad character, "=", treating it as non-alphabet data, if
// it is present before the end of the encoded data" and
// "the excess pad characters MAY also be ignored."
if (!strict_mode || ignorechar(BASE64_PAD, ignorechars, ignorecache)) {
Comment thread
gpshead marked this conversation as resolved.
continue;
}
Expand All @@ -662,6 +666,7 @@ binascii_a2b_base64_impl(PyObject *module, Py_buffer *data, int strict_mode,

unsigned char v = table_a2b_base64[this_ch];
if (v >= 64) {
// See RFC 4648, section-3.3.
if (strict_mode && !ignorechar(this_ch, ignorechars, ignorecache)) {
state = get_binascii_state(module);
if (state) {
Expand All @@ -672,7 +677,8 @@ binascii_a2b_base64_impl(PyObject *module, Py_buffer *data, int strict_mode,
continue;
}

// Characters that are not '=', in the middle of the padding, are not allowed
// Characters that are not '=', in the middle of the padding, are
// not allowed (except when they are). See RFC 4648, section-3.3.
if (pads && strict_mode &&
!ignorechar(BASE64_PAD, ignorechars, ignorecache))
{
Expand Down
Loading