Skip to content

Exception while handling exception in email.msg.get('message-id') #94306

Closed
@DRSpalding

Description

@DRSpalding

Possibly malformed Message-ID tag in header causes a raised exception but gets a second exception trying to deal with the defect and terminates. The Message-ID tag in question was received in the real-world.

Windows 10 up to date, Python 64-bit 3.10.5.

Code in bad-email-hdr.py:

#! python3

import email
from email import policy

msg_id = 'Message-ID'
b_msg_hdr_bad2 = b'Message-ID: <[anu10].1c69fb81.7aec0.5503SMTPIN_ADDED_[au5]@[anu13].EURPRD10.PROD.OUTLOOK.COM>\r\n\r\n'

msg2 = email.message_from_bytes(b_msg_hdr_bad2, policy=policy.default)

print(f'bad header {msg_id} = {msg2.get(msg_id)}')
print(f'msg bytes = {msg2.as_bytes()}')

Traceback:
c:\tmp\PyTest> bad-email-hdr.py
Traceback (most recent call last):
File "C:\Program Files\Python310\lib\email_header_value_parser.py", line 2069, in get_msg_id
token, value = get_dot_atom_text(value)
File "C:\Program Files\Python310\lib\email_header_value_parser.py", line 1334, in get_dot_atom_text
raise errors.HeaderParseError("expected atom at a start of "
email.errors.HeaderParseError: expected atom at a start of dot-atom-text but found '[anu10].1c69fb81.7aec0.5503SMTPIN_ADDED_[au5]@[anu13].EURPRD10.PROD.OUTLOOK.COM>'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\tmp\PyTest\bad-email-hdr.py", line 11, in
print(f'bad header {msg_id} = {msg2.get(msg_id)}')
File "C:\Program Files\Python310\lib\email\message.py", line 471, in get
return self.policy.header_fetch_parse(k, v)
File "C:\Program Files\Python310\lib\email\policy.py", line 163, in header_fetch_parse
return self.header_factory(name, value)
File "C:\Program Files\Python310\lib\email\headerregistry.py", line 604, in call
return self[name](name, value)
File "C:\Program Files\Python310\lib\email\headerregistry.py", line 192, in new
cls.parse(value, kwds)
File "C:\Program Files\Python310\lib\email\headerregistry.py", line 532, in parse
kwds['parse_tree'] = parse_tree = cls.value_parser(value)
File "C:\Program Files\Python310\lib\email_header_value_parser.py", line 2126, in parse_message_id
token, value = get_msg_id(value)
File "C:\Program Files\Python310\lib\email_header_value_parser.py", line 2073, in get_msg_id
token, value = get_obs_local_part(value)
File "C:\Program Files\Python310\lib\email_header_value_parser.py", line 1516, in get_obs_local_part
if (obs_local_part[0].token_type == 'dot' or
IndexError: list index out of range

Also seen on 3.10.0.
Windows 10 x64, up to date.

Tried Ubuntu and Python 3.8.10 and got same result as on Windows 3.10.5 version.

Tried Debian (Raspbian) 32-bit Python 3.7.3 and it appears that it encodes the Message-ID header into a =?utf-8?q? format, possibly as expected to deal with the characters tripping it up.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions