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: Date parsing helpers in email module incorrectly raise IndexError for some malformed inputs
Type: crash Stage: resolved
Components: email Versions: Python 3.11, Python 3.10, Python 3.9, Python 3.8, Python 3.7, Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: barry, lukasz.langa, miss-islington, ned.deily, r.david.murray, wbolster
Priority: normal Keywords: patch

Created on 2021-08-25 13:23 by wbolster, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 27946 merged wbolster, 2021-08-25 13:27
PR 27972 merged miss-islington, 2021-08-26 14:49
PR 27973 merged miss-islington, 2021-08-26 14:49
PR 27974 merged miss-islington, 2021-08-26 14:49
PR 27975 merged miss-islington, 2021-08-26 14:49
PR 27976 merged miss-islington, 2021-08-26 14:49
Messages (9)
msg400261 - (view) Author: wouter bolsterlee (wbolster) * Date: 2021-08-25 13:23
Various date parsing utilities in the email module, such as email.utils.parsedate(), are supposed to gracefully handle invalid input, typically by raising an appropriate exception or by returning None.

The internal email._parseaddr._parsedate_tz() helper used by some of these date parsing routines tries to be robust against malformed input, but unfortunately it can still crash ungracefully when a non-empty but whitespace-only input is passed. This manifests as an unexpected IndexError.

In practice, this can happen when parsing an email with only a newline inside a ‘Date:’ header, which unfortunately happens occasionally in the real world.

Here's a minimal example:

$ python
Python 3.9.6 (default, Jun 30 2021, 10:22:16) 
[GCC 11.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import email.utils
>>> email.utils.parsedate('foo')
>>> email.utils.parsedate(' ')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.9/email/_parseaddr.py", line 176, in parsedate
    t = parsedate_tz(data)
  File "/usr/lib/python3.9/email/_parseaddr.py", line 50, in parsedate_tz
    res = _parsedate_tz(data)
  File "/usr/lib/python3.9/email/_parseaddr.py", line 72, in _parsedate_tz
    if data[0].endswith(',') or data[0].lower() in _daynames:
IndexError: list index out of range


The fix is rather straight-forward; will open a pull request shortly.
msg400262 - (view) Author: wouter bolsterlee (wbolster) * Date: 2021-08-25 13:27
pull request with fix at https://github.com/python/cpython/pull/27946
msg400351 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-08-26 14:49
New changeset 989f6a3800f06b2bd31cfef7c3269a443ad94fac by wouter bolsterlee in branch 'main':
bpo-45001: Make email date parsing more robust against malformed input (GH-27946)
https://github.com/python/cpython/commit/989f6a3800f06b2bd31cfef7c3269a443ad94fac
msg400356 - (view) Author: miss-islington (miss-islington) Date: 2021-08-26 15:47
New changeset 9a79242567d79f42ad1a953cce2b1c4a94df23ea by Miss Islington (bot) in branch '3.10':
bpo-45001: Make email date parsing more robust against malformed input (GH-27946)
https://github.com/python/cpython/commit/9a79242567d79f42ad1a953cce2b1c4a94df23ea
msg400357 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-08-26 15:48
New changeset 2cdbd3b8b2bb4fa0dbcc04ce305fbafaeedd9e67 by Miss Islington (bot) in branch '3.9':
bpo-45001: Make email date parsing more robust against malformed input (GH-27946) (GH-27973)
https://github.com/python/cpython/commit/2cdbd3b8b2bb4fa0dbcc04ce305fbafaeedd9e67
msg400358 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-08-26 15:49
New changeset 81148c6f91092c3aa207a53b657b2548a20b230c by Miss Islington (bot) in branch '3.8':
bpo-45001: Make email date parsing more robust against malformed input (GH-27946) (GH-27974)
https://github.com/python/cpython/commit/81148c6f91092c3aa207a53b657b2548a20b230c
msg400655 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2021-08-30 18:48
New changeset e9b85afd7dc004460f6d914375ab67d617a8a7ff by Miss Islington (bot) in branch '3.7':
bpo-45001: Make email date parsing more robust against malformed input (GH-27946) (GH-27975)
https://github.com/python/cpython/commit/e9b85afd7dc004460f6d914375ab67d617a8a7ff
msg400657 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2021-08-30 18:57
New changeset da9d6c554697414b1d275c8502e00a07c2ce06e6 by Miss Islington (bot) in branch '3.6':
bpo-45001: Make email date parsing more robust against malformed input (GH-27946) (GH-27976)
https://github.com/python/cpython/commit/da9d6c554697414b1d275c8502e00a07c2ce06e6
msg400658 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2021-08-30 18:58
Thanks for the PR!
History
Date User Action Args
2022-04-11 14:59:49adminsetgithub: 89164
2021-08-30 18:58:53ned.deilysetstatus: open -> closed
resolution: fixed
messages: + msg400658

stage: patch review -> resolved
2021-08-30 18:57:27ned.deilysetmessages: + msg400657
2021-08-30 18:48:14ned.deilysetnosy: + ned.deily
messages: + msg400655
2021-08-26 15:51:01lukasz.langasetversions: + Python 3.6, Python 3.7, Python 3.8
2021-08-26 15:49:15lukasz.langasetmessages: + msg400358
2021-08-26 15:48:23lukasz.langasetmessages: + msg400357
2021-08-26 15:47:34miss-islingtonsetmessages: + msg400356
2021-08-26 14:49:33miss-islingtonsetpull_requests: + pull_request26423
2021-08-26 14:49:28miss-islingtonsetpull_requests: + pull_request26422
2021-08-26 14:49:23miss-islingtonsetpull_requests: + pull_request26421
2021-08-26 14:49:22lukasz.langasetnosy: + lukasz.langa
messages: + msg400351
2021-08-26 14:49:18miss-islingtonsetpull_requests: + pull_request26420
2021-08-26 14:49:14miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request26419
2021-08-25 13:27:33wbolstersetkeywords: + patch

stage: patch review
messages: + msg400262
pull_requests: + pull_request26392
2021-08-25 13:23:27wbolstercreate