diff --git a/Lib/email/_header_value_parser.py b/Lib/email/_header_value_parser.py index f396f4f9b32a9a..ff0c40fdf7344f 100644 --- a/Lib/email/_header_value_parser.py +++ b/Lib/email/_header_value_parser.py @@ -814,6 +814,11 @@ def params(self): import codecs codecs.lookup(charset) value = value.decode(charset, 'surrogateescape') + except UnicodeDecodeError: + # Some codecs reject incomplete sequences of ASCII bytes, + # which surrogateescape cannot handle. + value = value.decode(charset, 'replace') + param.defects.append(errors.UndecodableBytesDefect()) except (LookupError, UnicodeEncodeError): # XXX: there should really be a custom defect for # unknown character set to make it easy to find, diff --git a/Lib/test/test_email/test_headerregistry.py b/Lib/test/test_email/test_headerregistry.py index c118fd5e20d048..442cad8c6c3bcd 100644 --- a/Lib/test/test_email/test_headerregistry.py +++ b/Lib/test/test_email/test_headerregistry.py @@ -865,6 +865,18 @@ def cte_as_value(self, @parameterize class TestContentDisposition(TestHeaderBase): + def test_rfc2231_undecodable_parameter(self): + for charset, value in [('iso-2022-jp', '%1B%24%42%3A'), + ('utf-16', '%00')]: + with self.subTest(charset=charset): + header = self.make_header( + 'Content-Disposition', + f"attachment; filename*={charset}''{value}") + self.assertEqual(header.content_disposition, 'attachment') + self.assertEqual(header.params['filename'], '\uFFFD') + self.assertDefectsEqual( + header.defects, [errors.UndecodableBytesDefect]) + def content_disp_as_value(self, source, content_disposition, diff --git a/Lib/test/test_email/test_message.py b/Lib/test/test_email/test_message.py index 974b0ceaafbde4..d23621e1dcf60b 100644 --- a/Lib/test/test_email/test_message.py +++ b/Lib/test/test_email/test_message.py @@ -1054,6 +1054,25 @@ def do_test_invalid_header_names(self, policy, setter, name, value): setter(message, name, value) self.assertIn(f"{name!r}", str(cm.exception)) + def test_get_body_undecodable_attachment_filename(self): + msg = message_from_bytes( + b'Content-Type: multipart/mixed; boundary=BOUNDARY\r\n' + b'\r\n' + b'--BOUNDARY\r\n' + b'Content-Type: application/octet-stream\r\n' + b"Content-Disposition: attachment; filename*=iso-2022-jp''%1B%24%42%3A\r\n" + b'\r\n' + b'attachment data\r\n' + b'--BOUNDARY\r\n' + b'Content-Type: text/plain\r\n' + b'\r\n' + b'message body\r\n' + b'--BOUNDARY--\r\n', policy=policy.default) + self.assertEqual(msg.get_body().get_content(), 'message body') + attachment, = msg.iter_attachments() + self.assertEqual(attachment.get_payload(decode=True), b'attachment data') + self.assertEqual(attachment.get_filename(), '\uFFFD') + def test_get_body_malformed(self): """test for bpo-42892""" msg = textwrap.dedent("""\ diff --git a/Misc/NEWS.d/next/Library/2026-11-20-19-19-30.gh-issue-132794.MimeDe.rst b/Misc/NEWS.d/next/Library/2026-11-20-19-19-30.gh-issue-132794.MimeDe.rst new file mode 100644 index 00000000000000..d8d7f2a4a98f8b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-20-19-19-30.gh-issue-132794.MimeDe.rst @@ -0,0 +1,4 @@ +Fix :mod:`email` header parsing to record an +``email.errors.UndecodableBytesDefect`` instead of raising +:exc:`UnicodeDecodeError` when an RFC 2231 parameter cannot be decoded with +``surrogateescape``.