classification
Title: Some invalid email address groups cause an IndexError instead of a HeaderParseError
Type: behavior Stage: resolved
Components: email Versions: Python 3.9, Python 3.8, Python 3.7
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: barry, cheryl.sabella, maxking, miss-islington, mtorromeo, r.david.murray
Priority: normal Keywords: patch

Created on 2017-11-30 10:40 by mtorromeo, last changed 2019-08-11 21:09 by maxking. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 4642 closed mtorromeo, 2017-11-30 10:40
PR 15044 merged maxking, 2019-07-31 05:48
PR 15213 merged miss-islington, 2019-08-11 20:45
PR 15214 merged miss-islington, 2019-08-11 20:45
Messages (9)
msg307285 - (view) Author: Massimiliano Torromeo (mtorromeo) * Date: 2017-11-30 10:40
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>`
msg307291 - (view) Author: Massimiliano Torromeo (mtorromeo) * Date: 2017-11-30 11:25
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: ""
msg307293 - (view) Author: Massimiliano Torromeo (mtorromeo) * Date: 2017-11-30 11:29
Disregard my last message, I posted it in the wrong bug report by mistake.
Sorry about the noise
msg345627 - (view) Author: Abhilash Raj (maxking) * (Python committer) Date: 2019-06-14 18:42
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.
msg347248 - (view) Author: Cheryl Sabella (cheryl.sabella) * (Python committer) Date: 2019-07-03 23:49
Based on @maxking's comment, I'm going to close this issue.  Thank you!
msg347585 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2019-07-10 00:57
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
msg349407 - (view) Author: miss-islington (miss-islington) Date: 2019-08-11 20:45
New changeset 09a1872a8007048dcdf825a476816c5e3498b8f8 by Miss Islington (bot) (Abhilash Raj) in branch 'master':
bpo-32178: Fix IndexError trying to parse 'To' header starting with ':'. (GH-15044)
https://github.com/python/cpython/commit/09a1872a8007048dcdf825a476816c5e3498b8f8
msg349408 - (view) Author: miss-islington (miss-islington) Date: 2019-08-11 21:04
New changeset 9500bbe9372f6080decc49d2fd9365f0b927a0e2 by Miss Islington (bot) in branch '3.8':
bpo-32178: Fix IndexError trying to parse 'To' header starting with ':'. (GH-15044)
https://github.com/python/cpython/commit/9500bbe9372f6080decc49d2fd9365f0b927a0e2
msg349409 - (view) Author: miss-islington (miss-islington) Date: 2019-08-11 21:05
New changeset dec231a73c2a463b29f19c4e8357602c10a68856 by Miss Islington (bot) in branch '3.7':
bpo-32178: Fix IndexError trying to parse 'To' header starting with ':'. (GH-15044)
https://github.com/python/cpython/commit/dec231a73c2a463b29f19c4e8357602c10a68856
History
Date User Action Args
2019-08-11 21:09:42maxkingsetstatus: open -> closed
stage: patch review -> resolved
versions: + Python 3.7, Python 3.8, Python 3.9, - Python 3.4, Python 3.5, Python 3.6
2019-08-11 21:05:40miss-islingtonsetmessages: + msg349409
2019-08-11 21:04:34miss-islingtonsetmessages: + msg349408
2019-08-11 20:45:44miss-islingtonsetpull_requests: + pull_request14942
2019-08-11 20:45:37miss-islingtonsetpull_requests: + pull_request14941
2019-08-11 20:45:15miss-islingtonsetnosy: + miss-islington
messages: + msg349407
2019-07-31 05:48:07maxkingsetstage: resolved -> patch review
pull_requests: + pull_request14799
2019-07-10 00:57:54r.david.murraysetstatus: closed -> open
resolution: out of date ->
messages: + msg347585
2019-07-03 23:49:38cheryl.sabellasetstatus: open -> closed

nosy: + cheryl.sabella
messages: + msg347248

resolution: out of date
stage: patch review -> resolved
2019-06-14 18:42:22maxkingsetnosy: + maxking
messages: + msg345627
2017-11-30 11:29:01mtorromeosetmessages: + msg307293
2017-11-30 11:28:17mtorromeosetpull_requests: - pull_request4558
2017-11-30 11:25:27mtorromeosetkeywords: + patch

stage: patch review
messages: + msg307291
pull_requests: + pull_request4558
2017-11-30 10:40:34mtorromeocreate