]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-38708: email: Fix a potential IndexError when parsing Message-ID (GH-17504)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 9 Dec 2019 02:12:50 +0000 (18:12 -0800)
committerGitHub <noreply@github.com>
Mon, 9 Dec 2019 02:12:50 +0000 (18:12 -0800)
Fix a potential IndexError when passing an empty value to the message-id
parser. Instead, HeaderParseError should be raised.
(cherry picked from commit 3ae4ea1931361dd2743e464790e739d9285501bf)

Co-authored-by: Abhilash Raj <maxking@users.noreply.github.com>
Lib/email/_header_value_parser.py
Lib/test/test_email/test__header_value_parser.py
Misc/NEWS.d/next/Library/2019-12-07-22-25-39.bpo-38708.rZTUfk.rst [new file with mode: 0644]

index cb013225ec60cb3208dd0d6d146a5f203a032128..9c55ef7fb453bee0b1069e019c4dbd482f1bc6da 100644 (file)
@@ -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] != '<':
index 7c9f9877d7be2a598ce50ceefbc247038723de55..4b5b44258b228ff3edce83b52e01b3ca2aa9dc17 100644 (file)
@@ -2583,6 +2583,11 @@ class TestParser(TestParserMixin, TestEmailBase):
 
     # 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 @@ class TestParser(TestParserMixin, TestEmailBase):
         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 (file)
index 0000000..23a0a46
--- /dev/null
@@ -0,0 +1 @@
+Fix a potential IndexError in email parser when parsing an empty msg-id.