]> granicus.if.org Git - python/commitdiff
SF bug #1582282; decode_header() incorrectly splits not-conformant RFC
authorBarry Warsaw <barry@python.org>
Wed, 14 Mar 2007 04:29:06 +0000 (04:29 +0000)
committerBarry Warsaw <barry@python.org>
Wed, 14 Mar 2007 04:29:06 +0000 (04:29 +0000)
2047-like headers where there is no whitespace between encoded words.  This
fix changes the matching regexp to include a trailing lookahead assertion that
the closing ?= must be followed by whitespace, newline, or end-of-string.
This also changes the regexp to add the MULTILINE flag.

Lib/email/header.py
Lib/email/test/test_email.py
Lib/email/test/test_email_renamed.py

index 183c337560d2984f11b691d1ae7f834f5c789823..e139ccf64fa06c1e7347157f22d8f24f3579bf89 100644 (file)
@@ -39,7 +39,8 @@ ecre = re.compile(r'''
   \?                    # literal ?
   (?P<encoded>.*?)      # non-greedy up to the next ?= is the encoded string
   \?=                   # literal ?=
-  ''', re.VERBOSE | re.IGNORECASE)
+  (?=[ \t]|$)           # whitespace or the end of the string
+  ''', re.VERBOSE | re.IGNORECASE | re.MULTILINE)
 
 # Field name regexp, including trailing colon, but not separating whitespace,
 # according to RFC 2822.  Character range is from tilde to exclamation mark.
index 14b8a1b8dc23fc342bb590f59d5023537e65733c..a03299991ef92653c90763afee9ed9c441332cb0 100644 (file)
@@ -1527,6 +1527,18 @@ class TestRFC2047(unittest.TestCase):
         hu = make_header(dh).__unicode__()
         eq(hu, u'The quick brown fox jumped over the lazy dog')
 
+    def test_rfc2047_without_whitespace(self):
+        s = 'Sm=?ISO-8859-1?B?9g==?=rg=?ISO-8859-1?B?5Q==?=sbord'
+        dh = decode_header(s)
+        self.assertEqual(dh, [(s, None)])
+
+    def test_rfc2047_with_whitespace(self):
+        s = 'Sm =?ISO-8859-1?B?9g==?= rg =?ISO-8859-1?B?5Q==?= sbord'
+        dh = decode_header(s)
+        self.assertEqual(dh, [('Sm', None), ('\xf6', 'iso-8859-1'),
+                              ('rg', None), ('\xe5', 'iso-8859-1'),
+                              ('sbord', None)])
+
 
 \f
 # Test the MIMEMessage class
index 19950408718300756104c6d61d2402781cbd5330..44238c78f8c4c468ead5d1f4cbb5cfda06e4f5c3 100644 (file)
@@ -1525,6 +1525,18 @@ class TestRFC2047(unittest.TestCase):
         hu = make_header(dh).__unicode__()
         eq(hu, u'The quick brown fox jumped over the lazy dog')
 
+    def test_rfc2047_missing_whitespace(self):
+        s = 'Sm=?ISO-8859-1?B?9g==?=rg=?ISO-8859-1?B?5Q==?=sbord'
+        dh = decode_header(s)
+        self.assertEqual(dh, [(s, None)])
+
+    def test_rfc2047_with_whitespace(self):
+        s = 'Sm =?ISO-8859-1?B?9g==?= rg =?ISO-8859-1?B?5Q==?= sbord'
+        dh = decode_header(s)
+        self.assertEqual(dh, [('Sm', None), ('\xf6', 'iso-8859-1'),
+                              ('rg', None), ('\xe5', 'iso-8859-1'),
+                              ('sbord', None)])
+
 
 \f
 # Test the MIMEMessage class