From 4a5eb7b33bd965fafe868290675c0a5fe7c8d7c9 Mon Sep 17 00:00:00 2001 From: Vikash Date: Wed, 25 Nov 2026 03:42:45 +0000 Subject: [PATCH 1/2] fix(http.cookiejar): use RFC 6265 century pivot for two-digit years http2time() now interprets a two-digit Expires year with the RFC 6265 section 5.1.1 pivot instead of the current-year heuristic, so an expiry such as 01-Jan-70 actually deletes a stored cookie. --- Lib/http/cookiejar.py | 12 ++++++++-- Lib/test/test_http_cookiejar.py | 24 +++++++++++++++++++ ...-11-25-09-00-00.gh-issue-162592.Ab3xY9.rst | 1 + 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-11-25-09-00-00.gh-issue-162592.Ab3xY9.rst diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py index d87e9a90ecfcfe2..316d5ed13a11fea 100644 --- a/Lib/http/cookiejar.py +++ b/Lib/http/cookiejar.py @@ -255,8 +255,9 @@ def http2time(text): The parser ignores leading and trailing whitespace. The time may be absent. - If the year is given with only 2 digits, the function will select the - century that makes the year closest to the current date. + If the year is given with only 2 digits, 00-69 are interpreted as + 2000-2069 and 70-99 as 1970-1999, as specified by RFC 6265 section 5.1.1 + for cookie expiry dates. """ # fast exit for strictly conforming string @@ -284,6 +285,13 @@ def http2time(text): else: return None # bad format + # RFC 6265 section 5.1.1: a two-digit year is relative to a fixed + # pivot, not to the current date, so that cookie expiry dates such as + # "01-Jan-70" continue to mean 1970. + if len(yr) == 2: + yr = int(yr) + yr += 1900 if yr >= 70 else 2000 + return _str2time(day, mon, yr, hr, min, sec, tz) ISO_DATE_RE = re.compile( diff --git a/Lib/test/test_http_cookiejar.py b/Lib/test/test_http_cookiejar.py index d53e8c6b614d347..fcd26ab72ac109e 100644 --- a/Lib/test/test_http_cookiejar.py +++ b/Lib/test/test_http_cookiejar.py @@ -70,6 +70,17 @@ def parse_date(text): # this test will break around year 2048 self.assertEqual(parse_date("03-Feb-98"), (1998, 2, 3, 0, 0, 0.0)) + def test_http2time_two_digit_year(self): + # RFC 6265 section 5.1.1: two-digit years 00-69 mean 2000-2069 and + # 70-99 mean 1970-1999, independent of the current date. + self.assertEqual(http2time("Thu, 01-Jan-70 00:00:01 GMT"), 1) + self.assertEqual(time.gmtime(http2time("01-Jan-69"))[:6], + (2069, 1, 1, 0, 0, 0.0)) + self.assertEqual(time.gmtime(http2time("01-Jan-70"))[:6], + (1970, 1, 1, 0, 0, 0.0)) + self.assertEqual(time.gmtime(http2time("01-Jan-99"))[:6], + (1999, 1, 1, 0, 0, 0.0)) + def test_http2time_formats(self): # test http2time for supported dates. Test cases with 2 digit year # will probably break in year 2044. @@ -1536,6 +1547,19 @@ def cookiejar_from_cookie_headers(headers): self.assertIsNone(cookie.expires) + def test_expiry_with_two_digit_year(self): + # A two-digit Expires year of 70 means 1970 (RFC 6265 section 5.1.1), + # so this header deletes the stored cookie instead of replacing it + # with an empty cookie that would last until 2070. + c = CookieJar() + url = "http://www.acme.com/" + interact_netscape(c, url, "session=abc123; Path=/") + self.assertEqual([cookie.name for cookie in c], ["session"]) + interact_netscape(c, url, + "session=; Path=/; Expires=Thu, 01-Jan-70 00:00:01 GMT") + self.assertEqual(list(c), []) + + class LWPCookieTests(unittest.TestCase): # Tests taken from libwww-perl, with a few modifications and additions. diff --git a/Misc/NEWS.d/next/Library/2026-11-25-09-00-00.gh-issue-162592.Ab3xY9.rst b/Misc/NEWS.d/next/Library/2026-11-25-09-00-00.gh-issue-162592.Ab3xY9.rst new file mode 100644 index 000000000000000..7557bd75819503a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-25-09-00-00.gh-issue-162592.Ab3xY9.rst @@ -0,0 +1 @@ +Fix ``http.cookiejar.http2time()`` to interpret two-digit years per RFC 6265 section 5.1.1 (70-99 as 1970-1999, 00-69 as 2000-2069) instead of choosing the century closest to the current date. This allows a two-digit ``Expires`` year such as ``01-Jan-70`` to expire a cookie. From eff663b14c5bddfbf838711023d2c75e0273c49b Mon Sep 17 00:00:00 2001 From: Vikash Khandelwal Date: Wed, 25 Nov 2026 16:44:18 +0000 Subject: [PATCH 2/2] fix(http.cookiejar): address review feedback on year pivot --- Lib/http/cookiejar.py | 16 +++++++------ Lib/test/test_http_cookiejar.py | 23 +++++++++++++------ ...-11-25-09-00-00.gh-issue-162592.Ab3xY9.rst | 2 +- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py index 316d5ed13a11fea..b83c3a48df1c3d9 100644 --- a/Lib/http/cookiejar.py +++ b/Lib/http/cookiejar.py @@ -255,11 +255,16 @@ def http2time(text): The parser ignores leading and trailing whitespace. The time may be absent. - If the year is given with only 2 digits, 00-69 are interpreted as - 2000-2069 and 70-99 as 1970-1999, as specified by RFC 6265 section 5.1.1 - for cookie expiry dates. + If the year is given with only 2 digits, the century is selected using + the fixed pivot defined by RFC 6265 section 5.1.1. + + .. versionchanged:: 3.16 + Two-digit years 00-69 are interpreted as 2000-2069 and 70-99 as + 1970-1999. Previously, the century closest to the current date was + selected. """ + text = text.strip() # fast exit for strictly conforming string m = STRICT_DATE_RE.search(text) if m: @@ -271,8 +276,6 @@ def http2time(text): # No, we need some messy parsing... - # clean up - text = text.lstrip() text = WEEKDAY_RE.sub("", text, 1) # Useless weekday # tz is time zone specifier string @@ -289,8 +292,7 @@ def http2time(text): # pivot, not to the current date, so that cookie expiry dates such as # "01-Jan-70" continue to mean 1970. if len(yr) == 2: - yr = int(yr) - yr += 1900 if yr >= 70 else 2000 + yr = ("19" if yr >= "70" else "20") + yr return _str2time(day, mon, yr, hr, min, sec, tz) diff --git a/Lib/test/test_http_cookiejar.py b/Lib/test/test_http_cookiejar.py index fcd26ab72ac109e..98aaf28bc23d2c3 100644 --- a/Lib/test/test_http_cookiejar.py +++ b/Lib/test/test_http_cookiejar.py @@ -73,13 +73,22 @@ def parse_date(text): def test_http2time_two_digit_year(self): # RFC 6265 section 5.1.1: two-digit years 00-69 mean 2000-2069 and # 70-99 mean 1970-1999, independent of the current date. - self.assertEqual(http2time("Thu, 01-Jan-70 00:00:01 GMT"), 1) - self.assertEqual(time.gmtime(http2time("01-Jan-69"))[:6], + cases = [ + ("01-Jan-00", (2000, 1, 1, 0, 0, 0.0)), + ("01-Jan-69", (2069, 1, 1, 0, 0, 0.0)), + ("01-Jan-70", (1970, 1, 1, 0, 0, 0.0)), + ("01-Jan-99", (1999, 1, 1, 0, 0, 0.0)), + ("01-Jan-1970", (1970, 1, 1, 0, 0, 0.0)), + ("01-Jan-2069", (2069, 1, 1, 0, 0, 0.0)), + ("01-Jan-2070", (2070, 1, 1, 0, 0, 0.0)), + ] + for text, expected in cases: + with self.subTest(text=text): + self.assertEqual(time.gmtime(http2time(text))[:6], expected) + + self.assertEqual(http2time(" \tThu, 01-Jan-70 00:00:01 GMT\n"), 1) + self.assertEqual(time.gmtime(http2time(" 01-Jan-69 \n"))[:6], (2069, 1, 1, 0, 0, 0.0)) - self.assertEqual(time.gmtime(http2time("01-Jan-70"))[:6], - (1970, 1, 1, 0, 0, 0.0)) - self.assertEqual(time.gmtime(http2time("01-Jan-99"))[:6], - (1999, 1, 1, 0, 0, 0.0)) def test_http2time_formats(self): # test http2time for supported dates. Test cases with 2 digit year @@ -1557,7 +1566,7 @@ def test_expiry_with_two_digit_year(self): self.assertEqual([cookie.name for cookie in c], ["session"]) interact_netscape(c, url, "session=; Path=/; Expires=Thu, 01-Jan-70 00:00:01 GMT") - self.assertEqual(list(c), []) + self.assertListEqual(list(c), []) class LWPCookieTests(unittest.TestCase): diff --git a/Misc/NEWS.d/next/Library/2026-11-25-09-00-00.gh-issue-162592.Ab3xY9.rst b/Misc/NEWS.d/next/Library/2026-11-25-09-00-00.gh-issue-162592.Ab3xY9.rst index 7557bd75819503a..fd6f9f1d939b029 100644 --- a/Misc/NEWS.d/next/Library/2026-11-25-09-00-00.gh-issue-162592.Ab3xY9.rst +++ b/Misc/NEWS.d/next/Library/2026-11-25-09-00-00.gh-issue-162592.Ab3xY9.rst @@ -1 +1 @@ -Fix ``http.cookiejar.http2time()`` to interpret two-digit years per RFC 6265 section 5.1.1 (70-99 as 1970-1999, 00-69 as 2000-2069) instead of choosing the century closest to the current date. This allows a two-digit ``Expires`` year such as ``01-Jan-70`` to expire a cookie. +Fix ``http.cookiejar.http2time()`` to interpret two-digit years per :rfc:`6265#5.1.1` (70-99 as 1970-1999, 00-69 as 2000-2069) instead of choosing the century closest to the current date. This allows a two-digit ``Expires`` year such as ``01-Jan-70`` to expire a cookie.