From 09c98bef30e8e9e79a400034776b3d9aaac4e880 Mon Sep 17 00:00:00 2001 From: tonghuaroot Date: Mon, 13 Jul 2026 10:14:54 +0800 Subject: [PATCH 1/2] gh-153677: Fix ValueError in http.cookiejar.http2time() for a strict-format fake month The STRICT_DATE_RE fast path in http2time() matched date strings whose month field fit the pattern but named a non-existent month (for example "Foo"), then called MONTHS_LOWER.index() which raised a raw ValueError. The documented contract is to return None for unrecognized formats, and the slower parser already does so. Guard the fast-path month lookup and fall through to the slower parser on failure. --- Lib/http/cookiejar.py | 14 ++++++++++---- Lib/test/test_http_cookiejar.py | 5 +++++ .../2026-07-13-10-30-00.gh-issue-153677.dIxjOG.rst | 3 +++ 3 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-07-13-10-30-00.gh-issue-153677.dIxjOG.rst diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py index 13e5b104a81ea2b..6f3b01776661482 100644 --- a/Lib/http/cookiejar.py +++ b/Lib/http/cookiejar.py @@ -262,10 +262,16 @@ def http2time(text): m = STRICT_DATE_RE.search(text) if m: g = m.groups() - mon = MONTHS_LOWER.index(g[1].lower()) + 1 - tt = (int(g[2]), mon, int(g[0]), - int(g[3]), int(g[4]), float(g[5])) - return _timegm(tt) + try: + mon = MONTHS_LOWER.index(g[1].lower()) + 1 + except ValueError: + # The month field matched the pattern but is not a real month + # name, so fall through to the slower parser. + pass + else: + tt = (int(g[2]), mon, int(g[0]), + int(g[3]), int(g[4]), float(g[5])) + return _timegm(tt) # No, we need some messy parsing... diff --git a/Lib/test/test_http_cookiejar.py b/Lib/test/test_http_cookiejar.py index 04cb440cd4ccf66..1691029f5cb2635 100644 --- a/Lib/test/test_http_cookiejar.py +++ b/Lib/test/test_http_cookiejar.py @@ -121,6 +121,11 @@ def test_http2time_formats(self): '08-01-3697739', '09 Feb 19942632 22:23:32 GMT', 'Wed, 09 Feb 1994834 22:23:32 GMT', + # Strictly formatted string with a non-existent month name. The + # month field matches the STRICT_DATE_RE fast path but is not a + # real month, and must not leak a ValueError. + 'Wed, 09 Foo 1994 22:23:32 GMT', + 'Mon, 01 Aaa 2000 00:00:00 GMT', ]) def test_http2time_garbage(self, test): self.assertIsNone(http2time(test)) diff --git a/Misc/NEWS.d/next/Library/2026-07-13-10-30-00.gh-issue-153677.dIxjOG.rst b/Misc/NEWS.d/next/Library/2026-07-13-10-30-00.gh-issue-153677.dIxjOG.rst new file mode 100644 index 000000000000000..c180e4df9f1aac3 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-07-13-10-30-00.gh-issue-153677.dIxjOG.rst @@ -0,0 +1,3 @@ +:func:`!http.cookiejar.http2time` now returns ``None`` instead of raising +:exc:`ValueError` when the date string matches its strict fast-path pattern +but names a month that does not exist. Patch by tonghuaroot. From d30efdde868a793a7d079fafc20b435f726a91d2 Mon Sep 17 00:00:00 2001 From: tonghuaroot Date: Tue, 14 Jul 2026 10:40:16 +0800 Subject: [PATCH 2/2] gh-153677: Trim the comment and NEWS wording --- Lib/http/cookiejar.py | 3 +-- .../Library/2026-07-13-10-30-00.gh-issue-153677.dIxjOG.rst | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py index 6f3b01776661482..58a53b424daf51a 100644 --- a/Lib/http/cookiejar.py +++ b/Lib/http/cookiejar.py @@ -265,8 +265,7 @@ def http2time(text): try: mon = MONTHS_LOWER.index(g[1].lower()) + 1 except ValueError: - # The month field matched the pattern but is not a real month - # name, so fall through to the slower parser. + # Not a real month name; fall through to the slower parser. pass else: tt = (int(g[2]), mon, int(g[0]), diff --git a/Misc/NEWS.d/next/Library/2026-07-13-10-30-00.gh-issue-153677.dIxjOG.rst b/Misc/NEWS.d/next/Library/2026-07-13-10-30-00.gh-issue-153677.dIxjOG.rst index c180e4df9f1aac3..9faf1efd1071dbf 100644 --- a/Misc/NEWS.d/next/Library/2026-07-13-10-30-00.gh-issue-153677.dIxjOG.rst +++ b/Misc/NEWS.d/next/Library/2026-07-13-10-30-00.gh-issue-153677.dIxjOG.rst @@ -1,3 +1,3 @@ :func:`!http.cookiejar.http2time` now returns ``None`` instead of raising -:exc:`ValueError` when the date string matches its strict fast-path pattern -but names a month that does not exist. Patch by tonghuaroot. +:exc:`ValueError` for a strictly-formatted date with a non-existent month +name. Patch by tonghuaroot.