diff --git a/Lib/email/header.py b/Lib/email/header.py index 4ab0032bc66123..f186d193577908 100644 --- a/Lib/email/header.py +++ b/Lib/email/header.py @@ -136,7 +136,14 @@ def decode_header(header): last_word = last_charset = None for word, charset in decoded_words: if isinstance(word, str): - word = bytes(word, 'raw-unicode-escape') + word_tmp = bytes(word, 'raw-unicode-escape') + input_charset = charset or 'us-ascii' + try: + # Test to avoid UnicodeDecodeError in Header.append() + _ = word_tmp.decode(input_charset, errors='strict') + word = word_tmp + except UnicodeDecodeError: + word, charset = word.encode('utf-8'), 'utf-8' if last_word is None: last_word = word last_charset = charset diff --git a/Lib/test/test_email/test_email.py b/Lib/test/test_email/test_email.py index c29cc56203b1f7..b711887c3615a6 100644 --- a/Lib/test/test_email/test_email.py +++ b/Lib/test/test_email/test_email.py @@ -2396,6 +2396,14 @@ def test_multiline_header(self): self.assertEqual(str(make_header(decode_header(s))), '"Müller T" ') + def test_unicode_decode_error(self): + s = 'Hostel,=?UTF-8?B?UGFuYW3DoSBDaXR5?=, Panamá' + self.assertEqual(decode_header(s), + [(b'Hostel,', None), + (b'Panam\xc3\xa1 City, Panam\xc3\xa1', 'utf-8')]) + self.assertEqual(str(make_header(decode_header(s))), + 'Hostel, Panamá City, Panamá') + # Test the MIMEMessage class class TestMIMEMessage(TestEmailBase): diff --git a/Misc/NEWS.d/next/Library/2019-07-11-02-50-17.bpo-37532.efSn_v.rst b/Misc/NEWS.d/next/Library/2019-07-11-02-50-17.bpo-37532.efSn_v.rst new file mode 100644 index 00000000000000..3da640ef513d78 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-07-11-02-50-17.bpo-37532.efSn_v.rst @@ -0,0 +1 @@ +Avoid UnicodeDecodeError in email.header.make_header(). Patch by Aldwin Pollefeyt. \ No newline at end of file