Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork34k
gh-143919: Reject control characters in http cookies#143920
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
sethmlarson merged 5 commits intopython:mainfromsethmlarson:reject-control-characters-http-cookiesJan 20, 2026
+80 −9
Merged
Changes fromall commits
Commits
Show all changes
5 commits Select commitHold shift + click to select a range
ef01008 Add 'test.support' fixture for C0 control characters
sethmlarsonfdbf0c4 gh-143919: Reject control characters in http.cookies.Morsel
sethmlarson03f222d Apply suggestions from code review
sethmlarson6b999b5 Apply suggestions from code review
sethmlarson216e492 Fix doctest
sethmlarsonFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
4 changes: 2 additions & 2 deletionsDoc/library/http.cookies.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
25 changes: 22 additions & 3 deletionsLib/http/cookies.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletionsLib/test/support/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
52 changes: 48 additions & 4 deletionsLib/test/test_http_cookies.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -17,10 +17,10 @@ def test_basic(self): | ||
| 'repr':"<SimpleCookie: chips='ahoy' vienna='finger'>", | ||
| 'output':'Set-Cookie: chips=ahoy\nSet-Cookie: vienna=finger'}, | ||
| {'data':'keebler="E=mc2; L=\\"Loves\\"; fudge=;"', | ||
| 'dict': {'keebler' :'E=mc2; L="Loves"; fudge=;'}, | ||
| 'repr':'''<SimpleCookie: keebler='E=mc2; L="Loves"; fudge=;'>''', | ||
| 'output':'Set-Cookie: keebler="E=mc2; L=\\"Loves\\"; fudge=;"'}, | ||
| # Check illegal cookies that have an '=' char in an unquoted value | ||
| {'data':'keebler=E=mc2', | ||
| @@ -594,6 +594,50 @@ def test_repr(self): | ||
| r'Set-Cookie: key=coded_val; ' | ||
| r'expires=\w+, \d+ \w+ \d+ \d+:\d+:\d+ \w+') | ||
| deftest_control_characters(self): | ||
| forc0insupport.control_characters_c0(): | ||
| morsel=cookies.Morsel() | ||
| # .__setitem__() | ||
| withself.assertRaises(cookies.CookieError): | ||
| morsel[c0]="val" | ||
| withself.assertRaises(cookies.CookieError): | ||
| morsel["path"]=c0 | ||
| # .setdefault() | ||
| withself.assertRaises(cookies.CookieError): | ||
| morsel.setdefault("path",c0) | ||
| withself.assertRaises(cookies.CookieError): | ||
| morsel.setdefault(c0,"val") | ||
| # .set() | ||
| withself.assertRaises(cookies.CookieError): | ||
| morsel.set(c0,"val","coded-value") | ||
| withself.assertRaises(cookies.CookieError): | ||
| morsel.set("path",c0,"coded-value") | ||
| withself.assertRaises(cookies.CookieError): | ||
| morsel.set("path","val",c0) | ||
| deftest_control_characters_output(self): | ||
| # Tests that even if the internals of Morsel are modified | ||
| # that a call to .output() has control character safeguards. | ||
| forc0insupport.control_characters_c0(): | ||
| morsel=cookies.Morsel() | ||
| morsel.set("key","value","coded-value") | ||
| morsel._key=c0# Override private variable. | ||
| cookie=cookies.SimpleCookie() | ||
| cookie["cookie"]=morsel | ||
| withself.assertRaises(cookies.CookieError): | ||
| cookie.output() | ||
| morsel=cookies.Morsel() | ||
| morsel.set("key","value","coded-value") | ||
| morsel._coded_value=c0# Override private variable. | ||
| cookie=cookies.SimpleCookie() | ||
| cookie["cookie"]=morsel | ||
| withself.assertRaises(cookies.CookieError): | ||
| cookie.output() | ||
sethmlarson marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| defload_tests(loader,tests,pattern): | ||
| tests.addTest(doctest.DocTestSuite(cookies)) | ||
1 change: 1 addition & 0 deletionsMisc/NEWS.d/next/Security/2026-01-16-11-13-15.gh-issue-143919.kchwZV.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Reject control characters in:class:`http.cookies.Morsel` fields and values. |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.