Skip to content
Myr

Fixed XSS problem by escaping all slashes in JSON.

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

This also probes simplejson first to figure out if it escapes slashes which it did in earlier versions.

authored by Armin Ronacher · committed by Armin Ronacher · parent 268302f · Browse files · 2 files+13 added−1 removed
This commit

State

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

Diff stats

2 files+13 added−1 removed

  • tests:+2 added−0 removed
  • other:+11 added−1 removed

2 files+13 added−1 removed

modified:tests/flask_tests.py+2 added
243 unmodified lines
244 app = flask.Flask(__name__)245 with app.test_request_context():246 rv = flask.render_template_string('{{ "</script>"|tojson|safe }}')247 assert rv == '"<\\/script>"'248 rv = flask.render_template_string('{{ "<\0/script>"|tojson|safe }}')249 assert rv == '"<\\u0000\\/script>"'250251252class TemplatingTestCase(unittest.TestCase):253
51 unmodified lines
modified:flask.py+11 added-1 removed
8 unmodified lines
9 :copyright: (c) 2010 by Armin Ronacher.10 :license: BSD, see LICENSE for more details.11"""12from __future__ import with_statement13import re14import os15import sys1617from jinja2 import Environment, PackageLoader, FileSystemLoader
29 unmodified lines
47except (ImportError, AttributeError):48 pkg_resources = None495051# figure out if simplejson escapes slashes. This behaviour was changed52# from one version to another without reason.53if json_available:54 _json_escapes_slashes = '\\/' in json.dumps('/')555657class Request(RequestBase):58 """The request object used by default in flask. Remembers the59 matched endpoint and view arguments.60
216 unmodified lines
277def _tojson_filter(string, *args, **kwargs):278 """Calls dumps for the template engine, escaping Slashes properly."""279 if __debug__:280 _assert_have_json()274 return json.dumps(string, *args, **kwargs).replace('</', '<\\/')281 rv = json.dumps(string, *args, **kwargs)282 if not _json_escapes_slashes:283 rv = rv.replace('/', '\\/')284 return rv285286287class Flask(object):288 """The flask object implements a WSGI application and acts as the central
522 unmodified lines