From faec117e592e461891f1b0a5bf4ed490ad70dc27 Mon Sep 17 00:00:00 2001 From: wangshun <96341168+xixi-box@users.noreply.github.com> Date: Mon, 9 Nov 2026 00:56:43 +0000 Subject: [PATCH 1/4] fix: SMTP EmailPolicy not using the correct line length for RCF 2045 enc Signed-off-by: wangshun <96341168+xixi-box@users.noreply.github.com> --- Lib/email/contentmanager.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Lib/email/contentmanager.py b/Lib/email/contentmanager.py index c0090af716575d..4bbe07a4a92db6 100644 --- a/Lib/email/contentmanager.py +++ b/Lib/email/contentmanager.py @@ -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 @@ -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): @@ -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 From 3bfea9fb0245931fa59468a7dc5d39de443ee22e Mon Sep 17 00:00:00 2001 From: wangshun <96341168+xixi-box@users.noreply.github.com> Date: Fri, 20 Nov 2026 10:34:25 +0800 Subject: [PATCH 2/4] gh-78206: Add NEWS entry for encoded email body line lengths --- .../next/Library/2026-11-20-10-26-38.gh-issue-78206.RcF7vQ.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-11-20-10-26-38.gh-issue-78206.RcF7vQ.rst diff --git a/Misc/NEWS.d/next/Library/2026-11-20-10-26-38.gh-issue-78206.RcF7vQ.rst b/Misc/NEWS.d/next/Library/2026-11-20-10-26-38.gh-issue-78206.RcF7vQ.rst new file mode 100644 index 00000000000000..c45cc446480b69 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-20-10-26-38.gh-issue-78206.RcF7vQ.rst @@ -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. From 2eac1f57d6507fe7403b51ddfc282ca82242afba Mon Sep 17 00:00:00 2001 From: wangshun <96341168+xixi-box@users.noreply.github.com> Date: Sat, 21 Nov 2026 03:32:14 +0800 Subject: [PATCH 3/4] gh-78206: Limit base64-encoded attachment lines to 76 characters --- Lib/email/contentmanager.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Lib/email/contentmanager.py b/Lib/email/contentmanager.py index 4bbe07a4a92db6..6121bc2915a9d0 100644 --- a/Lib/email/contentmanager.py +++ b/Lib/email/contentmanager.py @@ -226,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, From a2a6447152b4403dc68f9ecb9eb4688b00036b41 Mon Sep 17 00:00:00 2001 From: wangshun <96341168+xixi-box@users.noreply.github.com> Date: Sat, 21 Nov 2026 03:32:46 +0800 Subject: [PATCH 4/4] gh-78206: Add coverage for text and attachment transfer line limits --- Lib/test/test_email/test_contentmanager.py | 38 ++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/Lib/test/test_email/test_contentmanager.py b/Lib/test/test_email/test_contentmanager.py index 3115941f870319..98c8a2d82df62c 100644 --- a/Lib/test/test_email/test_contentmanager.py +++ b/Lib/test/test_email/test_contentmanager.py @@ -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