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
Open
vikash1311994
wants to merge
2
commits into
python:main
Choose a base branch
from
vikash1311994:fix/gh-162592-cookiejar-two-digit-year
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+48
−4
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)), | ||
| ("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/" | ||
| 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. | ||
|
|
||
|
|
||
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Library/2026-11-25-09-00-00.gh-issue-162592.Ab3xY9.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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).