URL
https://opencores.org/ocsvn/openrisc/openrisc/trunk
Subversion Repositories openrisc
[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [html/] [template/] [doc.go] - Rev 747
Compare with Previous | Blame | View Log
// Copyright 2011 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file./*Package template (html/template) is a specialization of package text/templatethat automates the construction of HTML output that is safe against codeinjection.IntroductionThis package wraps package template so you can use the standard template APIto parse and execute templates.set, err := new(template.Set).Parse(...)// Error checking elidederr = set.Execute(out, "Foo", data)If successful, set will now be injection-safe. Otherwise, err is an errordefined in the docs for ErrorCode.HTML templates treat data values as plain text which should be encoded so theycan be safely embedded in an HTML document. The escaping is contextual, soactions can appear within JavaScript, CSS, and URI contexts.The security model used by this package assumes that template authors aretrusted, while Execute's data parameter is not. More details are provided below.Exampleimport "text/template"...t, err := template.New("foo").Parse(`{{define "T"}}Hello, {{.}}!{{end}}`)err = t.ExecuteTemplate(out, "T", "<script>alert('you have been pwned')</script>")producesHello, <script>alert('you have been pwned')</script>!but with contextual autoescaping,import "html/template"...t, err := template.New("foo").Parse(`{{define "T"}}Hello, {{.}}!{{end}}`)err = t.ExecuteTemplate(out, "T", "<script>alert('you have been pwned')</script>")produces safe, escaped HTML outputHello, <script>alert('you have been pwned')</script>!ContextsThis package understands HTML, CSS, JavaScript, and URIs. It adds sanitizingfunctions to each simple action pipeline, so given the excerpt<a href="/search?q={{.}}">{{.}}</a>At parse time each {{.}} is overwritten to add escaping functions as necessary.In this case it becomes<a href="/search?q={{. | urlquery}}">{{. | html}}</a>ErrorsSee the documentation of ErrorCode for details.A fuller pictureThe rest of this package comment may be skipped on first reading; it includesdetails necessary to understand escaping contexts and error messages. Most userswill not need to understand these details.ContextsAssuming {{.}} is `O'Reilly: How are <i>you</i>?`, the table below showshow {{.}} appears when used in the context to the left.Context {{.}} After{{.}} O'Reilly: How are <i>you</i>?<a title='{{.}}'> O'Reilly: How are you?<a href="/{{.}}"> O'Reilly: How are %3ci%3eyou%3c/i%3e?<a href="?q={{.}}"> O'Reilly%3a%20How%20are%3ci%3e...%3f<a onx='f("{{.}}")'> O\x27Reilly: How are \x3ci\x3eyou...?<a onx='f({{.}})'> "O\x27Reilly: How are \x3ci\x3eyou...?"<a onx='pattern = /{{.}}/;'> O\x27Reilly: How are \x3ci\x3eyou...\x3fIf used in an unsafe context, then the value might be filtered out:Context {{.}} After<a href="{{.}}"> #ZgotmplZsince "O'Reilly:" is not an allowed protocol like "http:".If {{.}} is the innocuous word, `left`, then it can appear more widely,Context {{.}} After{{.}} left<a title='{{.}}'> left<a href='{{.}}'> left<a href='/{{.}}'> left<a href='?dir={{.}}'> left<a style="border-{{.}}: 4px"> left<a style="align: {{.}}"> left<a style="background: '{{.}}'> left<a style="background: url('{{.}}')> left<style>p.{{.}} {color:red}</style> leftNon-string values can be used in JavaScript contexts.If {{.}} is[]struct{A,B string}{ "foo", "bar" }in the escaped template<script>var pair = {{.}};</script>then the template output is<script>var pair = {"A": "foo", "B": "bar"};</script>See package json to understand how non-string content is marshalled forembedding in JavaScript contexts.Typed StringsBy default, this package assumes that all pipelines produce a plain text string.It adds escaping pipeline stages necessary to correctly and safely embed thatplain text string in the appropriate context.When a data value is not plain text, you can make sure it is not over-escapedby marking it with its type.Types HTML, JS, URL, and others from content.go can carry safe content that isexempted from escaping.The templateHello, {{.}}!can be invoked withtmpl.Execute(out, HTML(`<b>World</b>`))to produceHello, <b>World</b>!instead of theHello, <b>World<b>!that would have been produced if {{.}} was a regular string.Security Modelhttp://js-quasis-libraries-and-repl.googlecode.com/svn/trunk/safetemplate.html#problem_definition defines "safe" as used by this package.This package assumes that template authors are trusted, that Execute's dataparameter is not, and seeks to preserve the properties below in the faceof untrusted data:Structure Preservation Property"... when a template author writes an HTML tag in a safe templating language,the browser will interpret the corresponding portion of the output as a tagregardless of the values of untrusted data, and similarly for other structuressuch as attribute boundaries and JS and CSS string boundaries."Code Effect Property"... only code specified by the template author should run as a result ofinjecting the template output into a page and all code specified by thetemplate author should run as a result of the same."Least Surprise Property"A developer (or code reviewer) familiar with HTML, CSS, and JavaScript, whoknows that contextual autoescaping happens should be able to look at a {{.}}and correctly infer what sanitization happens."*/package template
