Skip to content

Commit 429c016

Browse files
brettcsvstinner
andcommitted
[3.12] gh-161707: Refuse ; in all cookie attributes except Comment
(cherry picked from commit 2702684) Co-authored-by: Brett Smith <brett.smith@python.org> Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 3d6d2a6 commit 429c016

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
@@ -165,6 +165,11 @@ Morsel Objects
165165
.. versionchanged:: 3.8
166166
Added support for the :attr:`samesite` attribute.
167167

168+
.. versionchanged:: next
169+
:meth:`!__setitem__` and :meth:`!__ior__` now raise :exc:`CookieError` if
170+
a value string for any attribute except :attr:`comment` contains the
171+
reserved separator character ``;``. (If :attr:`comment` contains ``;``
172+
it will be escaped in the output.)
168173

169174
.. attribute:: Morsel.value
170175

@@ -224,6 +229,10 @@ Morsel Objects
224229
.. versionchanged:: 3.5
225230
an error is raised for invalid keys.
226231

232+
.. versionchanged:: next
233+
:exc:`CookieError` is now raised if a value string for any attribute
234+
except :attr:`comment` contains the reserved separator character ``;``.
235+
(If :attr:`comment` contains ``;`` it will be escaped in the output.)
227236

228237
.. method:: Morsel.copy(value)
229238

@@ -238,6 +247,10 @@ Morsel Objects
238247
Raise an error if key is not a valid :rfc:`2109` attribute, otherwise
239248
behave the same as :meth:`dict.setdefault`.
240249

250+
.. versionchanged:: next
251+
:exc:`CookieError` is now raised if the value string for any attribute
252+
except :attr:`comment` contains the reserved separator character ``;``.
253+
(If :attr:`comment` contains ``;`` it will be escaped in the output.)
241254

242255
.. _cookie-example:
243256

‎Lib/http/cookies.py‎

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -297,20 +297,31 @@ def value(self):
297297
def coded_value(self):
298298
return self._coded_value
299299

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

308323
def setdefault(self, key, val=None):
309-
key = key.lower()
310-
if key not in self._reserved:
311-
raise CookieError("Invalid attribute %r" % (key,))
312-
if _has_control_character(key, val):
313-
raise CookieError("Control characters are not allowed in cookies %r %r" % (key, val,))
324+
key = self._check_attribute(key, val)
314325
return dict.setdefault(self, key, val)
315326

316327
def __eq__(self, morsel):
@@ -332,12 +343,7 @@ def copy(self):
332343
def update(self, values):
333344
data = {}
334345
for key, val in dict(values).items():
335-
key = key.lower()
336-
if key not in self._reserved:
337-
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}")
346+
key = self._check_attribute(key, val)
341347
data[key] = val
342348
dict.update(self, data)
343349

‎Lib/test/test_http_cookies.py‎

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

610+
def test_no_semicolons_in_attribute_values(self):
611+
for attrkey, attrname in cookies.Morsel._reserved.items():
612+
if attrname == "Comment":
613+
continue
614+
with self.subTest(attrkey=attrkey, attrname=attrname):
615+
morsel = cookies.Morsel()
616+
with self.assertRaises(cookies.CookieError):
617+
morsel[attrkey] = "foo; SameSite=none"
618+
with self.assertRaises(cookies.CookieError):
619+
morsel.setdefault(attrkey, "foo; SameSite=none")
620+
with self.assertRaises(cookies.CookieError):
621+
morsel.update({attrkey: "foo; SameSite=none"})
622+
with self.assertRaises(cookies.CookieError):
623+
morsel |= {attrkey: "foo; SameSite=none"}
624+
625+
def test_semicolon_escaped_in_comment(self):
626+
morsel = cookies.Morsel()
627+
morsel.set("key", "value", "coded")
628+
morsel["comment"] = "foo;SameSite=none;Domain=example.com"
629+
self.assertEqual(
630+
morsel.OutputString(),
631+
r'key=coded; Comment="foo\073SameSite=none\073Domain=example.com"',
632+
)
633+
610634
def test_control_characters_output(self):
611635
# Tests that even if the internals of Morsel are modified
612636
# 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)