[3.14] gh-145599, CVE 2026-3644: Reject control characters in http.cookies.Morsel.update() (GH-145600) (#146023)

gh-145599, CVE 2026-3644: Reject control characters in `http.cookies.Morsel.update()` (GH-145600)

Reject control characters in `http.cookies.Morsel.update()` and `http.cookies.BaseCookie.js_output`.
(cherry picked from commit 57e88c1cf9)

Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Victor Stinner <victor.stinner@gmail.com>
This commit is contained in:
Miss Islington (bot) 2026-03-16 15:13:19 +01:00 committed by GitHub
parent 8bc3aa9ac3
commit 62ceb396fc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 62 additions and 4 deletions

View file

@ -337,9 +337,16 @@ 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 "
f"cookies {key!r} {val!r}")
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
@ -365,9 +372,15 @@ def __getstate__(self):
}
def __setstate__(self, state):
self._key = state['key']
self._value = state['value']
self._coded_value = state['coded_value']
key = state['key']
value = state['value']
coded_value = state['coded_value']
if _has_control_character(key, value, coded_value):
raise CookieError("Control characters are not allowed in cookies "
f"{key!r} {value!r} {coded_value!r}")
self._key = key
self._value = value
self._coded_value = coded_value
def output(self, attrs=None, header="Set-Cookie:"):
return "%s %s" % (header, self.OutputString(attrs))
@ -379,13 +392,16 @@ def __repr__(self):
def js_output(self, attrs=None):
# Print javascript
output_string = self.OutputString(attrs)
if _has_control_character(output_string):
raise CookieError("Control characters are not allowed in cookies")
return """
<script type="text/javascript">
<!-- begin hiding
document.cookie = \"%s\";
// end hiding -->
</script>
""" % (self.OutputString(attrs).replace('"', r'\"'))
""" % (output_string.replace('"', r'\"'))
def OutputString(self, attrs=None):
# Build up our result

View file

@ -581,6 +581,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)
@ -595,6 +603,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.
@ -615,6 +635,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))

View file

@ -0,0 +1,4 @@
Reject control characters in :class:`http.cookies.Morsel`
:meth:`~http.cookies.Morsel.update` and
:meth:`~http.cookies.BaseCookie.js_output`.
This addresses :cve:`2026-3644`.