]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-99576: Fix cookiejar file that was not truncated for some classes (GH-99616...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 20 Dec 2022 17:22:39 +0000 (09:22 -0800)
committerGitHub <noreply@github.com>
Tue, 20 Dec 2022 17:22:39 +0000 (18:22 +0100)
(cherry picked from commit 44892d45b038f919b0378590a776580a9d73b291)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Co-authored-by: Ɓukasz Langa <lukasz@langa.pl>
Lib/http/cookiejar.py
Lib/test/test_http_cookiejar.py
Misc/NEWS.d/next/Library/2022-11-20-11-59-54.gh-issue-99576.ZD7jU6.rst [new file with mode: 0644]

index 65c45e2b17dfc01dd52ca566c70aae538f65f68a..e622fc36cbfcf6203c05211ae73053cd7ddab058 100644 (file)
@@ -1890,7 +1890,10 @@ class LWPCookieJar(FileCookieJar):
             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 @@ class MozillaCookieJar(FileCookieJar):
             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:
index f0d205a60a4cad99eb57f3d2fc19e79a31b9429f..b90612867fef59c77d9e8f0c99f344a12d73cb49 100644 (file)
@@ -398,6 +398,32 @@ class FileCookieJarTests(unittest.TestCase):
         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 (file)
index 0000000..9cbeb64
--- /dev/null
@@ -0,0 +1,2 @@
+Fix ``.save()`` method for ``LWPCookieJar`` and ``MozillaCookieJar``: saved
+file was not truncated on repeated save.