Skip to content

Commit d055b66

Browse files
pSubmgorny
authored andcommitted
pythongh-79096: Protect cookie file created by {LWP,Mozilla}CookieJar.save() (pythonGH-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 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. (backported to 3.8 by Michał Górny)
1 parent ed28573 commit d055b66

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

Lib/http/cookiejar.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1887,7 +1887,7 @@ def save(self, filename=None, ignore_discard=False, ignore_expires=False):
18871887
if self.filename is not None: filename = self.filename
18881888
else: raise ValueError(MISSING_FILENAME_TEXT)
18891889

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

2085-
with open(filename, "w") as f:
2085+
with os.fdopen(os.open(filename, os.O_CREAT | os.O_WRONLY, 0o600), 'w') as f:
20862086
f.write(self.header)
20872087
now = time.time()
20882088
for cookie in self:

Lib/test/test_http_cookiejar.py

Lines changed: 31 additions & 0 deletions
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
import time
@@ -15,6 +16,7 @@
1516
reach, is_HDN, domain_match, user_domain_match, request_path,
1617
request_port, request_host)
1718

19+
mswindows = (sys.platform == "win32")
1820

1921
class DateTimeTests(unittest.TestCase):
2022

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

371+
@unittest.skipIf(mswindows, "windows file permissions are incompatible with file modes")
372+
def test_lwp_filepermissions(self):
373+
# Cookie file should only be readable by the creator
374+
filename = test.support.TESTFN
375+
c = LWPCookieJar()
376+
interact_netscape(c, "http://www.acme.com/", 'boo')
377+
try:
378+
c.save(filename, ignore_discard=True)
379+
status = os.stat(filename)
380+
print(status.st_mode)
381+
self.assertEqual(oct(status.st_mode)[-3:], '600')
382+
finally:
383+
try: os.unlink(filename)
384+
except OSError: pass
385+
386+
@unittest.skipIf(mswindows, "windows file permissions are incompatible with file modes")
387+
def test_mozilla_filepermissions(self):
388+
# Cookie file should only be readable by the creator
389+
filename = test.support.TESTFN
390+
c = MozillaCookieJar()
391+
interact_netscape(c, "http://www.acme.com/", 'boo')
392+
try:
393+
c.save(filename, ignore_discard=True)
394+
status = os.stat(filename)
395+
self.assertEqual(oct(status.st_mode)[-3:], '600')
396+
finally:
397+
try: os.unlink(filename)
398+
except OSError: pass
399+
369400
def test_bad_magic(self):
370401
# OSErrors (eg. file doesn't exist) are allowed to propagate
371402
filename = test.support.TESTFN
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
LWPCookieJar and MozillaCookieJar create files with file mode 600 instead of 644 (Microsoft Windows is not affected)

0 commit comments

Comments
 (0)