https://github.com/python/cpython/commit/1cf7d898287972947f746fd647bd8aa8fc0e5aae
commit: 1cf7d898287972947f746fd647bd8aa8fc0e5aae
branch: main
author: globalshrug <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2026-08-11T17:01:52Z
summary:

gh-82039: Relax cookiejar.py case-sensitive regex for the inconsequential first 
line of the cookie file (GH-15673)

Co-authored-by: Oleg Iarygin <[email protected]>
Co-authored-by: Serhiy Storchaka <[email protected]>

files:
A Misc/NEWS.d/next/Library/2023-02-11-09-49-49.gh-issue-82039.caTE7O.rst
M Lib/http/cookiejar.py
M Lib/test/test_http_cookiejar.py
M Misc/ACKS

diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py
index 13e5b104a81ea2..302bd3676a8144 100644
--- a/Lib/http/cookiejar.py
+++ b/Lib/http/cookiejar.py
@@ -53,7 +53,8 @@ def _debug(*args):
 HTTPONLY_ATTR = "HTTPOnly"
 HTTPONLY_PREFIX = "#HttpOnly_"
 DEFAULT_HTTP_PORT = str(http.client.HTTP_PORT)
-NETSCAPE_MAGIC_RGX = re.compile("#( Netscape)? HTTP Cookie File")
+NETSCAPE_MAGIC_RGX = re.compile("#( Netscape)? HTTP Cookie File",
+                                re.IGNORECASE | re.ASCII)
 MISSING_FILENAME_TEXT = ("a filename was not supplied (nor was the CookieJar "
                          "instance initialised with one)")
 NETSCAPE_HEADER_TEXT =  """\
diff --git a/Lib/test/test_http_cookiejar.py b/Lib/test/test_http_cookiejar.py
index 04cb440cd4ccf6..7f39b5c772bd10 100644
--- a/Lib/test/test_http_cookiejar.py
+++ b/Lib/test/test_http_cookiejar.py
@@ -459,6 +459,31 @@ def test_bad_magic(self):
         finally:
             os_helper.unlink(filename)
 
+    def test_magic_ignores_case(self):
+        filename = os_helper.TESTFN
+        self.addCleanup(os_helper.unlink, filename)
+        for magic in ("# Netscape HTTP Cookie File",
+                      "# netscape http cookie file",
+                      "# HTTP Cookie File",
+                      "# http cookie file"):
+            with self.subTest(magic=magic):
+                with open(filename, "w") as f:
+                    f.write(magic + "\n")
+                MozillaCookieJar().load(filename)
+
+    def test_magic_is_not_unicode(self):
+        # Unicode case folding must not be used: 'ſ' (U+017F) and 'K'
+        # (U+212A) are case-insensitively equal to 's' and 'k' in Unicode.
+        filename = os_helper.TESTFN
+        self.addCleanup(os_helper.unlink, filename)
+        for magic in ("# Netſcape HTTP Cookie File",
+                      "# Netscape HTTP CooKie File"):
+            with self.subTest(magic=magic):
+                with open(filename, "w", encoding="utf-8") as f:
+                    f.write(magic + "\n")
+                self.assertRaises(LoadError, MozillaCookieJar().load, filename)
+
+
 class CookieTests(unittest.TestCase):
     # XXX
     # Get rid of string comparisons where not actually testing str / repr.
diff --git a/Misc/ACKS b/Misc/ACKS
index fec00c1b272f4e..9316e935949958 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -748,6 +748,7 @@ Peter Harris
 Jonathan Hartley
 Travis B. Hartwell
 Henrik Harutyunyan
+Ashley Harvey
 Shane Harvey
 Larry Hastings
 Tim Hatch
diff --git 
a/Misc/NEWS.d/next/Library/2023-02-11-09-49-49.gh-issue-82039.caTE7O.rst 
b/Misc/NEWS.d/next/Library/2023-02-11-09-49-49.gh-issue-82039.caTE7O.rst
new file mode 100644
index 00000000000000..cb75ae83780eaf
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-02-11-09-49-49.gh-issue-82039.caTE7O.rst
@@ -0,0 +1,2 @@
+:meth:`http.cookiejar.FileCookieJar.load` now checks the first, format
+signature line in a case-insensitive manner. Patch by Ashley Harvey.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to