Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request is the first step in implementing auto escaping.
Why?
Most users of Stencil are going to be using Stencil as a HTML templating language, and subsequently it should provide auto escaping of HTML for XSS prevention. A Stencil template author should not have to think about XSS or escaping as it will be handled automatically.
Should auto escape be enable by default?
I've been thinking a lot about this, and leaning towards auto escaping to be off by default. However any web frameworks should create an environment with auto escaping enabled for rendering templates.
It may make sense for autoescaping HTML to be enable by default because most cases users will be using Templates with HTML.
How should the auto escape setting in Environment work?
Jinja2 has an approach of allowing users to provide a function which can be used to determine if auto escape can be enabled. The value can also be set to False/True to force.
This seems useful, especially for web frameworks where they may want to provide this exact behaviour of escaping HTML based on a
.html
extension. So that if there was.txt
templates for example an email template it would not be escaped.Allowing users to escape in any format
We shouldn't limit the API to only allow HTML autoescape, users should be able to write custom escaping rules for other content types. Most of the similiar template languages to Stencil don't allow serialising custom formats.
Allowing users to mark a value as already escaped
We provide the
HTMLEscaped
protocol which allows you to provide ahtml
property which will return an already escaped string. This might be useful if you need to include HTML inside a variable such as:Where form returns HTML representation of a HTML form.
There is also a template filter which can be used to wrap a value in the escaped protocol.
Force escaping:
Django also provides an
{% autoescape on/off %}{% endautoescape %}
block so users can enable/disable auto escaping in a scope.This pull request is not yet ready. The current state is to force HTML auto escaping, it should become optional with custom formats.