[3.11] gh-79096: Protect cookie file created by {LWP,Mozilla}CookieJa… · python/cpython@2084f94

GitHub

@@ -1,6 +1,8 @@

11"""Tests for http/cookiejar.py."""

2233importos

4+importstat

5+importsys

46importre

57importtest.support

68fromtest.supportimportos_helper

@@ -17,6 +19,7 @@

1719reach, is_HDN, domain_match, user_domain_match, request_path,

1820request_port, request_host)

192122+mswindows= (sys.platform=="win32")

20232124classDateTimeTests(unittest.TestCase):

2225@@ -364,10 +367,37 @@ def test_lwp_valueless_cookie(self):

364367c=LWPCookieJar()

365368c.load(filename, ignore_discard=True)

366369finally:

367-try: os.unlink(filename)

368-exceptOSError: pass

370+os_helper.unlink(filename)

369371self.assertEqual(c._cookies["www.acme.com"]["/"]["boo"].value, None)

[email protected](mswindows, "windows file permissions are incompatible with file modes")

374+@os_helper.skip_unless_working_chmod

375+deftest_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)

[email protected](mswindows, "windows file permissions are incompatible with file modes")

388+@os_helper.skip_unless_working_chmod

389+deftest_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+371401deftest_bad_magic(self):

372402# OSErrors (eg. file doesn't exist) are allowed to propagate

373403filename=os_helper.TESTFN

@@ -391,8 +421,7 @@ def test_bad_magic(self):

391421c=cookiejar_class()

392422self.assertRaises(LoadError, c.load, filename)

393423finally:

394-try: os.unlink(filename)

395-exceptOSError: pass

424+os_helper.unlink(filename)

396425397426classCookieTests(unittest.TestCase):

398427# XXX

@@ -496,7 +525,7 @@ def test_missing_value(self):

496525c=MozillaCookieJar(filename)

497526c.revert(ignore_expires=True, ignore_discard=True)

498527finally:

499-os.unlink(c.filename)

528+os_helper.unlink(c.filename)

500529# cookies unchanged apart from lost info re. whether path was specified

501530self.assertEqual(

502531repr(c),

@@ -1766,8 +1795,7 @@ def test_rejection(self):

17661795c=LWPCookieJar(policy=pol)

17671796c.load(filename, ignore_discard=True)

17681797finally:

1769-try: os.unlink(filename)

1770-exceptOSError: pass

1798+os_helper.unlink(filename)

1771179917721800self.assertEqual(old, repr(c))

17731801@@ -1826,8 +1854,7 @@ def save_and_restore(cj, ignore_discard):

18261854DefaultCookiePolicy(rfc2965=True))

18271855new_c.load(ignore_discard=ignore_discard)

18281856finally:

1829-try: os.unlink(filename)

1830-exceptOSError: pass

1857+os_helper.unlink(filename)

18311858returnnew_c

1832185918331860new_c=save_and_restore(c, True)