bpo-33972: Fix EmailMessage.iter_attachments raising AttributeError. (GH-14119)

* bpo-33972: Fix EmailMessage.iter_attachments raising AttributeError.

When certain malformed messages have content-type set to 'mulitpart/*' but
still have a single part body, iter_attachments can raise AttributeError. This
patch fixes it by returning a None value instead when the body is single part.
This commit is contained in:
Abhilash Raj 2019-06-25 10:03:19 -07:00 committed by Barry Warsaw
parent d7c87d982d
commit 02257012f6
3 changed files with 21 additions and 1 deletions

View File

@ -1041,7 +1041,16 @@ class MIMEPart(Message):
maintype, subtype = self.get_content_type().split('/') maintype, subtype = self.get_content_type().split('/')
if maintype != 'multipart' or subtype == 'alternative': if maintype != 'multipart' or subtype == 'alternative':
return return
parts = self.get_payload().copy() payload = self.get_payload()
# Certain malformed messages can have content type set to `multipart/*`
# but still have single part body, in which case payload.copy() can
# fail with AttributeError.
try:
parts = payload.copy()
except AttributeError:
# payload is not a list, it is most probably a string.
return
if maintype == 'multipart' and subtype == 'related': if maintype == 'multipart' and subtype == 'related':
# For related, we treat everything but the root as an attachment. # For related, we treat everything but the root as an attachment.
# The root may be indicated by 'start'; if there's no start or we # The root may be indicated by 'start'; if there's no start or we

View File

@ -929,6 +929,15 @@ class TestMIMEPart(TestEmailMessageBase, TestEmailBase):
m.set_content(content_manager=cm) m.set_content(content_manager=cm)
self.assertNotIn('MIME-Version', m) self.assertNotIn('MIME-Version', m)
def test_string_payload_with_multipart_content_type(self):
msg = message_from_string(textwrap.dedent("""\
Content-Type: multipart/mixed; charset="utf-8"
sample text
"""), policy=policy.default)
attachments = msg.iter_attachments()
self.assertEqual(list(attachments), [])
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View File

@ -0,0 +1,2 @@
Email with single part but content-type set to ``multipart/*`` doesn't raise
AttributeError anymore.