-
Notifications
You must be signed in to change notification settings - Fork 9
Description
…unless the template engine implementation will do it, as defined by a parameter on the TemplateEngine class implementation. (IIRC, Mustache does it by default, but Stencil does not.)
That is, ", ', <, &, and >. Perhaps the other HTML entities, too, but at least those ones.
Why?
- It encourages security by default.
- It abstracts away a difference between template engine implementations. I will have to do my own escaping when I use Stencil, but if I later decide to switch to Mustache, I will have to remove that escaping or else my variables will be double-escaped. On the other hand, if I'm using Mustache and depending on Mustache's escaping, then later decide to switch to Stencil, my site becomes vulnerable to XSS exploits until I implement my own escaping, and I may never even realize it's necessary to do so.
- …but more importantly, it encourages security by default!
This could be implemented by simply mapping throughcontext and performing escaping on anything in there we find that looks like a String.
We can implement a new InsecureUnescapedString struct which users can use when they do not want a particular string in their context dictionary to be escaped; also, an option in RenderingOptions could be set if the user wants to override the default behavior for the whole dictionary (that is, they could force escaping to happen even if the template engine says it shouldn't, or vice versa).
I'm willing to put in the work to implement this and submit a PR, but I figured I'd float the idea out there first.