Skip to content

bpo-17164: Make MozillaCookieJar use curl's format for session cookies #11792

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
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Lib/http/cookiejar.py
Original file line number Diff line number Diff line change
Expand Up @@ -2033,7 +2033,8 @@ def _really_load(self, f, filename, ignore_discard, ignore_expires):
assert domain_specified == initial_dot

discard = False
if expires == "":
# curl and Wget set expires to 0 for session cookies.
if expires == "0" or expires == "":
expires = None
discard = True

Expand Down Expand Up @@ -2081,7 +2082,7 @@ def save(self, filename=None, ignore_discard=False, ignore_expires=False):
if cookie.expires is not None:
expires = str(cookie.expires)
else:
expires = ""
expires = "0"
if cookie.value is None:
# cookies.txt regards 'Set-Cookie: foo' as a cookie
# with no name, whereas http.cookiejar regards it as a
Expand Down
27 changes: 27 additions & 0 deletions Lib/test/test_http_cookiejar.py
Original file line number Diff line number Diff line change
Expand Up @@ -1811,6 +1811,33 @@ def test_session_cookies(self):
# we didn't have session cookies in the first place
self.assertNotEqual(counter["session_before"], 0)

def test_curl_format(self):
# Check compatibility with curl / wget cookiejar format.
# See issue 17164
filename = test.support.TESTFN
try:
expires = int(time.time() + 3600)
with open(filename, "w") as f:
f.write(MozillaCookieJar.header)
f.write("www.foo.com\tFALSE\t/\tFALSE\t%u\tfoo1\tbar\n" %
expires)
f.write("www.foo.com\tFALSE\t/\tFALSE\t0\tfoo2\tbar\n")
f.write("www.foo.com\tFALSE\t/\tFALSE\t\tfoo3\tbar\n")
c = MozillaCookieJar()
c.revert(filename)
self.assertEqual(len(c), 1)
c.revert(filename, ignore_discard = True)
self.assertEqual(len(c), 3)
c.save(filename, ignore_discard = True)
with open(filename, "r") as f:
for line in f:
if line == '\n' or line.startswith('#'):
continue
self.assertRegex(line.split('\t')[4], '^\d+$')
finally:
try: os.unlink(filename)
except OSError: pass


def test_main(verbose=None):
test.support.run_unittest(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:class:`http.cookiejar.MozillaCookieJar` is now compatible with curl and wget cookiejar file
format. Contributed by Jérémie Detrey.