]> granicus.if.org Git - python/commitdiff
Merged revisions 77517,77525 via svnmerge from
authorR. David Murray <rdmurray@bitdance.com>
Sat, 16 Jan 2010 16:32:39 +0000 (16:32 +0000)
committerR. David Murray <rdmurray@bitdance.com>
Sat, 16 Jan 2010 16:32:39 +0000 (16:32 +0000)
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r77517 | r.david.murray | 2010-01-16 00:15:17 -0500 (Sat, 16 Jan 2010) | 6 lines

  Issue #1670765: Prevent email.generator.Generator from re-wrapping
  headers in multipart/signed MIME parts, which fixes one of the sources of
  invalid modifications to such parts by Generator.  Patch and tests by
  Martin von Gagern.
........
  r77525 | r.david.murray | 2010-01-16 11:08:32 -0500 (Sat, 16 Jan 2010) | 2 lines

  Fix issue number in comment.
........

Lib/email/generator.py
Lib/email/test/test_email.py
Misc/NEWS

index ed832a3e9a46ebb6d5bb67351c56d57c46d9dbe1..ba3ccd8988f71a7caf8b376043a493ebb3e98ad3 100644 (file)
@@ -235,6 +235,17 @@ class Generator:
             print >> self._fp
             self._fp.write(msg.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.
+        old_maxheaderlen = self._maxheaderlen
+        try:
+            self._maxheaderlen = 0
+            self._handle_multipart(msg)
+        finally:
+            self._maxheaderlen = old_maxheaderlen
+
     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
index 3a68f02b4880bea1ee8ddd4b30d0a93957178e7a..631c278be9fb6328d1a643b3bed586610714d9fa 100644 (file)
@@ -3266,6 +3266,42 @@ Content-Type: application/x-foo;
         eq(s, 'My Document For You')
 
 
+\f
+# Tests to ensure that signed parts of an email are completely preserved, as
+# required by RFC1847 section 2.1.  Note that these are incomplete, because the
+# email package does not currently always preserve the body.  See issue 1670765.
+class TestSigned(TestEmailBase):
+
+    def _msg_and_obj(self, filename):
+        fp = openfile(findfile(filename))
+        try:
+            original = fp.read()
+            msg = email.message_from_string(original)
+        finally:
+            fp.close()
+        return original, msg
+
+    def _signed_parts_eq(self, original, result):
+        # Extract the first mime part of each message
+        import re
+        repart = re.compile(r'^--([^\n]+)\n(.*?)\n--\1$', re.S | re.M)
+        inpart = repart.search(original).group(2)
+        outpart = repart.search(result).group(2)
+        self.assertEqual(outpart, inpart)
+
+    def test_long_headers_as_string(self):
+        original, msg = self._msg_and_obj('msg_45.txt')
+        result = msg.as_string()
+        self._signed_parts_eq(original, result)
+
+    def test_long_headers_flatten(self):
+        original, msg = self._msg_and_obj('msg_45.txt')
+        fp = StringIO()
+        Generator(fp).flatten(msg)
+        result = fp.getvalue()
+        self._signed_parts_eq(original, result)
+
+
 \f
 def _testclasses():
     mod = sys.modules[__name__]
index 1ddef4c670516e33ec00f8172b89b2bae108f5db..eb77a3db311ddd88110db004b85c734b2fee959e 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -48,6 +48,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #1670765: Prevent email.generator.Generator from re-wrapping
+  headers in multipart/signed MIME parts, which fixes one of the sources of
+  invalid modifications to such parts by Generator.
+
 - Issue #7701: Fix crash in binascii.b2a_uu() in debug mode when given a
   1-byte argument.  Patch by Victor Stinner.