Skip to content

Commit 324bf86

Browse files
committed
Fix HttpOnly Prefix Issue in MozillaCookieJar.save
Modified attribute checking in MozillaCookieJar.save to be case-insensitive, aligning with HTTP standards. This change resolves the issue where HttpOnly prefix was not correctly appended due to case-sensitive checks.
1 parent 6c22fef commit 324bf86

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

Lib/http/cookiejar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2116,7 +2116,7 @@ def save(self, filename=None, ignore_discard=False, ignore_expires=False):
21162116
else:
21172117
name = cookie.name
21182118
value = cookie.value
2119-
if cookie.has_nonstandard_attr(HTTPONLY_ATTR):
2119+
if cookie.has_nonstandard_attr(HTTPONLY_ATTR, case_insensitive=True):
21202120
domain = HTTPONLY_PREFIX + domain
21212121
f.write(
21222122
"\t".join([domain, initial_dot, cookie.path,

Lib/test/test_http_cookiejar.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1892,6 +1892,75 @@ def save_and_restore(cj, ignore_discard):
18921892
self.assertEqual(len(new_c), 4) # 2 of them discarded on save
18931893
self.assertIn("name='foo1', value='bar'", repr(new_c))
18941894

1895+
def test_mozilla_httponly_prefix(self):
1896+
# Save / load Mozilla/Netscape cookie file with HttpOnly prefix.
1897+
filename = os_helper.TESTFN
1898+
1899+
# Load the input file test
1900+
c1 = MozillaCookieJar(filename)
1901+
one_year_later = int(time.time()) + 365*24*60*60
1902+
try:
1903+
with open(filename, "w") as f:
1904+
f.write("# Netscape HTTP Cookie File\n")
1905+
f.write("#HttpOnly_.example.com\tTRUE\t/\tFALSE\t%d\tfoo\tbar\n"
1906+
% (one_year_later,))
1907+
c1.load()
1908+
finally:
1909+
os_helper.unlink(filename)
1910+
1911+
cookie = list(c1)[0]
1912+
self.assertIn("HttpOnly", repr(cookie))
1913+
self.assertTrue(cookie.has_nonstandard_attr("HttpOnly", case_insensitive=True))
1914+
self.assertTrue(cookie.has_nonstandard_attr("HTTPOnly", case_insensitive=True))
1915+
self.assertFalse(cookie.has_nonstandard_attr("HTTPOnly"))
1916+
1917+
# Save and read the output file test
1918+
c2 = MozillaCookieJar(filename)
1919+
year_plus_one = time.localtime()[0] + 1
1920+
expires = "expires=09-Nov-%d 23:12:40 GMT" % (year_plus_one,)
1921+
# foo1 has the HttpOnly flag set
1922+
interact_netscape(c2, "http://example.com/",
1923+
"foo1=bar1; %s; HttpOnly;" % expires)
1924+
# foo2 will have the HttpOnly flag set later
1925+
interact_netscape(c2, "http://example.com/",
1926+
"foo2=bar2; %s;" % expires)
1927+
# foo3 will have the HTTPOnly flag set later
1928+
interact_netscape(c2, "http://example.com/",
1929+
"foo3=bar3; %s;" % expires)
1930+
# foo4 does not have the HttpOnly flag set
1931+
interact_netscape(c2, "http://example.com/",
1932+
"foo4=bar4; %s;" % expires)
1933+
# Set flags manually
1934+
for cookie in c2:
1935+
if cookie.name == "foo2":
1936+
cookie.set_nonstandard_attr("HttpOnly", "")
1937+
if cookie.name == "foo3":
1938+
cookie.set_nonstandard_attr("HTTPOnly", "")
1939+
1940+
# Save and read the output file
1941+
try:
1942+
c2.save()
1943+
with open(filename, "r") as f:
1944+
lines = f.readlines()
1945+
finally:
1946+
os_helper.unlink(filename)
1947+
1948+
# Check that the HttpOnly prefix is added to the correct cookies
1949+
for value in ["foo1", "foo2", "foo3"]:
1950+
matches = [x for x in lines if value in x]
1951+
self.assertEqual(len(matches), 1,
1952+
"Incorrect number of matches for cookie with value %r" % value)
1953+
self.assertTrue(matches[0].startswith("#HttpOnly_"),
1954+
"Cookie with value %r is missing the HttpOnly prefix" % value)
1955+
1956+
# Check that the HttpOnly prefix is not added to the correct cookies
1957+
for value in ["foo4"]:
1958+
matches = [x for x in lines if value in x]
1959+
self.assertEqual(len(matches), 1,
1960+
"Incorrect number of matches for cookie with value %r" % value)
1961+
self.assertFalse(matches[0].startswith("#HttpOnly_"),
1962+
"Cookie with value %r has the HttpOnly prefix" % value)
1963+
18951964
def test_netscape_misc(self):
18961965
# Some additional Netscape cookies tests.
18971966
c = CookieJar()

0 commit comments

Comments
 (0)