summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorIdan Moral <idan22moral@gmail.com>2021-07-19 00:45:19 (GMT)
committerGitHub <noreply@github.com>2021-07-19 00:45:19 (GMT)
commit35b98e38b6edd63153fc8e092f94cb20725dacc1 (patch)
tree0075d1bd3b9f195c5bd60553f9adcb5ef72a6fa1 /Lib/test
parentb494685b2548489efcc66993cc6c13b027ce3b26 (diff)
downloadcpython-35b98e38b6edd63153fc8e092f94cb20725dacc1.zip
cpython-35b98e38b6edd63153fc8e092f94cb20725dacc1.tar.gz
cpython-35b98e38b6edd63153fc8e092f94cb20725dacc1.tar.bz2
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]
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_binascii.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/Lib/test/test_binascii.py b/Lib/test/test_binascii.py
index 4d1bf2c..74438d8 100644
--- a/Lib/test/test_binascii.py
+++ b/Lib/test/test_binascii.py
@@ -114,6 +114,47 @@ class BinASCIITest(unittest.TestCase):
# empty strings. TBD: shouldn't it raise an exception instead ?
self.assertEqual(binascii.a2b_base64(self.type2test(fillers)), b'')
+ def test_base64_strict_mode(self):
+ # Test base64 with strict mode on
+ def _assertRegexTemplate(assert_regex: str, data: bytes, non_strict_mode_expected_result: bytes):
+ with self.assertRaisesRegex(binascii.Error, assert_regex):
+ binascii.a2b_base64(self.type2test(data), strict_mode=True)
+ self.assertEqual(binascii.a2b_base64(self.type2test(data), strict_mode=False),
+ non_strict_mode_expected_result)
+ self.assertEqual(binascii.a2b_base64(self.type2test(data)),
+ non_strict_mode_expected_result)
+
+ def assertExcessData(data, non_strict_mode_expected_result: bytes):
+ _assertRegexTemplate(r'(?i)Excess data', data, non_strict_mode_expected_result)
+
+ 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):
+ _assertRegexTemplate(r'(?i)Leading padding', data, non_strict_mode_expected_result)
+
+ # Test excess data exceptions
+ assertExcessData(b'ab==a', b'i')
+ assertExcessData(b'ab===', b'i')
+ assertExcessData(b'ab==:', b'i')
+ assertExcessData(b'abc=a', b'i\xb7')
+ assertExcessData(b'abc=:', b'i\xb7')
+ assertExcessData(b'ab==\n', b'i')
+
+ # Test non-base64 data exceptions
+ assertNonBase64Data(b'\nab==', b'i')
+ assertNonBase64Data(b'ab:(){:|:&};:==', b'i')
+ assertNonBase64Data(b'a\nb==', b'i')
+ 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')
+
+
def test_base64errors(self):
# Test base64 with invalid padding
def assertIncorrectPadding(data):