diff --git a/CHANGES b/CHANGES index 5bd76b97..ff0a3e83 100644 --- a/CHANGES +++ b/CHANGES @@ -17,6 +17,8 @@ Release date to be decided. ``template_filter`` method family. - Set the content-length header for x-sendfile. - ``tojson`` filter now does not escape script blocks in HTML5 parsers. +- ``tojson`` used in templates is now safe by default due. This was + allowed due to the different escaping behavior. - Flask will now raise an error if you attempt to register a new function on an already used endpoint. - Added wrapper module around simplejson and added default serialization diff --git a/docs/api.rst b/docs/api.rst index 27333079..096741ae 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -364,7 +364,8 @@ JSON module: The :func:`~htmlsafe_dumps` function of this json module is also available as filter called ``|tojson`` in Jinja2. Note that inside `script` tags no escaping must take place, so make sure to disable escaping -with ``|safe`` if you intend to use it inside `script` tags: +with ``|safe`` if you intend to use it inside `script` tags unless +you are using Flask 0.10 which implies that: .. sourcecode:: html+jinja @@ -372,8 +373,6 @@ with ``|safe`` if you intend to use it inside `script` tags: doSomethingWith({{ user.username|tojson|safe }}); -Note that the ``|tojson`` filter escapes forward slashes properly. - .. autofunction:: jsonify .. autofunction:: dumps diff --git a/docs/patterns/jquery.rst b/docs/patterns/jquery.rst index 7aaa2803..9de99f61 100644 --- a/docs/patterns/jquery.rst +++ b/docs/patterns/jquery.rst @@ -63,9 +63,10 @@ like this: $SCRIPT_ROOT = {{ request.script_root|tojson|safe }}; -The ``|safe`` is necessary so that Jinja does not escape the JSON encoded -string with HTML rules. Usually this would be necessary, but we are -inside a `script` block here where different rules apply. +The ``|safe`` is necessary in Flask before 0.10 so that Jinja does not +escape the JSON encoded string with HTML rules. Usually this would be +necessary, but we are inside a `script` block here where different rules +apply. .. admonition:: Information for Pros @@ -76,6 +77,10 @@ inside a `script` block here where different rules apply. escape slashes for you (``{{ ""|tojson|safe }}`` is rendered as ``"<\/script>"``). + In Flask 0.10 it goes a step further and escapes all HTML tags with + unicode escapes. This makes it possible for Flask to automatically + mark the result as HTML safe. + JSON View Functions ------------------- diff --git a/docs/templating.rst b/docs/templating.rst index b6e1fc0a..4e432333 100644 --- a/docs/templating.rst +++ b/docs/templating.rst @@ -106,8 +106,8 @@ by Jinja2 itself: fly. Note that inside `script` tags no escaping must take place, so make - sure to disable escaping with ``|safe`` if you intend to use it inside - `script` tags: + sure to disable escaping with ``|safe`` before Flask 0.10 if you intend + to use it inside `script` tags: .. sourcecode:: html+jinja @@ -115,8 +115,6 @@ by Jinja2 itself: doSomethingWith({{ user.username|tojson|safe }}); - That the ``|tojson`` filter escapes forward slashes properly for you. - Controlling Autoescaping ------------------------ diff --git a/flask/app.py b/flask/app.py index b52af9b2..271c29cd 100644 --- a/flask/app.py +++ b/flask/app.py @@ -659,7 +659,7 @@ class Flask(_PackageBoundObject): session=session, g=g ) - rv.filters['tojson'] = json.htmlsafe_dumps + rv.filters['tojson'] = json.tojson_filter return rv def create_global_jinja_loader(self): @@ -1707,13 +1707,6 @@ class Flask(_PackageBoundObject): rv = func(exc) request_tearing_down.send(self, exc=exc) - # If this interpreter supports clearing the exception information - # we do that now. This will only go into effect on Python 2.x, - # on 3.x it disappears automatically at the end of the exception - # stack. - if hasattr(sys, 'exc_clear'): - sys.exc_clear() - def do_teardown_appcontext(self, exc=None): """Called when an application context is popped. This works pretty much the same as :meth:`do_teardown_request` but for the application diff --git a/flask/json.py b/flask/json.py index 2437a20c..d1cda5ae 100644 --- a/flask/json.py +++ b/flask/json.py @@ -15,6 +15,7 @@ from .globals import current_app, request from ._compat import text_type, PY2 from werkzeug.http import http_date +from jinja2 import Markup # Use the same json implementation as itsdangerous on which we # depend anyways. @@ -160,18 +161,26 @@ def load(fp, **kwargs): def htmlsafe_dumps(obj, **kwargs): """Works exactly like :func:`dumps` but is safe for use in ``"|tojson|safe }}') - self.assert_equal(rv, '"<\\/script>"') - rv = render('{{ "<\0/script>"|tojson|safe }}') - self.assert_equal(rv, '"<\\u0000\\/script>"') - rv = render('{{ "