Skip to content

Commit 5f6f42a

Browse files
Match the signature line only in ASCII, and add tests
1 parent 3e09695 commit 5f6f42a

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

Lib/http/cookiejar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def _debug(*args):
5454
HTTPONLY_PREFIX = "#HttpOnly_"
5555
DEFAULT_HTTP_PORT = str(http.client.HTTP_PORT)
5656
NETSCAPE_MAGIC_RGX = re.compile("#( Netscape)? HTTP Cookie File",
57-
re.IGNORECASE)
57+
re.IGNORECASE | re.ASCII)
5858
MISSING_FILENAME_TEXT = ("a filename was not supplied (nor was the CookieJar "
5959
"instance initialised with one)")
6060
NETSCAPE_HEADER_TEXT = """\

Lib/test/test_http_cookiejar.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,31 @@ def test_bad_magic(self):
459459
finally:
460460
os_helper.unlink(filename)
461461

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

0 commit comments

Comments
 (0)