Skip to content

Commit

Permalink
tojson no longer escapes script blocks in HTML5 parsers. Fixed #605
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Oct 7, 2012
1 parent 01ac057 commit c4f2075
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Release date to be decided.
- Added ``template_test`` methods in addition to the already existing
``template_filter`` method family.
- Set the content-length header for x-sendfile.
- ``tojson`` filter now does not escape script blocks in HTML5 parsers.

Version 0.9
-----------
Expand Down
12 changes: 7 additions & 5 deletions flask/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@

# figure out if simplejson escapes slashes. This behavior was changed
# from one version to another without reason.
if '\\/' not in json.dumps('/'):
def _tojson_filter(*args, **kwargs):
return json.dumps(*args, **kwargs).replace('/', '\\/')
else:
_tojson_filter = json.dumps
_slash_escape = '\\/' not in json.dumps('/')

def _tojson_filter(*args, **kwargs):
rv = json.dumps(*args, **kwargs)
if _slash_escape:
rv = rv.replace('/', '\\/')
return rv.replace('<!', '<\\u0021')


# sentinel
Expand Down
2 changes: 2 additions & 0 deletions flask/testsuite/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ def test_template_escaping(self):
self.assert_equal(rv, '"<\\/script>"')
rv = render('{{ "<\0/script>"|tojson|safe }}')
self.assert_equal(rv, '"<\\u0000\\/script>"')
rv = render('{{ "<!--<script>"|tojson|safe }}')
self.assert_equal(rv, '"<\\u0021--<script>"')

def test_modified_url_encoding(self):
class ModifiedRequest(flask.Request):
Expand Down

0 comments on commit c4f2075

Please sign in to comment.