Skip to content

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

Closed

Description

@mtorromeo
mannequin
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']

    Activity

    1. mtorromeo commented on Nov 30, 2017

      mtorromeomannequin
      MannequinAuthor

      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>

    2. added
      type-bug
      on Nov 30, 2017
    3. mtorromeo commented on Nov 30, 2017

      mtorromeomannequin
      MannequinAuthor

      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: ""

    4. mtorromeo commented on Nov 30, 2017

      mtorromeomannequin
      MannequinAuthor

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

    5. maxking commented on Jun 14, 2019

      @maxking
      Contributor

      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.

    6. csabella commented on Jul 3, 2019

      @csabella
      Contributor

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

    7. bitdancer commented on Jul 10, 2019

      @bitdancer
      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
    8. miss-islington commented on Aug 11, 2019

      @miss-islington
      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

    9. miss-islington commented on Aug 11, 2019

      @miss-islington
      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

    10. miss-islington commented on Aug 11, 2019

      @miss-islington
      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

    11. added
      3.7 (EOL)
      3.8 (EOL)
      3.9 (EOL)
      on Aug 11, 2019
    12. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.7 (EOL)3.8 (EOL)3.9 (EOL)topic-emailtype-bug

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions