Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Doc/library/http.cookies.rst
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ Morsel Objects
.. versionchanged:: 3.8
Added support for the :attr:`samesite` attribute.

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

.. attribute:: Morsel.value

Expand Down Expand Up @@ -224,6 +229,10 @@ Morsel Objects
.. versionchanged:: 3.5
an error is raised for invalid keys.

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

.. method:: Morsel.copy(value)

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

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

.. _cookie-example:

Expand Down
38 changes: 22 additions & 16 deletions Lib/http/cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,20 +297,31 @@ def value(self):
def coded_value(self):
return self._coded_value

def _check_attribute(self, key, val):
"""Normalize and validate an attribute name and value

Raise `CookieError` if the key is not a recognized attribute name or
value contains illegal characters. Otherwise return the canonicalized
key.
"""
key = key.lower()
val_str = str(val)
if key not in self._reserved:
raise CookieError(f"Invalid attribute {key!r}")
if _has_control_character(key, val_str):
raise CookieError(f"Control characters are not allowed in cookies {key!r} {val!r}")
# Comment is quoted like a cookie value so semicolons can be escaped.
# No other attribute expects `;` in the value, so refuse it.
if key != 'comment' and ';' in val_str:
raise CookieError(f"Reserved character ';' is not allowed in cookie attribute value {key!r} {val!r}")
return key

def __setitem__(self, K, V):
K = K.lower()
if not K in self._reserved:
raise CookieError("Invalid attribute %r" % (K,))
if _has_control_character(K, V):
raise CookieError(f"Control characters are not allowed in cookies {K!r} {V!r}")
K = self._check_attribute(K, V)
dict.__setitem__(self, K, V)

def setdefault(self, key, val=None):
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 cookies %r %r" % (key, val,))
key = self._check_attribute(key, val)
return dict.setdefault(self, key, val)

def __eq__(self, morsel):
Expand All @@ -332,12 +343,7 @@ def copy(self):
def update(self, values):
data = {}
for key, val in dict(values).items():
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}")
key = self._check_attribute(key, val)
data[key] = val
dict.update(self, data)

Expand Down
24 changes: 24 additions & 0 deletions Lib/test/test_http_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,30 @@ def test_control_characters(self):
with self.assertRaises(cookies.CookieError):
morsel |= {c0: "val"}

def test_no_semicolons_in_attribute_values(self):
for attrkey, attrname in cookies.Morsel._reserved.items():
if attrname == "Comment":
continue
with self.subTest(attrkey=attrkey, attrname=attrname):
morsel = cookies.Morsel()
with self.assertRaises(cookies.CookieError):
morsel[attrkey] = "foo; SameSite=none"
with self.assertRaises(cookies.CookieError):
morsel.setdefault(attrkey, "foo; SameSite=none")
with self.assertRaises(cookies.CookieError):
morsel.update({attrkey: "foo; SameSite=none"})
with self.assertRaises(cookies.CookieError):
morsel |= {attrkey: "foo; SameSite=none"}

def test_semicolon_escaped_in_comment(self):
morsel = cookies.Morsel()
morsel.set("key", "value", "coded")
morsel["comment"] = "foo;SameSite=none;Domain=example.com"
self.assertEqual(
morsel.OutputString(),
r'key=coded; Comment="foo\073SameSite=none\073Domain=example.com"',
)

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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.. cve: CVE-2026-16052

:mod:`http.cookies` now raises :exc:`~http.cookies.CookieError` on any attempt
to set a value string that contains the reserved separator character ``;`` to
any attribute except :attr:`~http.cookies.Morsel.comment`.
Loading