Skip to content

Commit 2676401

Browse files
Address commit review.
1 parent 1307572 commit 2676401

3 files changed

Lines changed: 12 additions & 6 deletions

File tree

Lib/test/test_binascii.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,17 +240,17 @@ def assertNonBase64Data(data, expected, ignorechars):
240240

241241
def test_base64_excess_data(self):
242242
# Test excess data exceptions
243-
def assertExcessData(data, non_strict_expected):
243+
def assertExcessData(data, expected):
244244
assert_regex = r'(?i)Excess data'
245245
data = self.type2test(data)
246246
with self.assertRaisesRegex(binascii.Error, assert_regex):
247247
binascii.a2b_base64(data, strict_mode=True)
248248
self.assertEqual(binascii.a2b_base64(data, strict_mode=False),
249-
non_strict_expected)
249+
expected)
250250
self.assertEqual(binascii.a2b_base64(data, strict_mode=True,
251251
ignorechars=b'='),
252-
non_strict_expected)
253-
self.assertEqual(binascii.a2b_base64(data), non_strict_expected)
252+
expected)
253+
self.assertEqual(binascii.a2b_base64(data), expected)
254254

255255
assertExcessData(b'ab==c=', b'i\xb7')
256256
assertExcessData(b'ab==cd', b'i\xb7\x1d')
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Base64 decoder (see :func:`binascii.a2b_base64`, :func:`base64.b64decode`, etc) no
22
longer ignores excess data after the first padded quad in non-strict
3-
(default) mode. Instead, in conformance with :rfc:`4648`, it ignores
3+
(default) mode. Instead, in conformance with :rfc:`4648`, section 3.3, it now ignores
44
the pad character, "=", if it is present before the end of the encoded data.

Modules/binascii.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,10 @@ binascii_a2b_base64_impl(PyObject *module, Py_buffer *data, int strict_mode,
643643
if (quad_pos >= 2 && quad_pos + pads <= 4) {
644644
continue;
645645
}
646+
// See RFC 4648, section-3.3: "specifications MAY ignore the
647+
// pad character, "=", treating it as non-alphabet data, if
648+
// it is present before the end of the encoded data" and
649+
// "the excess pad characters MAY also be ignored."
646650
if (!strict_mode || ignorechar(BASE64_PAD, ignorechars, ignorecache)) {
647651
continue;
648652
}
@@ -662,6 +666,7 @@ binascii_a2b_base64_impl(PyObject *module, Py_buffer *data, int strict_mode,
662666

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

675-
// Characters that are not '=', in the middle of the padding, are not allowed
680+
// Characters that are not '=', in the middle of the padding, are
681+
// not allowed (except when they are). See RFC 4648, section-3.3.
676682
if (pads && strict_mode &&
677683
!ignorechar(BASE64_PAD, ignorechars, ignorecache))
678684
{

0 commit comments

Comments
 (0)