Skip to content
Myr

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

7 files+41 added−5 removed

modified:docs/templating.rst+4 added-1 removed
16 unmodified lines
1718Unless customized, Jinja2 is configured by Flask as follows:1920- 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 by
197 unmodified lines
modified:docs/upgrading.rst+4 added
35 unmodified lines
36Now the inheritance hierarchy takes precedence and handlers for more37specific exception classes are executed instead of more general ones.38See :ref:`error-handlers` for specifics.3940The :func:`~flask.templating.render_template_string` function has changed to41autoescape template variables by default. This better matches the behavior42of :func:`~flask.templating.render_template`.4344.. note::4546 There used to be a logic error allowing you to register handlers47 only for exception *instances*. This was unintended and plain wrong,
356 unmodified lines
modified:flask/app.py+2 added-2 removed
722 unmodified lines
723 """724725 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`.728729 .. versionadded:: 0.5730 """731 if filename is None:732 return False732 return True733 return filename.endswith(('.html', '.htm', '.xml', '.xhtml'))734735 def update_template_context(self, context):736 """Update the template context with some commonly used variables.
1238 unmodified lines
modified:flask/templating.py+1 added-1 removed
125 unmodified lines
126127128def 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.131132 :param source: the source code of the template to be133 rendered134 :param context: the variables that should be available in the
6 unmodified lines
added: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 lines
80 b'<p>Hello World!'81 ]8283def 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'&lt;p&gt;Hello World!',97 b'<p>Hello World!',98 b'<p>Hello World!',99 b'<p>Hello World!'100 ]101102def 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>') == '&lt;test&gt;'107 assert flask.render_template('mail.txt', foo='<test>') == \108 '<test> Mail'109110def test_macros():
282 unmodified lines
modified:CHANGES+2 added
66 unmodified lines
67- 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``).7374Version 0.10.275--------------76
478 unmodified lines