From a84d66cb21a0cf89cacd50d4b3db7af79dfbd0aa Mon Sep 17 00:00:00 2001 From: Gustaf Date: Wed, 21 May 2025 11:28:25 -0400 Subject: [PATCH 01/14] added digit check to prevent forced 4-digit year evaluation --- Lib/email/_parseaddr.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/email/_parseaddr.py b/Lib/email/_parseaddr.py index 84917038874ba1..df7463175baa73 100644 --- a/Lib/email/_parseaddr.py +++ b/Lib/email/_parseaddr.py @@ -149,7 +149,7 @@ def _parsedate_tz(data): # calls for a two-digit yy, but RFC 2822 (which obsoletes RFC 822) # mandates a 4-digit yy. For more information, see the documentation for # the time module. - if yy < 100: + if len(str(yy)) >= 2 and yy < 100: # The year is between 1969 and 1999 (inclusive). if yy > 68: yy += 1900 From df899ea9a666426b4e4f17db54d4e74881e0b3b2 Mon Sep 17 00:00:00 2001 From: Gustaf Date: Wed, 21 May 2025 11:28:48 -0400 Subject: [PATCH 02/14] added tests for obscure year inputs to parsedate_to_datetime --- .../test_email/test_parsedate_to_datetime.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Lib/test/test_email/test_parsedate_to_datetime.py diff --git a/Lib/test/test_email/test_parsedate_to_datetime.py b/Lib/test/test_email/test_parsedate_to_datetime.py new file mode 100644 index 00000000000000..4ab13792265b02 --- /dev/null +++ b/Lib/test/test_email/test_parsedate_to_datetime.py @@ -0,0 +1,17 @@ +# Test to see if parsedate_to_datetime returns the correct year for different digit numbers, adhering to the RFC2822 spec + +import unittest +from email.utils import parsedate_to_datetime + +class ParsedateToDatetimeTest(unittest.TestCase): + def test(self): + expectations = { + "Sat, 15 Aug 0001 23:12:09 +0500": "0001", + "Thu, 1 Sep 1 23:12:09 +0800": "0001", + "Thu, 7 Oct 123 23:12:09 +0500": "0123", + } + for input_string, output_string in expectations.items(): + self.assertEqual(str(parsedate_to_datetime(input_string))[:4], output_string) + +if __name__ == '__main__': + unittest.main() \ No newline at end of file From 6112424138a7b16eedaa529a072afcab526bb842 Mon Sep 17 00:00:00 2001 From: Gustaf Date: Wed, 21 May 2025 11:29:00 -0400 Subject: [PATCH 03/14] updated test_email to correctly assert 1 digit year --- Lib/test/test_email/test_email.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_email/test_email.py b/Lib/test/test_email/test_email.py index 7b14305f997e5d..2679fc1178843c 100644 --- a/Lib/test/test_email/test_email.py +++ b/Lib/test/test_email/test_email.py @@ -3227,7 +3227,7 @@ def test_parsedate_y2k(self): """ self.assertEqual(utils.parsedate_tz('25 Feb 03 13:47:26 -0800'), - utils.parsedate_tz('25 Feb 2003 13:47:26 -0800')) + utils.parsedate_tz('25 Feb 3 13:47:26 -0800')) self.assertEqual(utils.parsedate_tz('25 Feb 71 13:47:26 -0800'), utils.parsedate_tz('25 Feb 1971 13:47:26 -0800')) From 639514f80b259bf174e5fa5c0c8d7c59fb1d7cc4 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 21 May 2025 15:32:05 +0000 Subject: [PATCH 04/14] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20b?= =?UTF-8?q?lurb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2025-05-21-15-32-04.gh-issue-126845.QhEGg6.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2025-05-21-15-32-04.gh-issue-126845.QhEGg6.rst diff --git a/Misc/NEWS.d/next/Library/2025-05-21-15-32-04.gh-issue-126845.QhEGg6.rst b/Misc/NEWS.d/next/Library/2025-05-21-15-32-04.gh-issue-126845.QhEGg6.rst new file mode 100644 index 00000000000000..90932a35c1ce7c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-05-21-15-32-04.gh-issue-126845.QhEGg6.rst @@ -0,0 +1,2 @@ +Fixed _parsedate_tz to not evaluate 1 digit years as as 4-digit years, adhering to RFC 2855 4.3 +Contributed by Gustaf Gyllensporre. From 5dccaebbb036557e0c3d3fd6c25e25c32ca23a92 Mon Sep 17 00:00:00 2001 From: Gustaf <79180496+GGyll@users.noreply.github.com> Date: Wed, 21 May 2025 17:38:22 +0200 Subject: [PATCH 05/14] Added newline at end of test_parsedate_to_datetime.py --- Lib/test/test_email/test_parsedate_to_datetime.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_email/test_parsedate_to_datetime.py b/Lib/test/test_email/test_parsedate_to_datetime.py index 4ab13792265b02..4ae005061479a5 100644 --- a/Lib/test/test_email/test_parsedate_to_datetime.py +++ b/Lib/test/test_email/test_parsedate_to_datetime.py @@ -14,4 +14,5 @@ def test(self): self.assertEqual(str(parsedate_to_datetime(input_string))[:4], output_string) if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main() + From 83bdc716ac76b6b56b910dd49e336f7652c5209c Mon Sep 17 00:00:00 2001 From: Gustaf <79180496+GGyll@users.noreply.github.com> Date: Wed, 21 May 2025 17:57:20 +0200 Subject: [PATCH 06/14] removed trailing whitespace in parsedate_to_datetime --- Lib/test/test_email/test_parsedate_to_datetime.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_email/test_parsedate_to_datetime.py b/Lib/test/test_email/test_parsedate_to_datetime.py index 4ae005061479a5..4f0f64e8b53594 100644 --- a/Lib/test/test_email/test_parsedate_to_datetime.py +++ b/Lib/test/test_email/test_parsedate_to_datetime.py @@ -15,4 +15,3 @@ def test(self): if __name__ == '__main__': unittest.main() - From 3cba68517a0d08370dce9468cc6819a147d359de Mon Sep 17 00:00:00 2001 From: Gustaf Date: Thu, 17 Jul 2025 21:53:37 +0200 Subject: [PATCH 07/14] fixed 3-digit year edge case --- Lib/email/_parseaddr.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/email/_parseaddr.py b/Lib/email/_parseaddr.py index df7463175baa73..f909208a4d31ab 100644 --- a/Lib/email/_parseaddr.py +++ b/Lib/email/_parseaddr.py @@ -149,7 +149,7 @@ def _parsedate_tz(data): # calls for a two-digit yy, but RFC 2822 (which obsoletes RFC 822) # mandates a 4-digit yy. For more information, see the documentation for # the time module. - if len(str(yy)) >= 2 and yy < 100: + if yy < 999: # The year is between 1969 and 1999 (inclusive). if yy > 68: yy += 1900 From da6ff70481ec5177d5367020fe2c520303230078 Mon Sep 17 00:00:00 2001 From: Gustaf Date: Thu, 17 Jul 2025 21:55:22 +0200 Subject: [PATCH 08/14] reset year check to previous value --- Lib/test/test_email/test_email.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_email/test_email.py b/Lib/test/test_email/test_email.py index 2679fc1178843c..7b14305f997e5d 100644 --- a/Lib/test/test_email/test_email.py +++ b/Lib/test/test_email/test_email.py @@ -3227,7 +3227,7 @@ def test_parsedate_y2k(self): """ self.assertEqual(utils.parsedate_tz('25 Feb 03 13:47:26 -0800'), - utils.parsedate_tz('25 Feb 3 13:47:26 -0800')) + utils.parsedate_tz('25 Feb 2003 13:47:26 -0800')) self.assertEqual(utils.parsedate_tz('25 Feb 71 13:47:26 -0800'), utils.parsedate_tz('25 Feb 1971 13:47:26 -0800')) From 693bce30d5cadae311b592946e77ba0e1d4f5ed0 Mon Sep 17 00:00:00 2001 From: Gustaf Date: Thu, 17 Jul 2025 21:56:29 +0200 Subject: [PATCH 09/14] moved ParsedateToDatetimeTest --- .../test_email/test_parsedate_to_datetime.py | 17 ----------------- Lib/test/test_email/test_utils.py | 13 +++++++++++++ 2 files changed, 13 insertions(+), 17 deletions(-) delete mode 100644 Lib/test/test_email/test_parsedate_to_datetime.py diff --git a/Lib/test/test_email/test_parsedate_to_datetime.py b/Lib/test/test_email/test_parsedate_to_datetime.py deleted file mode 100644 index 4f0f64e8b53594..00000000000000 --- a/Lib/test/test_email/test_parsedate_to_datetime.py +++ /dev/null @@ -1,17 +0,0 @@ -# Test to see if parsedate_to_datetime returns the correct year for different digit numbers, adhering to the RFC2822 spec - -import unittest -from email.utils import parsedate_to_datetime - -class ParsedateToDatetimeTest(unittest.TestCase): - def test(self): - expectations = { - "Sat, 15 Aug 0001 23:12:09 +0500": "0001", - "Thu, 1 Sep 1 23:12:09 +0800": "0001", - "Thu, 7 Oct 123 23:12:09 +0500": "0123", - } - for input_string, output_string in expectations.items(): - self.assertEqual(str(parsedate_to_datetime(input_string))[:4], output_string) - -if __name__ == '__main__': - unittest.main() diff --git a/Lib/test/test_email/test_utils.py b/Lib/test/test_email/test_utils.py index c9d09098b502f9..c3739dbcd3633c 100644 --- a/Lib/test/test_email/test_utils.py +++ b/Lib/test/test_email/test_utils.py @@ -186,5 +186,18 @@ def test_formatdate_with_localtime(self): string = utils.formatdate(timeval, localtime=True) self.assertEqual(string, 'Thu, 01 Dec 2011 18:00:00 +0300') +# Issue #126845: Some edge cases seem to differ from RFC28222 spec +class ParsedateToDatetimeTest(unittest.TestCase): + def test_year_parsing_edge_cases(self): + expectations = { + "Sat, 15 Aug 0001 23:12:09 +0500": "2001", + "Thu, 1 Sep 1 23:12:09 +0800": "2001", + "Thu, 7 Oct 123 23:12:09 +0500": "2023", + "Tue, 17 Nov 2026 12:12:09 +0500": "2026", + } + for input_string, output_string in expectations.items(): + with self.subTest(input_string=input_string): + self.assertEqual(str(utils.parsedate_to_datetime(input_string))[:4], output_string) + if __name__ == '__main__': unittest.main() From 87fef0c7d97a54b9e0115345e97d940b83ac97aa Mon Sep 17 00:00:00 2001 From: Gustaf Date: Tue, 2 Jun 2026 18:47:27 +0200 Subject: [PATCH 10/14] change yy to < 1000 --- Lib/email/_parseaddr.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Lib/email/_parseaddr.py b/Lib/email/_parseaddr.py index f909208a4d31ab..babad9516e1383 100644 --- a/Lib/email/_parseaddr.py +++ b/Lib/email/_parseaddr.py @@ -147,13 +147,14 @@ def _parsedate_tz(data): # Check for a yy specified in two-digit format, then convert it to the # appropriate four-digit format, according to the POSIX standard. RFC 822 # calls for a two-digit yy, but RFC 2822 (which obsoletes RFC 822) - # mandates a 4-digit yy. For more information, see the documentation for - # the time module. - if yy < 999: - # The year is between 1969 and 1999 (inclusive). + # mandated a 4-digit yyyy, and RFC 5322 (which obsoletes RFC 2822) + # continues this requirement. Two digit years between 69 and 99 inclusive, + # and three digit years, are to be interpreted as 1900s dates, while those + # between 0 and 68 are to be treated as 2000s dates. + # (https://datatracker.ietf.org/doc/html/rfc5322#section-4.3) + if yy < 1000: if yy > 68: yy += 1900 - # The year is between 2000 and 2068 (inclusive). else: yy += 2000 tzoffset = None From 9a40eedcb2912244c4567c4238d792cdf405aa7b Mon Sep 17 00:00:00 2001 From: Gustaf Date: Tue, 2 Jun 2026 18:47:39 +0200 Subject: [PATCH 11/14] updated test edge cases --- Lib/test/test_email/test_utils.py | 37 ++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/Lib/test/test_email/test_utils.py b/Lib/test/test_email/test_utils.py index c3739dbcd3633c..9141230995cb9a 100644 --- a/Lib/test/test_email/test_utils.py +++ b/Lib/test/test_email/test_utils.py @@ -77,6 +77,30 @@ def test_parsedate_to_datetime_with_invalid_raises_valueerror(self): with self.subTest(dtstr=dtstr): self.assertRaises(ValueError, utils.parsedate_to_datetime, dtstr) + def test_parsedate_to_datetime_year_edge_cases(self): + expectations = { + # Various short-year formats that get expanded + "Sat, 15 Aug 0001 23:12:09 +0500": "2001", + "Thu, 1 Sep 1 23:12:09 +0800": "2001", + "Thu, 7 Oct 123 23:12:09 +0500": "2023", + "Tue, 17 Nov 2026 12:12:09 +0500": "2026", + # RFC 5322 section 4.3 boundaries for 2-digit years + "Mon, 1 Jan 0 00:00:00 +0000": "2000", + "Mon, 1 Jan 68 00:00:00 +0000": "2068", + "Mon, 1 Jan 69 00:00:00 +0000": "1969", + "Mon, 1 Jan 99 00:00:00 +0000": "1999", + # 3-digit year boundary + "Mon, 1 Jan 999 00:00:00 +0000": "2899", + # Pre-1900 four-digit year: illegal per RFC but we accept it + "Mon, 1 Jan 1000 00:00:00 +0000": "1000", + } + for input_string, expected_year in expectations.items(): + with self.subTest(input_string=input_string): + self.assertEqual( + str(utils.parsedate_to_datetime(input_string))[:4], + expected_year, + ) + class LocaltimeTests(unittest.TestCase): def test_localtime_is_tz_aware_daylight_true(self): @@ -186,18 +210,5 @@ def test_formatdate_with_localtime(self): string = utils.formatdate(timeval, localtime=True) self.assertEqual(string, 'Thu, 01 Dec 2011 18:00:00 +0300') -# Issue #126845: Some edge cases seem to differ from RFC28222 spec -class ParsedateToDatetimeTest(unittest.TestCase): - def test_year_parsing_edge_cases(self): - expectations = { - "Sat, 15 Aug 0001 23:12:09 +0500": "2001", - "Thu, 1 Sep 1 23:12:09 +0800": "2001", - "Thu, 7 Oct 123 23:12:09 +0500": "2023", - "Tue, 17 Nov 2026 12:12:09 +0500": "2026", - } - for input_string, output_string in expectations.items(): - with self.subTest(input_string=input_string): - self.assertEqual(str(utils.parsedate_to_datetime(input_string))[:4], output_string) - if __name__ == '__main__': unittest.main() From 5f589ba411b8ee7fc14a52952b660a023ad4f738 Mon Sep 17 00:00:00 2001 From: "R. David Murray" Date: Tue, 2 Jun 2026 15:01:22 -0400 Subject: [PATCH 12/14] Update 2025-05-21-15-32-04.gh-issue-126845.QhEGg6.rst --- .../2025-05-21-15-32-04.gh-issue-126845.QhEGg6.rst | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2025-05-21-15-32-04.gh-issue-126845.QhEGg6.rst b/Misc/NEWS.d/next/Library/2025-05-21-15-32-04.gh-issue-126845.QhEGg6.rst index 90932a35c1ce7c..cee5589e777e7f 100644 --- a/Misc/NEWS.d/next/Library/2025-05-21-15-32-04.gh-issue-126845.QhEGg6.rst +++ b/Misc/NEWS.d/next/Library/2025-05-21-15-32-04.gh-issue-126845.QhEGg6.rst @@ -1,2 +1,7 @@ -Fixed _parsedate_tz to not evaluate 1 digit years as as 4-digit years, adhering to RFC 2855 4.3 -Contributed by Gustaf Gyllensporre. +Fixed the :mod:`email` parsing of three digit dates to +conform to :rfc:`5322`: three digit dates were previously +turned in to non-conformant four digit dates with a +leading `0`. Now, per the RFC section 4.3, `1990` is added +to such dates to form compliant four digit years. + +Contributed by Gustaf Gyllensporre. From 526cafd4caa613bb35f0d9c40d62a5f3c3a76786 Mon Sep 17 00:00:00 2001 From: "R. David Murray" Date: Tue, 2 Jun 2026 16:15:38 -0400 Subject: [PATCH 13/14] Update 2025-05-21-15-32-04.gh-issue-126845.QhEGg6.rst --- .../Library/2025-05-21-15-32-04.gh-issue-126845.QhEGg6.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2025-05-21-15-32-04.gh-issue-126845.QhEGg6.rst b/Misc/NEWS.d/next/Library/2025-05-21-15-32-04.gh-issue-126845.QhEGg6.rst index cee5589e777e7f..0730665656478e 100644 --- a/Misc/NEWS.d/next/Library/2025-05-21-15-32-04.gh-issue-126845.QhEGg6.rst +++ b/Misc/NEWS.d/next/Library/2025-05-21-15-32-04.gh-issue-126845.QhEGg6.rst @@ -1,7 +1,7 @@ -Fixed the :mod:`email` parsing of three digit dates to +Fixed the :mod:`email` module parsing of three digit dates to conform to :rfc:`5322`: three digit dates were previously turned in to non-conformant four digit dates with a -leading `0`. Now, per the RFC section 4.3, `1990` is added +leading ``0``. Now, per the RFC section 4.3, `1990` is added to such dates to form compliant four digit years. Contributed by Gustaf Gyllensporre. From 67ded8f1edc989fb147f6879261ed2615a34d2ed Mon Sep 17 00:00:00 2001 From: "R. David Murray" Date: Tue, 2 Jun 2026 16:19:00 -0400 Subject: [PATCH 14/14] Update 2025-05-21-15-32-04.gh-issue-126845.QhEGg6.rst --- .../next/Library/2025-05-21-15-32-04.gh-issue-126845.QhEGg6.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-05-21-15-32-04.gh-issue-126845.QhEGg6.rst b/Misc/NEWS.d/next/Library/2025-05-21-15-32-04.gh-issue-126845.QhEGg6.rst index 0730665656478e..a1c5db9e1110f0 100644 --- a/Misc/NEWS.d/next/Library/2025-05-21-15-32-04.gh-issue-126845.QhEGg6.rst +++ b/Misc/NEWS.d/next/Library/2025-05-21-15-32-04.gh-issue-126845.QhEGg6.rst @@ -1,7 +1,7 @@ Fixed the :mod:`email` module parsing of three digit dates to conform to :rfc:`5322`: three digit dates were previously turned in to non-conformant four digit dates with a -leading ``0``. Now, per the RFC section 4.3, `1990` is added +leading ``0``. Now, per the RFC section 4.3, ``1990`` is added to such dates to form compliant four digit years. Contributed by Gustaf Gyllensporre.