Enable autoescape for `render_template_string`
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Alan Hamlett · committed by Markus Unterwaditzer · parent e4f635f · Browse files · 7 files·+41 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+41 added−5 removed
- tests:+28 added−1 removed
- other:+13 added−4 removed
Provenance
- Commit
99c99c4- Tree
6bf0c18- Author
Alan Hamlett <alan.hamlett@gmail.com>- Committer
Markus Unterwaditzer <markus@unterwaditzer.net>- Parent
e4f635f
7 files changed+41 added−5 removed
modified:docs/templating.rst+4 added-1 removed
16 unmodified lines17 18 Unless customized, Jinja2 is configured by Flask as follows:19 20 - autoescaping is enabled for all templates ending in ``.html``,21- ``.htm``, ``.xml`` as well as ``.xhtml``21+ ``.htm``, ``.xml`` as well as ``.xhtml`` when using22+ :func:`~flask.templating.render_template`.23+- autoescaping is enabled for all strings when using24+ :func:`~flask.templating.render_template_string`.25 - a template has the ability to opt in/out autoescaping with the26 ``{% autoescape %}`` tag.27 - Flask inserts a couple of global functions and helpers into the28 Jinja2 context, additionally to the values that are present by197 unmodified linesmodified:docs/upgrading.rst+4 added
35 unmodified lines36 Now the inheritance hierarchy takes precedence and handlers for more37 specific exception classes are executed instead of more general ones.38 See :ref:`error-handlers` for specifics.39 40+The :func:`~flask.templating.render_template_string` function has changed to41+autoescape template variables by default. This better matches the behavior42+of :func:`~flask.templating.render_template`.43+44 .. note::45 46 There used to be a logic error allowing you to register handlers47 only for exception *instances*. This was unintended and plain wrong,356 unmodified linesmodified:flask/app.py+2 added-2 removed
722 unmodified lines723 """724 725 def select_jinja_autoescape(self, filename):726 """Returns ``True`` if autoescaping should be active for the given727- template name.727+ template name. If no template name is given, returns `True`.728 729 .. versionadded:: 0.5730 """731 if filename is None:732- return False732+ return True733 return filename.endswith(('.html', '.htm', '.xml', '.xhtml'))734 735 def update_template_context(self, context):736 """Update the template context with some commonly used variables.1238 unmodified linesmodified:flask/templating.py+1 added-1 removed
125 unmodified lines126 127 128 def render_template_string(source, **context):129 """Renders a template from the given template source string130- with the given context.130+ with the given context. Template variables will be autoescaped.131 132 :param source: the source code of the template to be133 rendered134 :param context: the variables that should be available in the6 unmodified linesadded:tests/templates/non_escaping_template.txt+8 added
1+{{ text }}2+{{ html }}3+{% autoescape false %}{{ text }}4+{{ html }}{% endautoescape %}5+{% autoescape true %}{{ text }}6+{{ html }}{% endautoescape %}7+{{ text }}8+{{ html }}modified:tests/test_templating.py+20 added-1 removed
79 unmodified lines80 b'<p>Hello World!'81 ]82 83 def test_no_escaping():84+ text = '<p>Hello World!'85 app = flask.Flask(__name__)86+ @app.route('/')87+ def index():88+ return flask.render_template('non_escaping_template.txt', text=text,89+ html=flask.Markup(text))90+ lines = app.test_client().get('/').data.splitlines()91+ assert lines == [92+ b'<p>Hello World!',93+ b'<p>Hello World!',94+ b'<p>Hello World!',95+ b'<p>Hello World!',96+ b'<p>Hello World!',97+ b'<p>Hello World!',98+ b'<p>Hello World!',99+ b'<p>Hello World!'100+ ]101+102+def test_escaping_without_template_filename():103+ app = flask.Flask(__name__)104 with app.test_request_context():105 assert flask.render_template_string(87- '{{ foo }}', foo='<test>') == '<test>'106+ '{{ foo }}', foo='<test>') == '<test>'107 assert flask.render_template('mail.txt', foo='<test>') == \108 '<test> Mail'109 110 def test_macros():282 unmodified linesmodified:CHANGES+2 added
66 unmodified lines67 - Don't leak exception info of already catched exceptions to context teardown68 handlers (pull request ``#1393``).69 - Allow custom Jinja environment subclasses (pull request ``#1422``).70 - ``flask.g`` now has ``pop()`` and ``setdefault`` methods.71+- Turn on autoescape for ``flask.templating.render_template_string`` by default72+ (pull request ``#1515``).73 74 Version 0.10.275 --------------76 478 unmodified lines