Skip to content
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

Some invalid email address groups cause an IndexError instead of a HeaderParseError #76359

Closed
mtorromeo mannequin opened this issue Nov 30, 2017 · 9 comments
Closed

Some invalid email address groups cause an IndexError instead of a HeaderParseError #76359

mtorromeo mannequin opened this issue Nov 30, 2017 · 9 comments
Labels
3.7 (EOL) end of life 3.8 (EOL) end of life 3.9 only security fixes topic-email type-bug An unexpected behavior, bug, or error

Comments

@mtorromeo
Copy link
Mannequin

mtorromeo mannequin commented Nov 30, 2017

BPO 32178
Nosy @warsaw, @bitdancer, @maxking, @csabella, @mtorromeo, @miss-islington
PRs
  • bpo-32178: Fix IndexError on invalid email group header parsing #4642
  • bpo-32178: Fix IndexError trying to parse 'To' header starting with ':'. #15044
  • [3.8] bpo-32178: Fix IndexError trying to parse 'To' header starting with ':'. (GH-15044) #15213
  • [3.7] bpo-32178: Fix IndexError trying to parse 'To' header starting with ':'. (GH-15044) #15214
  • 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 = <Date 2019-08-11.21:09:42.939>
    created_at = <Date 2017-11-30.10:40:34.903>
    labels = ['3.8', 'type-bug', '3.7', 'expert-email', '3.9']
    title = 'Some invalid email address groups cause an IndexError instead of a HeaderParseError'
    updated_at = <Date 2019-08-11.21:09:42.931>
    user = 'https://github.com/mtorromeo'

    bugs.python.org fields:

    activity = <Date 2019-08-11.21:09:42.931>
    actor = 'maxking'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-08-11.21:09:42.939>
    closer = 'maxking'
    components = ['email']
    creation = <Date 2017-11-30.10:40:34.903>
    creator = 'mtorromeo'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 32178
    keywords = ['patch']
    message_count = 9.0
    messages = ['307285', '307291', '307293', '345627', '347248', '347585', '349407', '349408', '349409']
    nosy_count = 6.0
    nosy_names = ['barry', 'r.david.murray', 'maxking', 'cheryl.sabella', 'mtorromeo', 'miss-islington']
    pr_nums = ['4642', '15044', '15213', '15214']
    priority = 'normal'
    resolution = None
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue32178'
    versions = ['Python 3.7', 'Python 3.8', 'Python 3.9']

    @mtorromeo
    Copy link
    Mannequin Author

    mtorromeo mannequin commented Nov 30, 2017

    With some malformed email address list, the parser for email groups raises an IndexError instead of the correct HeaderParseError.

    This results in a complete failure to parse the email while it is still preferable to just ignore the malformed header.

    An example of such a malformed list is this:
    To: :Foo <foo@example.com> <bar@example.com>

    @mtorromeo mtorromeo mannequin added topic-email type-bug An unexpected behavior, bug, or error labels Nov 30, 2017
    @mtorromeo
    Copy link
    Mannequin Author

    mtorromeo mannequin commented Nov 30, 2017

    In case an address email header contains and empty string, the tokenizer return a BareQuotedString
    which is also a TokenList, but this list is empty and the parser fails to check this and insteads raises an IndexError.

    For example an email with this header will trigger the IndexError:
    ReplyTo: ""

    @mtorromeo
    Copy link
    Mannequin Author

    mtorromeo mannequin commented Nov 30, 2017

    Disregard my last message, I posted it in the wrong bug report by mistake.
    Sorry about the noise

    @maxking
    Copy link
    Contributor

    maxking commented Jun 14, 2019

    I don't think this is an issue anymore, I guess this was fixed as a part of some other PR.

    I tested this out on a recent branch:

       >>> import email
       >>> msg = email.message_from_string('From: Abhilash <maxking@email.com> <Another@some.com>')
       >>> msg['From'] 
       'Abhilash <maxking@email.com> <Another@some.com>'

    @david, @barry: I think we can close this issue.

    @csabella
    Copy link
    Contributor

    csabella commented Jul 3, 2019

    Based on @maxking's comment, I'm going to close this issue. Thank you!

    @csabella csabella closed this as completed Jul 3, 2019
    @bitdancer
    Copy link
    Member

    The fact that the original report mentions HeaderParserError implies that the new API is being used, though the report didn't make that clear. The problem still exists:

    >>> m = message_from_string("To: :Foo <foo@example.com> <bar@example.com>\n\n", policy=default)
    >>> m['To']
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/home/rdmurray/python/p38/Lib/email/message.py", line 391, in __getitem__
        return self.get(name)
      File "/home/rdmurray/python/p38/Lib/email/message.py", line 471, in get
        return self.policy.header_fetch_parse(k, v)
      File "/home/rdmurray/python/p38/Lib/email/policy.py", line 163, in header_fetch_parse
        return self.header_factory(name, value)
      File "/home/rdmurray/python/p38/Lib/email/headerregistry.py", line 602, in __call__
        return self[name](name, value)
      File "/home/rdmurray/python/p38/Lib/email/headerregistry.py", line 197, in __new__
        cls.parse(value, kwds)
      File "/home/rdmurray/python/p38/Lib/email/headerregistry.py", line 343, in parse
        groups.append(Group(addr.display_name,
      File "/home/rdmurray/python/p38/Lib/email/_header_value_parser.py", line 315, in display_name
        return self[0].display_name
      File "/home/rdmurray/python/p38/Lib/email/_header_value_parser.py", line 382, in display_name
        return self[0].display_name
      File "/home/rdmurray/python/p38/Lib/email/_header_value_parser.py", line 564, in display_name
        if res[0].token_type == 'cfws':
    IndexError: list index out of range

    @bitdancer bitdancer reopened this Jul 10, 2019
    @miss-islington
    Copy link
    Contributor

    New changeset 09a1872 by Miss Islington (bot) (Abhilash Raj) in branch 'master':
    bpo-32178: Fix IndexError trying to parse 'To' header starting with ':'. (GH-15044)
    09a1872

    @miss-islington
    Copy link
    Contributor

    New changeset 9500bbe by Miss Islington (bot) in branch '3.8':
    bpo-32178: Fix IndexError trying to parse 'To' header starting with ':'. (GH-15044)
    9500bbe

    @miss-islington
    Copy link
    Contributor

    New changeset dec231a by Miss Islington (bot) in branch '3.7':
    bpo-32178: Fix IndexError trying to parse 'To' header starting with ':'. (GH-15044)
    dec231a

    @maxking maxking added 3.7 (EOL) end of life 3.8 (EOL) end of life 3.9 only security fixes labels Aug 11, 2019
    @maxking maxking closed this as completed Aug 11, 2019
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life 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