forked from orbit-oss/flask
tojson no longer escapes script blocks in HTML5 parsers. Fixed #605
This commit is contained in:
parent
01ac057d36
commit
c4f2075f4c
3 changed files with 10 additions and 5 deletions
1
CHANGES
1
CHANGES
|
|
@ -14,6 +14,7 @@ Release date to be decided.
|
||||||
- Added ``template_test`` methods in addition to the already existing
|
- Added ``template_test`` methods in addition to the already existing
|
||||||
``template_filter`` method family.
|
``template_filter`` method family.
|
||||||
- Set the content-length header for x-sendfile.
|
- Set the content-length header for x-sendfile.
|
||||||
|
- ``tojson`` filter now does not escape script blocks in HTML5 parsers.
|
||||||
|
|
||||||
Version 0.9
|
Version 0.9
|
||||||
-----------
|
-----------
|
||||||
|
|
|
||||||
|
|
@ -45,11 +45,13 @@ from .globals import session, _request_ctx_stack, _app_ctx_stack, \
|
||||||
|
|
||||||
# figure out if simplejson escapes slashes. This behavior was changed
|
# figure out if simplejson escapes slashes. This behavior was changed
|
||||||
# from one version to another without reason.
|
# from one version to another without reason.
|
||||||
if '\\/' not in json.dumps('/'):
|
_slash_escape = '\\/' not in json.dumps('/')
|
||||||
def _tojson_filter(*args, **kwargs):
|
|
||||||
return json.dumps(*args, **kwargs).replace('/', '\\/')
|
def _tojson_filter(*args, **kwargs):
|
||||||
else:
|
rv = json.dumps(*args, **kwargs)
|
||||||
_tojson_filter = json.dumps
|
if _slash_escape:
|
||||||
|
rv = rv.replace('/', '\\/')
|
||||||
|
return rv.replace('<!', '<\\u0021')
|
||||||
|
|
||||||
|
|
||||||
# sentinel
|
# sentinel
|
||||||
|
|
|
||||||
|
|
@ -97,6 +97,8 @@ class JSONTestCase(FlaskTestCase):
|
||||||
self.assert_equal(rv, '"<\\/script>"')
|
self.assert_equal(rv, '"<\\/script>"')
|
||||||
rv = render('{{ "<\0/script>"|tojson|safe }}')
|
rv = render('{{ "<\0/script>"|tojson|safe }}')
|
||||||
self.assert_equal(rv, '"<\\u0000\\/script>"')
|
self.assert_equal(rv, '"<\\u0000\\/script>"')
|
||||||
|
rv = render('{{ "<!--<script>"|tojson|safe }}')
|
||||||
|
self.assert_equal(rv, '"<\\u0021--<script>"')
|
||||||
|
|
||||||
def test_modified_url_encoding(self):
|
def test_modified_url_encoding(self):
|
||||||
class ModifiedRequest(flask.Request):
|
class ModifiedRequest(flask.Request):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue