Skip to content

get_obs_local_part fails to handle empty local part #86650

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
dicksonch mannequin opened this issue Nov 27, 2020 · 4 comments
Closed

get_obs_local_part fails to handle empty local part #86650

dicksonch mannequin opened this issue Nov 27, 2020 · 4 comments
Labels
3.8 (EOL) end of life 3.9 only security fixes topic-email type-bug An unexpected behavior, bug, or error

Comments

@dicksonch
Copy link
Mannequin

dicksonch mannequin commented Nov 27, 2020

BPO 42484
Nosy @warsaw, @bitdancer, @ZackerySpytz, @dicksonch
PRs
  • gh-86650: get_obs_local_part() fails to handle empty local part #24669
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2020-11-27.12:36:59.942>
    labels = ['type-bug', '3.8', 'expert-email', '3.9']
    title = 'get_obs_local_part fails to handle empty local part'
    updated_at = <Date 2021-02-28.16:44:21.314>
    user = 'https://github.com/dicksonch'

    bugs.python.org fields:

    activity = <Date 2021-02-28.16:44:21.314>
    actor = 'ZackerySpytz'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['email']
    creation = <Date 2020-11-27.12:36:59.942>
    creator = 'dxn126'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 42484
    keywords = ['patch']
    message_count = 2.0
    messages = ['381947', '382153']
    nosy_count = 4.0
    nosy_names = ['barry', 'r.david.murray', 'ZackerySpytz', 'dxn126']
    pr_nums = ['24669']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue42484'
    versions = ['Python 3.8', 'Python 3.9']

    Linked PRs

    @dicksonch
    Copy link
    Mannequin Author

    dicksonch mannequin commented Nov 27, 2020

    parse_message_id in the email module crashes with bogus message-id

    Having a Message-ID '<[>' gives me an IndexError: list index out of range

    This happens when

    • creating an EmailMessage with the said Message-ID
      msg = EmailMessage()
      msg['Message-ID'] = '<[>'

    • accessing the bogus Message-ID through
      msg.items()
      or
      msg.get('Message-ID')

    this doesn't happen with python 3.6 or 3.7 when MessageIDHeader didn't exist

    3.8/Lib/email/headerregistry.py line 542

    _default_header_map = {
        ....
        'message-id': MessageIDHeader,
        }

    Traceback (most recent call last):
      File "/usr/lib/python3.8/email/_header_value_parser.py", line 2069, in get_msg_id
        token, value = get_dot_atom_text(value)
      File "/usr/lib/python3.8/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 "main.py", line 4, in <module>
        msg['Message-ID'] = '<[>'
      File "/usr/lib/python3.8/email/message.py", line 409, in __setitem__
        self._headers.append(self.policy.header_store_parse(name, val))
      File "/usr/lib/python3.8/email/policy.py", line 148, in header_store_parse
        return (name, self.header_factory(name, value))
      File "/usr/lib/python3.8/email/headerregistry.py", line 607, in __call__
        return self[name](name, value)
      File "/usr/lib/python3.8/email/headerregistry.py", line 202, in __new__
        cls.parse(value, kwds)
      File "/usr/lib/python3.8/email/headerregistry.py", line 535, in parse
        kwds['parse_tree'] = parse_tree = cls.value_parser(value)
      File "/usr/lib/python3.8/email/_header_value_parser.py", line 2126, in parse_message_id
        token, value = get_msg_id(value)
      File "/usr/lib/python3.8/email/_header_value_parser.py", line 2073, in get_msg_id
        token, value = get_obs_local_part(value)
      File "/usr/lib/python3.8/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

    as you can see in the traceback
    get_msg_id() calls get_obs_local_part()
    and in get_obs_local_part(), you have this

    def get_obs_local_part(value):
    
        obs_local_part = ObsLocalPart()
    
        while value and (value[0]=='\\' or value[0] not in PHRASE_ENDS):
            ...
        if (obs_local_part[0].token_type == 'dot':
            ...

    if value does not satisfy the condition in the while loop,
    this gives an IndexError as obs_local_part is empty
    (the value in my example is '[>' from the message id '<[>')

    shouldn't we have a proper Error or default back to no parsing if parsing fails?
    There's no way of bypassing the parser and getting the Message-ID and
    I can't even handle the error with a try catch

    @dicksonch dicksonch mannequin added 3.8 (EOL) end of life 3.9 only security fixes topic-email type-bug An unexpected behavior, bug, or error labels Nov 27, 2020
    @bitdancer
    Copy link
    Member

    Yep, you've found another in a category of bugs that have shown up in the parser: places where there is a missing check for there being any value at all before checking character [0].

    In this case, the fix should be to add

        if not obs_local_part:
            return obs_local_part, value

    just before the if that is blowing up.

    @bitdancer bitdancer changed the title parse_message_id, get_msg_id, get_obs_local_part is poorly written get_obs_local_part fails to handle empty local part Nov 30, 2020
    @bitdancer bitdancer changed the title parse_message_id, get_msg_id, get_obs_local_part is poorly written get_obs_local_part fails to handle empty local part Nov 30, 2020
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @lesleyxyz
    Copy link

    lesleyxyz commented Feb 27, 2023

    I am currently experiencing this issue, has this been looked at yet? Is there a workaround?

    @fsc-eriker
    Copy link
    Contributor

    Duplicate of #105802

    serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Apr 16, 2024
    miss-islington pushed a commit to miss-islington/cpython that referenced this issue Apr 17, 2024
    …-ID (pythonGH-117934)
    
    In particularly, one-off addresses generated by Microsoft Outlook:
    https://learn.microsoft.com/en-us/office/client-developer/outlook/mapi/one-off-addresses
    
    (cherry picked from commit f74e512)
    
    Co-authored-by: Serhiy Storchaka <[email protected]>
    Co-authored-by: fsc-eriker <[email protected]>
    serhiy-storchaka added a commit that referenced this issue Apr 17, 2024
    …e-ID (GH-117934) (GH-117965)
    
    In particularly, one-off addresses generated by Microsoft Outlook:
    https://learn.microsoft.com/en-us/office/client-developer/outlook/mapi/one-off-addresses
    
    (cherry picked from commit f74e512)
    
    Co-authored-by: Serhiy Storchaka <[email protected]>
    Co-authored-by: fsc-eriker <[email protected]>
    diegorusso pushed a commit to diegorusso/cpython that referenced this issue Apr 17, 2024
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 (EOL) end of life 3.9 only security fixes topic-email type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants