diff options
author | Tal Einat <taleinat+github@gmail.com> | 2018-06-12 15:46:22 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-06-12 15:46:22 +0300 |
commit | c3f55be7dd012b7e92901627d0b31c21e983ccb4 (patch) | |
tree | 86fb2e7e33615aee453d73f27063a4ec345e89fa /Lib/test | |
parent | 5a9820918077a65db90f24733edc8935c3e2130e (diff) | |
download | cpython-git-c3f55be7dd012b7e92901627d0b31c21e983ccb4.tar.gz |
bpo-27397: Make email module properly handle invalid-length base64 strings (#7583)
When attempting to base64-decode a payload of invalid length (1 mod 4),
properly recognize and handle it. The given data will be returned as-is,
i.e. not decoded, along with a new defect, InvalidBase64LengthDefect.
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_email/test__encoded_words.py | 6 | ||||
-rw-r--r-- | Lib/test/test_email/test__header_value_parser.py | 9 | ||||
-rw-r--r-- | Lib/test/test_email/test_defect_handling.py | 17 |
3 files changed, 32 insertions, 0 deletions
diff --git a/Lib/test/test_email/test__encoded_words.py b/Lib/test/test_email/test__encoded_words.py index 900e1d0e64..5a59aebba8 100644 --- a/Lib/test/test_email/test__encoded_words.py +++ b/Lib/test/test_email/test__encoded_words.py @@ -33,7 +33,10 @@ class TestDecodeB(TestEmailBase): self._test(b'Zm9v', b'foo') def test_missing_padding(self): + # 1 missing padding character self._test(b'dmk', b'vi', [errors.InvalidBase64PaddingDefect]) + # 2 missing padding characters + self._test(b'dg', b'v', [errors.InvalidBase64PaddingDefect]) def test_invalid_character(self): self._test(b'dm\x01k===', b'vi', [errors.InvalidBase64CharactersDefect]) @@ -42,6 +45,9 @@ class TestDecodeB(TestEmailBase): self._test(b'dm\x01k', b'vi', [errors.InvalidBase64CharactersDefect, errors.InvalidBase64PaddingDefect]) + def test_invalid_length(self): + self._test(b'abcde', b'abcde', [errors.InvalidBase64LengthDefect]) + class TestDecode(TestEmailBase): diff --git a/Lib/test/test_email/test__header_value_parser.py b/Lib/test/test_email/test__header_value_parser.py index 5cdc4bceca..5036de2ca0 100644 --- a/Lib/test/test_email/test__header_value_parser.py +++ b/Lib/test/test_email/test__header_value_parser.py @@ -347,6 +347,15 @@ class TestParser(TestParserMixin, TestEmailBase): errors.InvalidBase64PaddingDefect], '') + def test_get_unstructured_invalid_base64_length(self): + # bpo-27397: Return the encoded string since there's no way to decode. + self._test_get_x(self._get_unst, + '=?utf-8?b?abcde?=', + 'abcde', + 'abcde', + [errors.InvalidBase64LengthDefect], + '') + def test_get_unstructured_no_whitespace_between_ews(self): self._test_get_x(self._get_unst, '=?utf-8?q?foo?==?utf-8?q?bar?=', diff --git a/Lib/test/test_email/test_defect_handling.py b/Lib/test/test_email/test_defect_handling.py index f36b907573..781f657418 100644 --- a/Lib/test/test_email/test_defect_handling.py +++ b/Lib/test/test_email/test_defect_handling.py @@ -254,6 +254,23 @@ class TestDefectsBase: self.assertDefectsEqual(self.get_defects(msg), [errors.InvalidBase64CharactersDefect]) + def test_invalid_length_of_base64_payload(self): + source = textwrap.dedent("""\ + Subject: test + MIME-Version: 1.0 + Content-Type: text/plain; charset="utf-8" + Content-Transfer-Encoding: base64 + + abcde + """) + msg = self._str_msg(source) + with self._raise_point(errors.InvalidBase64LengthDefect): + payload = msg.get_payload(decode=True) + if self.raise_expected: return + self.assertEqual(payload, b'abcde') + self.assertDefectsEqual(self.get_defects(msg), + [errors.InvalidBase64LengthDefect]) + def test_missing_ending_boundary(self): source = textwrap.dedent("""\ To: 1@harrydomain4.com |