Skip to content

Commit 87d0887

Browse files
Patch
1 parent 0c29f83 commit 87d0887

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

Lib/http/cookies.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,15 @@ def update(self, values):
337337
key = key.lower()
338338
if key not in self._reserved:
339339
raise CookieError("Invalid attribute %r" % (key,))
340+
if _has_control_character(key, val):
341+
raise CookieError("Control characters are not allowed in cookies %r %r" % (key, val))
340342
data[key] = val
341343
dict.update(self, data)
342344

345+
def __ior__(self, values):
346+
self.update(values)
347+
return self
348+
343349
def isReservedKey(self, K):
344350
return K.lower() in self._reserved
345351

@@ -524,6 +530,8 @@ def js_output(self, attrs=None):
524530
result = []
525531
items = sorted(self.items())
526532
for key, value in items:
533+
if _has_control_character(value.OutputString(attrs)):
534+
raise CookieError("Control characters are not allowed in cookies")
527535
result.append(value.js_output(attrs))
528536
return _nulljoin(result)
529537

Lib/test/test_http_cookies.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,18 @@ def test_control_characters(self):
618618
with self.assertRaises(cookies.CookieError):
619619
morsel.set("path", "val", c0)
620620

621+
# .update()
622+
with self.assertRaises(cookies.CookieError):
623+
morsel.update({"path": c0})
624+
with self.assertRaises(cookies.CookieError):
625+
morsel.update({c0: "val"})
626+
627+
# .__ior__()
628+
with self.assertRaises(cookies.CookieError):
629+
morsel |= {"path": c0}
630+
with self.assertRaises(cookies.CookieError):
631+
morsel |= {c0: "val"}
632+
621633
def test_control_characters_output(self):
622634
# Tests that even if the internals of Morsel are modified
623635
# that a call to .output() has control character safeguards.
@@ -638,6 +650,24 @@ def test_control_characters_output(self):
638650
with self.assertRaises(cookies.CookieError):
639651
cookie.output()
640652

653+
# Tests that .js_output() also has control character safeguards.
654+
for c0 in support.control_characters_c0():
655+
morsel = cookies.Morsel()
656+
morsel.set("key", "value", "coded-value")
657+
morsel._key = c0 # Override private variable.
658+
cookie = cookies.SimpleCookie()
659+
cookie["cookie"] = morsel
660+
with self.assertRaises(cookies.CookieError):
661+
cookie.js_output()
662+
663+
morsel = cookies.Morsel()
664+
morsel.set("key", "value", "coded-value")
665+
morsel._coded_value = c0 # Override private variable.
666+
cookie = cookies.SimpleCookie()
667+
cookie["cookie"] = morsel
668+
with self.assertRaises(cookies.CookieError):
669+
cookie.js_output()
670+
641671

642672
def load_tests(loader, tests, pattern):
643673
tests.addTest(doctest.DocTestSuite(cookies))

0 commit comments

Comments
 (0)