Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Lib/email/_header_value_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_email/test_headerregistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_email/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("""\
Expand Down
Original file line number Diff line number Diff line change
@@ -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``.
Loading