]> granicus.if.org Git - python/commitdiff
#15232: correctly mangle From lines in MIME preamble and epilogue
authorR David Murray <rdmurray@bitdance.com>
Mon, 23 Jul 2012 01:47:53 +0000 (21:47 -0400)
committerR David Murray <rdmurray@bitdance.com>
Mon, 23 Jul 2012 01:47:53 +0000 (21:47 -0400)
Lib/email/generator.py
Lib/email/test/test_email.py
Misc/NEWS

index 04c021018308d4f8ef4172b9abb083a36a3bff77..02487e3082571bb0803eb6a9a0ae3a1354571c18 100644 (file)
@@ -233,7 +233,11 @@ class Generator:
             msg.set_boundary(boundary)
         # If there's a preamble, write it out, with a trailing CRLF
         if msg.preamble is not None:
-            self.write(msg.preamble + self._NL)
+            if self._mangle_from_:
+                preamble = fcre.sub('>From ', msg.preamble)
+            else:
+                preamble = msg.preamble
+            self.write(preamble + self._NL)
         # dash-boundary transport-padding CRLF
         self.write('--' + boundary + self._NL)
         # body-part
@@ -251,7 +255,11 @@ class Generator:
         self.write(self._NL + '--' + boundary + '--')
         if msg.epilogue is not None:
             self.write(self._NL)
-            self.write(msg.epilogue)
+            if self._mangle_from_:
+                epilogue = fcre.sub('>From ', msg.epilogue)
+            else:
+                epilogue = msg.epilogue
+            self.write(epilogue)
 
     def _handle_multipart_signed(self, msg):
         # The contents of signed parts has to stay unmodified in order to keep
index 65b3ebd21186acdce98f1de7513fe2eca1c55bb9..95dc4af7c35fe59f74c031c53e30f2735f2efeba 100644 (file)
@@ -1275,6 +1275,28 @@ From the desk of A.A.A.:
 Blah blah blah
 """)
 
+    def test_mangle_from_in_preamble_and_epilog(self):
+        s = StringIO()
+        g = Generator(s, mangle_from_=True)
+        msg = email.message_from_string(textwrap.dedent("""\
+            From: foo@bar.com
+            Mime-Version: 1.0
+            Content-Type: multipart/mixed; boundary=XXX
+
+            From somewhere unknown
+
+            --XXX
+            Content-Type: text/plain
+
+            foo
+
+            --XXX--
+
+            From somewhere unknowable
+            """))
+        g.flatten(msg)
+        self.assertEqual(len([1 for x in s.getvalue().split('\n')
+                                  if x.startswith('>From ')]), 2)
 
 
 # Test the basic MIMEAudio class
index 0b607db85825b551e91efda7eeeab17065f38ec3..e45c1037ffc59caf53b0c1353db7afcfb3657b1d 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -98,6 +98,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #15232: when mangle_from is True, email.Generator now correctly mangles
+  lines that start with 'From' that occur in a MIME preamble or epilogue.
+
 - Issue #13922: argparse no longer incorrectly strips '--'s that appear
   after the first one.