Skip to content

Commit 68b2df8

Browse files
Write 0 in the expiration time field for session cookies
curl and Wget ignore the line if this field is empty.
1 parent e37cf49 commit 68b2df8

3 files changed

Lines changed: 35 additions & 1 deletion

File tree

Lib/http/cookiejar.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2109,7 +2109,9 @@ def save(self, filename=None, ignore_discard=False, ignore_expires=False):
21092109
if cookie.expires is not None:
21102110
expires = str(cookie.expires)
21112111
else:
2112-
expires = ""
2112+
# curl and Wget use 0 for session cookies, and ignore
2113+
# the line if this field is empty.
2114+
expires = "0"
21132115
if cookie.value is None:
21142116
# cookies.txt regards 'Set-Cookie: foo' as a cookie
21152117
# with no name, whereas http.cookiejar regards it as a

Lib/test/test_http_cookiejar.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2048,6 +2048,35 @@ 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_save_session_cookies(self):
2052+
# Session cookies are saved with 0 in the expiration time field,
2053+
# as curl and Wget do (gh-61366).
2054+
filename = os_helper.TESTFN
2055+
self.addCleanup(os_helper.unlink, filename)
2056+
expires = int(time.time() + 3600)
2057+
c = MozillaCookieJar()
2058+
c.set_cookie(Cookie(0, "perm", "bar", None, False,
2059+
"www.foo.com", True, False, "/", False, False,
2060+
expires, False, None, None, {}))
2061+
c.set_cookie(Cookie(0, "session", "bar", None, False,
2062+
"www.foo.com", True, False, "/", False, False,
2063+
None, True, None, None, {}))
2064+
c.save(filename, ignore_discard=True)
2065+
2066+
saved = {}
2067+
with open(filename) as f:
2068+
for line in f:
2069+
if line.strip() and not line.startswith("#"):
2070+
fields = line.split("\t")
2071+
saved[fields[5]] = fields[4]
2072+
self.assertEqual(saved, {"perm": str(expires), "session": "0"})
2073+
2074+
# The saved file can be read back.
2075+
c = MozillaCookieJar()
2076+
c.revert(filename, ignore_discard=True)
2077+
self.assertEqual(sorted(cookie.name for cookie in c),
2078+
["perm", "session"])
2079+
20512080
def test_load_session_cookies(self):
20522081
# curl and Wget write 0 in the expires field for session cookies,
20532082
# while we write an empty field. Both should be read (gh-61366).
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:meth:`http.cookiejar.MozillaCookieJar.save` now writes ``0`` in the
2+
expiration time field for session cookies, as curl and Wget do. Previously
3+
this field was left empty, and such lines were ignored by curl and Wget.

0 commit comments

Comments
 (0)