Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit da9d6c5

Browse filesBrowse files
bpo-45001: Make email date parsing more robust against malformed input (GH-27946) (GH-27976)
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: guard against empty lists, after splitting on whitespace, but before accessing the first element. (cherry picked from commit 989f6a3) Co-authored-by: wouter bolsterlee <wouter@bolsterl.ee>
1 parent 0a0a135 commit da9d6c5
Copy full SHA for da9d6c5

File tree

3 files changed

+6
-0
lines changed
Filter options

3 files changed

+6
-0
lines changed

‎Lib/email/_parseaddr.py

Copy file name to clipboardExpand all lines: Lib/email/_parseaddr.py
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ def _parsedate_tz(data):
6767
if not data:
6868
return
6969
data = data.split()
70+
if not data: # This happens for whitespace-only input.
71+
return None
7072
# The FWS after the comma after the day-of-week is optional, so search and
7173
# adjust for this.
7274
if data[0].endswith(',') or data[0].lower() in _daynames:

‎Lib/test/test_email/test_email.py

Copy file name to clipboardExpand all lines: Lib/test/test_email/test_email.py
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2961,6 +2961,8 @@ def test_formatdate_usegmt(self):
29612961
def test_parsedate_returns_None_for_invalid_strings(self):
29622962
self.assertIsNone(utils.parsedate(''))
29632963
self.assertIsNone(utils.parsedate_tz(''))
2964+
self.assertIsNone(utils.parsedate(' '))
2965+
self.assertIsNone(utils.parsedate_tz(' '))
29642966
self.assertIsNone(utils.parsedate('0'))
29652967
self.assertIsNone(utils.parsedate_tz('0'))
29662968
self.assertIsNone(utils.parsedate('A Complete Waste of Time'))
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Made email date parsing more robust against malformed input, namely a
2+
whitespace-only ``Date:`` header. Patch by Wouter Bolsterlee.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.