From 26ab02661889ab41ba456d12d586d46034901f84 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sun, 20 Nov 2022 12:01:05 +0300 Subject: [PATCH 1/2] gh-99576: Fix cookiejar file that was not truncated for some classes --- Lib/http/cookiejar.py | 8 ++++-- Lib/test/test_http_cookiejar.py | 26 +++++++++++++++++++ ...2-11-20-11-59-54.gh-issue-99576.ZD7jU6.rst | 2 ++ 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2022-11-20-11-59-54.gh-issue-99576.ZD7jU6.rst diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py index 65c45e2b17dfc0..26f81290ee90b4 100644 --- a/Lib/http/cookiejar.py +++ b/Lib/http/cookiejar.py @@ -1890,7 +1890,9 @@ 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 +2088,9 @@ 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 f8291c2aa32cfe..97e9c82cde9ec4 100644 --- a/Lib/test/test_http_cookiejar.py +++ b/Lib/test/test_http_cookiejar.py @@ -397,6 +397,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 00000000000000..9cbeb64b56250b --- /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. From 7715d924bc6fca55b4cd1eee870690ccfadec3ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Langa?= Date: Tue, 20 Dec 2022 17:10:03 +0100 Subject: [PATCH 2/2] Fix crimes against bracket pair formatting --- Lib/http/cookiejar.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py index 26f81290ee90b4..e622fc36cbfcf6 100644 --- a/Lib/http/cookiejar.py +++ b/Lib/http/cookiejar.py @@ -1891,8 +1891,9 @@ def save(self, filename=None, ignore_discard=False, ignore_expires=False): else: raise ValueError(MISSING_FILENAME_TEXT) with os.fdopen( - os.open(filename, os.O_CREAT | os.O_WRONLY | os.O_TRUNC, 0o600, - ), 'w') as f: + 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. @@ -2089,8 +2090,9 @@ def save(self, filename=None, ignore_discard=False, ignore_expires=False): else: raise ValueError(MISSING_FILENAME_TEXT) with os.fdopen( - os.open(filename, os.O_CREAT | os.O_WRONLY | os.O_TRUNC, 0o600, - ), 'w') as f: + 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: