Skip to content
Open
6 changes: 4 additions & 2 deletions Lib/http/cookiejar.py
Original file line number Diff line number Diff line change
Expand Up @@ -2037,8 +2037,10 @@ def _really_load(self, f, filename, ignore_discard, ignore_expires):
line.strip() == ""):
continue

domain, domain_specified, path, secure, expires, name, value = \
line.split("\t")
fields = line.split("\t")
if len(fields) != 7:
raise LoadError("invalid fields in Netscape format cookies file %r: %r" % (filename, line))
domain, domain_specified, path, secure, expires, name, value = fields
secure = (secure == "TRUE")
domain_specified = (domain_specified == "TRUE")
if name == "":
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_http_cookiejar.py
Original file line number Diff line number Diff line change
Expand Up @@ -2023,6 +2023,19 @@ def test_session_cookies(self):
# we didn't have session cookies in the first place
self.assertNotEqual(counter["session_before"], 0)

def test_mozilla_cookiejar_loaderror_on_malformed_file(self):
# Malformed cookie file should raise LoadError
filename = os_helper.TESTFN
with open(filename, "w") as f:
f.write("# Netscape HTTP Cookie File\ninvalid_line\n")
jar = MozillaCookieJar(filename)
try:
with self.assertRaisesRegex(LoadError, "invalid fields in Netscape format cookies file"):
jar.load()
finally:
os_helper.unlink(filename)



if __name__ == "__main__":
unittest.main()
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1534,6 +1534,7 @@ Kirill (python273) R.
Abhilash Raj
Shorya Raj
Ajith Ramachandran
Paul Ramshaw
Dhushyanth Ramasamy
Ashwin Ramaswami
Jeff Ramnani
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**http.cookiejar**: Netscape/Mozilla cookie files with malformed lines now
raise :exc:`LoadError` instead of an uncaught :exc:`ValueError`.
Loading