Skip to content
Myr

Fixed a security problem caused by changed simplejson semantics.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent 07e515b · Browse files · 4 files+16 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Notice: this was never in a release version of Flask.

Diff stats

4 files+16 added−2 removed

  • tests:+6 added−0 removed
  • other:+10 added−2 removed

4 files+16 added−2 removed

modified:docs/patterns/jquery.rst+2 added-1 removed
75 unmodified lines
76 In HTML the `script` tag is declared `CDATA` which means that entities77 will not be parsed. Everything until ``</script>`` is handled as script.78 This also means that there must never be any ``</`` between the script79 tags. ``|tojson`` is kindly enough to do the right thing here and80 escape backslashes for you.80 escape slashes for you (``{{ "</script>"|tojson|safe }`` is rendered as81 ``"<\/script>"``).828384JSON View Functions85-------------------
75 unmodified lines
modified:docs/api.rst+2 added
248 unmodified lines
249 <script type=text/javascript>250 doSomethingWith({{ user.username|tojson|safe }});251 </script>252253 Note that the ``|tojson`` filter escapes forward slashes properly.254255Template Rendering256------------------257258.. autofunction:: render_template
4 unmodified lines
modified:tests/flask_tests.py+6 added
192 unmodified lines
193 rv = c.post('/add', data=flask.json.dumps({'a': 1, 'b': 2}),194 content_type='application/json')195 assert rv.data == '3'196197 def test_template_escaping(self):198 app = flask.Flask(__name__)199 with app.test_request_context():200 rv = flask.render_template_string('{{ "</script>"|tojson|safe }}')201 assert rv == '"<\\/script>"'202203204class TemplatingTestCase(unittest.TestCase):205206 def test_context_processing(self):207 app = flask.Flask(__name__)
35 unmodified lines
modified:flask.py+6 added-1 removed
257 unmodified lines
258 except (KeyError, AttributeError):259 return os.getcwd()260261262def _tojson_filter(string, *args, **kwargs):263 """Calls dumps for the template engine, escaping Slashes properly."""264 return json.dumps(string, *args, **kwargs).replace('/', '\\/')265266267class Flask(object):268 """The flask object implements a WSGI application and acts as the central269 object. It is passed the name of the module or package of the270 application. Once it is created it will act as a central registry for
112 unmodified lines
383 url_for=url_for,384 get_flashed_messages=get_flashed_messages385 )386 if json_available:382 self.jinja_env.filters['tojson'] = json.dumps387 self.jinja_env.filters['tojson'] = _tojson_filter388389 def create_jinja_loader(self):390 """Creates the Jinja loader. By default just a package loader for391 the configured package is returned that looks up templates in the
374 unmodified lines