Skip to content

[3.12] gh-98188: Fix EmailMessage.get_payload to decode data when CTE value has extra text (GH-127547) #128529

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 7, 2025
Merged
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
8 changes: 6 additions & 2 deletions Lib/email/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,12 @@ def get_payload(self, i=None, decode=False):
if i is not None and not isinstance(self._payload, list):
raise TypeError('Expected list, got %s' % type(self._payload))
payload = self._payload
# cte might be a Header, so for now stringify it.
cte = str(self.get('content-transfer-encoding', '')).lower()
cte = self.get('content-transfer-encoding', '')
if hasattr(cte, 'cte'):
cte = cte.cte
else:
# cte might be a Header, so for now stringify it.
cte = str(cte).strip().lower()
# payload may be bytes here.
if not decode:
if isinstance(payload, str) and utils._has_surrogates(payload):
Expand Down
44 changes: 44 additions & 0 deletions Lib/test/test_email/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,16 @@ def test_unicode_body_defaults_to_utf8_encoding(self):
w4kgdGVzdGFiYwo=
"""))

def test_string_payload_with_base64_cte(self):
msg = email.message_from_string(textwrap.dedent("""\
Content-Transfer-Encoding: base64

SGVsbG8uIFRlc3Rpbmc=
"""), policy=email.policy.default)
self.assertEqual(msg.get_payload(decode=True), b"Hello. Testing")
self.assertDefectsEqual(msg['content-transfer-encoding'].defects, [])



# Test the email.encoders module
class TestEncoders(unittest.TestCase):
Expand Down Expand Up @@ -2352,6 +2362,40 @@ def test_missing_header_body_separator(self):
self.assertDefectsEqual(msg.defects,
[errors.MissingHeaderBodySeparatorDefect])

def test_string_payload_with_extra_space_after_cte(self):
# https://github.com/python/cpython/issues/98188
cte = "base64 "
msg = email.message_from_string(textwrap.dedent(f"""\
Content-Transfer-Encoding: {cte}

SGVsbG8uIFRlc3Rpbmc=
"""), policy=email.policy.default)
self.assertEqual(msg.get_payload(decode=True), b"Hello. Testing")
self.assertDefectsEqual(msg['content-transfer-encoding'].defects, [])

def test_string_payload_with_extra_text_after_cte(self):
msg = email.message_from_string(textwrap.dedent("""\
Content-Transfer-Encoding: base64 some text

SGVsbG8uIFRlc3Rpbmc=
"""), policy=email.policy.default)
self.assertEqual(msg.get_payload(decode=True), b"Hello. Testing")
cte = msg['content-transfer-encoding']
self.assertDefectsEqual(cte.defects, [email.errors.InvalidHeaderDefect])

def test_string_payload_with_extra_space_after_cte_compat32(self):
cte = "base64 "
msg = email.message_from_string(textwrap.dedent(f"""\
Content-Transfer-Encoding: {cte}

SGVsbG8uIFRlc3Rpbmc=
"""), policy=email.policy.compat32)
pasted_cte = msg['content-transfer-encoding']
self.assertEqual(pasted_cte, cte)
self.assertEqual(msg.get_payload(decode=True), b"Hello. Testing")
self.assertDefectsEqual(msg.defects, [])



# Test RFC 2047 header encoding and decoding
class TestRFC2047(TestEmailBase):
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_email/test_headerregistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,11 @@ def cte_as_value(self,
'7bit',
[errors.InvalidHeaderDefect]),

'extra_space_after_cte': (
'base64 ',
'base64',
[]),

}


Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,7 @@ Gregor Lingl
Everett Lipman
Mirko Liss
Alexander Liu
Hui Liu
Yuan Liu
Nick Lockwood
Stephanie Lockwood
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix an issue in :meth:`email.message.Message.get_payload` where data
cannot be decoded if the Content Transfer Encoding mechanism contains
trailing whitespaces or additional junk text. Patch by Hui Liu.
Loading