Skip to content

Commit 4b79d9f

Browse files
miss-islingtonRémi Lapeyreserhiy-storchaka
authored
[3.13] gh-61366: Read session cookies written by curl and Wget (GH-11792) (GH-155605)
(cherry picked from commit e37cf49) Co-authored-by: Rémi Lapeyre <remi.lapeyre@henki.fr> Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 4e37cb5 commit 4b79d9f

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

20552055
discard = False
2056-
if expires == "":
2056+
# curl and Wget set expires to 0 for session cookies.
2057+
if expires == "0" or expires == "":
20572058
expires = None
20582059
discard = True
20592060

Lib/test/test_http_cookiejar.py

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

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

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

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

20452073
if __name__ == "__main__":
20462074
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)