Skip to content

Commit 2702684

Browse files
brettcsvstinner
andauthored
gh-161707: Refuse ; in all cookie attributes except Comment
Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 020c5f3 commit 2702684

4 files changed

Lines changed: 64 additions & 16 deletions

File tree

‎Doc/library/http.cookies.rst‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,11 @@ Morsel Objects
188188
.. versionchanged:: 3.14
189189
Added support for the :attr:`partitioned` attribute.
190190

191+
.. versionchanged:: next
192+
:meth:`!__setitem__` and :meth:`!__ior__` now raise :exc:`CookieError` if
193+
a value string for any attribute except :attr:`comment` contains the
194+
reserved separator character ``;``. (If :attr:`comment` contains ``;``
195+
it will be escaped in the output.)
191196

192197
.. attribute:: Morsel.value
193198

@@ -253,6 +258,10 @@ Morsel Objects
253258
.. versionchanged:: 3.5
254259
an error is raised for invalid keys.
255260

261+
.. versionchanged:: next
262+
:exc:`CookieError` is now raised if a value string for any attribute
263+
except :attr:`comment` contains the reserved separator character ``;``.
264+
(If :attr:`comment` contains ``;`` it will be escaped in the output.)
256265

257266
.. method:: Morsel.copy(value)
258267

@@ -267,6 +276,10 @@ Morsel Objects
267276
Raise an error if key is not a valid :rfc:`2109` attribute, otherwise
268277
behave the same as :meth:`dict.setdefault`.
269278

279+
.. versionchanged:: next
280+
:exc:`CookieError` is now raised if the value string for any attribute
281+
except :attr:`comment` contains the reserved separator character ``;``.
282+
(If :attr:`comment` contains ``;`` it will be escaped in the output.)
270283

271284
.. _cookie-example:
272285

‎Lib/http/cookies.py‎

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -300,20 +300,31 @@ def value(self):
300300
def coded_value(self):
301301
return self._coded_value
302302

303+
def _check_attribute(self, key, val):
304+
"""Normalize and validate an attribute name and value
305+
306+
Raise `CookieError` if the key is not a recognized attribute name or
307+
value contains illegal characters. Otherwise return the canonicalized
308+
key.
309+
"""
310+
key = key.lower()
311+
val_str = str(val)
312+
if key not in self._reserved:
313+
raise CookieError(f"Invalid attribute {key!r}")
314+
if _has_control_character(key, val_str):
315+
raise CookieError(f"Control characters are not allowed in cookies {key!r} {val!r}")
316+
# Comment is quoted like a cookie value so semicolons can be escaped.
317+
# No other attribute expects `;` in the value, so refuse it.
318+
if key != 'comment' and ';' in val_str:
319+
raise CookieError(f"Reserved character ';' is not allowed in cookie attribute value {key!r} {val!r}")
320+
return key
321+
303322
def __setitem__(self, K, V):
304-
K = K.lower()
305-
if not K in self._reserved:
306-
raise CookieError("Invalid attribute %r" % (K,))
307-
if _has_control_character(K, V):
308-
raise CookieError(f"Control characters are not allowed in cookies {K!r} {V!r}")
323+
K = self._check_attribute(K, V)
309324
dict.__setitem__(self, K, V)
310325

311326
def setdefault(self, key, val=None):
312-
key = key.lower()
313-
if key not in self._reserved:
314-
raise CookieError("Invalid attribute %r" % (key,))
315-
if _has_control_character(key, val):
316-
raise CookieError("Control characters are not allowed in cookies %r %r" % (key, val,))
327+
key = self._check_attribute(key, val)
317328
return dict.setdefault(self, key, val)
318329

319330
def __eq__(self, morsel):
@@ -335,12 +346,7 @@ def copy(self):
335346
def update(self, values):
336347
data = {}
337348
for key, val in dict(values).items():
338-
key = key.lower()
339-
if key not in self._reserved:
340-
raise CookieError("Invalid attribute %r" % (key,))
341-
if _has_control_character(key, val):
342-
raise CookieError("Control characters are not allowed in "
343-
f"cookies {key!r} {val!r}")
349+
key = self._check_attribute(key, val)
344350
data[key] = val
345351
dict.update(self, data)
346352

‎Lib/test/test_http_cookies.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,30 @@ def test_control_characters(self):
628628
with self.assertRaises(cookies.CookieError):
629629
morsel |= {c0: "val"}
630630

631+
def test_no_semicolons_in_attribute_values(self):
632+
for attrkey, attrname in cookies.Morsel._reserved.items():
633+
if attrname == "Comment":
634+
continue
635+
with self.subTest(attrkey=attrkey, attrname=attrname):
636+
morsel = cookies.Morsel()
637+
with self.assertRaises(cookies.CookieError):
638+
morsel[attrkey] = "foo; SameSite=none"
639+
with self.assertRaises(cookies.CookieError):
640+
morsel.setdefault(attrkey, "foo; SameSite=none")
641+
with self.assertRaises(cookies.CookieError):
642+
morsel.update({attrkey: "foo; SameSite=none"})
643+
with self.assertRaises(cookies.CookieError):
644+
morsel |= {attrkey: "foo; SameSite=none"}
645+
646+
def test_semicolon_escaped_in_comment(self):
647+
morsel = cookies.Morsel()
648+
morsel.set("key", "value", "coded")
649+
morsel["comment"] = "foo;SameSite=none;Domain=example.com"
650+
self.assertEqual(
651+
morsel.OutputString(),
652+
r'key=coded; Comment="foo\073SameSite=none\073Domain=example.com"',
653+
)
654+
631655
def test_control_characters_output(self):
632656
# Tests that even if the internals of Morsel are modified
633657
# that a call to .output() has control character safeguards.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.. cve: CVE-2026-16052
2+
3+
:mod:`http.cookies` now raises :exc:`~http.cookies.CookieError` on any attempt
4+
to set a value string that contains the reserved separator character ``;`` to
5+
any attribute except :attr:`~http.cookies.Morsel.comment`.

0 commit comments

Comments
 (0)