Skip to content

Commit ce998c0

Browse files
committed
pythongh-79096: Protect cookie file created by {LWP,Mozilla}CookieJar.save()
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 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 ce998c0

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-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

+31
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Tests for http/cookiejar.py."""
22

33
import os
4+
import sys
45
import re
56
import test.support
67
from test.support import os_helper
@@ -17,6 +18,7 @@
1718
reach, is_HDN, domain_match, user_domain_match, request_path,
1819
request_port, request_host)
1920

21+
mswindows = (sys.platform == "win32")
2022

2123
class DateTimeTests(unittest.TestCase):
2224

@@ -368,6 +370,35 @@ def test_lwp_valueless_cookie(self):
368370
except OSError: pass
369371
self.assertEqual(c._cookies["www.acme.com"]["/"]["boo"].value, None)
370372

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

0 commit comments

Comments
 (0)