This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: MessageID parser raises IndexError: string index out of range
Type: behavior Stage:
Components: email Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: barry, bpoaugust, r.david.murray
Priority: normal Keywords:

Created on 2022-01-19 17:01 by bpoaugust, last changed 2022-04-11 14:59 by admin.

Messages (1)
msg410972 - (view) Author: (bpoaugust) Date: 2022-01-19 17:01
The Message-ID parser can crash on truncated input.

For example:

import email.policy
message=email.message_from_string("Message-id: <A@", policy=email.policy.default)
message['Message-id']

results in:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/email/_header_value_parser.py", line 2095, in get_msg_id
    token, value = get_dot_atom_text(value)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/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 ''

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/email/_header_value_parser.py", line 2098, in get_msg_id
    token, value = get_no_fold_literal(value)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/email/_header_value_parser.py", line 2035, in get_no_fold_literal
    raise errors.HeaderParseError(
email.errors.HeaderParseError: expected no-fold-literal but found ''

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/private/var/sebb/git/pmfoal/local/testm1.py", line 23, in <module>
    message['Message-id']
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/email/message.py", line 391, in __getitem__
    return self.get(name)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/email/message.py", line 471, in get
    return self.policy.header_fetch_parse(k, v)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/email/policy.py", line 163, in header_fetch_parse
    return self.header_factory(name, value)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/email/headerregistry.py", line 604, in __call__
    return self[name](name, value)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/email/headerregistry.py", line 192, in __new__
    cls.parse(value, kwds)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/email/headerregistry.py", line 532, in parse
    kwds['parse_tree'] = parse_tree = cls.value_parser(value)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/email/_header_value_parser.py", line 2126, in parse_message_id
    token, value = get_msg_id(value)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/email/_header_value_parser.py", line 2101, in get_msg_id
    token, value = get_domain(value)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/email/_header_value_parser.py", line 1604, in get_domain
    if value[0] in CFWS_LEADER:
IndexError: string index out of range
History
Date User Action Args
2022-04-11 14:59:54adminsetgithub: 90593
2022-01-23 19:28:19iritkatrielsettitle: MessageID parser can crash with IndexError: string index out of range -> MessageID parser raises IndexError: string index out of range
type: crash -> behavior
versions: + Python 3.11
2022-01-19 17:01:36bpoaugustcreate