Skip to content

Commit d088e17

Browse files
committed
gh-159304: Fix AttributeError in email get_body() for nested malformed multipart
MIMEPart._find_body() checked self.is_multipart() instead of part.is_multipart() when recursing.
1 parent 50082b8 commit d088e17

3 files changed

Lines changed: 22 additions & 1 deletion

File tree

‎Lib/email/message.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,7 @@ def _find_body(self, part, preferencelist):
10261026
if subtype in preferencelist:
10271027
yield (preferencelist.index(subtype), part)
10281028
return
1029-
if maintype != 'multipart' or not self.is_multipart():
1029+
if maintype != 'multipart' or not part.is_multipart():
10301030
return
10311031
if subtype != 'related':
10321032
for subpart in part.iter_parts():

‎Lib/test/test_email/test_message.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,25 @@ def test_get_body_malformed(self):
10811081
# AttributeError: 'str' object has no attribute 'is_attachment'
10821082
m.get_body()
10831083

1084+
def test_get_body_malformed_nested_multipart(self):
1085+
# gh-159304: a nested multipart part without a boundary has a
1086+
# string payload, which get_body() must not iterate over.
1087+
msg = textwrap.dedent("""\
1088+
From: sender@example.com
1089+
To: receiver@example.com
1090+
Subject: Malformed
1091+
Content-Type: multipart/related; boundary=OUTER
1092+
1093+
--OUTER
1094+
Content-Type: multipart/related
1095+
1096+
raw body no boundary
1097+
--OUTER--
1098+
""")
1099+
m = self._str_msg(msg)
1100+
self.assertIsNone(m.get_body(('plain', 'html')))
1101+
self.assertIs(m.get_body(('related',)), m)
1102+
10841103
def test_get_bytes_payload_with_quoted_printable_encoding(self):
10851104
# We use a memoryview to avoid directly changing the private payload
10861105
# and to prevent using the dedicated paths for string or bytes objects.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :exc:`AttributeError` in :meth:`email.message.MIMEPart.get_body` when a
2+
nested ``multipart`` part is malformed and its payload is a string.

0 commit comments

Comments
 (0)