Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c4e69cc

Browse files
committed
#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.
1 parent df7f2fd commit c4e69cc

4 files changed

Lines changed: 17 additions & 1 deletion

File tree

Lib/email/header.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ def decode_header(header):
9494
word = email.quoprimime.header_decode(encoded_string)
9595
decoded_words.append((word, charset))
9696
elif encoding == 'b':
97+
paderr = len(encoded_string) % 4 # Postel's law: add missing padding
98+
if paderr:
99+
encoded_string += '==='[:4 - paderr]
97100
try:
98101
word = email.base64mime.decode(encoded_string)
99102
except binascii.Error:

Lib/email/test/test_email.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1649,6 +1649,15 @@ def test_rfc2047_with_whitespace(self):
16491649
(b'rg', None), (b'\xe5', 'iso-8859-1'),
16501650
(b'sbord', None)])
16511651

1652+
def test_rfc2047_B_bad_padding(self):
1653+
s = '=?iso-8859-1?B?%s?='
1654+
data = [ # only test complete bytes
1655+
('dm==', b'v'), ('dm=', b'v'), ('dm', b'v'),
1656+
('dmk=', b'vi'), ('dmk', b'vi')
1657+
]
1658+
for q, a in data:
1659+
dh = decode_header(s % q)
1660+
self.assertEqual(dh, [(a, 'iso-8859-1')])
16521661

16531662

16541663
# Test the MIMEMessage class
@@ -3176,7 +3185,7 @@ def test_whitespace_eater(self):
31763185

31773186
def test_broken_base64_header(self):
31783187
raises = self.assertRaises
3179-
s = 'Subject: =?EUC-KR?B?CSixpLDtKSC/7Liuvsax4iC6uLmwMcijIKHaILzSwd/H0SC8+LCjwLsgv7W/+Mj3IQ?='
3188+
s = 'Subject: =?EUC-KR?B?CSixpLDtKSC/7Liuvsax4iC6uLmwMcijIKHaILzSwd/H0SC8+LCjwLsgv7W/+Mj3I ?='
31803189
raises(errors.HeaderParseError, decode_header, s)
31813190

31823191

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,7 @@ Felix Wiemann
854854
Gerry Wiener
855855
Frank Wierzbicki
856856
Bryce "Zooko" Wilcox-O'Hearn
857+
Jason Williams
857858
John Williams
858859
Sue Williams
859860
Gerald S. Williams

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ Extensions
3737
Library
3838
-------
3939

40+
- Issue #3196: email header decoding is now forgiving if an RFC2047
41+
encoded word encoded in base64 is lacking padding.
42+
4043
- Issue #9444: Argparse now uses the first element of prefix_chars as
4144
the option character for the added 'h/help' option if prefix_chars
4245
does not contain a '-', instead of raising an error.

0 commit comments

Comments
 (0)