Skip to content

Commit af2bcf5

Browse files
committed
fix(http.cookiejar): address review feedback on year pivot
1 parent 4a5eb7b commit af2bcf5

3 files changed

Lines changed: 26 additions & 15 deletions

File tree

‎Lib/http/cookiejar.py‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,16 @@ def http2time(text):
255255
The parser ignores leading and trailing whitespace. The time may be
256256
absent.
257257

258-
If the year is given with only 2 digits, 00-69 are interpreted as
259-
2000-2069 and 70-99 as 1970-1999, as specified by RFC 6265 section 5.1.1
260-
for cookie expiry dates.
258+
If the year is given with only 2 digits, the century is selected using
259+
the fixed pivot defined by RFC 6265 section 5.1.1.
260+
261+
.. versionchanged:: 3.16
262+
Two-digit years 00-69 are interpreted as 2000-2069 and 70-99 as
263+
1970-1999. Previously, the century closest to the current date was
264+
selected.
261265

262266
"""
267+
text = text.strip()
263268
# fast exit for strictly conforming string
264269
m = STRICT_DATE_RE.search(text)
265270
if m:
@@ -271,8 +276,6 @@ def http2time(text):
271276

272277
# No, we need some messy parsing...
273278

274-
# clean up
275-
text = text.lstrip()
276279
text = WEEKDAY_RE.sub("", text, 1) # Useless weekday
277280

278281
# tz is time zone specifier string
@@ -289,8 +292,7 @@ def http2time(text):
289292
# pivot, not to the current date, so that cookie expiry dates such as
290293
# "01-Jan-70" continue to mean 1970.
291294
if len(yr) == 2:
292-
yr = int(yr)
293-
yr += 1900 if yr >= 70 else 2000
295+
yr = ("19" if yr >= "70" else "20") + yr
294296

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

‎Lib/test/test_http_cookiejar.py‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,22 @@ def parse_date(text):
7373
def test_http2time_two_digit_year(self):
7474
# RFC 6265 section 5.1.1: two-digit years 00-69 mean 2000-2069 and
7575
# 70-99 mean 1970-1999, independent of the current date.
76-
self.assertEqual(http2time("Thu, 01-Jan-70 00:00:01 GMT"), 1)
77-
self.assertEqual(time.gmtime(http2time("01-Jan-69"))[:6],
76+
cases = [
77+
("01-Jan-00", (2000, 1, 1, 0, 0, 0.0)),
78+
("01-Jan-69", (2069, 1, 1, 0, 0, 0.0)),
79+
("01-Jan-70", (1970, 1, 1, 0, 0, 0.0)),
80+
("01-Jan-99", (1999, 1, 1, 0, 0, 0.0)),
81+
("01-Jan-1970", (1970, 1, 1, 0, 0, 0.0)),
82+
("01-Jan-2069", (2069, 1, 1, 0, 0, 0.0)),
83+
("01-Jan-2070", (2070, 1, 1, 0, 0, 0.0)),
84+
]
85+
for text, expected in cases:
86+
with self.subTest(text=text):
87+
self.assertEqual(time.gmtime(http2time(text))[:6], expected)
88+
89+
self.assertEqual(http2time(" \tThu, 01-Jan-70 00:00:01 GMT\n"), 1)
90+
self.assertEqual(time.gmtime(http2time(" 01-Jan-69 \n"))[:6],
7891
(2069, 1, 1, 0, 0, 0.0))
79-
self.assertEqual(time.gmtime(http2time("01-Jan-70"))[:6],
80-
(1970, 1, 1, 0, 0, 0.0))
81-
self.assertEqual(time.gmtime(http2time("01-Jan-99"))[:6],
82-
(1999, 1, 1, 0, 0, 0.0))
8392

8493
def test_http2time_formats(self):
8594
# test http2time for supported dates. Test cases with 2 digit year
@@ -1557,7 +1566,7 @@ def test_expiry_with_two_digit_year(self):
15571566
self.assertEqual([cookie.name for cookie in c], ["session"])
15581567
interact_netscape(c, url,
15591568
"session=; Path=/; Expires=Thu, 01-Jan-70 00:00:01 GMT")
1560-
self.assertEqual(list(c), [])
1569+
self.assertListEqual(list(c), [])
15611570

15621571

15631572
class LWPCookieTests(unittest.TestCase):
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
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.
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.

0 commit comments

Comments
 (0)