Repository navigation
SimpleCookie.js_output is vulnerable to HTML injection #90309
Description
Activity
In /Lib/http/cookies.py, the output from SimpleCookie.js_output might be parsed as HTML if it contained < and >.
from http import cookies c = cookies.SimpleCookie() c["fig"] = "newton</script><script>alert(document.domain)</script>"; // c.js_output() <script type="text/javascript"> <!-- begin hiding document.cookie = "fig=\"newton</script><script>alert(document.domain)</script>\""; // end hiding --> </script>We can't simply escape all the special characters because the encoding method is treated differently depending on the document types. For example, the following snippet (from The Tangled Web) is safe in HTML but not in XHTML:
<script type="text/javascript"> var tmp = 'I am harmless! '+alert(1);// Or am I?'; </script>To avoid messing with the encoding methods, we could encode the cookie string in base64 and let the browser decode it.
// c.js_output() <script type="text/javascript"> document.cookie = base64decode(<ENCODED>); </script>After searching around on Github, I think this function is rarely used so making it deprecated is also an option.
- added3.11only security fixesonly security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-securityA security issueA security issue
on Dec 22, 2021 I have not found any use of that method on GitHub. Only forks of the stdlib and typeshed, and variables with accidentally same name.
It seems that it was written when support of
<script>in browsers was rare. I do not think that "<!-- begin hiding" is valid JavaScript. This code will likely does not work in modern browsers.I support deprecation of
js_output().I also support deprecation of this function.
- added a commit that references this issue
on Apr 25, 2026 - added 4 commits that reference this issue
on Apr 28, 2026 - added a commit that references this issue
on Jun 22, 2026
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDone
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs