-
-
Notifications
You must be signed in to change notification settings - Fork 164
Description
Jinja treats all objects with a __html__() method as HTML safe and does not perform any automatic escaping on them. Commit a5f2297 added a __html__() method to the LazyString class to allow for LazyStrings to automatically be serialized by flask.json.JSONEncoder.
This __html__() method makes the content of all gettext strings in a template potentially unsafe. I am using Jinja and Flask-Babel with Transifex in a project. Recently I experienced an issue where a string translated by an external translator broke our HTML as the translated string was not HTML escaped.
This unexpected behavior is particularly risky when an application is relying on external and/or crowd-sourced translations. A translator could inject JavaScript in an translation and potentially compromise a service. Translations changes may be less well reviewed than direct code changes.
I'm not sure what is the best way to resolve this risk. Flask-Babel should certainly choose a safe default. Perhaps the Flask-Babel documentation could give an example of a custom flask.json.JSONEncoder subclass which explicitly handles LazyStrings? Alternatively the Flask-Babel extension could have an unsafe option which could be set explicitly during initialization?