Skip to content

Commit 5043853

Browse files
remilapeyreRémi Lapeyreserhiy-storchaka
authored andcommitted
gh-61366: Read session cookies written by curl and Wget (GH-11792)
(cherry picked from commit e37cf49) Co-authored-by: Rémi Lapeyre <remi@elements.me> Co-authored-by: Rémi Lapeyre <remi.lapeyre@henki.fr> Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 1bcc1e4 commit 5043853

3 files changed

Lines changed: 34 additions & 2 deletions

File tree

Lib/http/cookiejar.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2055,7 +2055,8 @@ def _really_load(self, f, filename, ignore_discard, ignore_expires):
20552055
assert domain_specified == initial_dot
20562056

20572057
discard = False
2058-
if expires == "":
2058+
# curl and Wget set expires to 0 for session cookies.
2059+
if expires == "0" or expires == "":
20592060
expires = None
20602061
discard = True
20612062

Lib/test/test_http_cookiejar.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
CookieJar, DefaultCookiePolicy, LWPCookieJar, MozillaCookieJar,
1717
LoadError, lwp_cookie_str, DEFAULT_HTTP_PORT, escape_path,
1818
reach, is_HDN, domain_match, user_domain_match, request_path,
19-
request_port, request_host)
19+
request_port, request_host, NETSCAPE_HEADER_TEXT)
2020

2121
mswindows = (sys.platform == "win32")
2222

@@ -2023,6 +2023,34 @@ def test_session_cookies(self):
20232023
# we didn't have session cookies in the first place
20242024
self.assertNotEqual(counter["session_before"], 0)
20252025

2026+
def test_load_session_cookies(self):
2027+
# curl and Wget write 0 in the expires field for session cookies,
2028+
# while we write an empty field. Both should be read (gh-61366).
2029+
filename = os_helper.TESTFN
2030+
self.addCleanup(os_helper.unlink, filename)
2031+
expires = int(time.time() + 3600)
2032+
with open(filename, "w") as f:
2033+
f.write(NETSCAPE_HEADER_TEXT)
2034+
f.write("www.foo.com\tFALSE\t/\tFALSE\t%u\tperm\tbar\n" % expires)
2035+
f.write("www.foo.com\tFALSE\t/\tFALSE\t0\tcurl_session\tbar\n")
2036+
f.write("www.foo.com\tFALSE\t/\tFALSE\t\tour_session\tbar\n")
2037+
2038+
c = MozillaCookieJar()
2039+
c.revert(filename)
2040+
self.assertEqual([cookie.name for cookie in c], ["perm"])
2041+
2042+
c = MozillaCookieJar()
2043+
c.revert(filename, ignore_discard=True)
2044+
self.assertEqual(sorted(cookie.name for cookie in c),
2045+
["curl_session", "our_session", "perm"])
2046+
for cookie in c:
2047+
if cookie.name == "perm":
2048+
self.assertEqual(cookie.expires, expires)
2049+
self.assertFalse(cookie.discard)
2050+
else:
2051+
self.assertIsNone(cookie.expires)
2052+
self.assertTrue(cookie.discard)
2053+
20262054

20272055
if __name__ == "__main__":
20282056
unittest.main()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:class:`http.cookiejar.MozillaCookieJar` now reads session cookies written
2+
by curl and Wget, which use ``0`` in the expiration time field.
3+
Contributed by Jérémie Detrey.

0 commit comments

Comments
 (0)