From ee122d37e6d638bb521c868f7e2e447244bb2f73 Mon Sep 17 00:00:00 2001 From: wouter bolsterlee Date: Thu, 21 Jul 2022 10:23:34 +0200 Subject: [PATCH] gh-95087: make email date parsing more robust Similar to bpo-45001 (GH-89164), this makes email date parsing more robust against malformed input. parsedate_tz() is supposed to return None for malformed input, but could crash on certain inputs, e.g. >>> email.utils.parsedate_tz('17 June , 2022') IndexError: string index out of range Fixes gh-95087. --- Lib/email/_parseaddr.py | 2 +- Lib/test/test_email/test_email.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/email/_parseaddr.py b/Lib/email/_parseaddr.py index ba5ad5a36d06b74..024055e7e4bd078 100644 --- a/Lib/email/_parseaddr.py +++ b/Lib/email/_parseaddr.py @@ -110,7 +110,7 @@ def _parsedate_tz(data): yy, tm = tm, yy if yy[-1] == ',': yy = yy[:-1] - if not yy[0].isdigit(): + if yy and not yy[0].isdigit(): yy, tz = tz, yy if tm[-1] == ',': tm = tm[:-1] diff --git a/Lib/test/test_email/test_email.py b/Lib/test/test_email/test_email.py index 0bf679b4c56ca0e..1925c6211a5d52f 100644 --- a/Lib/test/test_email/test_email.py +++ b/Lib/test/test_email/test_email.py @@ -3053,6 +3053,7 @@ def test_parsedate_returns_None_for_invalid_strings(self): self.assertIsNone(utils.parsedate_tz(' ')) self.assertIsNone(utils.parsedate('0')) self.assertIsNone(utils.parsedate_tz('0')) + self.assertIsNone(utils.parsedate_tz('17 June , 2022')) self.assertIsNone(utils.parsedate('A Complete Waste of Time')) self.assertIsNone(utils.parsedate_tz('A Complete Waste of Time')) self.assertIsNone(utils.parsedate_tz('Wed, 3 Apr 2002 12.34.56.78+0800'))