Index: Lib/email/test/data/msg_45.txt =================================================================== --- Lib/email/test/data/msg_45.txt (revision 0) +++ Lib/email/test/data/msg_45.txt (revision 0) @@ -0,0 +1,33 @@ +From: +To: +Subject: test +X-Long-Line: Some really long line contains a lot of text and thus has to be rewrapped because it is some + really long + line +MIME-Version: 1.0 +Content-Type: multipart/signed; boundary="borderline"; + protocol="application/pgp-signature"; micalg=pgp-sha1 + +This is an OpenPGP/MIME signed message (RFC 2440 and 3156) +--borderline +Content-Type: text/plain +X-Long-Line: Another really long line contains a lot of text and thus has to be rewrapped because it is another + really long + line + +This is the signed contents. + +--borderline +Content-Type: application/pgp-signature; name="signature.asc" +Content-Description: OpenPGP digital signature +Content-Disposition: attachment; filename="signature.asc" + +-----BEGIN PGP SIGNATURE----- +Version: GnuPG v2.0.6 (GNU/Linux) + +iD8DBQFG03voRhp6o4m9dFsRApSZAKCCAN3IkJlVRg6NvAiMHlvvIuMGPQCeLZtj +FGwfnRHFBFO/S4/DKysm0lI= +=t7+s +-----END PGP SIGNATURE----- + +--borderline-- Index: Lib/email/test/data/msg_46.txt =================================================================== --- Lib/email/test/data/msg_46.txt (revision 0) +++ Lib/email/test/data/msg_46.txt (revision 0) @@ -0,0 +1,31 @@ +From: +To: +Subject: test +X-Indented-Header: Some header with extra indentation +X-Unindented-Header:Some header with no indentation +MIME-Version: 1.0 +Content-Type: multipart/signed; boundary="borderline"; + protocol="application/pgp-signature"; micalg=pgp-sha1 + +This is an OpenPGP/MIME signed message (RFC 2440 and 3156) +--borderline +Content-Type: text/plain +X-Indented-Header: Some header with extra indentation +X-Unindented-Header:Some header with no indentation + +This is the signed contents. + +--borderline +Content-Type: application/pgp-signature; name="signature.asc" +Content-Description: OpenPGP digital signature +Content-Disposition: attachment; filename="signature.asc" + +-----BEGIN PGP SIGNATURE----- +Version: GnuPG v2.0.14 (GNU/Linux) + +iEYEARECAAYFAktQq5oACgkQRhp6o4m9dFsw/ACfX43wGUfvXP6Uz6Pz8GS00TG1 +20MAnjbEgak7yXDdZoQ/e79DhQWea6z7 +=52wS +-----END PGP SIGNATURE----- + +--borderline-- Index: Lib/email/test/test_email.py =================================================================== --- Lib/email/test/test_email.py (revision 77511) +++ Lib/email/test/test_email.py (working copy) @@ -3387,6 +3387,57 @@ +# Tests to ensure that signed parts of an email are completely preserved +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_as_string_maxheaderlen(self): + original, msg = self._msg_and_obj('msg_45.txt') + result = msg.as_string(maxheaderlen=60) + 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) + + def disabled_test_indented_headers_as_string(self): + original, msg = self._msg_and_obj('msg_46.txt') + result = msg.as_string() + self._signed_parts_eq(original, result) + + def disabled_test_indented_headers_flatten(self): + original, msg = self._msg_and_obj('msg_46.txt') + fp = StringIO() + Generator(fp).flatten(msg) + result = fp.getvalue() + self._signed_parts_eq(original, result) + + + def _testclasses(): mod = sys.modules[__name__] return [getattr(mod, name) for name in dir(mod) if name.startswith('Test')] Index: Lib/email/generator.py =================================================================== --- Lib/email/generator.py (revision 77511) +++ Lib/email/generator.py (working copy) @@ -215,6 +215,16 @@ print(file=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, so we disable header wrapping. + 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