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 // Package html/template statically examines each path through an
79 // {{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. The context of {{.X}} is
82 // used to figure out how to escape it, but that context depends on
83 // the run-time value of {{.C}} which is not statically known.
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 // Executed templates should produce a DocumentFragment of HTML.
99 // Templates that end without closing tags will trigger this error.
100 // Templates that should not be used in an HTML context or that
101 // produce incomplete Fragments should not be executed directly.
103 // {{define "main"}} <script>{{template "helper"}}</script> {{end}}
104 // {{define "helper"}} document.write(' <div title=" ') {{end}}
106 // "helper" does not produce a valid document fragment, so should
107 // not be Executed directly.
110 // ErrNoSuchTemplate: "no such template ..."
112 // {{define "main"}}<div {{template "attrs"}}>{{end}}
113 // {{define "attrs"}}href="{{.URL}}"{{end}}
115 // Package html/template looks through template calls to compute the
117 // Here the {{.URL}} in "attrs" must be treated as a URL when called
118 // from "main", but you will get this error if "attrs" is not defined
119 // when "main" is parsed.
122 // ErrOutputContext: "cannot compute output context for template ..."
124 // {{define "t"}}{{if .T}}{{template "t" .T}}{{end}}{{.H}}",{{end}}
126 // A recursive template does not end in the same context in which it
127 // starts, and a reliable output context cannot be computed.
128 // Look for typos in the named template.
129 // If the template should not be called in the named start context,
130 // look for calls to that template in unexpected contexts.
131 // Maybe refactor recursive templates to not be recursive.
134 // ErrPartialCharset: "unfinished JS regexp charset in ..."
136 // <script>var pattern = /foo[{{.Chars}}]/</script>
138 // Package html/template does not support interpolation into regular
139 // expression literal character sets.
142 // ErrPartialEscape: "unfinished escape sequence in ..."
144 // <script>alert("\{{.X}}")</script>
146 // Package html/template does not support actions following a
148 // This is usually an error and there are better solutions; for
150 // <script>alert("{{.X}}")</script>
151 // should work, and if {{.X}} is a partial escape sequence such as
152 // "xA0", mark the whole sequence as safe content: JSStr(`\xA0`)
155 // ErrRangeLoopReentry: "on range loop re-entry: ..."
157 // <script>var x = [{{range .}}'{{.}},{{end}}]</script>
159 // If an iteration through a range would cause it to end in a
160 // different context than an earlier pass, there is no single context.
161 // In the example, there is missing a quote, so it is not clear
162 // whether {{.}} is meant to be inside a JS string or in a JS value
163 // context. The second iteration would produce something like
165 // <script>var x = ['firstValue,'secondValue]</script>
168 // ErrSlashAmbig: '/' could start a division or regexp.
171 // {{if .C}}var x = 1{{end}}
172 // /-{{.N}}/i.test(x) ? doThis : doThat();
175 // The example above could produce `var x = 1/-2/i.test(s)...`
176 // in which the first '/' is a mathematical division operator or it
177 // could produce `/-2/i.test(s)` in which the first '/' starts a
179 // Look for missing semicolons inside branches, and maybe add
180 // parentheses to make it clear which interpretation you intend.
184 func (e *Error) Error() string {
186 return fmt.Sprintf("html/template:%s:%d: %s", e.Name, e.Line, e.Description)
187 } else if e.Name != "" {
188 return fmt.Sprintf("html/template:%s: %s", e.Name, e.Description)
190 return "html/template: " + e.Description
193 // errorf creates an error given a format string f and args.
194 // The template Name still needs to be supplied.
195 func errorf(k ErrorCode, line int, f string, args ...interface{}) *Error {
196 return &Error{k, "", line, fmt.Sprintf(f, args...)}