Repository navigation
gh-162592: use RFC 6265 century pivot for two-digit years in http.cookiejar
#162596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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: | ||
|
|
@@ -270,8 +276,6 @@ def http2time(text): | |
|
|
||
| # No, we need some messy parsing... | ||
|
|
||
| # clean up | ||
| text = text.lstrip() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why was this changed? is it an agent doing the work? |
||
| text = WEEKDAY_RE.sub("", text, 1) # Useless weekday | ||
|
|
||
| # tz is time zone specifier string | ||
|
|
@@ -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: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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)), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also add the cases 1969 if you use 2069, |
||
| ("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. | ||
|
|
@@ -1536,6 +1556,19 @@ def cookiejar_from_cookie_headers(headers): | |
| self.assertIsNone(cookie.expires) | ||
|
|
||
|
|
||
| def test_expiry_with_two_digit_year(self): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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/" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use example.com (i think this is what we use elsewhere) |
||
| interact_netscape(c, url, "session=abc123; Path=/") | ||
| self.assertEqual([cookie.name for cookie in c], ["session"]) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
|
||
|
|
||
| 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. |
There was a problem hiding this comment.
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.