diff options
author | R. David Murray <rdmurray@bitdance.com> | 2010-08-03 22:14:10 (GMT) |
---|---|---|
committer | R. David Murray <rdmurray@bitdance.com> | 2010-08-03 22:14:10 (GMT) |
commit | c4e69cc1d84a8e025c6b1894b4749cda8bb1d156 (patch) | |
tree | 586e2b0cb340e1107e34f9e85217055dffc5a350 /Lib/email | |
parent | df7f2fd350c215d15faf14ed01838ae3420ac0ae (diff) | |
download | cpython-c4e69cc1d84a8e025c6b1894b4749cda8bb1d156.zip cpython-c4e69cc1d84a8e025c6b1894b4749cda8bb1d156.tar.gz cpython-c4e69cc1d84a8e025c6b1894b4749cda8bb1d156.tar.bz2 |
#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.
Diffstat (limited to 'Lib/email')
-rw-r--r-- | Lib/email/header.py | 3 | ||||
-rw-r--r-- | Lib/email/test/test_email.py | 11 |
2 files changed, 13 insertions, 1 deletions
diff --git a/Lib/email/header.py b/Lib/email/header.py index c924d3a..89c1391 100644 --- a/Lib/email/header.py +++ b/Lib/email/header.py @@ -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: diff --git a/Lib/email/test/test_email.py b/Lib/email/test/test_email.py index 09f51df..c311469 100644 --- a/Lib/email/test/test_email.py +++ b/Lib/email/test/test_email.py @@ -1649,6 +1649,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')]) # Test the MIMEMessage class @@ -3176,7 +3185,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) |