Skip to content

gh-99533: Fix issue where email.generator.Generator ignores policy when using multipart/signed #100204

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
11 changes: 0 additions & 11 deletions Lib/email/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,17 +313,6 @@ def _handle_multipart(self, msg):
epilogue = msg.epilogue
self._write_lines(epilogue)

def _handle_multipart_signed(self, msg):
# The contents of signed parts has to stay unmodified in order to keep
# the signature intact per RFC1847 2.1, so we disable header wrapping.
# RDM: This isn't enough to completely preserve the part, but it helps.
p = self.policy
self.policy = p.clone(max_line_length=0)
try:
self._handle_multipart(msg)
finally:
self.policy = p

def _handle_message_delivery_status(self, msg):
# We can't just write the headers directly to self's file object
# because this will leave an extra newline between the last header
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test_email/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -5564,6 +5564,13 @@ def test_long_headers_flatten(self):
result = fp.getvalue()
self._signed_parts_eq(original, result)

def test_multipart_signed(self):
inner = Message()
outer = Message()
outer.set_type("multipart/signed")
outer.attach(inner)
self.assertTrue(inner.as_string() in outer.as_string())

class TestHeaderRegistry(TestEmailBase):
# See issue gh-93010.
def test_HeaderRegistry(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix issue where email.generator.Generator ignores policy when using multipart/signed due to old functions in generator.py
:func:`_handle_multipart_signed`. Patch by Jack Pendley.