Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f74e512

Browse filesBrowse files
gh-86650: Fix IndexError when parse emails with invalid Message-ID (GH-117934)
In particularly, one-off addresses generated by Microsoft Outlook: https://learn.microsoft.com/en-us/office/client-developer/outlook/mapi/one-off-addresses Co-authored-by: fsc-eriker <72394365+fsc-eriker@users.noreply.github.com>
1 parent 8cc9adb commit f74e512
Copy full SHA for f74e512

File tree

Expand file treeCollapse file tree

3 files changed

+32
-0
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+32
-0
lines changed

‎Lib/email/_header_value_parser.py

Copy file name to clipboardExpand all lines: Lib/email/_header_value_parser.py
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,13 +1520,18 @@ def get_obs_local_part(value):
15201520
raise
15211521
token, value = get_cfws(value)
15221522
obs_local_part.append(token)
1523+
if not obs_local_part:
1524+
raise errors.HeaderParseError(
1525+
"expected obs-local-part but found '{}'".format(value))
15231526
if (obs_local_part[0].token_type == 'dot' or
15241527
obs_local_part[0].token_type=='cfws' and
1528+
len(obs_local_part) > 1 and
15251529
obs_local_part[1].token_type=='dot'):
15261530
obs_local_part.defects.append(errors.InvalidHeaderDefect(
15271531
"Invalid leading '.' in local part"))
15281532
if (obs_local_part[-1].token_type == 'dot' or
15291533
obs_local_part[-1].token_type=='cfws' and
1534+
len(obs_local_part) > 1 and
15301535
obs_local_part[-2].token_type=='dot'):
15311536
obs_local_part.defects.append(errors.InvalidHeaderDefect(
15321537
"Invalid trailing '.' in local part"))

‎Lib/test/test_email/test__header_value_parser.py

Copy file name to clipboardExpand all lines: Lib/test/test_email/test__header_value_parser.py
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2724,6 +2724,31 @@ def test_get_msg_id_no_angle_end(self):
27242724
)
27252725
self.assertEqual(msg_id.token_type, 'msg-id')
27262726

2727+
def test_get_msg_id_empty_id_left(self):
2728+
with self.assertRaises(errors.HeaderParseError):
2729+
parser.get_msg_id("<@domain>")
2730+
2731+
def test_get_msg_id_empty_id_right(self):
2732+
with self.assertRaises(errors.HeaderParseError):
2733+
parser.get_msg_id("<simplelocal@>")
2734+
2735+
def test_get_msg_id_with_brackets(self):
2736+
# Microsof Outlook generates non-standard one-off addresses:
2737+
# https://learn.microsoft.com/en-us/office/client-developer/outlook/mapi/one-off-addresses
2738+
with self.assertRaises(errors.HeaderParseError):
2739+
parser.get_msg_id("<[abrakadabra@microsoft.com]>")
2740+
2741+
def test_get_msg_id_ws_only_local(self):
2742+
msg_id = self._test_get_x(
2743+
parser.get_msg_id,
2744+
"< @domain>",
2745+
"< @domain>",
2746+
"< @domain>",
2747+
[errors.ObsoleteHeaderDefect],
2748+
""
2749+
)
2750+
self.assertEqual(msg_id.token_type, 'msg-id')
2751+
27272752

27282753

27292754
@parameterize
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix IndexError when parse some emails with invalid Message-ID (including
2+
one-off addresses generated by Microsoft Outlook).

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.