]> Cypherpunks repositories - gostls13.git/commit
exp/template/html: pre-sanitized content
authorMike Samuel <mikesamuel@gmail.com>
Thu, 15 Sep 2011 15:51:55 +0000 (08:51 -0700)
committerMike Samuel <mikesamuel@gmail.com>
Thu, 15 Sep 2011 15:51:55 +0000 (08:51 -0700)
commitce008f8c37e07190a27368f6e546617a40267fd1
tree161c48bd67662cab62fd65e80dccadae73e78822
parentf41ab6c7c80ab4d854b84308fc370b92b1312b30
exp/template/html: pre-sanitized content

Not all content is plain text.  Sometimes content comes from a trusted
source, such as another template invocation, an HTML tag whitelister,
etc.

Template authors can deal with over-escaping in two ways.

1) They can encapsulate known-safe content via
   type HTML, type CSS, type URL, and friends in content.go.
2) If they know that the for a particular action never needs escaping
   then they can add |noescape to the pipeline.
   {{.KnownSafeContent | noescape}}
   which will prevent any escaping directives from being added.

This CL defines string type aliases: HTML, CSS, JS, URI, ...
It then modifies stringify to unpack the content type.
Finally it modifies the escaping functions to use the content type and
decline to escape content that does not require it.

There are minor changes to escapeAction and helpers to treat as
equivalent explicit escaping directives such as "html" and "urlquery"
and the escaping directives defined in the contextual autoescape module
and to recognize the special "noescape" directive.

The html escaping functions are rearranged.  Instead of having one
escaping function used in each {{.}} in

    {{.}} : <textarea title="{{.}}">{{.}}</textarea>

a slightly different escaping function is used for each.
When {{.}} binds to a pre-sanitized string of HTML

    `one < <i>two</i> &amp; two < "3"`

we produces something like

     one < <i>two</i> &amp; two < "3" :
     <textarea title="one &lt; two &amp; two &lt; &#34;3&#34;">
       one &lt; &lt;i&gt;two&lt;/i&gt; &amp; two &lt; "3"
     </textarea>

Although escaping is not required in <textarea> normally, if the
substring </textarea> is injected, then it breaks, so we normalize
special characters in RCDATA and do the same to preserve attribute
boundaries.  We also strip tags since developers never intend
typed HTML injected in an attribute to contain tags escaped, but
do occasionally confuse pre-escaped HTML with HTML from a
tag-whitelister.

R=golang-dev, nigeltao
CC=golang-dev
https://golang.org/cl/4962067
src/pkg/exp/template/html/Makefile
src/pkg/exp/template/html/content.go [new file with mode: 0644]
src/pkg/exp/template/html/content_test.go [new file with mode: 0644]
src/pkg/exp/template/html/css.go
src/pkg/exp/template/html/doc.go
src/pkg/exp/template/html/escape.go
src/pkg/exp/template/html/escape_test.go
src/pkg/exp/template/html/html.go
src/pkg/exp/template/html/html_test.go
src/pkg/exp/template/html/js.go
src/pkg/exp/template/html/url.go