1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
11 // Error describes a problem encountered during template Escaping.
13 // ErrorCode describes the kind of error.
15 // Name is the name of the template in which the error was encountered.
17 // Line is the line number of the error in the template source or 0.
19 // Description is a human-readable description of the problem.
23 // ErrorCode is a code for a kind of error.
26 // We define codes for each error that manifests while escaping templates, but
27 // escaped templates may also fail at runtime.
32 // where {{.X}} evaluates to `javascript:...`
34 // "ZgotmplZ" is a special value that indicates that unsafe content reached a
35 // CSS or URL context at runtime. The output of the example will be
36 // <img src="#ZgotmplZ">
37 // If the data comes from a trusted source, use content types to exempt it
38 // from filtering: URL(`javascript:...`).
40 // OK indicates the lack of an error.
43 // ErrAmbigContext: "... appears in an ambiguous URL context"
54 // {{.X}} is in an ambiguous URL context since, depending on {{.C}},
55 // it may be either a URL suffix or a query parameter.
56 // Moving {{.X}} into the condition removes the ambiguity:
57 // <a href="{{if .C}}/path/{{.X}}{{else}}/search?q={{.X}}">
60 // ErrBadHTML: "expected space, attr name, or end of tag, but got ...",
61 // "... in unquoted attr", "... in attribute name"
63 // <a href = /search?q=foo>
68 // This is often due to a typo in an HTML element, but some runes
69 // are banned in tag names, attribute names, and unquoted attribute
70 // values because they can tickle parser ambiguities.
71 // Quoting all attributes is the best policy.
74 // ErrBranchEnd: "{{if}} branches end in different contexts"
76 // {{if .C}}<a href="{{end}}{{.X}}
78 // EscapeSet statically examines each possible path when it encounters
79 // a {{if}}, {{range}}, or {{with}} to escape any following pipelines.
80 // The example is ambiguous since {{.X}} might be an HTML text node,
81 // or a URL prefix in an HTML attribute. EscapeSet needs to understand
82 // the context of {{.X}} to escape it, but that depends on the
83 // run-time value of {{.C}}.
85 // The problem is usually something like missing quotes or angle
86 // brackets, or can be avoided by refactoring to put the two contexts
87 // into different branches of an if, range or with. If the problem
88 // is in a {{range}} over a collection that should never be empty,
89 // adding a dummy {{else}} can help.
92 // ErrEndContext: "... ends in a non-text context: ..."
95 // <div title="no close quote>
98 // EscapeSet assumes the ouput is a DocumentFragment of HTML.
99 // Templates that end without closing tags will trigger this error.
100 // Templates that produce incomplete Fragments should not be named
101 // in the call to EscapeSet.
103 // If you have a helper template in your set that is not meant to
104 // produce a document fragment, then do not pass its name to
105 // EscapeSet(set, ...names).
107 // {{define "main"}} <script>{{template "helper"}}</script> {{end}}
108 // {{define "helper"}} document.write(' <div title=" ') {{end}}
110 // "helper" does not produce a valid document fragment, though it does
111 // produce a valid JavaScript Program.
114 // ErrNoNames: "must specify names of top level templates"
116 // EscapeSet does not assume that all templates in a set produce HTML.
117 // Some may be helpers that produce snippets of other languages.
118 // Passing in no template names is most likely an error,
119 // so EscapeSet(set) will panic.
120 // If you call EscapeSet with a slice of names, guard it with len:
122 // if len(names) != 0 {
123 // set, err := EscapeSet(set, ...names)
127 // ErrNoSuchTemplate: "no such template ..."
129 // {{define "main"}}<div {{template "attrs"}}>{{end}}
130 // {{define "attrs"}}href="{{.URL}}"{{end}}
132 // EscapeSet looks through template calls to compute the context.
133 // Here the {{.URL}} in "attrs" must be treated as a URL when called
134 // from "main", but if "attrs" is not in set when
135 // EscapeSet(&set, "main") is called, this error will arise.
138 // ErrOutputContext: "cannot compute output context for template ..."
140 // {{define "t"}}{{if .T}}{{template "t" .T}}{{end}}{{.H}}",{{end}}
142 // A recursive template does not end in the same context in which it
143 // starts, and a reliable output context cannot be computed.
144 // Look for typos in the named template.
145 // If the template should not be called in the named start context,
146 // look for calls to that template in unexpected contexts.
147 // Maybe refactor recursive templates to not be recursive.
150 // ErrPartialCharset: "unfinished JS regexp charset in ..."
152 // <script>var pattern = /foo[{{.Chars}}]/</script>
154 // EscapeSet does not support interpolation into regular expression
155 // literal character sets.
158 // ErrPartialEscape: "unfinished escape sequence in ..."
160 // <script>alert("\{{.X}}")</script>
162 // EscapeSet does not support actions following a backslash.
163 // This is usually an error and there are better solutions; for
165 // <script>alert("{{.X}}")</script>
166 // should work, and if {{.X}} is a partial escape sequence such as
167 // "xA0", mark the whole sequence as safe content: JSStr(`\xA0`)
170 // ErrRangeLoopReentry: "on range loop re-entry: ..."
172 // {{range .}}<p class={{.}}{{end}}
174 // If an iteration through a range would cause it to end in a
175 // different context than an earlier pass, there is no single context.
176 // In the example, the <p> tag is missing a '>'.
177 // EscapeSet cannot tell whether {{.}} is meant to be an HTML class or
178 // the content of a broken <p> element and complains because the
179 // second iteration would produce something like
181 // <p class=foo<p class=bar
184 // ErrSlashAmbig: '/' could start a division or regexp.
187 // {{if .C}}var x = 1{{end}}
188 // /-{{.N}}/i.test(x) ? doThis : doThat();
191 // The example above could produce `var x = 1/-2/i.test(s)...`
192 // in which the first '/' is a mathematical division operator or it
193 // could produce `/-2/i.test(s)` in which the first '/' starts a
195 // Look for missing semicolons inside branches, and maybe add
196 // parentheses to make it clear which interpretation you intend.
200 func (e *Error) Error() string {
202 return fmt.Sprintf("exp/template/html:%s:%d: %s", e.Name, e.Line, e.Description)
203 } else if e.Name != "" {
204 return fmt.Sprintf("exp/template/html:%s: %s", e.Name, e.Description)
206 return "exp/template/html: " + e.Description
209 // errorf creates an error given a format string f and args.
210 // The template Name still needs to be supplied.
211 func errorf(k ErrorCode, line int, f string, args ...interface{}) *Error {
212 return &Error{k, "", line, fmt.Sprintf(f, args...)}