summaryrefslogtreecommitdiffstats
path: root/Lib/http
diff options
context:
space:
mode:
authorPascal Wittmann <mail@pascal-wittmann.de>2022-06-07 08:11:03 (GMT)
committerGitHub <noreply@github.com>2022-06-07 08:11:03 (GMT)
commitc6f6ede728df144c6c39858f558ea65a2aac7cda (patch)
treef7e53a912a6e5fb0cb85d8cabcc92cdb1f26b005 /Lib/http
parent57e8ee7086bf3f5c50c901e9d5896275644bbb54 (diff)
downloadcpython-c6f6ede728df144c6c39858f558ea65a2aac7cda.zip
cpython-c6f6ede728df144c6c39858f558ea65a2aac7cda.tar.gz
cpython-c6f6ede728df144c6c39858f558ea65a2aac7cda.tar.bz2
gh-79096: Protect cookie file created by {LWP,Mozilla}CookieJar.save() (GH-93463)
Note: This change is not effective on Microsoft Windows. Cookies can store sensitive information and should therefore be protected against unauthorized third parties. This is also described in issue #79096. The filesystem permissions are currently set to 644, everyone can read the file. This commit changes the permissions to 600, only the creater of the file can read and modify it. This improves security, because it reduces the attack surface. Now the attacker needs control of the user that created the cookie or a ways to circumvent the filesystems permissions. This change is backwards incompatible. Systems that rely on world-readable cookies will breake. However, one could argue that those are misconfigured in the first place.
Diffstat (limited to 'Lib/http')
-rw-r--r--Lib/http/cookiejar.py4
1 files changed, 2 insertions, 2 deletions
diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py
index f19a366..c514e0d 100644
--- a/Lib/http/cookiejar.py
+++ b/Lib/http/cookiejar.py
@@ -1890,7 +1890,7 @@ class LWPCookieJar(FileCookieJar):
if self.filename is not None: filename = self.filename
else: raise ValueError(MISSING_FILENAME_TEXT)
- with open(filename, "w") as f:
+ with os.fdopen(os.open(filename, os.O_CREAT | os.O_WRONLY, 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 +2086,7 @@ class MozillaCookieJar(FileCookieJar):
if self.filename is not None: filename = self.filename
else: raise ValueError(MISSING_FILENAME_TEXT)
- with open(filename, "w") as f:
+ with os.fdopen(os.open(filename, os.O_CREAT | os.O_WRONLY, 0o600), 'w') as f:
f.write(NETSCAPE_HEADER_TEXT)
now = time.time()
for cookie in self: