summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorAndrei Troie <andreitroie90@gmail.com>2019-10-05 16:19:15 (GMT)
committerAbhilash Raj <maxking@users.noreply.github.com>2019-10-05 16:19:15 (GMT)
commit65dcc8a8dc41d3453fd6b987073a5f1b30c5c0fd (patch)
tree95dc8edc797c00bcc5a8020e63268fbc0009975b /Lib
parent3faf826e5879536d2272f1a51c58965a16827f81 (diff)
downloadcpython-65dcc8a8dc41d3453fd6b987073a5f1b30c5c0fd.zip
cpython-65dcc8a8dc41d3453fd6b987073a5f1b30c5c0fd.tar.gz
cpython-65dcc8a8dc41d3453fd6b987073a5f1b30c5c0fd.tar.bz2
bpo-38332: Catch KeyError from unknown cte in encoded-word. (GH-16503)
KeyError should cause a failure in parsing the encoded word and should be caught and raised as a _InvalidEWError instead.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/email/_header_value_parser.py2
-rw-r--r--Lib/test/test_email/test__encoded_words.py2
-rw-r--r--Lib/test/test_email/test__header_value_parser.py12
3 files changed, 15 insertions, 1 deletions
diff --git a/Lib/email/_header_value_parser.py b/Lib/email/_header_value_parser.py
index 16c1990..1668b4a 100644
--- a/Lib/email/_header_value_parser.py
+++ b/Lib/email/_header_value_parser.py
@@ -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
diff --git a/Lib/test/test_email/test__encoded_words.py b/Lib/test/test_email/test__encoded_words.py
index 5a59aeb..0b8b1de 100644
--- a/Lib/test/test_email/test__encoded_words.py
+++ b/Lib/test/test_email/test__encoded_words.py
@@ -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)
diff --git a/Lib/test/test_email/test__header_value_parser.py b/Lib/test/test_email/test__header_value_parser.py
index 058d902..46d90b3 100644
--- a/Lib/test/test_email/test__header_value_parser.py
+++ b/Lib/test/test_email/test__header_value_parser.py
@@ -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):