Debian Codegraph @

yamltemplate/yamltemplate.go

raw
1 // Copyright 2023 Google LLC.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // Package yamltemplate is a drop-in-replacement for using text/template to produce YAML, that adds automatic detection for YAML injection
16 package yamltemplate
17
18 import (
19 "bytes"
20 "errors"
21 "fmt"
22 "io"
23 "io/fs"
24 "os"
25 "path"
26 "path/filepath"
27 "reflect"
28 "text/template"
29 "text/template/parse"
30 "unicode"
31 "unicode/utf8"
32
33 "github.com/pborman/uuid"
34 "gopkg.in/yaml.v3"
35
36 "github.com/google/safetext/common"
37 )
38
39 // ErrInvalidYAMLTemplate indicates the requested template is not valid YAML.
40 var ErrInvalidYAMLTemplate error = errors.New("Invalid YAML Template")
41
42 // ErrYAMLInjection indicates the inputs resulted in YAML injection.
43 var ErrYAMLInjection error = errors.New("YAML Injection Detected")
44
45 // ExecError is the custom error type returned when Execute has an
46 // error evaluating its template. (If a write error occurs, the actual
47 // error is returned; it will not be of type ExecError.)
48 type ExecError = template.ExecError
49
50 // FuncMap is the type of the map defining the mapping from names to functions.
51 // Each function must have either a single return value, or two return values of
52 // which the second has type error. In that case, if the second (error)
53 // return value evaluates to non-nil during execution, execution terminates and
54 // Execute returns that error.
55 //
56 // Errors returned by Execute wrap the underlying error; call errors.As to
57 // uncover them.
58 //
59 // When template execution invokes a function with an argument list, that list
60 // must be assignable to the function's parameter types. Functions meant to
61 // apply to arguments of arbitrary type can use parameters of type any or
62 // of type reflect.Value. Similarly, functions meant to return a result of arbitrary
63 // type can return any or reflect.Value.
64 type FuncMap = template.FuncMap
65
66 // Template is the representation of a parsed template. The *parse.Tree
67 // field is exported only for use by html/template and should be treated
68 // as unexported by all other clients.
69 type Template struct {
70 unsafeTemplate *template.Template
71 uuid string
72 }
73
74 // New allocates a new, undefined template with the given name.
75 func New(name string) *Template {
76 id := uuid.New()
77 funcMap := common.BuildTextTemplateFuncMap(id)
78 return &Template{unsafeTemplate: template.New(name).Funcs(funcMap), uuid: id}
79 }
80
81 const yamlSpecialCharacters = "{}[]&*#?|-.<>=!%@:\"'`,\r\n"
82
83 func mapOrArray(in any) bool {
84 return in != nil && (reflect.TypeOf(in).Kind() == reflect.Map || reflect.TypeOf(in).Kind() == reflect.Slice || reflect.TypeOf(in).Kind() == reflect.Array)
85 }
86
87 func allKeysMatch(base any, a any, b any) bool {
88 if base == nil {
89 return a == nil && b == nil
90 }
91
92 switch reflect.TypeOf(base).Kind() {
93 case reflect.Pointer:
94 if reflect.TypeOf(a).Kind() != reflect.Pointer || reflect.TypeOf(b).Kind() != reflect.Pointer {
95 return false
96 }
97
98 if !allKeysMatch(reflect.ValueOf(base).Elem().Interface(), reflect.ValueOf(a).Elem().Interface(), reflect.ValueOf(b).Elem().Interface()) {
99 return true
100 }
101 case reflect.Map:
102 if reflect.TypeOf(a).Kind() != reflect.Map || reflect.TypeOf(b).Kind() != reflect.Map {
103 return false
104 }
105
106 if reflect.ValueOf(a).Len() != reflect.ValueOf(base).Len() || reflect.ValueOf(b).Len() != reflect.ValueOf(base).Len() {
107 return false
108 }
109
110 basei := reflect.ValueOf(base).MapRange()
111 for basei.Next() {
112 av := reflect.ValueOf(a).MapIndex(basei.Key())
113 bv := reflect.ValueOf(b).MapIndex(basei.Key())
114 if !av.IsValid() || !bv.IsValid() ||
115 !allKeysMatch(basei.Value().Interface(), av.Interface(), bv.Interface()) {
116 return false
117 }
118 }
119 case reflect.Slice, reflect.Array:
120 if reflect.TypeOf(a).Kind() != reflect.Slice && reflect.TypeOf(a).Kind() != reflect.Array &&
121 reflect.TypeOf(b).Kind() != reflect.Slice && reflect.TypeOf(b).Kind() != reflect.Array {
122 return false
123 }
124
125 if reflect.ValueOf(a).Len() != reflect.ValueOf(base).Len() || reflect.ValueOf(b).Len() != reflect.ValueOf(base).Len() {
126 return false
127 }
128
129 for i := 0; i < reflect.ValueOf(base).Len(); i++ {
130 if !allKeysMatch(reflect.ValueOf(base).Index(i).Interface(), reflect.ValueOf(a).Index(i).Interface(), reflect.ValueOf(b).Index(i).Interface()) {
131 return false
132 }
133 }
134 case reflect.Struct:
135 n := reflect.ValueOf(base).NumField()
136 for i := 0; i < n; i++ {
137 baseit := reflect.TypeOf(base).Field(i)
138 ait := reflect.TypeOf(a).Field(i)
139 bit := reflect.TypeOf(b).Field(i)
140
141 if baseit.Name != ait.Name || baseit.Name != bit.Name {
142 return false
143 }
144
145 // Only compare public members (private members cannot be overwritten by text/template)
146 decodedName, _ := utf8.DecodeRuneInString(baseit.Name)
147 if unicode.IsUpper(decodedName) {
148 basei := reflect.ValueOf(base).Field(i)
149 ai := reflect.ValueOf(a).Field(i)
150 bi := reflect.ValueOf(b).Field(i)
151
152 if !allKeysMatch(basei.Interface(), ai.Interface(), bi.Interface()) {
153 return false
154 }
155 }
156 }
157 case reflect.String:
158 // Baseline type of string was chosen arbitrarily, so just check that there isn't a new a map or slice/array injected, which would change the structure of the YAML
159 if mapOrArray(a) || mapOrArray(b) {
160 return false
161 }
162 default:
163 if reflect.TypeOf(a) != reflect.TypeOf(base) || reflect.TypeOf(b) != reflect.TypeOf(base) {
164 return false
165 }
166 }
167
168 return true
169 }
170
171 func unmarshalYaml(data []byte) ([]any, error) {
172 r := make([]any, 0)
173
174 decoder := yaml.NewDecoder(bytes.NewReader(data))
175 for {
176 var t any
177 err := decoder.Decode(&t)
178
179 if err == io.EOF {
180 break
181 } else if err == nil {
182 r = append(r, t)
183 } else {
184 return nil, err
185 }
186 }
187
188 return r, nil
189 }
190
191 // Mutation algorithm
192 func mutateString(s string) string {
193 // Longest possible output string is 2x the original
194 out := make([]rune, utf8.RuneCountInString(s)*2)
195
196 i := 0
197 for _, r := range s {
198 out[i] = r
199 i++
200
201 // Don't repeat quoting-related characters so as to not allow YAML context change in the mutation result
202 if r != '\\' && r != '\'' && r != '"' {
203 out[i] = r
204 i++
205 }
206 }
207
208 return string(out[:i])
209 }
210
211 // Execute applies a parsed template to the specified data object,
212 // and writes the output to wr.
213 // If an error occurs executing the template or writing its output,
214 // execution stops, but partial results may already have been written to
215 // the output writer.
216 // A template may be executed safely in parallel, although if parallel
217 // executions share a Writer the output may be interleaved.
218 //
219 // If data is a reflect.Value, the template applies to the concrete
220 // value that the reflect.Value holds, as in fmt.Print.
221 func (t *Template) Execute(wr io.Writer, data any) (err error) {
222 if data == nil {
223 return common.ExecuteWithCallback(t.unsafeTemplate, t.uuid, common.EchoString, wr, data)
224 }
225
226 // An attacker may be able to cause type confusion or nil dereference panic during allKeysMatch
227 defer func() {
228 if r := recover(); r != nil {
229 err = ErrYAMLInjection
230 }
231 }()
232
233 // Calculate requested result first
234 var requestedResult bytes.Buffer
235
236 if err := common.ExecuteWithCallback(t.unsafeTemplate, t.uuid, common.EchoString, &requestedResult, data); err != nil {
237 return err
238 }
239
240 walked, err := t.unsafeTemplate.Clone()
241 if err != nil {
242 return err
243 }
244 walked.Tree = walked.Tree.Copy()
245
246 common.WalkApplyFuncToNonDeclaractiveActions(walked, walked.Tree.Root)
247
248 // Get baseline
249 var baselineResult bytes.Buffer
250 if err = common.ExecuteWithCallback(walked, t.uuid, common.BaselineString, &baselineResult, data); err != nil {
251 return err
252 }
253
254 parsedBaselineResult, err := unmarshalYaml(baselineResult.Bytes())
255 if err != nil {
256 return ErrInvalidYAMLTemplate
257 }
258
259 // If baseline was valid, request must also be valid YAML for no injection to have occurred
260 parsedRequestedResult, err := unmarshalYaml(requestedResult.Bytes())
261 if err != nil {
262 return ErrYAMLInjection
263 }
264
265 // Mutate the input
266 var mutatedResult bytes.Buffer
267 if err = common.ExecuteWithCallback(walked, t.uuid, mutateString, &mutatedResult, data); err != nil {
268 return err
269 }
270
271 parsedMutatedResult, err := unmarshalYaml(mutatedResult.Bytes())
272 if err != nil {
273 return ErrYAMLInjection
274 }
275
276 // Compare results
277 if !allKeysMatch(parsedBaselineResult, parsedRequestedResult, parsedMutatedResult) {
278 return ErrYAMLInjection
279 }
280
281 requestedResult.WriteTo(wr)
282 return nil
283 }
284
285 // Name returns the name of the template.
286 func (t *Template) Name() string {
287 return t.unsafeTemplate.Name()
288 }
289
290 // New allocates a new, undefined template associated with the given one and with the same
291 // delimiters. The association, which is transitive, allows one template to
292 // invoke another with a {{template}} action.
293 //
294 // Because associated templates share underlying data, template construction
295 // cannot be done safely in parallel. Once the templates are constructed, they
296 // can be executed in parallel.
297 func (t *Template) New(name string) *Template {
298 id := uuid.New()
299 funcMap := common.BuildTextTemplateFuncMap(id)
300 return &Template{unsafeTemplate: t.unsafeTemplate.New(name).Funcs(funcMap), uuid: id}
301 }
302
303 // Clone returns a duplicate of the template, including all associated
304 // templates. The actual representation is not copied, but the name space of
305 // associated templates is, so further calls to Parse in the copy will add
306 // templates to the copy but not to the original. Clone can be used to prepare
307 // common templates and use them with variant definitions for other templates
308 // by adding the variants after the clone is made.
309 func (t *Template) Clone() (*Template, error) {
310 id := uuid.New()
311 nt, err := t.unsafeTemplate.Clone()
312 return &Template{unsafeTemplate: nt, uuid: id}, err
313 }
314
315 // AddParseTree associates the argument parse tree with the template t, giving
316 // it the specified name. If the template has not been defined, this tree becomes
317 // its definition. If it has been defined and already has that name, the existing
318 // definition is replaced; otherwise a new template is created, defined, and returned.
319 func (t *Template) AddParseTree(name string, tree *parse.Tree) (*Template, error) {
320 nt, err := t.unsafeTemplate.AddParseTree(name, tree)
321
322 if nt != t.unsafeTemplate {
323 id := uuid.New()
324 return &Template{unsafeTemplate: nt, uuid: id}, err
325 }
326 return t, err
327 }
328
329 // Option sets options for the template. Options are described by
330 // strings, either a simple string or "key=value". There can be at
331 // most one equals sign in an option string. If the option string
332 // is unrecognized or otherwise invalid, Option panics.
333 //
334 // Known options:
335 //
336 // missingkey: Control the behavior during execution if a map is
337 // indexed with a key that is not present in the map.
338 //
339 // "missingkey=default" or "missingkey=invalid"
340 // The default behavior: Do nothing and continue execution.
341 // If printed, the result of the index operation is the string
342 // "<no value>".
343 // "missingkey=zero"
344 // The operation returns the zero value for the map type's element.
345 // "missingkey=error"
346 // Execution stops immediately with an error.
347 func (t *Template) Option(opt ...string) *Template {
348 for _, s := range opt {
349 t.unsafeTemplate.Option(s)
350 }
351 return t
352 }
353
354 // Templates returns a slice of defined templates associated with t.
355 func (t *Template) Templates() []*Template {
356 s := t.unsafeTemplate.Templates()
357 var id string
358
359 var ns []*Template
360 for _, nt := range s {
361 id = uuid.New()
362 ns = append(ns, &Template{unsafeTemplate: nt, uuid: id})
363 }
364
365 return ns
366 }
367
368 // ExecuteTemplate applies the template associated with t that has the given name
369 // to the specified data object and writes the output to wr.
370 // If an error occurs executing the template or writing its output,
371 // execution stops, but partial results may already have been written to
372 // the output writer.
373 // A template may be executed safely in parallel, although if parallel
374 // executions share a Writer the output may be interleaved.
375 func (t *Template) ExecuteTemplate(wr io.Writer, name string, data any) error {
376 tmpl := t.Lookup(name)
377 if tmpl == nil {
378 return fmt.Errorf("template: no template %q associated with template %q", name, t.Name())
379 }
380 return tmpl.Execute(wr, data)
381 }
382
383 // Delims sets the action delimiters to the specified strings, to be used in
384 // subsequent calls to Parse, ParseFiles, or ParseGlob. Nested template
385 // definitions will inherit the settings. An empty delimiter stands for the
386 // corresponding default: {{ or }}.
387 // The return value is the template, so calls can be chained.
388 func (t *Template) Delims(left, right string) *Template {
389 t.unsafeTemplate.Delims(left, right)
390 return t
391 }
392
393 // DefinedTemplates returns a string listing the defined templates,
394 // prefixed by the string "; defined templates are: ". If there are none,
395 // it returns the empty string. For generating an error message here
396 // and in html/template.
397 func (t *Template) DefinedTemplates() string {
398 return t.unsafeTemplate.DefinedTemplates()
399 }
400
401 // Funcs adds the elements of the argument map to the template's function map.
402 // It must be called before the template is parsed.
403 // It panics if a value in the map is not a function with appropriate return
404 // type or if the name cannot be used syntactically as a function in a template.
405 // It is legal to overwrite elements of the map. The return value is the template,
406 // so calls can be chained.
407 func (t *Template) Funcs(funcMap FuncMap) *Template {
408 t.unsafeTemplate.Funcs(funcMap)
409 return t
410 }
411
412 // Lookup returns the template with the given name that is associated with t.
413 // It returns nil if there is no such template or the template has no definition.
414 func (t *Template) Lookup(name string) *Template {
415 nt := t.unsafeTemplate.Lookup(name)
416
417 if nt == nil {
418 return nil
419 }
420
421 if nt != t.unsafeTemplate {
422 id := uuid.New()
423 return &Template{unsafeTemplate: nt, uuid: id}
424 }
425
426 return t
427 }
428
429 // Parse parses text as a template body for t.
430 // Named template definitions ({{define ...}} or {{block ...}} statements) in text
431 // define additional templates associated with t and are removed from the
432 // definition of t itself.
433 //
434 // Templates can be redefined in successive calls to Parse.
435 // A template definition with a body containing only white space and comments
436 // is considered empty and will not replace an existing template's body.
437 // This allows using Parse to add new named template definitions without
438 // overwriting the main template body.
439 func (t *Template) Parse(text string) (*Template, error) {
440 nt, err := t.unsafeTemplate.Parse(text)
441
442 if nt != t.unsafeTemplate {
443 id := uuid.New()
444 return &Template{unsafeTemplate: nt, uuid: id}, err
445 }
446
447 return t, err
448 }
449
450 // Must is a helper that wraps a call to a function returning (*Template, error)
451 // and panics if the error is non-nil. It is intended for use in variable
452 // initializations such as
453 //
454 // var t = template.Must(template.New("name").Parse("text"))
455 func Must(t *Template, err error) *Template {
456 if err != nil {
457 panic(err)
458 }
459 return t
460 }
461
462 func readFileOS(file string) (name string, b []byte, err error) {
463 name = filepath.Base(file)
464 b, err = os.ReadFile(file)
465 return
466 }
467
468 func readFileFS(fsys fs.FS) func(string) (string, []byte, error) {
469 return func(file string) (name string, b []byte, err error) {
470 name = path.Base(file)
471 b, err = fs.ReadFile(fsys, file)
472 return
473 }
474 }
475
476 func parseFiles(t *Template, readFile func(string) (string, []byte, error), filenames ...string) (*Template, error) {
477 if len(filenames) == 0 {
478 // Not really a problem, but be consistent.
479 return nil, fmt.Errorf("template: no files named in call to ParseFiles")
480 }
481 for _, filename := range filenames {
482 name, b, err := readFile(filename)
483 if err != nil {
484 return nil, err
485 }
486 s := string(b)
487 // First template becomes return value if not already defined,
488 // and we use that one for subsequent New calls to associate
489 // all the templates together. Also, if this file has the same name
490 // as t, this file becomes the contents of t, so
491 // t, err := New(name).Funcs(xxx).ParseFiles(name)
492 // works. Otherwise we create a new template associated with t.
493 var tmpl *Template
494 if t == nil {
495 t = New(name)
496 }
497 if name == t.Name() {
498 tmpl = t
499 } else {
500 tmpl = t.New(name)
501 }
502 _, err = tmpl.Parse(s)
503 if err != nil {
504 return nil, err
505 }
506 }
507 return t, nil
508 }
509
510 // parseGlob is the implementation of the function and method ParseGlob.
511 func parseGlob(t *Template, pattern string) (*Template, error) {
512 filenames, err := filepath.Glob(pattern)
513 if err != nil {
514 return nil, err
515 }
516 if len(filenames) == 0 {
517 return nil, fmt.Errorf("template: pattern matches no files: %#q", pattern)
518 }
519 return parseFiles(t, readFileOS, filenames...)
520 }
521
522 func parseFS(t *Template, fsys fs.FS, patterns []string) (*Template, error) {
523 var filenames []string
524 for _, pattern := range patterns {
525 list, err := fs.Glob(fsys, pattern)
526 if err != nil {
527 return nil, err
528 }
529 if len(list) == 0 {
530 return nil, fmt.Errorf("template: pattern matches no files: %#q", pattern)
531 }
532 filenames = append(filenames, list...)
533 }
534 return parseFiles(t, readFileFS(fsys), filenames...)
535 }
536
537 // ParseFiles creates a new Template and parses the template definitions from
538 // the named files. The returned template's name will have the base name and
539 // parsed contents of the first file. There must be at least one file.
540 // If an error occurs, parsing stops and the returned *Template is nil.
541 //
542 // When parsing multiple files with the same name in different directories,
543 // the last one mentioned will be the one that results.
544 // For instance, ParseFiles("a/foo", "b/foo") stores "b/foo" as the template
545 // named "foo", while "a/foo" is unavailable.
546 func ParseFiles(filenames ...string) (*Template, error) {
547 return parseFiles(nil, readFileOS, filenames...)
548 }
549
550 // ParseFiles parses the named files and associates the resulting templates with
551 // t. If an error occurs, parsing stops and the returned template is nil;
552 // otherwise it is t. There must be at least one file.
553 // Since the templates created by ParseFiles are named by the base
554 // names of the argument files, t should usually have the name of one
555 // of the (base) names of the files. If it does not, depending on t's
556 // contents before calling ParseFiles, t.Execute may fail. In that
557 // case use t.ExecuteTemplate to execute a valid template.
558 //
559 // When parsing multiple files with the same name in different directories,
560 // the last one mentioned will be the one that results.
561 func (t *Template) ParseFiles(filenames ...string) (*Template, error) {
562 // Ensure template is inited
563 t.Option()
564
565 return parseFiles(t, readFileOS, filenames...)
566 }
567
568 // ParseGlob creates a new Template and parses the template definitions from
569 // the files identified by the pattern. The files are matched according to the
570 // semantics of filepath.Match, and the pattern must match at least one file.
571 // The returned template will have the (base) name and (parsed) contents of the
572 // first file matched by the pattern. ParseGlob is equivalent to calling
573 // ParseFiles with the list of files matched by the pattern.
574 //
575 // When parsing multiple files with the same name in different directories,
576 // the last one mentioned will be the one that results.
577 func ParseGlob(pattern string) (*Template, error) {
578 return parseGlob(nil, pattern)
579 }
580
581 // ParseGlob parses the template definitions in the files identified by the
582 // pattern and associates the resulting templates with t. The files are matched
583 // according to the semantics of filepath.Match, and the pattern must match at
584 // least one file. ParseGlob is equivalent to calling t.ParseFiles with the
585 // list of files matched by the pattern.
586 //
587 // When parsing multiple files with the same name in different directories,
588 // the last one mentioned will be the one that results.
589 func (t *Template) ParseGlob(pattern string) (*Template, error) {
590 // Ensure template is inited
591 t.Option()
592
593 return parseGlob(t, pattern)
594 }
595
596 // ParseFS is like ParseFiles or ParseGlob but reads from the file system fsys
597 // instead of the host operating system's file system.
598 // It accepts a list of glob patterns.
599 // (Note that most file names serve as glob patterns matching only themselves.)
600 func ParseFS(fsys fs.FS, patterns ...string) (*Template, error) {
601 return parseFS(nil, fsys, patterns)
602 }
603
604 // ParseFS is like ParseFiles or ParseGlob but reads from the file system fsys
605 // instead of the host operating system's file system.
606 // It accepts a list of glob patterns.
607 // (Note that most file names serve as glob patterns matching only themselves.)
608 func (t *Template) ParseFS(fsys fs.FS, patterns ...string) (*Template, error) {
609 // Ensure template is inited
610 t.Option()
611
612 return parseFS(t, fsys, patterns)
613 }
614
615 // HTMLEscape writes to w the escaped HTML equivalent of the plain text data b.
616 func HTMLEscape(w io.Writer, b []byte) {
617 template.HTMLEscape(w, b)
618 }
619
620 // HTMLEscapeString returns the escaped HTML equivalent of the plain text data s.
621 func HTMLEscapeString(s string) string {
622 return template.HTMLEscapeString(s)
623 }
624
625 // HTMLEscaper returns the escaped HTML equivalent of the textual
626 // representation of its arguments.
627 func HTMLEscaper(args ...any) string {
628 return template.HTMLEscaper(args)
629 }
630
631 // IsTrue reports whether the value is 'true', in the sense of not the zero of its type,
632 // and whether the value has a meaningful truth value. This is the definition of
633 // truth used by if and other such actions.
634 func IsTrue(val any) (truth, ok bool) {
635 return template.IsTrue(val)
636 }
637
638 // JSEscape writes to w the escaped JavaScript equivalent of the plain text data b.
639 func JSEscape(w io.Writer, b []byte) {
640 template.JSEscape(w, b)
641 }
642
643 // JSEscapeString returns the escaped JavaScript equivalent of the plain text data s.
644 func JSEscapeString(s string) string {
645 return template.JSEscapeString(s)
646 }
647
648 // JSEscaper returns the escaped JavaScript equivalent of the textual
649 // representation of its arguments.
650 func JSEscaper(args ...any) string {
651 return template.JSEscaper(args)
652 }
653
654 // URLQueryEscaper returns the escaped value of the textual representation of
655 // its arguments in a form suitable for embedding in a URL query.
656 func URLQueryEscaper(args ...any) string {
657 return template.URLQueryEscaper(args)
658 }