Skip to content

gh-162592: use RFC 6265 century pivot for two-digit years in http.cookiejar - #162596

Open
vikash1311994 wants to merge 2 commits into
python:mainfrom
vikash1311994:fix/gh-162592-cookiejar-two-digit-year
Open

vikash1311994 wants to merge 2 commits into
python:mainfrom
vikash1311994:fix/gh-162592-cookiejar-two-digit-year

Conversation

@vikash1311994

Copy link
Copy Markdown

Fixes python/cpython#162592.

Problem

http.cookiejar.http2time() interprets a two-digit Expires year by choosing the century nearest the current date (_str2time's "obvious year" heuristic). With the clock in 2026, 70 becomes 2070, so a Set-Cookie that tries to delete a cookie with Expires=Thu, 01-Jan-70 00:00:01 GMT instead stores an empty cookie valid until 2070. The result is clock-dependent and contradicts RFC 6265 section 5.1.1, which requires a fixed pivot: 70-99 -> 1970-1999, 00-69 -> 2000-2069.

Root cause

http2time() passed the raw year string to _str2time(), which applies the current-century heuristic to every year below 1000.

Fix

Before calling _str2time(), apply the RFC 6265 pivot when the parsed year has exactly two digits and pass the now-four-digit year through. Four-digit years and legacy three-digit/zero-padded years are untouched. Updated the http2time() docstring and added a NEWS entry.

Tests

New regression tests in Lib/test/test_http_cookiejar.py:

  • DateTimeTests.test_http2time_two_digit_year (69/70/99 boundary values)
  • CookieTests.test_expiry_with_two_digit_year (a two-digit Expires actually deletes the stored cookie)

Verification evidence:

  • Pre-fix: both new tests FAIL (3155760001 != 1; a cookie with expires=3155760001 remains in the jar).
  • With fix: both new tests PASS.
  • Sabotage check: restored the pre-fix cookiejar.py -> 2 failures; restored the fix -> 0 failures.
  • Full targeted suite ./python -m test test_http_cookiejar: 87 tests, SUCCESS.

Risk

Low. Only two-digit years in the loose HTTP-date parser change, and they now follow the RFC rule that the existing tests already assume for 94, 98, and 20. The change also removes the clock dependence that made those tests time-bombed ("will break around year 2048/2070").

Exclusions

  • No change to _str2time()'s heuristic for other sub-1000 years.
  • No change to iso2time() (its year regex requires four digits).
  • No change to cookie-policy/domain handling or any other stdlib module.
  • No unrelated formatting or cleanup.

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.
@picnixz picnixz changed the title fix(http.cookiejar): use RFC 6265 century pivot for two-digit years (gh-162592) gh-162592: use RFC 6265 century pivot for two-digit years in http.cookiejar Nov 25, 2026
Comment thread Lib/http/cookiejar.py Outdated

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the docs of this change with a versionchanged because it may be slightly breaking people.

@@ -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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use the Sphinx role for rendering RFC sections (:rfc:`6265#5.1.1` works I think, if not check in other NEWS entries how we formatted them or in 3.14/3.15 whatsnew because I know that I did use that once).

c = CookieJar()
url = "http://www.acme.com/"
interact_netscape(c, url, "session=abc123; Path=/")
self.assertEqual([cookie.name for cookie in c], ["session"])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use assertListEqual

self.assertIsNone(cookie.expires)


def test_expiry_with_two_digit_year(self):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add some tests with year 69 and full years that are lose (if any)

Comment thread Lib/http/cookiejar.py
# 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:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure that yr will not fail to be converted by int? according to the regex it should be the case but the numeric category \d may mean more IIRC (I can't see if we are using ASCII flags or not).

Comment thread Lib/http/cookiejar.py Outdated
# "01-Jan-70" continue to mean 1970.
if len(yr) == 2:
yr = int(yr)
yr += 1900 if yr >= 70 else 2000

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes yr an int while before it was a string. Is it ok for _str2time?

@python-cla-bot

python-cla-bot Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@vikash1311994
vikash1311994 force-pushed the fix/gh-162592-cookiejar-two-digit-year branch from af2bcf5 to eff663b Compare November 25, 2026 16:44
Comment thread Lib/http/cookiejar.py
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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not meant to be put here, it's for the RST docs not the docstring.

Comment thread Lib/http/cookiejar.py
# No, we need some messy parsing...

# clean up
text = text.lstrip()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this changed? is it an agent doing the work?

("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)),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add the cases 1969 if you use 2069,

# 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/"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use example.com (i think this is what we use elsewhere)

@picnixz picnixz added the AI-needs-explanation AI-assisted work that needs further explanation and follow up from the contributor label Nov 28, 2026
@bedevere-app

bedevere-app Bot commented Nov 28, 2026

Copy link
Copy Markdown

Thank you for your interest in contributing to Python. This is a community-driven project, and we want it to be a welcoming place to learn and collaborate.

Contributors are responsible for reviewing and understanding what they submit, checking its claims, and being able to explain changes in their own words. This helps reviewers focus on the contribution and leaves more time to support other contributors.

This contribution appears to have been authored with AI assistance. Please explain your contribution in your own words and describe how you checked its correctness.

Please read our guidelines for using AI tools.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI-needs-explanation AI-assisted work that needs further explanation and follow up from the contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

http.cookiejar reads a two-digit Expires year by the current year's century, so Expires=...-70... no longer deletes a cookie

2 participants