]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-38332: Catch KeyError from unknown cte in encoded-word. (GH-16503)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 12 Oct 2019 17:03:24 +0000 (10:03 -0700)
committerGitHub <noreply@github.com>
Sat, 12 Oct 2019 17:03:24 +0000 (10:03 -0700)
KeyError should cause a failure in parsing the encoded word and should be caught and raised as a _InvalidEWError instead.
(cherry picked from commit 65dcc8a8dc41d3453fd6b987073a5f1b30c5c0fd)

Co-authored-by: Andrei Troie <andreitroie90@gmail.com>
Lib/email/_header_value_parser.py
Lib/test/test_email/test__encoded_words.py
Lib/test/test_email/test__header_value_parser.py
Misc/NEWS.d/next/Library/2019-10-05-02-07-52.bpo-38332.hwrPN7.rst [new file with mode: 0644]

index 16c19907d68d5915c7fbc3fb7655f16f5b7d5bb0..1668b4a14e9b91c0359f854f171bf3294bb00f8c 100644 (file)
@@ -1057,7 +1057,7 @@ def get_encoded_word(value):
     value = ''.join(remainder)
     try:
         text, charset, lang, defects = _ew.decode('=?' + tok + '?=')
-    except ValueError:
+    except (ValueError, KeyError):
         raise _InvalidEwError(
             "encoded word format invalid: '{}'".format(ew.cte))
     ew.charset = charset
index 5a59aebba89be4c7f7fb09364bec7ac82a1807c1..0b8b1de3359aa6e68e04110e6b40de4dcd94f6a8 100644 (file)
@@ -58,6 +58,8 @@ class TestDecode(TestEmailBase):
             _ew.decode('=?')
         with self.assertRaises(ValueError):
             _ew.decode('')
+        with self.assertRaises(KeyError):
+            _ew.decode('=?utf-8?X?somevalue?=')
 
     def _test(self, source, result, charset='us-ascii', lang='', defects=[]):
         res, char, l, d = _ew.decode(source)
index dd33b065c804bc75e81972d809a48cf02aaaefc2..e442c44a2a74d0bdf0db6cd98207f0e31e66319d 100644 (file)
@@ -89,6 +89,10 @@ class TestParser(TestParserMixin, TestEmailBase):
         with self.assertRaises(errors.HeaderParseError):
             parser.get_encoded_word('=?abc?=')
 
+    def test_get_encoded_word_invalid_cte(self):
+        with self.assertRaises(errors.HeaderParseError):
+            parser.get_encoded_word('=?utf-8?X?somevalue?=')
+
     def test_get_encoded_word_valid_ew(self):
         self._test_get_x(parser.get_encoded_word,
                          '=?us-ascii?q?this_is_a_test?=  bird',
@@ -399,6 +403,14 @@ class TestParser(TestParserMixin, TestEmailBase):
             [],
             '')
 
+    def test_get_unstructured_invalid_ew_cte(self):
+        self._test_get_x(self._get_unst,
+            '=?utf-8?X?=somevalue?=',
+            '=?utf-8?X?=somevalue?=',
+            '=?utf-8?X?=somevalue?=',
+            [],
+            '')
+
     # get_qp_ctext
 
     def test_get_qp_ctext_only(self):
diff --git a/Misc/NEWS.d/next/Library/2019-10-05-02-07-52.bpo-38332.hwrPN7.rst b/Misc/NEWS.d/next/Library/2019-10-05-02-07-52.bpo-38332.hwrPN7.rst
new file mode 100644 (file)
index 0000000..600c702
--- /dev/null
@@ -0,0 +1,3 @@
+Prevent :exc:`KeyError` thrown by :func:`_encoded_words.decode` when given
+an encoded-word with invalid content-type encoding from propagating all the
+way to :func:`email.message.get`.