Skip to content

Commit 736de53

Browse files
StanFromIrelandvstinner
authored andcommitted
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 57e88c1) 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>
1 parent 57cc1bd commit 736de53

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

Lib/http/cookies.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,16 @@ def update(self, values):
335335
key = key.lower()
336336
if key not in self._reserved:
337337
raise CookieError("Invalid attribute %r" % (key,))
338+
if _has_control_character(key, val):
339+
raise CookieError("Control characters are not allowed in "
340+
f"cookies {key!r} {val!r}")
338341
data[key] = val
339342
dict.update(self, data)
340343

344+
def __ior__(self, values):
345+
self.update(values)
346+
return self
347+
341348
def isReservedKey(self, K):
342349
return K.lower() in self._reserved
343350

@@ -363,9 +370,15 @@ def __getstate__(self):
363370
}
364371

365372
def __setstate__(self, state):
366-
self._key = state['key']
367-
self._value = state['value']
368-
self._coded_value = state['coded_value']
373+
key = state['key']
374+
value = state['value']
375+
coded_value = state['coded_value']
376+
if _has_control_character(key, value, coded_value):
377+
raise CookieError("Control characters are not allowed in cookies "
378+
f"{key!r} {value!r} {coded_value!r}")
379+
self._key = key
380+
self._value = value
381+
self._coded_value = coded_value
369382

370383
def output(self, attrs=None, header="Set-Cookie:"):
371384
return "%s %s" % (header, self.OutputString(attrs))
@@ -377,13 +390,16 @@ def __repr__(self):
377390

378391
def js_output(self, attrs=None):
379392
# Print javascript
393+
output_string = self.OutputString(attrs)
394+
if _has_control_character(output_string):
395+
raise CookieError("Control characters are not allowed in cookies")
380396
return """
381397
<script type="text/javascript">
382398
<!-- begin hiding
383399
document.cookie = \"%s\";
384400
// end hiding -->
385401
</script>
386-
""" % (self.OutputString(attrs).replace('"', r'\"'))
402+
""" % (output_string.replace('"', r'\"'))
387403

388404
def OutputString(self, attrs=None):
389405
# Build up our result

Lib/test/test_http_cookies.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,14 @@ def test_control_characters(self):
573573
with self.assertRaises(cookies.CookieError):
574574
morsel["path"] = c0
575575

576+
# .__setstate__()
577+
with self.assertRaises(cookies.CookieError):
578+
morsel.__setstate__({'key': c0, 'value': 'val', 'coded_value': 'coded'})
579+
with self.assertRaises(cookies.CookieError):
580+
morsel.__setstate__({'key': 'key', 'value': c0, 'coded_value': 'coded'})
581+
with self.assertRaises(cookies.CookieError):
582+
morsel.__setstate__({'key': 'key', 'value': 'val', 'coded_value': c0})
583+
576584
# .setdefault()
577585
with self.assertRaises(cookies.CookieError):
578586
morsel.setdefault("path", c0)
@@ -587,6 +595,18 @@ def test_control_characters(self):
587595
with self.assertRaises(cookies.CookieError):
588596
morsel.set("path", "val", c0)
589597

598+
# .update()
599+
with self.assertRaises(cookies.CookieError):
600+
morsel.update({"path": c0})
601+
with self.assertRaises(cookies.CookieError):
602+
morsel.update({c0: "val"})
603+
604+
# .__ior__()
605+
with self.assertRaises(cookies.CookieError):
606+
morsel |= {"path": c0}
607+
with self.assertRaises(cookies.CookieError):
608+
morsel |= {c0: "val"}
609+
590610
def test_control_characters_output(self):
591611
# Tests that even if the internals of Morsel are modified
592612
# that a call to .output() has control character safeguards.
@@ -607,6 +627,24 @@ def test_control_characters_output(self):
607627
with self.assertRaises(cookies.CookieError):
608628
cookie.output()
609629

630+
# Tests that .js_output() also has control character safeguards.
631+
for c0 in support.control_characters_c0():
632+
morsel = cookies.Morsel()
633+
morsel.set("key", "value", "coded-value")
634+
morsel._key = c0 # Override private variable.
635+
cookie = cookies.SimpleCookie()
636+
cookie["cookie"] = morsel
637+
with self.assertRaises(cookies.CookieError):
638+
cookie.js_output()
639+
640+
morsel = cookies.Morsel()
641+
morsel.set("key", "value", "coded-value")
642+
morsel._coded_value = c0 # Override private variable.
643+
cookie = cookies.SimpleCookie()
644+
cookie["cookie"] = morsel
645+
with self.assertRaises(cookies.CookieError):
646+
cookie.js_output()
647+
610648

611649
def load_tests(loader, tests, pattern):
612650
tests.addTest(doctest.DocTestSuite(cookies))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Reject control characters in :class:`http.cookies.Morsel`
2+
:meth:`~http.cookies.Morsel.update` and
3+
:meth:`~http.cookies.BaseCookie.js_output`.
4+
This addresses :cve:`2026-3644`.

0 commit comments

Comments
 (0)