On Apr 9, 12:34 am, David Symonds <
dsymo...@golang.org> wrote:
> There's also the "noescape" function that is, for some reason, undocumented.
>
> {{.Body | noescape}}
This is correct. It's meant to allow for auditable exceptions to the
rule.
The problem with |noescape is that it's not type-safe.
<title>{{.KnownSafeHtml |noescape}}</title>
is probably fine, but
<a href="{{.KnownSafeHtml |noescape}}">
is not since not every string of innocuous HTML is an innocuous URL.
E.g. "javascript:alert(1337)" contains no side-effects when parsed as
HTML, but does when interpreted as a URL.
If you're writing new code (instead of migrating), I'd recommend using
template.HTML as Nigel suggests or one of the other safe content types
defined at
http://code.google.com/p/go/source/browse/src/pkg/html/template/content.go
For example, if you've got known-safe HTML coming from a template or
from an HTML sanitizer, just use the template.HTML type. If you've
got a known-safe URL, use template.URL.
Do this consistently and you're less vulnerable.
Finally, I'm a little confused as to why you would do this
specifically with <title>. <title> can't contain tags, only entities,
so there's really no value in pre-escaping the content of <title>.