bpo-43086: Add handling for out-of-spec data in a2b_base64 (GH-24402) · python/cpython@35b98e3 · GitHub
Skip to content

Commit 35b98e3

Browse files
authored
bpo-43086: Add handling for out-of-spec data in a2b_base64 (GH-24402)
binascii.a2b_base64 gains a strict_mode= parameter. When enabled it will raise an error on input that deviates from the base64 spec in any way. The default remains False for backward compatibility. Code reviews and minor tweaks by: Gregory P. Smith <greg@krypto.org> [Google]
1 parent b494685 commit 35b98e3

5 files changed

Lines changed: 132 additions & 15 deletions

File tree

Doc/library/binascii.rst

Lines changed: 13 additions & 1 deletion

Lib/test/test_binascii.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,47 @@ def addnoise(line):
114114
# empty strings. TBD: shouldn't it raise an exception instead ?
115115
self.assertEqual(binascii.a2b_base64(self.type2test(fillers)), b'')
116116

117+
def test_base64_strict_mode(self):
118+
# Test base64 with strict mode on
119+
def _assertRegexTemplate(assert_regex: str, data: bytes, non_strict_mode_expected_result: bytes):
120+
with self.assertRaisesRegex(binascii.Error, assert_regex):
121+
binascii.a2b_base64(self.type2test(data), strict_mode=True)
122+
self.assertEqual(binascii.a2b_base64(self.type2test(data), strict_mode=False),
123+
non_strict_mode_expected_result)
124+
self.assertEqual(binascii.a2b_base64(self.type2test(data)),
125+
non_strict_mode_expected_result)
126+
127+
def assertExcessData(data, non_strict_mode_expected_result: bytes):
128+
_assertRegexTemplate(r'(?i)Excess data', data, non_strict_mode_expected_result)
129+
130+
def assertNonBase64Data(data, non_strict_mode_expected_result: bytes):
131+
_assertRegexTemplate(r'(?i)Only base64 data', data, non_strict_mode_expected_result)
132+
133+
def assertMalformedPadding(data, non_strict_mode_expected_result: bytes):
134+
_assertRegexTemplate(r'(?i)Leading padding', data, non_strict_mode_expected_result)
135+
136+
# Test excess data exceptions
137+
assertExcessData(b'ab==a', b'i')
138+
assertExcessData(b'ab===', b'i')
139+
assertExcessData(b'ab==:', b'i')
140+
assertExcessData(b'abc=a', b'i\xb7')
141+
assertExcessData(b'abc=:', b'i\xb7')
142+
assertExcessData(b'ab==\n', b'i')
143+
144+
# Test non-base64 data exceptions
145+
assertNonBase64Data(b'\nab==', b'i')
146+
assertNonBase64Data(b'ab:(){:|:&};:==', b'i')
147+
assertNonBase64Data(b'a\nb==', b'i')
148+
assertNonBase64Data(b'a\x00b==', b'i')
149+
150+
# Test malformed padding
151+
assertMalformedPadding(b'=', b'')
152+
assertMalformedPadding(b'==', b'')
153+
assertMalformedPadding(b'===', b'')
154+
assertMalformedPadding(b'ab=c=', b'i\xb7')
155+
assertMalformedPadding(b'ab=ab==', b'i\xb6\x9b')
156+
157+
117158
def test_base64errors(self):
118159
# Test base64 with invalid padding
119160
def assertIncorrectPadding(data):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Added a new optional :code:`strict_mode` parameter to *binascii.a2b_base64*.
2+
When :code:`scrict_mode` is set to :code:`True`, the *a2b_base64* function will accept only valid base64 content.
3+
More details about what "valid base64 content" is, can be found in the function's documentation.

Modules/binascii.c

Lines changed: 46 additions & 6 deletions

Modules/clinic/binascii.c.h

Lines changed: 29 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)