Skip to content

Commit 2084f94

Browse files
ambvpSubtiran
authored
[3.11] gh-79096: Protect cookie file created by {LWP,Mozilla}CookieJar.save() (GH-93463) (GH-93636)
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. Co-authored-by: Łukasz Langa <[email protected]> Co-authored-by: Pascal Wittmann <[email protected]> Co-authored-by: Christian Heimes <[email protected]>
1 parent 92f8786 commit 2084f94

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
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

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

33
import os
4+
import stat
5+
import sys
46
import re
57
import test.support
68
from test.support import os_helper
@@ -17,6 +19,7 @@
1719
reach, is_HDN, domain_match, user_domain_match, request_path,
1820
request_port, request_host)
1921

22+
mswindows = (sys.platform == "win32")
2023

2124
class DateTimeTests(unittest.TestCase):
2225

@@ -364,10 +367,37 @@ def test_lwp_valueless_cookie(self):
364367
c = LWPCookieJar()
365368
c.load(filename, ignore_discard=True)
366369
finally:
367-
try: os.unlink(filename)
368-
except OSError: pass
370+
os_helper.unlink(filename)
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+
@os_helper.skip_unless_working_chmod
375+
def test_lwp_filepermissions(self):
376+
# Cookie file should only be readable by the creator
377+
filename = os_helper.TESTFN
378+
c = LWPCookieJar()
379+
interact_netscape(c, "http://www.acme.com/", 'boo')
380+
try:
381+
c.save(filename, ignore_discard=True)
382+
st = os.stat(filename)
383+
self.assertEqual(stat.S_IMODE(st.st_mode), 0o600)
384+
finally:
385+
os_helper.unlink(filename)
386+
387+
@unittest.skipIf(mswindows, "windows file permissions are incompatible with file modes")
388+
@os_helper.skip_unless_working_chmod
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+
st = os.stat(filename)
397+
self.assertEqual(stat.S_IMODE(st.st_mode), 0o600)
398+
finally:
399+
os_helper.unlink(filename)
400+
371401
def test_bad_magic(self):
372402
# OSErrors (eg. file doesn't exist) are allowed to propagate
373403
filename = os_helper.TESTFN
@@ -391,8 +421,7 @@ def test_bad_magic(self):
391421
c = cookiejar_class()
392422
self.assertRaises(LoadError, c.load, filename)
393423
finally:
394-
try: os.unlink(filename)
395-
except OSError: pass
424+
os_helper.unlink(filename)
396425

397426
class CookieTests(unittest.TestCase):
398427
# XXX
@@ -496,7 +525,7 @@ def test_missing_value(self):
496525
c = MozillaCookieJar(filename)
497526
c.revert(ignore_expires=True, ignore_discard=True)
498527
finally:
499-
os.unlink(c.filename)
528+
os_helper.unlink(c.filename)
500529
# cookies unchanged apart from lost info re. whether path was specified
501530
self.assertEqual(
502531
repr(c),
@@ -1766,8 +1795,7 @@ def test_rejection(self):
17661795
c = LWPCookieJar(policy=pol)
17671796
c.load(filename, ignore_discard=True)
17681797
finally:
1769-
try: os.unlink(filename)
1770-
except OSError: pass
1798+
os_helper.unlink(filename)
17711799

17721800
self.assertEqual(old, repr(c))
17731801

@@ -1826,8 +1854,7 @@ def save_and_restore(cj, ignore_discard):
18261854
DefaultCookiePolicy(rfc2965=True))
18271855
new_c.load(ignore_discard=ignore_discard)
18281856
finally:
1829-
try: os.unlink(filename)
1830-
except OSError: pass
1857+
os_helper.unlink(filename)
18311858
return new_c
18321859

18331860
new_c = save_and_restore(c, True)
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)