Skip to content
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

gh-124452: Fix header mismatches when folding/unfolding with email message #125919

Merged
merged 15 commits into from
Nov 16, 2024
2 changes: 1 addition & 1 deletion Lib/email/_policybase.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ def header_source_parse(self, sourcelines):

RanKKI marked this conversation as resolved.
Show resolved Hide resolved
"""
name, value = sourcelines[0].split(':', 1)
value = value.lstrip(' \t') + ''.join(sourcelines[1:])
value = ''.join((value, *sourcelines[1:])).lstrip(' \t\r\n')
return (name, value.rstrip('\r\n'))

def header_store_parse(self, name, value):
Expand Down
2 changes: 1 addition & 1 deletion Lib/email/policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def header_source_parse(self, sourcelines):

RanKKI marked this conversation as resolved.
Show resolved Hide resolved
"""
name, value = sourcelines[0].split(':', 1)
value = value.lstrip(' \t') + ''.join(sourcelines[1:])
value = ''.join((value, *sourcelines[1:])).lstrip(' \t\r\n')
return (name, value.rstrip('\r\n'))

def header_store_parse(self, name, value):
Expand Down
50 changes: 48 additions & 2 deletions Lib/test/test_email/test_message.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest
import textwrap
from email import policy, message_from_string
import unittest
from email import message_from_bytes, message_from_string, policy
from email.message import EmailMessage, MIMEPart
from test.test_email import TestEmailBase, parameterize

Expand Down Expand Up @@ -958,6 +958,52 @@ def test_folding_with_utf8_encoding_8(self):
b'123456789-123456789\n 123456789 Hello '
b'=?utf-8?q?W=C3=B6rld!?= 123456789 123456789\n\n')

def test_folding_with_short_nospace_1(self):
# bpo-36520
#
# Fold a line that contains a long whitespace after
# the fold point.

m = EmailMessage(policy.default)
m['Message-ID'] = '123456789'*3
parsed_msg = message_from_bytes(m.as_bytes(), policy=policy.default)
RanKKI marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(parsed_msg['Message-ID'], m['Message-ID'])

def test_folding_with_long_nospace_default_policy_1(self):
# Fixed: https://github.com/python/cpython/issues/124452
RanKKI marked this conversation as resolved.
Show resolved Hide resolved
#
# When the value is too long, it should be converted back
# to its original form without any modifications.

m = EmailMessage(policy.default)
message = '123456789' * 10
m['Message-ID'] = message
self.assertEqual(m.as_bytes(),
f'Message-ID:\n {message}\n\n'.encode())
parsed_msg = message_from_bytes(m.as_bytes(), policy=policy.default)
self.assertEqual(parsed_msg['Message-ID'], m['Message-ID'])

def test_folding_with_long_nospace_compat32_policy_1(self):
m = EmailMessage(policy.compat32)
message = '123456789' * 10
m['Message-ID'] = message
parsed_msg = message_from_bytes(m.as_bytes(), policy=policy.default)
self.assertEqual(parsed_msg['Message-ID'], m['Message-ID'])

def test_folding_with_long_nospace_smtp_policy_1(self):
m = EmailMessage(policy.SMTP)
message = '123456789' * 10
m['Message-ID'] = message
parsed_msg = message_from_bytes(m.as_bytes(), policy=policy.default)
self.assertEqual(parsed_msg['Message-ID'], m['Message-ID'])

def test_folding_with_long_nospace_http_policy_1(self):
m = EmailMessage(policy.HTTP)
message = '123456789' * 10
m['Message-ID'] = message
parsed_msg = message_from_bytes(m.as_bytes(), policy=policy.default)
self.assertEqual(parsed_msg['Message-ID'], m['Message-ID'])

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 @@
Fix email parsing bug that introduced spurious leading whitespace into header values when the header includes a newline character after the ';' and before the value.
RanKKI marked this conversation as resolved.
Show resolved Hide resolved
Loading