[rejected AI] tojson: escape U+2028 and U+2029 (JavaScript line/paragraph separators) - #2266
Closed
IMGillusion wants to merge 2 commits into
Closed
[rejected AI] tojson: escape U+2028 and U+2029 (JavaScript line/paragraph separators)#2266IMGillusion wants to merge 2 commits into
IMGillusion wants to merge 2 commits into
Conversation
added 2 commits
September 6, 2026 05:32
JavaScript treats U+2028 (LINE SEPARATOR) and U+2029 (PARAGRAPH SEPARATOR) as line terminators. When json.dumps is called with ensure_ascii=False (the common config) the raw separators are emitted and a string could break out of a <script> block. Escape them the same way Django's json_script does. The default (ensure_ascii=True) path is unaffected: json.dumps already escapes these non-ASCII characters, so the new .replace() calls are no-ops there. Closes pallets#1882
Member
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
htmlsafe_json_dumps(the|tojsonfilter) escapes<,>,&and'so its output is safe to embed in HTML and<script>tags. It does not escape the Unicode line separatorU+2028and paragraph separatorU+2029.JavaScript treats those two characters as line terminators, exactly like
\n. Whenjson.dumpsis told to emit raw Unicode (ensure_ascii=False, a very common config), the separators are emitted unescaped, so a crafted string can break out of a<script>block:Fix
Escape
U+2028/U+2029to\\u2028/\\u2029, matching what Django'sjson_scriptfilter does. On the default path (ensure_ascii=True)json.dumpsalready escapes them as non-ASCII, so the new.replace()calls are harmless no-ops there.Closes #1882