[Python-checkins] bpo-38708: email: Fix a potential IndexError when parsing Message-ID (GH-17504)
Abhilash Raj
webhook-mailer at python.org
Sun Dec 8 20:37:39 EST 2019
https://github.com/python/cpython/commit/3ae4ea1931361dd2743e464790e739d9285501bf
commit: 3ae4ea1931361dd2743e464790e739d9285501bf
branch: master
author: Abhilash Raj <maxking at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2019年12月08日T17:37:34-08:00
summary:
bpo-38708: email: Fix a potential IndexError when parsing Message-ID (GH-17504)
Fix a potential IndexError when passing an empty value to the message-id
parser. Instead, HeaderParseError should be raised.
files:
A Misc/NEWS.d/next/Library/2019-12-07-22-25-39.bpo-38708.rZTUfk.rst
M Lib/email/_header_value_parser.py
M Lib/test/test_email/test__header_value_parser.py
diff --git a/Lib/email/_header_value_parser.py b/Lib/email/_header_value_parser.py
index cb013225ec60c..9c55ef7fb453b 100644
--- a/Lib/email/_header_value_parser.py
+++ b/Lib/email/_header_value_parser.py
@@ -2047,7 +2047,7 @@ def get_msg_id(value):
no-fold-literal = "[" *dtext "]"
"""
msg_id = MsgID()
- if value[0] in CFWS_LEADER:
+ if value and value[0] in CFWS_LEADER:
token, value = get_cfws(value)
msg_id.append(token)
if not value or value[0] != '<':
diff --git a/Lib/test/test_email/test__header_value_parser.py b/Lib/test/test_email/test__header_value_parser.py
index d59d70117b810..1bdcfa129b4c8 100644
--- a/Lib/test/test_email/test__header_value_parser.py
+++ b/Lib/test/test_email/test__header_value_parser.py
@@ -2583,6 +2583,11 @@ def test_invalid_content_transfer_encoding(self):
# get_msg_id
+ def test_get_msg_id_empty(self):
+ # bpo-38708: Test that HeaderParseError is raised and not IndexError.
+ with self.assertRaises(errors.HeaderParseError):
+ parser.get_msg_id('')
+
def test_get_msg_id_valid(self):
msg_id = self._test_get_x(
parser.get_msg_id,
@@ -2694,6 +2699,7 @@ def test_get_msg_id_no_angle_end(self):
self.assertEqual(msg_id.token_type, 'msg-id')
+
@parameterize
class Test_parse_mime_parameters(TestParserMixin, TestEmailBase):
diff --git a/Misc/NEWS.d/next/Library/2019-12-07-22-25-39.bpo-38708.rZTUfk.rst b/Misc/NEWS.d/next/Library/2019-12-07-22-25-39.bpo-38708.rZTUfk.rst
new file mode 100644
index 0000000000000..23a0a46d1fea1
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-12-07-22-25-39.bpo-38708.rZTUfk.rst
@@ -0,0 +1 @@
+Fix a potential IndexError in email parser when parsing an empty msg-id.
More information about the Python-checkins
mailing list