| 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. |
| 4 |
|
| 5 |
package template |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
|
| 10 |
"github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate/parse" |
| 11 |
) |
| 12 |
|
| 13 |
// Error describes a problem encountered during template Escaping. |
| 14 |
type Error struct { |
| 15 |
// ErrorCode describes the kind of error. |
| 16 |
ErrorCode ErrorCode |
| 17 |
// Node is the node that caused the problem, if known. |
| 18 |
// If not nil, it overrides Name and Line. |
| 19 |
Node parse.Node |
| 20 |
// Name is the name of the template in which the error was encountered. |
| 21 |
Name string |
| 22 |
// Line is the line number of the error in the template source or 0. |
| 23 |
Line int |
| 24 |
// Description is a human-readable description of the problem. |
| 25 |
Description string |
| 26 |
} |
| 27 |
|
| 28 |
// ErrorCode is a code for a kind of error. |
| 29 |
type ErrorCode int |
| 30 |
|
| 31 |
// We define codes for each error that manifests while escaping templates, but |
| 32 |
// escaped templates may also fail at runtime. |
| 33 |
// |
| 34 |
// Output: "ZgotmplZ" |
| 35 |
// Example: |
| 36 |
// |
| 37 |
// <img src="{{.X}}"> |
| 38 |
// where {{.X}} evaluates to `javascript:...` |
| 39 |
// |
| 40 |
// Discussion: |
| 41 |
// |
| 42 |
// "ZgotmplZ" is a special value that indicates that unsafe content reached a |
| 43 |
// CSS or URL context at runtime. The output of the example will be |
| 44 |
// <img src="#ZgotmplZ"> |
| 45 |
// If the data comes from a trusted source, use content types to exempt it |
| 46 |
// from filtering: URL(`javascript:...`). |
| 47 |
const ( |
| 48 |
// OK indicates the lack of an error. |
| 49 |
OK ErrorCode = iota |
| 50 |
|
| 51 |
// ErrAmbigContext: "... appears in an ambiguous context within a URL" |
| 52 |
// Example: |
| 53 |
// <a href=" |
| 54 |
// {{if .C}} |
| 55 |
// /path/ |
| 56 |
// {{else}} |
| 57 |
// /search?q= |
| 58 |
// {{end}} |
| 59 |
// {{.X}} |
| 60 |
// "> |
| 61 |
// Discussion: |
| 62 |
// {{.X}} is in an ambiguous URL context since, depending on {{.C}}, |
| 63 |
// it may be either a URL suffix or a query parameter. |
| 64 |
// Moving {{.X}} into the condition removes the ambiguity: |
| 65 |
// <a href="{{if .C}}/path/{{.X}}{{else}}/search?q={{.X}}"> |
| 66 |
ErrAmbigContext |
| 67 |
|
| 68 |
// ErrBadHTML: "expected space, attr name, or end of tag, but got ...", |
| 69 |
// "... in unquoted attr", "... in attribute name" |
| 70 |
// Example: |
| 71 |
// <a href = /search?q=foo> |
| 72 |
// <href=foo> |
| 73 |
// <form na<e=...> |
| 74 |
// <option selected< |
| 75 |
// Discussion: |
| 76 |
// This is often due to a typo in an HTML element, but some runes |
| 77 |
// are banned in tag names, attribute names, and unquoted attribute |
| 78 |
// values because they can tickle parser ambiguities. |
| 79 |
// Quoting all attributes is the best policy. |
| 80 |
ErrBadHTML |
| 81 |
|
| 82 |
// ErrBranchEnd: "{{if}} branches end in different contexts" |
| 83 |
// Examples: |
| 84 |
// {{if .C}}<a href="{{end}}{{.X}} |
| 85 |
// <script {{with .T}}type="{{.}}"{{end}}> |
| 86 |
// Discussion: |
| 87 |
// Package html/template statically examines each path through an |
| 88 |
// {{if}}, {{range}}, or {{with}} to escape any following pipelines. |
| 89 |
// The first example is ambiguous since {{.X}} might be an HTML text node, |
| 90 |
// or a URL prefix in an HTML attribute. The context of {{.X}} is |
| 91 |
// used to figure out how to escape it, but that context depends on |
| 92 |
// the run-time value of {{.C}} which is not statically known. |
| 93 |
// The second example is ambiguous as the script type attribute |
| 94 |
// can change the type of escaping needed for the script contents. |
| 95 |
// |
| 96 |
// The problem is usually something like missing quotes or angle |
| 97 |
// brackets, or can be avoided by refactoring to put the two contexts |
| 98 |
// into different branches of an if, range or with. If the problem |
| 99 |
// is in a {{range}} over a collection that should never be empty, |
| 100 |
// adding a dummy {{else}} can help. |
| 101 |
ErrBranchEnd |
| 102 |
|
| 103 |
// ErrEndContext: "... ends in a non-text context: ..." |
| 104 |
// Examples: |
| 105 |
// <div |
| 106 |
// <div title="no close quote> |
| 107 |
// <script>f() |
| 108 |
// Discussion: |
| 109 |
// Executed templates should produce a DocumentFragment of HTML. |
| 110 |
// Templates that end without closing tags will trigger this error. |
| 111 |
// Templates that should not be used in an HTML context or that |
| 112 |
// produce incomplete Fragments should not be executed directly. |
| 113 |
// |
| 114 |
// {{define "main"}} <script>{{template "helper"}}</script> {{end}} |
| 115 |
// {{define "helper"}} document.write(' <div title=" ') {{end}} |
| 116 |
// |
| 117 |
// "helper" does not produce a valid document fragment, so should |
| 118 |
// not be Executed directly. |
| 119 |
ErrEndContext |
| 120 |
|
| 121 |
// ErrNoSuchTemplate: "no such template ..." |
| 122 |
// Examples: |
| 123 |
// {{define "main"}}<div {{template "attrs"}}>{{end}} |
| 124 |
// {{define "attrs"}}href="{{.URL}}"{{end}} |
| 125 |
// Discussion: |
| 126 |
// Package html/template looks through template calls to compute the |
| 127 |
// context. |
| 128 |
// Here the {{.URL}} in "attrs" must be treated as a URL when called |
| 129 |
// from "main", but you will get this error if "attrs" is not defined |
| 130 |
// when "main" is parsed. |
| 131 |
ErrNoSuchTemplate |
| 132 |
|
| 133 |
// ErrOutputContext: "cannot compute output context for template ..." |
| 134 |
// Examples: |
| 135 |
// {{define "t"}}{{if .T}}{{template "t" .T}}{{end}}{{.H}}",{{end}} |
| 136 |
// Discussion: |
| 137 |
// A recursive template does not end in the same context in which it |
| 138 |
// starts, and a reliable output context cannot be computed. |
| 139 |
// Look for typos in the named template. |
| 140 |
// If the template should not be called in the named start context, |
| 141 |
// look for calls to that template in unexpected contexts. |
| 142 |
// Maybe refactor recursive templates to not be recursive. |
| 143 |
ErrOutputContext |
| 144 |
|
| 145 |
// ErrPartialCharset: "unfinished JS regexp charset in ..." |
| 146 |
// Example: |
| 147 |
// <script>var pattern = /foo[{{.Chars}}]/</script> |
| 148 |
// Discussion: |
| 149 |
// Package html/template does not support interpolation into regular |
| 150 |
// expression literal character sets. |
| 151 |
ErrPartialCharset |
| 152 |
|
| 153 |
// ErrPartialEscape: "unfinished escape sequence in ..." |
| 154 |
// Example: |
| 155 |
// <script>alert("\{{.X}}")</script> |
| 156 |
// Discussion: |
| 157 |
// Package html/template does not support actions following a |
| 158 |
// backslash. |
| 159 |
// This is usually an error and there are better solutions; for |
| 160 |
// example |
| 161 |
// <script>alert("{{.X}}")</script> |
| 162 |
// should work, and if {{.X}} is a partial escape sequence such as |
| 163 |
// "xA0", mark the whole sequence as safe content: JSStr(`\xA0`) |
| 164 |
ErrPartialEscape |
| 165 |
|
| 166 |
// ErrRangeLoopReentry: "on range loop re-entry: ..." |
| 167 |
// Example: |
| 168 |
// <script>var x = [{{range .}}'{{.}},{{end}}]</script> |
| 169 |
// Discussion: |
| 170 |
// If an iteration through a range would cause it to end in a |
| 171 |
// different context than an earlier pass, there is no single context. |
| 172 |
// In the example, there is missing a quote, so it is not clear |
| 173 |
// whether {{.}} is meant to be inside a JS string or in a JS value |
| 174 |
// context. The second iteration would produce something like |
| 175 |
// |
| 176 |
// <script>var x = ['firstValue,'secondValue]</script> |
| 177 |
ErrRangeLoopReentry |
| 178 |
|
| 179 |
// ErrSlashAmbig: '/' could start a division or regexp. |
| 180 |
// Example: |
| 181 |
// <script> |
| 182 |
// {{if .C}}var x = 1{{end}} |
| 183 |
// /-{{.N}}/i.test(x) ? doThis : doThat(); |
| 184 |
// </script> |
| 185 |
// Discussion: |
| 186 |
// The example above could produce `var x = 1/-2/i.test(s)...` |
| 187 |
// in which the first '/' is a mathematical division operator or it |
| 188 |
// could produce `/-2/i.test(s)` in which the first '/' starts a |
| 189 |
// regexp literal. |
| 190 |
// Look for missing semicolons inside branches, and maybe add |
| 191 |
// parentheses to make it clear which interpretation you intend. |
| 192 |
ErrSlashAmbig |
| 193 |
|
| 194 |
// ErrPredefinedEscaper: "predefined escaper ... disallowed in template" |
| 195 |
// Example: |
| 196 |
// <div class={{. | html}}>Hello<div> |
| 197 |
// Discussion: |
| 198 |
// Package html/template already contextually escapes all pipelines to |
| 199 |
// produce HTML output safe against code injection. Manually escaping |
| 200 |
// pipeline output using the predefined escapers "html" or "urlquery" is |
| 201 |
// unnecessary, and may affect the correctness or safety of the escaped |
| 202 |
// pipeline output in Go 1.8 and earlier. |
| 203 |
// |
| 204 |
// In most cases, such as the given example, this error can be resolved by |
| 205 |
// simply removing the predefined escaper from the pipeline and letting the |
| 206 |
// contextual autoescaper handle the escaping of the pipeline. In other |
| 207 |
// instances, where the predefined escaper occurs in the middle of a |
| 208 |
// pipeline where subsequent commands expect escaped input, e.g. |
| 209 |
// {{.X | html | makeALink}} |
| 210 |
// where makeALink does |
| 211 |
// return `<a href="`+input+`">link</a>` |
| 212 |
// consider refactoring the surrounding template to make use of the |
| 213 |
// contextual autoescaper, i.e. |
| 214 |
// <a href="{{.X}}">link</a> |
| 215 |
// |
| 216 |
// To ease migration to Go 1.9 and beyond, "html" and "urlquery" will |
| 217 |
// continue to be allowed as the last command in a pipeline. However, if the |
| 218 |
// pipeline occurs in an unquoted attribute value context, "html" is |
| 219 |
// disallowed. Avoid using "html" and "urlquery" entirely in new templates. |
| 220 |
ErrPredefinedEscaper |
| 221 |
|
| 222 |
// ErrJSTemplate: "... appears in a JS template literal" |
| 223 |
// Example: |
| 224 |
// <script>var tmpl = `{{.Interp}}`</script> |
| 225 |
// Discussion: |
| 226 |
// Package html/template does not support actions inside of JS template |
| 227 |
// literals. |
| 228 |
// |
| 229 |
// Deprecated: ErrJSTemplate is no longer returned when an action is present |
| 230 |
// in a JS template literal. Actions inside of JS template literals are now |
| 231 |
// escaped as expected. |
| 232 |
ErrJSTemplate |
| 233 |
) |
| 234 |
|
| 235 |
func (e *Error) Error() string { |
| 236 |
switch { |
| 237 |
case e.Node != nil: |
| 238 |
loc, _ := (*parse.Tree)(nil).ErrorContext(e.Node) |
| 239 |
return fmt.Sprintf("html/template:%s: %s", loc, e.Description) |
| 240 |
case e.Line != 0: |
| 241 |
return fmt.Sprintf("html/template:%s:%d: %s", e.Name, e.Line, e.Description) |
| 242 |
case e.Name != "": |
| 243 |
return fmt.Sprintf("html/template:%s: %s", e.Name, e.Description) |
| 244 |
} |
| 245 |
return "html/template: " + e.Description |
| 246 |
} |
| 247 |
|
| 248 |
// errorf creates an error given a format string f and args. |
| 249 |
// The template Name still needs to be supplied. |
| 250 |
func errorf(k ErrorCode, node parse.Node, line int, f string, args ...any) *Error { |
| 251 |
return &Error{k, node, "", line, fmt.Sprintf(f, args...)} |
| 252 |
} |