[Python-checkins] [3.11] gh-99576: Fix cookiejar file that was not truncated for some classes (GH-99616) (GH-100377)
ambv
webhook-mailer at python.org
Tue Dec 20 12:22:55 EST 2022
https://github.com/python/cpython/commit/73d2b15bd278e84e89de83dbd2ee64eac2e63cda
commit: 73d2b15bd278e84e89de83dbd2ee64eac2e63cda
branch: 3.11
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: ambv <lukasz at langa.pl>
date: 2022-12-20T18:22:39+01:00
summary:
[3.11] gh-99576: Fix cookiejar file that was not truncated for some classes (GH-99616) (GH-100377)
(cherry picked from commit 44892d45b038f919b0378590a776580a9d73b291)
Co-authored-by: Nikita Sobolev <mail at sobolevn.me>
Co-authored-by: Łukasz Langa <lukasz at langa.pl>
files:
A Misc/NEWS.d/next/Library/2022-11-20-11-59-54.gh-issue-99576.ZD7jU6.rst
M Lib/http/cookiejar.py
M Lib/test/test_http_cookiejar.py
diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py
index 65c45e2b17df..e622fc36cbfc 100644
--- a/Lib/http/cookiejar.py
+++ b/Lib/http/cookiejar.py
@@ -1890,7 +1890,10 @@ def save(self, filename=None, ignore_discard=False, ignore_expires=False):
if self.filename is not None: filename = self.filename
else: raise ValueError(MISSING_FILENAME_TEXT)
- with os.fdopen(os.open(filename, os.O_CREAT | os.O_WRONLY, 0o600), 'w') as f:
+ with os.fdopen(
+ os.open(filename, os.O_CREAT | os.O_WRONLY | os.O_TRUNC, 0o600),
+ 'w',
+ ) as f:
# There really isn't an LWP Cookies 2.0 format, but this indicates
# that there is extra information in here (domain_dot and
# 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):
if self.filename is not None: filename = self.filename
else: raise ValueError(MISSING_FILENAME_TEXT)
- with os.fdopen(os.open(filename, os.O_CREAT | os.O_WRONLY, 0o600), 'w') as f:
+ with os.fdopen(
+ os.open(filename, os.O_CREAT | os.O_WRONLY | os.O_TRUNC, 0o600),
+ 'w',
+ ) as f:
f.write(NETSCAPE_HEADER_TEXT)
now = time.time()
for cookie in self:
diff --git a/Lib/test/test_http_cookiejar.py b/Lib/test/test_http_cookiejar.py
index f0d205a60a4c..b90612867fef 100644
--- a/Lib/test/test_http_cookiejar.py
+++ b/Lib/test/test_http_cookiejar.py
@@ -398,6 +398,32 @@ def test_mozilla_filepermissions(self):
finally:
os_helper.unlink(filename)
+ @unittest.skipIf(mswindows, "windows file permissions are incompatible with file modes")
+ @os_helper.skip_unless_working_chmod
+ def test_cookie_files_are_truncated(self):
+ filename = os_helper.TESTFN
+ for cookiejar_class in (LWPCookieJar, MozillaCookieJar):
+ c = cookiejar_class(filename)
+
+ req = urllib.request.Request("http://www.acme.com/")
+ headers = ["Set-Cookie: pll_lang=en; Max-Age=31536000; path=/"]
+ res = FakeResponse(headers, "http://www.acme.com/")
+ c.extract_cookies(res, req)
+ self.assertEqual(len(c), 1)
+
+ try:
+ # Save the first version with contents:
+ c.save()
+ # Now, clear cookies and re-save:
+ c.clear()
+ c.save()
+ # Check that file was truncated:
+ c.load()
+ finally:
+ os_helper.unlink(filename)
+
+ self.assertEqual(len(c), 0)
+
def test_bad_magic(self):
# OSErrors (eg. file doesn't exist) are allowed to propagate
filename = os_helper.TESTFN
diff --git a/Misc/NEWS.d/next/Library/2022-11-20-11-59-54.gh-issue-99576.ZD7jU6.rst b/Misc/NEWS.d/next/Library/2022-11-20-11-59-54.gh-issue-99576.ZD7jU6.rst
new file mode 100644
index 000000000000..9cbeb64b5625
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-11-20-11-59-54.gh-issue-99576.ZD7jU6.rst
@@ -0,0 +1,2 @@
+Fix ``.save()`` method for ``LWPCookieJar`` and ``MozillaCookieJar``: saved
+file was not truncated on repeated save.
More information about the Python-checkins
mailing list