Repository navigation
gh-162592: use RFC 6265 century pivot for two-digit years in http.cookiejar - #162596
vikash1311994 wants to merge 2 commits into
Conversation
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.
http.cookiejar
| 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 |
There was a problem hiding this comment.
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. | |||
There was a problem hiding this comment.
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"]) |
| self.assertIsNone(cookie.expires) | ||
|
|
||
|
|
||
| def test_expiry_with_two_digit_year(self): |
There was a problem hiding this comment.
Add some tests with year 69 and full years that are lose (if any)
| # 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: |
There was a problem hiding this comment.
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).
| # "01-Jan-70" continue to mean 1970. | ||
| if len(yr) == 2: | ||
| yr = int(yr) | ||
| yr += 1900 if yr >= 70 else 2000 |
There was a problem hiding this comment.
This makes yr an int while before it was a string. Is it ok for _str2time?
af2bcf5 to
eff663b
Compare
Fixes python/cpython#162592.
Problem
http.cookiejar.http2time()interprets a two-digitExpiresyear by choosing the century nearest the current date (_str2time's "obvious year" heuristic). With the clock in 2026,70becomes 2070, so aSet-Cookiethat tries to delete a cookie withExpires=Thu, 01-Jan-70 00:00:01 GMTinstead 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 thehttp2time()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-digitExpiresactually deletes the stored cookie)Verification evidence:
3155760001 != 1; a cookie withexpires=3155760001remains in the jar).cookiejar.py-> 2 failures; restored the fix -> 0 failures../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, and20. The change also removes the clock dependence that made those tests time-bombed ("will break around year 2048/2070").Exclusions
_str2time()'s heuristic for other sub-1000 years.iso2time()(its year regex requires four digits).