Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions Lib/http/cookiejar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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:

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

yr = ("19" if yr >= "70" else "20") + yr

return _str2time(day, mon, yr, hr, min, sec, tz)

ISO_DATE_RE = re.compile(
Expand Down
33 changes: 33 additions & 0 deletions Lib/test/test_http_cookiejar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -1536,6 +1556,19 @@ def cookiejar_from_cookie_headers(headers):
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)

# 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"])

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

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.

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Loading