]> granicus.if.org Git - python/commitdiff
Merged revisions 83690 via svnmerge from
authorR. David Murray <rdmurray@bitdance.com>
Wed, 4 Aug 2010 00:05:50 +0000 (00:05 +0000)
committerR. David Murray <rdmurray@bitdance.com>
Wed, 4 Aug 2010 00:05:50 +0000 (00:05 +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
Lib/email/test/test_email_renamed.py
Misc/ACKS
Misc/NEWS

index 6e03922aa03d47eb1700531915e8fadbea2ab56a..9e91dc20774ebf8aa1fbee5e6535636e70e467d1 100644 (file)
@@ -92,6 +92,9 @@ def decode_header(header):
                 if encoding == 'q':
                     dec = email.quoprimime.header_decode(encoded)
                 elif encoding == 'b':
+                    paderr = len(encoded) % 4   # Postel's law: add missing padding
+                    if paderr:
+                        encoded += '==='[:4 - paderr]
                     try:
                         dec = email.base64mime.decode(encoded)
                     except binascii.Error:
index 067937a975717d84de353a832af0ddb2d08eb5eb..e1754e9f753456cd5fc7fe2faed2c754b6dc4f10 100644 (file)
@@ -1611,6 +1611,15 @@ class TestRFC2047(unittest.TestCase):
                               ('rg', None), ('\xe5', 'iso-8859-1'),
                               ('sbord', None)])
 
+    def test_rfc2047_B_bad_padding(self):
+        s = '=?iso-8859-1?B?%s?='
+        data = [                                # only test complete bytes
+            ('dm==', 'v'), ('dm=', 'v'), ('dm', 'v'),
+            ('dmk=', 'vi'), ('dmk', 'vi')
+          ]
+        for q, a in data:
+            dh = decode_header(s % q)
+            self.assertEqual(dh, [(a, 'iso-8859-1')])
 
 \f
 # Test the MIMEMessage class
@@ -3064,7 +3073,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 976d892f4a7104b9a30bf14fcbe9b6e3fe2880d2..481c40f1354f24bc248677b6f69c4033df77e94f 100644 (file)
@@ -2958,7 +2958,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 197a5022caa191be06db3bd9a44fb42f98bbc3f9..089939ee1e2af1500bae03d17b25a1128360a33c 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -831,6 +831,7 @@ Felix Wiemann
 Gerry Wiener
 Frank Wierzbicki
 Bryce "Zooko" Wilcox-O'Hearn
+Jason Williams
 John Williams
 Sue Williams
 Gerald S. Williams
index d8b079bdb5038277d8d39dea6510ab9f906ece7c..545ef74f21c8a67c2cff78e7e6d7dfe70840ad92 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -24,6 +24,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #3196: email header decoding is now forgiving if an RFC2047
+  encoded word encoded in base64 is lacking padding.
+
 - Issue #9444: Argparse now uses the first element of prefix_chars as
   the option character for the added 'h/help' option if prefix_chars
   does not contain a '-', instead of raising an error.