https://github.com/python/cpython/commit/27026842f6774c167295f4b59972589f12ec5987 commit: 27026842f6774c167295f4b59972589f12ec5987 branch: main author: Brett Smith <[email protected]> committer: sethmlarson <[email protected]> date: 2026-11-12T15:50:42Z summary:
gh-161707: Refuse `;` in all cookie attributes except Comment Co-authored-by: Victor Stinner <[email protected]> files: A Misc/NEWS.d/next/Security/2026-11-09-17-19-29.gh-issue-161707.xWzwqQ.rst M Doc/library/http.cookies.rst M Lib/http/cookies.py M Lib/test/test_http_cookies.py diff --git a/Doc/library/http.cookies.rst b/Doc/library/http.cookies.rst index 4a8d461213f4e57..5ac1a318be80f8a 100644 --- a/Doc/library/http.cookies.rst +++ b/Doc/library/http.cookies.rst @@ -188,6 +188,11 @@ Morsel Objects .. versionchanged:: 3.14 Added support for the :attr:`partitioned` 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 @@ -253,6 +258,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) @@ -267,6 +276,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: diff --git a/Lib/http/cookies.py b/Lib/http/cookies.py index 9a6f01dfb5e69a6..57ee1bd12f6ff22 100644 --- a/Lib/http/cookies.py +++ b/Lib/http/cookies.py @@ -300,20 +300,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): @@ -335,12 +346,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) diff --git a/Lib/test/test_http_cookies.py b/Lib/test/test_http_cookies.py index d1df2ec42f0d146..6632c39159865c8 100644 --- a/Lib/test/test_http_cookies.py +++ b/Lib/test/test_http_cookies.py @@ -628,6 +628,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. diff --git a/Misc/NEWS.d/next/Security/2026-11-09-17-19-29.gh-issue-161707.xWzwqQ.rst b/Misc/NEWS.d/next/Security/2026-11-09-17-19-29.gh-issue-161707.xWzwqQ.rst new file mode 100644 index 000000000000000..63731daafff41ef --- /dev/null +++ b/Misc/NEWS.d/next/Security/2026-11-09-17-19-29.gh-issue-161707.xWzwqQ.rst @@ -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`. _______________________________________________ Python-checkins mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3//lists/python-checkins.python.org Member address: [email protected]
