sunng87 / handlebars-rust

Rust templating with Handlebars
MIT License
1.28k stars 138 forks source link

Security issue: escaping is not contextual, potential XSS #393

Open empijei opened 3 years ago

empijei commented 3 years ago

Handlerbars currently escapes only based on one function and allows users to specify a custom one, but within a certain template execution it only uses one escaping mechanism.

This means that values interpolated in different contexts (html attributes, html text, script, style, urls) are all escaped in the same way, which is known to be a cause for XSS.

This makes tera inherently unsafe. You can read more about it here (please see "Strawman I: Non-contextual auto-sanitization").

This should either be:

Contextual auto escaping is implemented in many safe templates like Java Soy, Python Soy, Angular templates, Go standard html/template and safehtml/template or Jinja2 (even though in Jinja2 is disabled by default) and others.

The TL;DR of the mechanism is to parse the HTML sources, understand the context that user data is being interpolated in and pick the appropriate escaping function accordingly.

sunng87 commented 3 years ago

Thanks for the great insight.

Soy template seems to have the contextautoesc module as an example. I will definitely look into this.