Skip to content

Commit f62a372

Browse files
bpo-30835: email: Fix AttributeError when parsing invalid CTE (GH-13598)
* bpo-30835: email: Fix AttributeError when parsing invalid Content-Transfer-Encoding Parsing an email containing a multipart Content-Type, along with a Content-Transfer-Encoding containing an invalid (non-ASCII-decodable) byte will fail. email.feedparser.FeedParser._parsegen() gets the header and attempts to convert it to lowercase before comparing it with the accepted encodings, but as the header contains an invalid byte, it's returned as a Header object rather than a str. Cast the Content-Transfer-Encoding header to a str to avoid this. Found using the AFL fuzzer. Reported-by: Daniel Axtens <[email protected]> Signed-off-by: Andrew Donnellan <[email protected]> * Add email and NEWS entry for the bugfix. (cherry picked from commit aa79707) Co-authored-by: Abhilash Raj <[email protected]>
1 parent 3d75bd1 commit f62a372

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

Lib/email/feedparser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ def _parsegen(self):
320320
self._cur.set_payload(EMPTYSTRING.join(lines))
321321
return
322322
# Make sure a valid content type was specified per RFC 2045:6.4.
323-
if (self._cur.get('content-transfer-encoding', '8bit').lower()
323+
if (str(self._cur.get('content-transfer-encoding', '8bit')).lower()
324324
not in ('7bit', '8bit', 'binary')):
325325
defect = errors.InvalidMultipartContentTransferEncodingDefect()
326326
self.policy.handle_defect(self._cur, defect)

Lib/test/test_email/test_email.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,6 +1466,15 @@ def test_mangled_from_with_bad_bytes(self):
14661466
g.flatten(msg)
14671467
self.assertEqual(b.getvalue(), source + b'>From R\xc3\xb6lli\n')
14681468

1469+
def test_mutltipart_with_bad_bytes_in_cte(self):
1470+
# bpo30835
1471+
source = textwrap.dedent("""\
1472+
1473+
Content-Type: multipart/mixed; boundary="1"
1474+
Content-Transfer-Encoding: \xc8
1475+
""").encode('utf-8')
1476+
msg = email.message_from_bytes(source)
1477+
14691478

14701479
# Test the basic MIMEAudio class
14711480
class TestMIMEAudio(unittest.TestCase):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed a bug in email parsing where a message with invalid bytes in
2+
content-transfer-encoding of a multipart message can cause an AttributeError.
3+
Patch by Andrew Donnellan.

0 commit comments

Comments
 (0)