gh-145599: Reject control characters in `http.cookies.Morsel.update()` and `http.cookies.BaseCookie.js_output` by StanFromIreland · Pull Request #145600 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions Lib/http/cookies.py
38 changes: 38 additions & 0 deletions Lib/test/test_http_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,14 @@ def test_control_characters(self):
with self.assertRaises(cookies.CookieError):
morsel["path"] = c0

# .__setstate__()
with self.assertRaises(cookies.CookieError):
morsel.__setstate__({'key': c0, 'value': 'val', 'coded_value': 'coded'})
with self.assertRaises(cookies.CookieError):
morsel.__setstate__({'key': 'key', 'value': c0, 'coded_value': 'coded'})
with self.assertRaises(cookies.CookieError):
morsel.__setstate__({'key': 'key', 'value': 'val', 'coded_value': c0})

# .setdefault()
with self.assertRaises(cookies.CookieError):
morsel.setdefault("path", c0)
Expand All @@ -618,6 +626,18 @@ def test_control_characters(self):
with self.assertRaises(cookies.CookieError):
morsel.set("path", "val", c0)

# .update()
with self.assertRaises(cookies.CookieError):
morsel.update({"path": c0})
with self.assertRaises(cookies.CookieError):
morsel.update({c0: "val"})

# .__ior__()
with self.assertRaises(cookies.CookieError):
morsel |= {"path": c0}
with self.assertRaises(cookies.CookieError):
morsel |= {c0: "val"}

def test_control_characters_output(self):
# Tests that even if the internals of Morsel are modified
# that a call to .output() has control character safeguards.
Expand All @@ -638,6 +658,24 @@ def test_control_characters_output(self):
with self.assertRaises(cookies.CookieError):
cookie.output()

# Tests that .js_output() also has control character safeguards.
for c0 in support.control_characters_c0():
morsel = cookies.Morsel()
morsel.set("key", "value", "coded-value")
morsel._key = c0 # Override private variable.
cookie = cookies.SimpleCookie()
cookie["cookie"] = morsel
with self.assertRaises(cookies.CookieError):
cookie.js_output()

morsel = cookies.Morsel()
morsel.set("key", "value", "coded-value")
morsel._coded_value = c0 # Override private variable.
cookie = cookies.SimpleCookie()
cookie["cookie"] = morsel
with self.assertRaises(cookies.CookieError):
cookie.js_output()


def load_tests(loader, tests, pattern):
tests.addTest(doctest.DocTestSuite(cookies))
Expand Down
Loading