Skip to content

[3.12] gh-86650: Fix IndexError when parse emails with invalid Message-ID (GH-117934) #117965

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Lib/email/_header_value_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1514,13 +1514,18 @@ def get_obs_local_part(value):
raise
token, value = get_cfws(value)
obs_local_part.append(token)
if not obs_local_part:
raise errors.HeaderParseError(
"expected obs-local-part but found '{}'".format(value))
if (obs_local_part[0].token_type == 'dot' or
obs_local_part[0].token_type=='cfws' and
len(obs_local_part) > 1 and
obs_local_part[1].token_type=='dot'):
obs_local_part.defects.append(errors.InvalidHeaderDefect(
"Invalid leading '.' in local part"))
if (obs_local_part[-1].token_type == 'dot' or
obs_local_part[-1].token_type=='cfws' and
len(obs_local_part) > 1 and
obs_local_part[-2].token_type=='dot'):
obs_local_part.defects.append(errors.InvalidHeaderDefect(
"Invalid trailing '.' in local part"))
Expand Down
25 changes: 25 additions & 0 deletions Lib/test/test_email/test__header_value_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2698,6 +2698,31 @@ def test_get_msg_id_no_angle_end(self):
)
self.assertEqual(msg_id.token_type, 'msg-id')

def test_get_msg_id_empty_id_left(self):
with self.assertRaises(errors.HeaderParseError):
parser.get_msg_id("<@domain>")

def test_get_msg_id_empty_id_right(self):
with self.assertRaises(errors.HeaderParseError):
parser.get_msg_id("<simplelocal@>")

def test_get_msg_id_with_brackets(self):
# Microsof Outlook generates non-standard one-off addresses:
# https://learn.microsoft.com/en-us/office/client-developer/outlook/mapi/one-off-addresses
with self.assertRaises(errors.HeaderParseError):
parser.get_msg_id("<[abrakadabra@microsoft.com]>")

def test_get_msg_id_ws_only_local(self):
msg_id = self._test_get_x(
parser.get_msg_id,
"< @domain>",
"< @domain>",
"< @domain>",
[errors.ObsoleteHeaderDefect],
""
)
self.assertEqual(msg_id.token_type, 'msg-id')



@parameterize
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix IndexError when parse some emails with invalid Message-ID (including
one-off addresses generated by Microsoft Outlook).
Loading