Skip to content

Commit 22063a7

Browse files
globalshrugarhadthedevserhiy-storchaka
authored andcommitted
gh-82039: Relax cookiejar.py case-sensitive regex for the inconsequential first line of the cookie file (GH-15673)
(cherry picked from commit 1cf7d89) Co-authored-by: globalshrug <machone@gmail.com> Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net> Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 3d80925 commit 22063a7

4 files changed

Lines changed: 30 additions & 1 deletion

File tree

Lib/http/cookiejar.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ def _debug(*args):
5353
HTTPONLY_ATTR = "HTTPOnly"
5454
HTTPONLY_PREFIX = "#HttpOnly_"
5555
DEFAULT_HTTP_PORT = str(http.client.HTTP_PORT)
56-
NETSCAPE_MAGIC_RGX = re.compile("#( Netscape)? HTTP Cookie File")
56+
NETSCAPE_MAGIC_RGX = re.compile("#( Netscape)? HTTP Cookie File",
57+
re.IGNORECASE | re.ASCII)
5758
MISSING_FILENAME_TEXT = ("a filename was not supplied (nor was the CookieJar "
5859
"instance initialised with one)")
5960
NETSCAPE_HEADER_TEXT = """\

Lib/test/test_http_cookiejar.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,31 @@ def test_bad_magic(self):
452452
finally:
453453
os_helper.unlink(filename)
454454

455+
def test_magic_ignores_case(self):
456+
filename = os_helper.TESTFN
457+
self.addCleanup(os_helper.unlink, filename)
458+
for magic in ("# Netscape HTTP Cookie File",
459+
"# netscape http cookie file",
460+
"# HTTP Cookie File",
461+
"# http cookie file"):
462+
with self.subTest(magic=magic):
463+
with open(filename, "w") as f:
464+
f.write(magic + "\n")
465+
MozillaCookieJar().load(filename)
466+
467+
def test_magic_is_not_unicode(self):
468+
# Unicode case folding must not be used: 'ſ' (U+017F) and 'K'
469+
# (U+212A) are case-insensitively equal to 's' and 'k' in Unicode.
470+
filename = os_helper.TESTFN
471+
self.addCleanup(os_helper.unlink, filename)
472+
for magic in ("# Netſcape HTTP Cookie File",
473+
"# Netscape HTTP CooKie File"):
474+
with self.subTest(magic=magic):
475+
with open(filename, "w", encoding="utf-8") as f:
476+
f.write(magic + "\n")
477+
self.assertRaises(LoadError, MozillaCookieJar().load, filename)
478+
479+
455480
class CookieTests(unittest.TestCase):
456481
# XXX
457482
# Get rid of string comparisons where not actually testing str / repr.

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,7 @@ Brian Harring
728728
Jonathan Hartley
729729
Travis B. Hartwell
730730
Henrik Harutyunyan
731+
Ashley Harvey
731732
Shane Harvey
732733
Larry Hastings
733734
Tim Hatch
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:meth:`http.cookiejar.FileCookieJar.load` now checks the first, format
2+
signature line in a case-insensitive manner. Patch by Ashley Harvey.

0 commit comments

Comments
 (0)