Skip to content

Commit 44892d4

Browse files
sobolevnambv
andauthored
gh-99576: Fix cookiejar file that was not truncated for some classes (GH-99616)
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
1 parent cb60b61 commit 44892d4

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

Lib/http/cookiejar.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -1890,7 +1890,10 @@ def save(self, filename=None, ignore_discard=False, ignore_expires=False):
18901890
if self.filename is not None: filename = self.filename
18911891
else: raise ValueError(MISSING_FILENAME_TEXT)
18921892

1893-
with os.fdopen(os.open(filename, os.O_CREAT | os.O_WRONLY, 0o600), 'w') as f:
1893+
with os.fdopen(
1894+
os.open(filename, os.O_CREAT | os.O_WRONLY | os.O_TRUNC, 0o600),
1895+
'w',
1896+
) as f:
18941897
# There really isn't an LWP Cookies 2.0 format, but this indicates
18951898
# that there is extra information in here (domain_dot and
18961899
# port_spec) while still being compatible with libwww-perl, I hope.
@@ -2081,7 +2084,10 @@ def save(self, filename=None, ignore_discard=False, ignore_expires=False):
20812084
if self.filename is not None: filename = self.filename
20822085
else: raise ValueError(MISSING_FILENAME_TEXT)
20832086

2084-
with os.fdopen(os.open(filename, os.O_CREAT | os.O_WRONLY, 0o600), 'w') as f:
2087+
with os.fdopen(
2088+
os.open(filename, os.O_CREAT | os.O_WRONLY | os.O_TRUNC, 0o600),
2089+
'w',
2090+
) as f:
20852091
f.write(NETSCAPE_HEADER_TEXT)
20862092
now = time.time()
20872093
for cookie in self:

Lib/test/test_http_cookiejar.py

+26
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,32 @@ def test_mozilla_filepermissions(self):
397397
finally:
398398
os_helper.unlink(filename)
399399

400+
@unittest.skipIf(mswindows, "windows file permissions are incompatible with file modes")
401+
@os_helper.skip_unless_working_chmod
402+
def test_cookie_files_are_truncated(self):
403+
filename = os_helper.TESTFN
404+
for cookiejar_class in (LWPCookieJar, MozillaCookieJar):
405+
c = cookiejar_class(filename)
406+
407+
req = urllib.request.Request("http://www.acme.com/")
408+
headers = ["Set-Cookie: pll_lang=en; Max-Age=31536000; path=/"]
409+
res = FakeResponse(headers, "http://www.acme.com/")
410+
c.extract_cookies(res, req)
411+
self.assertEqual(len(c), 1)
412+
413+
try:
414+
# Save the first version with contents:
415+
c.save()
416+
# Now, clear cookies and re-save:
417+
c.clear()
418+
c.save()
419+
# Check that file was truncated:
420+
c.load()
421+
finally:
422+
os_helper.unlink(filename)
423+
424+
self.assertEqual(len(c), 0)
425+
400426
def test_bad_magic(self):
401427
# OSErrors (eg. file doesn't exist) are allowed to propagate
402428
filename = os_helper.TESTFN
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix ``.save()`` method for ``LWPCookieJar`` and ``MozillaCookieJar``: saved
2+
file was not truncated on repeated save.

0 commit comments

Comments
 (0)