Skip to content
Open
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
15 changes: 11 additions & 4 deletions Lib/email/contentmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ def _finalize_set(msg, disposition, filename, cid, params):
def _encode_text(string, charset, cte, policy):
# If max_line_length is 0 or None, there is no limit.
maxlen = policy.max_line_length or sys.maxsize
# RFC 2045 limits the length of content-transfer-encoded lines to 76
# characters, while max_line_length (78 by default) is the RFC 5322
# limit for header lines.
encoded_maxlen = min(maxlen, 76) if policy.max_line_length else maxlen
lines = string.encode(charset).splitlines()
linesep = policy.linesep.encode('ascii')
def embedded_body(lines): return linesep.join(lines) + linesep
Expand All @@ -146,7 +150,7 @@ def normal_body(lines): return b'\n'.join(lines) + b'\n'
if policy.cte_type == '8bit':
return '8bit', normal_body(lines).decode('ascii', 'surrogateescape')
sniff = embedded_body(lines[:10])
sniff_qp = quoprimime.body_encode(sniff.decode('latin-1'), maxlen)
sniff_qp = quoprimime.body_encode(sniff.decode('latin-1'), encoded_maxlen)
sniff_base64 = binascii.b2a_base64(sniff)
# This is a little unfair to qp; it includes lineseps, base64 doesn't.
if len(sniff_qp) > len(sniff_base64):
Expand All @@ -161,9 +165,9 @@ def normal_body(lines): return b'\n'.join(lines) + b'\n'
data = normal_body(lines).decode('ascii', 'surrogateescape')
elif cte == 'quoted-printable':
data = quoprimime.body_encode(normal_body(lines).decode('latin-1'),
maxlen)
encoded_maxlen)
elif cte == 'base64':
data = binascii.b2a_base64(embedded_body(lines), wrapcol=maxlen).decode('ascii')
data = binascii.b2a_base64(embedded_body(lines), wrapcol=encoded_maxlen).decode('ascii')
else:
raise ValueError("Unknown content transfer encoding {}".format(cte))
return cte, data
Expand Down Expand Up @@ -222,7 +226,10 @@ def set_bytes_content(msg, data, maintype, subtype, cte='base64',
params=None, headers=None):
_prepare_set(msg, maintype, subtype, headers)
if cte == 'base64':
data = binascii.b2a_base64(data, wrapcol=msg.policy.max_line_length)
maxlen = msg.policy.max_line_length
if maxlen:
maxlen = min(maxlen, 76)
data = binascii.b2a_base64(data, wrapcol=maxlen)
data = data.decode('ascii')
elif cte == 'quoted-printable':
# XXX: quoprimime.body_encode won't encode newline characters in data,
Expand Down
38 changes: 38 additions & 0 deletions Lib/test/test_email/test_contentmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,44 @@ def test_set_image_jpg(self):
self.assertEqual(m.get_payload(decode=True), content)
self.assertEqual(m.get_content(), content)

def test_set_text_encoded_body_line_length(self):
content = "é" * 100 + "\n"
for cte in ('base64', 'quoted-printable'):
for max_line_length in (60, 78, 100):
with self.subTest(cte=cte,
max_line_length=max_line_length):
m = self.message(policy=self.policy.clone(
max_line_length=max_line_length))
raw_data_manager.set_content(m, content, cte=cte)
lines = m.get_payload().splitlines()
self.assertTrue(lines)
self.assertLessEqual(max(map(len, lines)),
min(max_line_length, 76))
self.assertEqual(m.get_content(), content)

def test_set_bytes_base64_body_line_length(self):
data = b'\xff' * 150
for content in (data, bytearray(data), memoryview(data)):
for max_line_length in (60, 78, 100):
with self.subTest(content_type=type(content).__name__,
max_line_length=max_line_length):
m = self.message(policy=self.policy.clone(
max_line_length=max_line_length))
raw_data_manager.set_content(
m, content, 'application', 'octet-stream')
lines = m.get_payload().splitlines()
self.assertTrue(lines)
self.assertLessEqual(max(map(len, lines)),
min(max_line_length, 76))
self.assertEqual(m.get_payload(decode=True), data)

# A zero policy limit explicitly disables wrapping.
m = self.message(policy=self.policy.clone(max_line_length=0))
raw_data_manager.set_content(
m, data, 'application', 'octet-stream')
self.assertEqual(len(m.get_payload().splitlines()), 1)
self.assertEqual(m.get_payload(decode=True), data)

def test_set_audio_aif_with_quoted_printable_cte(self):
# Why you would use qp, I don't know, but it is technically supported.
# XXX: the incorrect line length is because binascii.b2a_qp doesn't
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :mod:`email` to limit base64 and quoted-printable encoded message body
lines to 76 characters when the email policy allows longer header lines.
Loading