8000 Translate </ to <\/ in json output to avoid issues with the string · githubapitest/tornado@f0a3d89 · GitHub
[go: up one dir, main page]

Skip to content

Commit f0a3d89

Browse files
author
Ben Darnell
committed
Translate </ to <\/ in json output to avoid issues with the string
"</script>".
1 parent 26af9e4 commit f0a3d89

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

tornado/escape.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,13 @@ def xhtml_unescape(value):
5454

5555
def json_encode(value):
5656
"""JSON-encodes the given Python object."""
57-
return _json_encode(value)
57+
# JSON permits but does not require forward slashes to be escaped.
58+
# This is useful when json data is emitted in a <script> tag
59+
# in HTML, as it prevents </script> tags from prematurely terminating
60+
# the javscript. Some json libraries do this escaping by default,
61+
# although python's standard library does not, so we do it here.
62+
# http://stackoverflow.com/questions/1580647/json-why-are-forward-slashes-escaped
63+
return _json_encode(value).replace("</", "<\\/")
5864

5965

6066
def json_decode(value):

0 commit comments

Comments
 (0)
0