Skip to content

Commit 73d2b15

Browse files
miss-islingtonsobolevnambv
authored
[3.11] gh-99576: Fix cookiejar file that was not truncated for some classes (GH-99616) (GH-100377)
(cherry picked from commit 44892d4) Co-authored-by: Nikita Sobolev <mail@sobolevn.me> Co-authored-by: Łukasz Langa <lukasz@langa.pl>
1 parent 1332fda commit 73d2b15

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.
@@ -2086,7 +2089,10 @@ def save(self, filename=None, ignore_discard=False, ignore_expires=False):
20862089
if self.filename is not None: filename = self.filename
20872090
else: raise ValueError(MISSING_FILENAME_TEXT)
20882091

2089-
with os.fdopen(os.open(filename, os.O_CREAT | os.O_WRONLY, 0o600), 'w') as f:
2092+
with os.fdopen(
2093+
os.open(filename, os.O_CREAT | os.O_WRONLY | os.O_TRUNC, 0o600),
2094+
'w',
2095+
) as f:
20902096
f.write(NETSCAPE_HEADER_TEXT)
20912097
now = time.time()
20922098
for cookie in self:

Lib/test/test_http_cookiejar.py

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

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