]> granicus.if.org Git - python/commitdiff
Merged revisions 83690 via svnmerge from
authorR. David Murray <rdmurray@bitdance.com>
Tue, 3 Aug 2010 23:35:44 +0000 (23:35 +0000)
committerR. David Murray <rdmurray@bitdance.com>
Tue, 3 Aug 2010 23:35:44 +0000 (23:35 +0000)
svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r83690 | r.david.murray | 2010-08-03 18:14:10 -0400 (Tue, 03 Aug 2010) | 10 lines

  #3196: if needed pad a short base64 encoded word before trying to decode.

  The RFCs encourage following Postel's law: be liberal in what you accept.
  So if someone forgot to pad the base64 encoded word payload to an
  even four bytes, we add the padding before handing it to base64mime.decode.
  Previously, missing padding resulted in a HeaderParseError.

  Patch by Jason Williams.
........

Lib/email/header.py
Lib/email/test/test_email.py
Misc/ACKS
Misc/NEWS

index 3215a80f9b6db1730ae3f6bee60ae4be4cca6f14..afcea4045801f52aec74e025017c311a44c01d57 100644 (file)
@@ -94,6 +94,9 @@ def decode_header(header):
             word = email.quoprimime.header_decode(encoded_string)
             decoded_words.append((word, charset))
         elif encoding == 'b':
+            paderr = len(encoded_string) % 4   # Postel's law: add missing padding
+            if paderr:
+                encoded_string += '==='[:4 - paderr]
             try:
                 word = email.base64mime.decode(encoded_string)
             except binascii.Error:
index 7d91d25884e06f553ab41e19ac1336080f5f28ec..05591422c23341bf7a8a5191963fc761aeb71fa5 100644 (file)
@@ -1645,6 +1645,15 @@ Re: =?mac-iceland?q?r=8Aksm=9Arg=8Cs?= baz foo bar =?mac-iceland?q?r=8Aksm?=
                               (b'rg', None), (b'\xe5', 'iso-8859-1'),
                               (b'sbord', None)])
 
+    def test_rfc2047_B_bad_padding(self):
+        s = '=?iso-8859-1?B?%s?='
+        data = [                                # only test complete bytes
+            ('dm==', b'v'), ('dm=', b'v'), ('dm', b'v'),
+            ('dmk=', b'vi'), ('dmk', b'vi')
+          ]
+        for q, a in data:
+            dh = decode_header(s % q)
+            self.assertEqual(dh, [(a, 'iso-8859-1')])
 
 \f
 # Test the MIMEMessage class
@@ -3172,7 +3181,7 @@ A very long line that must get split to something other than at the
 
     def test_broken_base64_header(self):
         raises = self.assertRaises
-        s = 'Subject: =?EUC-KR?B?CSixpLDtKSC/7Liuvsax4iC6uLmwMcijIKHaILzSwd/H0SC8+LCjwLsgv7W/+Mj3IQ?='
+        s = 'Subject: =?EUC-KR?B?CSixpLDtKSC/7Liuvsax4iC6uLmwMcijIKHaILzSwd/H0SC8+LCjwLsgv7W/+Mj3I ?='
         raises(errors.HeaderParseError, decode_header, s)
 
 
index 03f96ebf5c0f3fc0036787a26d647d6bb3fb6cfa..6486e794ca63fc43fc39b0bbe8db93e78f176e6b 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -817,6 +817,7 @@ Felix Wiemann
 Gerry Wiener
 Frank Wierzbicki
 Bryce "Zooko" Wilcox-O'Hearn
+Jason Williams
 John Williams
 Sue Williams
 Gerald S. Williams
index 8e164a9c3cbc6cd0bc016e65d825f1171515bc79..262bd5e054ed42a99462334e01ee7364258cf4bf 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -84,6 +84,9 @@ C-API
 Library
 -------
 
+- Issue #3196: email header decoding is now forgiving if an RFC2047
+  encoded word encoded in base64 is lacking padding.
+
 - Issue #8447: Make distutils.sysconfig follow symlinks in the path to
   the interpreter executable.  This fixes a failure of test_httpservers
   on OS X.