Skip to content

Commit 7450386

Browse files
committed
pythongh-79096: Protect cookie file created by {LWP,Mozilla}CookieJar.save()
Cookies can store sensitive information and should therefore be protected against unauthorized third parties. This is also described in issue python#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.
1 parent 8a221a8 commit 7450386

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

Lib/http/cookiejar.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1890,7 +1890,7 @@ def save(self, filename=None, ignore_discard=False, ignore_expires=False):
18901890
if self.filename is not None: filename = self.filename
18911891
else: raise ValueError(MISSING_FILENAME_TEXT)
18921892

1893-
with open(filename, "w") as f:
1893+
with os.fdopen(os.open(filename, os.O_CREAT | os.O_WRONLY, 0o600), 'w') as f:
18941894
# There really isn't an LWP Cookies 2.0 format, but this indicates
18951895
# that there is extra information in here (domain_dot and
18961896
# port_spec) while still being compatible with libwww-perl, I hope.
@@ -2086,7 +2086,7 @@ def save(self, filename=None, ignore_discard=False, ignore_expires=False):
20862086
if self.filename is not None: filename = self.filename
20872087
else: raise ValueError(MISSING_FILENAME_TEXT)
20882088

2089-
with open(filename, "w") as f:
2089+
with os.fdopen(os.open(filename, os.O_CREAT | os.O_WRONLY, 0o600), 'w') as f:
20902090
f.write(NETSCAPE_HEADER_TEXT)
20912091
now = time.time()
20922092
for cookie in self:

Lib/test/test_http_cookiejar.py

+27
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,33 @@ def test_lwp_valueless_cookie(self):
368368
except OSError: pass
369369
self.assertEqual(c._cookies["www.acme.com"]["/"]["boo"].value, None)
370370

371+
def test_lwp_filepermissions(self):
372+
# Cookie file should only be readable by the creator
373+
filename = os_helper.TESTFN
374+
c = LWPCookieJar()
375+
interact_netscape(c, "http://www.acme.com/", 'boo')
376+
try:
377+
c.save(filename, ignore_discard=True)
378+
status = os.stat(filename)
379+
print(status.st_mode)
380+
self.assertEqual(oct(status.st_mode)[-3:], '600')
381+
finally:
382+
try: os.unlink(filename)
383+
except OSError: pass
384+
385+
def test_mozilla_filepermissions(self):
386+
# Cookie file should only be readable by the creator
387+
filename = os_helper.TESTFN
388+
c = MozillaCookieJar()
389+
interact_netscape(c, "http://www.acme.com/", 'boo')
390+
try:
391+
c.save(filename, ignore_discard=True)
392+
status = os.stat(filename)
393+
self.assertEqual(oct(status.st_mode)[-3:], '600')
394+
finally:
395+
try: os.unlink(filename)
396+
except OSError: pass
397+
371398
def test_bad_magic(self):
372399
# OSErrors (eg. file doesn't exist) are allowed to propagate
373400
filename = os_helper.TESTFN

0 commit comments

Comments
 (0)