Skip to content

Commit

Permalink
Update for pythonGH-104802: register a defect instead of raising an e…
Browse files Browse the repository at this point in the history
…rror

Based on PR review
  • Loading branch information
fsc-eriker committed Feb 1, 2024
1 parent bf6d7b5 commit a614ff1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
17 changes: 9 additions & 8 deletions Lib/email/_header_value_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1514,18 +1514,19 @@ def get_obs_local_part(value):
token, value = get_cfws(value)
obs_local_part.append(token)
if not obs_local_part:
raise(errors.HeaderParseError(
"abandoning parse; truncated value? ({})".format(value)))
if (obs_local_part[0].token_type == 'dot' or
obs_local_part.defects.append(errors.InvalidHeaderDefect(
"abandoned parse; truncated value?"))
else:
if (obs_local_part[0].token_type == 'dot' or
obs_local_part[0].token_type=='cfws' 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.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"))
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
19 changes: 15 additions & 4 deletions Lib/test/test_email/test__header_value_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2588,10 +2588,21 @@ def test_get_msg_id_empty(self):
with self.assertRaises(errors.HeaderParseError):
parser.get_msg_id('')

def test_get_msg_id_botched(self):
# gh-105802: ditto for broken Microsoft Message-Id
with self.assertRaises(errors.HeaderParseError):
parser.get_msg_id('<[83c48dddbea7492e873224a5ae1c04be-JFBVALKQOJXWILKNK4YVA7CBPJ2XEZKEMV3E64DTPRCW2YLJNR6EK6DPKNWXI4A=@microsoft.com]>')
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
# 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(
Expand Down

0 comments on commit a614ff1

Please sign in to comment.