Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-99576: Fix cookiejar file that was not truncated for some classes#99616

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
ambv merged 2 commits intopython:mainfromsobolevn:issue-99576
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletionsLib/http/cookiejar.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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.
Expand DownExpand Up@@ -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:
Expand Down
26 changes: 26 additions & 0 deletionsLib/test/test_http_cookiejar.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
Fix ``.save()`` method for ``LWPCookieJar`` and ``MozillaCookieJar``: saved
file was not truncated on repeated save.

[8]ページ先頭

©2009-2025 Movatter.jp