diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py index d87e9a90ecfcfe..b83c3a48df1c3d 100644 --- a/Lib/http/cookiejar.py +++ b/Lib/http/cookiejar.py @@ -255,10 +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, the function will select the - century that makes the year closest to the current date. + 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: @@ -270,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 @@ -284,6 +288,12 @@ 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 = ("19" if yr >= "70" else "20") + yr + 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 d53e8c6b614d34..98aaf28bc23d2c 100644 --- a/Lib/test/test_http_cookiejar.py +++ b/Lib/test/test_http_cookiejar.py @@ -70,6 +70,26 @@ 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. + 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)) + def test_http2time_formats(self): # test http2time for supported dates. Test cases with 2 digit year # will probably break in year 2044. @@ -1536,6 +1556,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.assertListEqual(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 00000000000000..fd6f9f1d939b02 --- /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#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.