Skip to content

Commit 028b607

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 eb730a3 commit 028b607

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
@@ -1903,6 +1903,75 @@ def save_and_restore(cj, ignore_discard):
19031903
self.assertEqual(len(new_c), 4) # 2 of them discarded on save
19041904
self.assertIn("name='foo1', value='bar'", repr(new_c))
19051905

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

0 commit comments

Comments
 (0)