template

This package is not in the latest version of its module.

Go to latest Published: Sep 5, 2024 License: BSD-3-Clause

Opens a new window with license information. Opens a new window with list of imports. Opens a new window with list of known importers.

Details

Repository

Links

Documentation ¶

Overview ¶

Package template (html/template) implements data-driven templates for generating HTML output safe against code injection. It provides the same interface as text/template and should be used instead of text/template whenever the output is HTML.

The documentation here focuses on the security features of the package. For information about how to program the templates themselves, see the documentation for text/template.

Introduction ¶

This package wraps text/template so you can share its template API to parse and execute HTML templates safely.

tmpl, err := template.New("name").Parse(. ) // Error checking elided err = tmpl.Execute(out, data)

If successful, tmpl will now be injection-safe. Otherwise, err is an error defined in the docs for ErrorCode.

HTML templates treat data values as plain text which should be encoded so they can be safely embedded in an HTML document. The escaping is contextual, so actions can appear within JavaScript, CSS, and URI contexts.

The security model used by this package assumes that template authors are trusted, while Execute's data parameter is not. More details are provided below.

import "text/template" . t, err := template.New("foo").Parse(`>Hello, >!>`) err = t.ExecuteTemplate(out, "T", "")
Hello, !

but the contextual autoescaping in html/template

import "html/template" . t, err := template.New("foo").Parse(`>Hello, >!>`) err = t.ExecuteTemplate(out, "T", "")

produces safe, escaped HTML output

Hello, <script>alert('you have been pwned')</script>!

Contexts ¶

This package understands HTML, CSS, JavaScript, and URIs. It adds sanitizing functions to each simple action pipeline, so given the excerpt

At parse time each > is overwritten to add escaping functions as necessary. In this case it becomes

where urlescaper, attrescaper, and htmlescaper are aliases for internal escaping functions.

For these internal escaping functions, if an action pipeline evaluates to a nil interface value, it is treated as though it were an empty string.

Namespaced and data- attributes ¶

Attributes with a namespace are treated as if they had no namespace. Given the excerpt

At parse time the attribute will be treated as if it were just "href". So at parse time the template becomes:

Similarly to attributes with namespaces, attributes with a "data-" prefix are treated as if they had no "data-" prefix. So given

At parse time this becomes

If an attribute has both a namespace and a "data-" prefix, only the namespace will be removed when determining the context. For example

This is handled as if "my:data-href" was just "data-href" and not "href" as it would be if the "data-" prefix were to be ignored too. Thus at parse time this becomes just

As a special case, attributes with the namespace "xmlns" are always treated as containing URLs. Given the excerpts

At parse time they become:

Errors ¶

See the documentation of ErrorCode for details.

A fuller picture ¶

The rest of this package comment may be skipped on first reading; it includes details necessary to understand escaping contexts and error messages. Most users will not need to understand these details.

Contexts ¶

Assuming > is `O'Reilly: How are you?`, the table below shows how > appears when used in the context to the left.

Context > After > O'Reilly: How are <i>you</i>?  O'Reilly: How are you?  O'Reilly: How are %3ci%3eyou%3c/i%3e?  O'Reilly%3a%20How%20are%3ci%3e. %3f  O\x27Reilly: How are \x3ci\x3eyou.  "O\x27Reilly: How are \x3ci\x3eyou. "  O\x27Reilly: How are \x3ci\x3eyou. \x3f

If used in an unsafe context, then the value might be filtered out:

Context > After  #ZgotmplZ

since "O'Reilly:" is not an allowed protocol like "http:".

If > is the innocuous word, `left`, then it can appear more widely,

Context > After > left  left  left  left  left  left  left ')> left p.> left

Non-string values can be used in JavaScript contexts. If > is

struct

in the escaped template

 

then the template output is

 

See package json to understand how non-string content is marshaled for embedding in JavaScript contexts.

Typed Strings ¶

By default, this package assumes that all pipelines produce a plain text string. It adds escaping pipeline stages necessary to correctly and safely embed that plain text string in the appropriate context.

When a data value is not plain text, you can make sure it is not over-escaped by marking it with its type.

Types HTML, JS, URL, and others from content.go can carry safe content that is exempted from escaping.

Hello, >!

can be invoked with

tmpl.Execute(out, template.HTML(`World`))
Hello, World!
Hello, <b>World<b>!

that would have been produced if > was a regular string.

Security Model ¶

This package assumes that template authors are trusted, that Execute's data parameter is not, and seeks to preserve the properties below in the face of 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 tag regardless of the values of untrusted data, and similarly for other structures such as attribute boundaries and JS and CSS string boundaries."

Code Effect Property: ". only code specified by the template author should run as a result of injecting the template output into a page and all code specified by the template author should run as a result of the same."

Least Surprise Property: "A developer (or code reviewer) familiar with HTML, CSS, and JavaScript, who knows that contextual autoescaping happens should be able to look at a > and correctly infer what sanitization happens."

Previously, ECMAScript 6 template literal were disabled by default, and could be enabled with the GODEBUG=jstmpllitinterp=1 environment variable. Template literals are now supported by default, and setting jstmpllitinterp has no effect.

package main import ( "html/template" "log" "os" ) func main() < const tpl = `    >  >
>
>
no rows
> ` check := func(err error) < if err != nil < log.Fatal(err) >> t, err := template.New("webpage").Parse(tpl) check(err) data := struct < Title string Items []string >< Title: "My page", Items: []string< "My photos", "My blog", >, > err = t.Execute(os.Stdout, data) check(err) noItems := struct < Title string Items []string >< Title: "My another page", Items: []string<>, > err = t.Execute(os.Stdout, noItems) check(err) >
Output:     My page  
My photos
My blog
My another page
no rows

Share Format Run

Example (Autoescaping) ¶
package main import ( "html/template" "log" "os" ) func main() < check := func(err error) < if err != nil < log.Fatal(err) >> t, err := template.New("foo").Parse(`>Hello, >!>`) check(err) err = t.ExecuteTemplate(os.Stdout, "T", "") check(err) >
Output: Hello, <script>alert('you have been pwned')</script>! 

Share Format Run

Example (Escape) ¶
package main import ( "fmt" "html/template" "os" ) func main() < const s = `"Fran & Freddie's Diner" ` v := []any<`"Fran & Freddie's Diner"`, ' ', ``> fmt.Println(template.HTMLEscapeString(s)) template.HTMLEscape(os.Stdout, []byte(s)) fmt.Fprintln(os.Stdout, "") fmt.Println(template.HTMLEscaper(v. )) fmt.Println(template.JSEscapeString(s)) template.JSEscape(os.Stdout, []byte(s)) fmt.Fprintln(os.Stdout, "") fmt.Println(template.JSEscaper(v. )) fmt.Println(template.URLQueryEscaper(v. )) >
Output: "Fran & Freddie's Diner" <tasty@example.com> "Fran & Freddie's Diner" <tasty@example.com> "Fran & Freddie's Diner"32<tasty@example.com> \"Fran \u0026 Freddie\'s Diner\" \u003Ctasty@example.com\u003E \"Fran \u0026 Freddie\'s Diner\" \u003Ctasty@example.com\u003E \"Fran \u0026 Freddie\'s Diner\"32\u003Ctasty@example.com\u003E %22Fran+%26+Freddie%27s+Diner%2232%3Ctasty%40example.com%3E 

Share Format Run

Index ¶