Skip to content
Open
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
8 changes: 8 additions & 0 deletions Lib/http/cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,15 @@ def update(self, values):
key = key.lower()
if key not in self._reserved:
raise CookieError("Invalid attribute %r" % (key,))
if _has_control_character(key, val):
raise CookieError("Control characters are not allowed in cookies %r %r" % (key, val))
data[key] = val
dict.update(self, data)

def __ior__(self, values):
self.update(values)
return self

def isReservedKey(self, K):
return K.lower() in self._reserved

Expand Down Expand Up @@ -524,6 +530,8 @@ def js_output(self, attrs=None):
result = []
items = sorted(self.items())
for key, value in items:
if _has_control_character(value.OutputString(attrs)):
raise CookieError("Control characters are not allowed in cookies")
result.append(value.js_output(attrs))
return _nulljoin(result)

Expand Down
30 changes: 30 additions & 0 deletions Lib/test/test_http_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,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 +650,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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Reject control characters in :class:`http.cookies.Morsel`
:meth:`~http.cookies.Morsel.update` and
:meth:`~http.cookies.BaseCookie.js_output`.
Loading