Skip to content

Security risk with LazyString strings being treated as HTML safe by Jinja #121

@DonnchaC

Description

@DonnchaC

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions