Skip to content

gh-105802: email._header_value_parser: prevent IndexError in get_obs_local_part #108133

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

Closed
wants to merge 7 commits into from
Closed
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
24 changes: 15 additions & 9 deletions Lib/email/_header_value_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1513,16 +1513,22 @@ def get_obs_local_part(value):
raise
token, value = get_cfws(value)
obs_local_part.append(token)
if (obs_local_part[0].token_type == 'dot' or
obs_local_part[0].token_type=='cfws' and
obs_local_part[1].token_type=='dot'):
if not obs_local_part:
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
obs_local_part[-2].token_type=='dot'):
obs_local_part.defects.append(errors.InvalidHeaderDefect(
"Invalid trailing '.' in local part"))
"abandoned parse; truncated value?"))
else:
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"))
if obs_local_part.defects:
obs_local_part.token_type = 'invalid-obs-local-part'
return obs_local_part, value
Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_email/test__header_value_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2588,6 +2588,22 @@ def test_get_msg_id_empty(self):
with self.assertRaises(errors.HeaderParseError):
parser.get_msg_id('')

def test_get_msg_id_square_brackets(self):
# gh-105802: test for broken Microsoft Message-Id with square brackets.
msg_id = self._test_get_x(
parser.get_msg_id,
'<[[email protected]]>',
'<', # sic
'<', # sic
Comment on lines +2596 to +2597
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do these "sic" mean?

# This also triggers
# ObsoleteHeaderDefect('obsolete id-left in msg-id')
# and InvalidHeaderDefect('msg-id with no id-right')
[errors.ObsoleteHeaderDefect, errors.InvalidHeaderDefect,
errors.InvalidHeaderDefect],
'[[email protected]]>',
)
self.assertEqual(msg_id.token_type,'msg-id')

def test_get_msg_id_valid(self):
msg_id = self._test_get_x(
parser.get_msg_id,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
In :mod:`email` library, avoid another IndexError in Message-Id parsing for
improper tokens; fixes <[[email protected]]> case