Skip to content

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

Backlog

Description

@coreyolson

Problem

http.cookiejar uses the current local year to choose a century for a two-digit Expires year. With the clock in 2026, it reads 70 through 76 as 2070 through 2076. In the example below, a response intended to expire an existing cookie replaces it with an empty cookie whose expiry is in 2070.

import email.message, urllib.request
from http.cookiejar import CookieJar

class Response:
    def __init__(self, *set_cookie):
        self._h = email.message.Message()
        for v in set_cookie:
            self._h["Set-Cookie"] = v
    def info(self):
        return self._h

jar = CookieJar()
req = urllib.request.Request("https://example.com/")
jar.extract_cookies(Response("session=abc123; Path=/"), req)
print([c.name for c in jar])        # ['session']

# The response attempts to delete the existing cookie.
jar.extract_cookies(Response("session=; Path=/; Expires=Thu, 01-Jan-70 00:00:01 GMT"), req)
print([(c.name, c.expires) for c in jar])

Output on 3.15.2:

['session']
[('session', 3155760001)]

3155760001 is 2070-01-01 00:00:01 UTC. So the cookie is not removed: it becomes an empty cookie that lasts until 2070. urllib.request.HTTPCookieProcessor and anything else built on CookieJar sees the same thing. http.cookiejar.http2time("Thu, 01-Jan-70 00:00:01 GMT") returns 3155760001 too.

The example uses a legacy hyphenated date with a two-digit year. RFC 6265 §4.1.1 recommends a four-digit year for servers, and §5.1.1 specifies how clients interpret two-digit years.

Expected

RFC 6265, section 5.1.1, gives the rule for parsing a cookie's Expires date:

  1. If the year-value is greater than or equal to 70 and less than or equal to 99, increment the year-value by 1900.

  2. If the year-value is greater than or equal to 0 and less than or equal to 69, increment the year-value by 2000.

The expiry in the example is therefore 1970-01-01 00:00:01 UTC. The incoming cookie matches the stored cookie's name, domain, and path. Under §5.3, it replaces the stored cookie and is then evicted because its expiry is in the past. The second print() should show [].

RFC 6265 section 5.3 says:

The user agent MUST evict all expired cookies from the cookie store if, at any time, an expired cookie exists in the cookie store.

Cause

_str2time in Lib/http/cookiejar.py picks the century from the current year:

if yr < 1000:
    # find "obvious" year
    cur_yr = time.localtime(time.time())[0]
    m = cur_yr % 100
    tmp = yr
    yr = yr + cur_yr - m
    m = m - tmp
    if abs(m) > 50:
        if m > 0: yr = yr + 100
        else: yr = yr - 100

In 2026 this maps 00-76 to 2000-2076 and 77-99 to 1977-1999. The http2time docstring describes this heuristic. It means the result depends on the clock: 70 meant 1970 until 2019 and has meant 2070 since 2020.

Suggested fix

Apply the RFC 6265 pivot to exactly two-digit years in the loose http2time() parser. Insert this immediately before its call to _str2time().

if len(yr) == 2:
    yr = int(yr)
    yr += 1900 if yr >= 70 else 2000

Update the http2time() docstring to say: "Two-digit years 00-69 are interpreted as 2000-2069, and 70-99 as 1970-1999." Add a regression test for the 69/70 boundary and for removal of the cookie in the example. Applying the pivot only to exactly two digits leaves ordinary four-digit years, and legacy three-digit and zero-padded years, unchanged.

Tested with

The original reproduction used CPython 3.15.2 on macOS arm64. The same year-parsing code is present on main (8eb3d2c).

An independent check loaded the v3.15.2 module on CPython 3.14.3. With the proposed http2time() change, the second print() produced []. Of 1,204 Expires inputs, 53 changed, all with years from 70 through 76. All 53 then matched the RFC reference results. The 81 tests in the v3.15.2 test_http_cookiejar module passed under the same interpreter with a test-harness compatibility shim.

Found with a new AI-assisted methodology from a Pullboard discovery. A paper describing it will follow.

Links

Linked PRs

Activity

  1. added
    type-bug
    stdlib
    on Nov 25, 2026
  2. moved this from Todo to In Progress in HTTP issueson Nov 25, 2026
  3. added this to Cookies issues 🍪 and removed this from HTTP issueson Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibtype-bug

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions