Skip to content

Commit e37cf49

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

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
@@ -2056,7 +2056,8 @@ def _really_load(self, f, filename, ignore_discard, ignore_expires):
20562056
assert domain_specified == initial_dot
20572057

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

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

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

2051+
def test_load_session_cookies(self):
2052+
# curl and Wget write 0 in the expires field for session cookies,
2053+
# while we write an empty field. Both should be read (gh-61366).
2054+
filename = os_helper.TESTFN
2055+
self.addCleanup(os_helper.unlink, filename)
2056+
expires = int(time.time() + 3600)
2057+
with open(filename, "w") as f:
2058+
f.write(NETSCAPE_HEADER_TEXT)
2059+
f.write("www.foo.com\tFALSE\t/\tFALSE\t%u\tperm\tbar\n" % expires)
2060+
f.write("www.foo.com\tFALSE\t/\tFALSE\t0\tcurl_session\tbar\n")
2061+
f.write("www.foo.com\tFALSE\t/\tFALSE\t\tour_session\tbar\n")
2062+
2063+
c = MozillaCookieJar()
2064+
c.revert(filename)
2065+
self.assertEqual([cookie.name for cookie in c], ["perm"])
2066+
2067+
c = MozillaCookieJar()
2068+
c.revert(filename, ignore_discard=True)
2069+
self.assertEqual(sorted(cookie.name for cookie in c),
2070+
["curl_session", "our_session", "perm"])
2071+
for cookie in c:
2072+
if cookie.name == "perm":
2073+
self.assertEqual(cookie.expires, expires)
2074+
self.assertFalse(cookie.discard)
2075+
else:
2076+
self.assertIsNone(cookie.expires)
2077+
self.assertTrue(cookie.discard)
2078+
20512079

20522080
if __name__ == "__main__":
20532081
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)