| 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 yamltemplatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/ |
| 17 | |
| 18 | import ( |
| 19 | "bytesscip-go gomod github.com/golang/go/src go1.21 bytes/" |
| 20 | "errorsscip-go gomod github.com/golang/go/src go1.21 errors/" |
| 21 | "fmtscip-go gomod github.com/golang/go/src go1.21 fmt/" |
| 22 | "ioscip-go gomod github.com/golang/go/src go1.21 io/" |
| 23 | "io/fsscip-go gomod github.com/golang/go/src go1.21 `io/fs`/" |
| 24 | "osscip-go gomod github.com/golang/go/src go1.21 os/" |
| 25 | "pathscip-go gomod github.com/golang/go/src go1.21 path/" |
| 26 | "path/filepathscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/" |
| 27 | "reflectscip-go gomod github.com/golang/go/src go1.21 reflect/" |
| 28 | "text/templatescip-go gomod github.com/golang/go/src go1.21 `text/template`/" |
| 29 | "text/template/parsescip-go gomod github.com/golang/go/src go1.21 `text/template/parse`/" |
| 30 | "unicodescip-go gomod github.com/golang/go/src go1.21 unicode/" |
| 31 | "unicode/utf8scip-go gomod github.com/golang/go/src go1.21 `unicode/utf8`/" |
| 32 | |
| 33 | "github.com/pborman/uuidscip-go gomod github.com/pborman/uuid v1.2.1 `github.com/pborman/uuid`/" |
| 34 | "gopkg.in/yaml.v3scip-go gomod gopkg.in/yaml.v3 v3.0.1 `gopkg.in/yaml.v3`/" |
| 35 | |
| 36 | "github.com/google/safetext/commonscip-go gomod github.com/google/safetext . `github.com/google/safetext/common`/" |
| 37 | ) |
| 38 | |
| 39 | // ErrInvalidYAMLTemplate indicates the requested template is not valid YAML. |
| 40 | var ErrInvalidYAMLTemplatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/ErrInvalidYAMLTemplate. error = errorsscip-go gomod github.com/golang/go/src go1.21 errors/.Newscip-go gomod github.com/golang/go/src go1.21 errors/New().("Invalid YAML Template") |
| 41 | |
| 42 | // ErrYAMLInjection indicates the inputs resulted in YAML injection. |
| 43 | var ErrYAMLInjectionscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/ErrYAMLInjection. error = errorsscip-go gomod github.com/golang/go/src go1.21 errors/.Newscip-go gomod github.com/golang/go/src go1.21 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 ExecErrorscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/ExecError# = templatescip-go gomod github.com/golang/go/src go1.21 `text/template`/.ExecErrorscip-go gomod github.com/golang/go/src go1.21 `text/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 FuncMapscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/FuncMap# = templatescip-go gomod github.com/golang/go/src go1.21 `text/template`/.FuncMapscip-go gomod github.com/golang/go/src go1.21 `text/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 Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template# struct { |
| 70 | unsafeTemplatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#unsafeTemplate. *templatescip-go gomod github.com/golang/go/src go1.21 `text/template`/.Templatescip-go gomod github.com/golang/go/src go1.21 `text/template`/Template# |
| 71 | uuidscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#uuid. string |
| 72 | } |
| 73 | |
| 74 | // New allocates a new, undefined template with the given name. |
| 75 | func Newscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/New().(name string) *Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template# { |
| 76 | id := uuidscip-go gomod github.com/pborman/uuid v1.2.1 `github.com/pborman/uuid`/.Newscip-go gomod github.com/pborman/uuid v1.2.1 `github.com/pborman/uuid`/New().() |
| 77 | funcMap := commonscip-go gomod github.com/google/safetext . `github.com/google/safetext/common`/.BuildTextTemplateFuncMapscip-go gomod github.com/google/safetext . `github.com/google/safetext/common`/BuildTextTemplateFuncMap().(id) |
| 78 | return &Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#{unsafeTemplatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#unsafeTemplate.: templatescip-go gomod github.com/golang/go/src go1.21 `text/template`/.Newscip-go gomod github.com/golang/go/src go1.21 `text/template`/New().(name).Funcsscip-go gomod github.com/golang/go/src go1.21 `text/template`/Template#Funcs().(funcMap), uuidscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#uuid.: id} |
| 79 | } |
| 80 | |
| 81 | const yamlSpecialCharactersscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/yamlSpecialCharacters. = "{}[]&*#?|-.<>=!%@:\"'`,\r\n" |
| 82 | |
| 83 | func mapOrArrayscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/mapOrArray().(in any) bool { |
| 84 | return in != nil && (reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.21 reflect/TypeOf().(in).Kindscip-go gomod github.com/golang/go/src go1.21 reflect/Type#Kind().() == reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.Mapscip-go gomod github.com/golang/go/src go1.21 reflect/Map. || reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.21 reflect/TypeOf().(in).Kindscip-go gomod github.com/golang/go/src go1.21 reflect/Type#Kind().() == reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.Slicescip-go gomod github.com/golang/go/src go1.21 reflect/Slice. || reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.21 reflect/TypeOf().(in).Kindscip-go gomod github.com/golang/go/src go1.21 reflect/Type#Kind().() == reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.Arrayscip-go gomod github.com/golang/go/src go1.21 reflect/Array.) |
| 85 | } |
| 86 | |
| 87 | func allKeysMatchscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/allKeysMatch().(base any, a any, b any) bool { |
| 88 | if base == nil { |
| 89 | return a == nil && b == nil |
| 90 | } |
| 91 | |
| 92 | switch reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.21 reflect/TypeOf().(base).Kindscip-go gomod github.com/golang/go/src go1.21 reflect/Type#Kind().() { |
| 93 | case reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.Pointerscip-go gomod github.com/golang/go/src go1.21 reflect/Pointer.: |
| 94 | if reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.21 reflect/TypeOf().(a).Kindscip-go gomod github.com/golang/go/src go1.21 reflect/Type#Kind().() != reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.Pointerscip-go gomod github.com/golang/go/src go1.21 reflect/Pointer. || reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.21 reflect/TypeOf().(b).Kindscip-go gomod github.com/golang/go/src go1.21 reflect/Type#Kind().() != reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.Pointerscip-go gomod github.com/golang/go/src go1.21 reflect/Pointer. { |
| 95 | return false |
| 96 | } |
| 97 | |
| 98 | if !allKeysMatchscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/allKeysMatch().(reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.21 reflect/ValueOf().(base).Elemscip-go gomod github.com/golang/go/src go1.21 reflect/Value#Elem().().Interfacescip-go gomod github.com/golang/go/src go1.21 reflect/Value#Interface().(), reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.21 reflect/ValueOf().(a).Elemscip-go gomod github.com/golang/go/src go1.21 reflect/Value#Elem().().Interfacescip-go gomod github.com/golang/go/src go1.21 reflect/Value#Interface().(), reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.21 reflect/ValueOf().(b).Elemscip-go gomod github.com/golang/go/src go1.21 reflect/Value#Elem().().Interfacescip-go gomod github.com/golang/go/src go1.21 reflect/Value#Interface().()) { |
| 99 | return true |
| 100 | } |
| 101 | case reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.Mapscip-go gomod github.com/golang/go/src go1.21 reflect/Map.: |
| 102 | if reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.21 reflect/TypeOf().(a).Kindscip-go gomod github.com/golang/go/src go1.21 reflect/Type#Kind().() != reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.Mapscip-go gomod github.com/golang/go/src go1.21 reflect/Map. || reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.21 reflect/TypeOf().(b).Kindscip-go gomod github.com/golang/go/src go1.21 reflect/Type#Kind().() != reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.Mapscip-go gomod github.com/golang/go/src go1.21 reflect/Map. { |
| 103 | return false |
| 104 | } |
| 105 | |
| 106 | if reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.21 reflect/ValueOf().(a).Lenscip-go gomod github.com/golang/go/src go1.21 reflect/Value#Len().() != reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.21 reflect/ValueOf().(base).Lenscip-go gomod github.com/golang/go/src go1.21 reflect/Value#Len().() || reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.21 reflect/ValueOf().(b).Lenscip-go gomod github.com/golang/go/src go1.21 reflect/Value#Len().() != reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.21 reflect/ValueOf().(base).Lenscip-go gomod github.com/golang/go/src go1.21 reflect/Value#Len().() { |
| 107 | return false |
| 108 | } |
| 109 | |
| 110 | basei := reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.21 reflect/ValueOf().(base).MapRangescip-go gomod github.com/golang/go/src go1.21 reflect/Value#MapRange().() |
| 111 | for basei.Nextscip-go gomod github.com/golang/go/src go1.21 reflect/MapIter#Next().() { |
| 112 | av := reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.21 reflect/ValueOf().(a).MapIndexscip-go gomod github.com/golang/go/src go1.21 reflect/Value#MapIndex().(basei.Keyscip-go gomod github.com/golang/go/src go1.21 reflect/MapIter#Key().()) |
| 113 | bv := reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.21 reflect/ValueOf().(b).MapIndexscip-go gomod github.com/golang/go/src go1.21 reflect/Value#MapIndex().(basei.Keyscip-go gomod github.com/golang/go/src go1.21 reflect/MapIter#Key().()) |
| 114 | if !av.IsValidscip-go gomod github.com/golang/go/src go1.21 reflect/Value#IsValid().() || !bv.IsValidscip-go gomod github.com/golang/go/src go1.21 reflect/Value#IsValid().() || |
| 115 | !allKeysMatchscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/allKeysMatch().(basei.Valuescip-go gomod github.com/golang/go/src go1.21 reflect/MapIter#Value().().Interfacescip-go gomod github.com/golang/go/src go1.21 reflect/Value#Interface().(), av.Interfacescip-go gomod github.com/golang/go/src go1.21 reflect/Value#Interface().(), bv.Interfacescip-go gomod github.com/golang/go/src go1.21 reflect/Value#Interface().()) { |
| 116 | return false |
| 117 | } |
| 118 | } |
| 119 | case reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.Slicescip-go gomod github.com/golang/go/src go1.21 reflect/Slice., reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.Arrayscip-go gomod github.com/golang/go/src go1.21 reflect/Array.: |
| 120 | if reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.21 reflect/TypeOf().(a).Kindscip-go gomod github.com/golang/go/src go1.21 reflect/Type#Kind().() != reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.Slicescip-go gomod github.com/golang/go/src go1.21 reflect/Slice. && reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.21 reflect/TypeOf().(a).Kindscip-go gomod github.com/golang/go/src go1.21 reflect/Type#Kind().() != reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.Arrayscip-go gomod github.com/golang/go/src go1.21 reflect/Array. && |
| 121 | reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.21 reflect/TypeOf().(b).Kindscip-go gomod github.com/golang/go/src go1.21 reflect/Type#Kind().() != reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.Slicescip-go gomod github.com/golang/go/src go1.21 reflect/Slice. && reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.21 reflect/TypeOf().(b).Kindscip-go gomod github.com/golang/go/src go1.21 reflect/Type#Kind().() != reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.Arrayscip-go gomod github.com/golang/go/src go1.21 reflect/Array. { |
| 122 | return false |
| 123 | } |
| 124 | |
| 125 | if reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.21 reflect/ValueOf().(a).Lenscip-go gomod github.com/golang/go/src go1.21 reflect/Value#Len().() != reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.21 reflect/ValueOf().(base).Lenscip-go gomod github.com/golang/go/src go1.21 reflect/Value#Len().() || reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.21 reflect/ValueOf().(b).Lenscip-go gomod github.com/golang/go/src go1.21 reflect/Value#Len().() != reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.21 reflect/ValueOf().(base).Lenscip-go gomod github.com/golang/go/src go1.21 reflect/Value#Len().() { |
| 126 | return false |
| 127 | } |
| 128 | |
| 129 | for i := 0; i < reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.21 reflect/ValueOf().(base).Lenscip-go gomod github.com/golang/go/src go1.21 reflect/Value#Len().(); i++ { |
| 130 | if !allKeysMatchscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/allKeysMatch().(reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.21 reflect/ValueOf().(base).Indexscip-go gomod github.com/golang/go/src go1.21 reflect/Value#Index().(i).Interfacescip-go gomod github.com/golang/go/src go1.21 reflect/Value#Interface().(), reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.21 reflect/ValueOf().(a).Indexscip-go gomod github.com/golang/go/src go1.21 reflect/Value#Index().(i).Interfacescip-go gomod github.com/golang/go/src go1.21 reflect/Value#Interface().(), reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.21 reflect/ValueOf().(b).Indexscip-go gomod github.com/golang/go/src go1.21 reflect/Value#Index().(i).Interfacescip-go gomod github.com/golang/go/src go1.21 reflect/Value#Interface().()) { |
| 131 | return false |
| 132 | } |
| 133 | } |
| 134 | case reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.Structscip-go gomod github.com/golang/go/src go1.21 reflect/Struct.: |
| 135 | n := reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.21 reflect/ValueOf().(base).NumFieldscip-go gomod github.com/golang/go/src go1.21 reflect/Value#NumField().() |
| 136 | for i := 0; i < n; i++ { |
| 137 | baseit := reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.21 reflect/TypeOf().(base).Fieldscip-go gomod github.com/golang/go/src go1.21 reflect/Type#Field().(i) |
| 138 | ait := reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.21 reflect/TypeOf().(a).Fieldscip-go gomod github.com/golang/go/src go1.21 reflect/Type#Field().(i) |
| 139 | bit := reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.21 reflect/TypeOf().(b).Fieldscip-go gomod github.com/golang/go/src go1.21 reflect/Type#Field().(i) |
| 140 | |
| 141 | if baseit.Namescip-go gomod github.com/golang/go/src go1.21 reflect/StructField#Name. != ait.Namescip-go gomod github.com/golang/go/src go1.21 reflect/StructField#Name. || baseit.Namescip-go gomod github.com/golang/go/src go1.21 reflect/StructField#Name. != bit.Namescip-go gomod github.com/golang/go/src go1.21 reflect/StructField#Name. { |
| 142 | return false |
| 143 | } |
| 144 | |
| 145 | // Only compare public members (private members cannot be overwritten by text/template) |
| 146 | decodedName, _ := utf8scip-go gomod github.com/golang/go/src go1.21 `unicode/utf8`/.DecodeRuneInStringscip-go gomod github.com/golang/go/src go1.21 `unicode/utf8`/DecodeRuneInString().(baseit.Namescip-go gomod github.com/golang/go/src go1.21 reflect/StructField#Name.) |
| 147 | if unicodescip-go gomod github.com/golang/go/src go1.21 unicode/.IsUpperscip-go gomod github.com/golang/go/src go1.21 unicode/IsUpper().(decodedName) { |
| 148 | basei := reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.21 reflect/ValueOf().(base).Fieldscip-go gomod github.com/golang/go/src go1.21 reflect/Value#Field().(i) |
| 149 | ai := reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.21 reflect/ValueOf().(a).Fieldscip-go gomod github.com/golang/go/src go1.21 reflect/Value#Field().(i) |
| 150 | bi := reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.21 reflect/ValueOf().(b).Fieldscip-go gomod github.com/golang/go/src go1.21 reflect/Value#Field().(i) |
| 151 | |
| 152 | if !allKeysMatchscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/allKeysMatch().(basei.Interfacescip-go gomod github.com/golang/go/src go1.21 reflect/Value#Interface().(), ai.Interfacescip-go gomod github.com/golang/go/src go1.21 reflect/Value#Interface().(), bi.Interfacescip-go gomod github.com/golang/go/src go1.21 reflect/Value#Interface().()) { |
| 153 | return false |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | case reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.Stringscip-go gomod github.com/golang/go/src go1.21 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 mapOrArrayscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/mapOrArray().(a) || mapOrArrayscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/mapOrArray().(b) { |
| 160 | return false |
| 161 | } |
| 162 | default: |
| 163 | if reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.21 reflect/TypeOf().(a) != reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.21 reflect/TypeOf().(base) || reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.21 reflect/TypeOf().(b) != reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.21 reflect/TypeOf().(base) { |
| 164 | return false |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | return true |
| 169 | } |
| 170 | |
| 171 | func unmarshalYamlscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/unmarshalYaml().(data []byte) ([]any, error) { |
| 172 | r := make([]any, 0) |
| 173 | |
| 174 | decoder := yamlscip-go gomod gopkg.in/yaml.v3 v3.0.1 `gopkg.in/yaml.v3`/.NewDecoderscip-go gomod gopkg.in/yaml.v3 v3.0.1 `gopkg.in/yaml.v3`/NewDecoder().(bytesscip-go gomod github.com/golang/go/src go1.21 bytes/.NewReaderscip-go gomod github.com/golang/go/src go1.21 bytes/NewReader().(data)) |
| 175 | for { |
| 176 | var t any |
| 177 | err := decoder.Decodescip-go gomod gopkg.in/yaml.v3 v3.0.1 `gopkg.in/yaml.v3`/Decoder#Decode().(&t) |
| 178 | |
| 179 | if err == ioscip-go gomod github.com/golang/go/src go1.21 io/.EOFscip-go gomod github.com/golang/go/src go1.21 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 mutateStringscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/mutateString().(s string) string { |
| 193 | // Longest possible output string is 2x the original |
| 194 | out := make([]rune, utf8scip-go gomod github.com/golang/go/src go1.21 `unicode/utf8`/.RuneCountInStringscip-go gomod github.com/golang/go/src go1.21 `unicode/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 *Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#) Executescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#Execute().(wr ioscip-go gomod github.com/golang/go/src go1.21 io/.Writerscip-go gomod github.com/golang/go/src go1.21 io/Writer#, data any) (err error) { |
| 222 | if data == nil { |
| 223 | return commonscip-go gomod github.com/google/safetext . `github.com/google/safetext/common`/.ExecuteWithCallbackscip-go gomod github.com/google/safetext . `github.com/google/safetext/common`/ExecuteWithCallback().(t.unsafeTemplatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#unsafeTemplate., t.uuidscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#uuid., commonscip-go gomod github.com/google/safetext . `github.com/google/safetext/common`/.EchoStringscip-go gomod github.com/google/safetext . `github.com/google/safetext/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 = ErrYAMLInjectionscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/ErrYAMLInjection. |
| 230 | } |
| 231 | }() |
| 232 | |
| 233 | // Calculate requested result first |
| 234 | var requestedResult bytesscip-go gomod github.com/golang/go/src go1.21 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.21 bytes/Buffer# |
| 235 | |
| 236 | if err := commonscip-go gomod github.com/google/safetext . `github.com/google/safetext/common`/.ExecuteWithCallbackscip-go gomod github.com/google/safetext . `github.com/google/safetext/common`/ExecuteWithCallback().(t.unsafeTemplatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#unsafeTemplate., t.uuidscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#uuid., commonscip-go gomod github.com/google/safetext . `github.com/google/safetext/common`/.EchoStringscip-go gomod github.com/google/safetext . `github.com/google/safetext/common`/EchoString()., &requestedResult, data); err != nil { |
| 237 | return err |
| 238 | } |
| 239 | |
| 240 | walked, err := t.unsafeTemplatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#unsafeTemplate..Clonescip-go gomod github.com/golang/go/src go1.21 `text/template`/Template#Clone().() |
| 241 | if err != nil { |
| 242 | return err |
| 243 | } |
| 244 | walked.Treescip-go gomod github.com/golang/go/src go1.21 `text/template`/Template#Tree. = walked.Treescip-go gomod github.com/golang/go/src go1.21 `text/template`/Template#Tree..Copyscip-go gomod github.com/golang/go/src go1.21 `text/template/parse`/Tree#Copy().() |
| 245 | |
| 246 | commonscip-go gomod github.com/google/safetext . `github.com/google/safetext/common`/.WalkApplyFuncToNonDeclaractiveActionsscip-go gomod github.com/google/safetext . `github.com/google/safetext/common`/WalkApplyFuncToNonDeclaractiveActions().(walked, walked.Treescip-go gomod github.com/golang/go/src go1.21 `text/template`/Template#Tree..Rootscip-go gomod github.com/golang/go/src go1.21 `text/template/parse`/Tree#Root.) |
| 247 | |
| 248 | // Get baseline |
| 249 | var baselineResult bytesscip-go gomod github.com/golang/go/src go1.21 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.21 bytes/Buffer# |
| 250 | if err = commonscip-go gomod github.com/google/safetext . `github.com/google/safetext/common`/.ExecuteWithCallbackscip-go gomod github.com/google/safetext . `github.com/google/safetext/common`/ExecuteWithCallback().(walked, t.uuidscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#uuid., commonscip-go gomod github.com/google/safetext . `github.com/google/safetext/common`/.BaselineStringscip-go gomod github.com/google/safetext . `github.com/google/safetext/common`/BaselineString()., &baselineResult, data); err != nil { |
| 251 | return err |
| 252 | } |
| 253 | |
| 254 | parsedBaselineResult, err := unmarshalYamlscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/unmarshalYaml().(baselineResult.Bytesscip-go gomod github.com/golang/go/src go1.21 bytes/Buffer#Bytes().()) |
| 255 | if err != nil { |
| 256 | return ErrInvalidYAMLTemplatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/ErrInvalidYAMLTemplate. |
| 257 | } |
| 258 | |
| 259 | // If baseline was valid, request must also be valid YAML for no injection to have occurred |
| 260 | parsedRequestedResult, err := unmarshalYamlscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/unmarshalYaml().(requestedResult.Bytesscip-go gomod github.com/golang/go/src go1.21 bytes/Buffer#Bytes().()) |
| 261 | if err != nil { |
| 262 | return ErrYAMLInjectionscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/ErrYAMLInjection. |
| 263 | } |
| 264 | |
| 265 | // Mutate the input |
| 266 | var mutatedResult bytesscip-go gomod github.com/golang/go/src go1.21 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.21 bytes/Buffer# |
| 267 | if err = commonscip-go gomod github.com/google/safetext . `github.com/google/safetext/common`/.ExecuteWithCallbackscip-go gomod github.com/google/safetext . `github.com/google/safetext/common`/ExecuteWithCallback().(walked, t.uuidscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#uuid., mutateStringscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/mutateString()., &mutatedResult, data); err != nil { |
| 268 | return err |
| 269 | } |
| 270 | |
| 271 | parsedMutatedResult, err := unmarshalYamlscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/unmarshalYaml().(mutatedResult.Bytesscip-go gomod github.com/golang/go/src go1.21 bytes/Buffer#Bytes().()) |
| 272 | if err != nil { |
| 273 | return ErrYAMLInjectionscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/ErrYAMLInjection. |
| 274 | } |
| 275 | |
| 276 | // Compare results |
| 277 | if !allKeysMatchscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/allKeysMatch().(parsedBaselineResult, parsedRequestedResult, parsedMutatedResult) { |
| 278 | return ErrYAMLInjectionscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/ErrYAMLInjection. |
| 279 | } |
| 280 | |
| 281 | requestedResult.WriteToscip-go gomod github.com/golang/go/src go1.21 bytes/Buffer#WriteTo().(wr) |
| 282 | return nil |
| 283 | } |
| 284 | |
| 285 | // Name returns the name of the template. |
| 286 | func (t *Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#) Namescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#Name().() string { |
| 287 | return t.unsafeTemplatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#unsafeTemplate..Namescip-go gomod github.com/golang/go/src go1.21 `text/template`/Template#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 *Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#) Newscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#New().(name string) *Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template# { |
| 298 | id := uuidscip-go gomod github.com/pborman/uuid v1.2.1 `github.com/pborman/uuid`/.Newscip-go gomod github.com/pborman/uuid v1.2.1 `github.com/pborman/uuid`/New().() |
| 299 | funcMap := commonscip-go gomod github.com/google/safetext . `github.com/google/safetext/common`/.BuildTextTemplateFuncMapscip-go gomod github.com/google/safetext . `github.com/google/safetext/common`/BuildTextTemplateFuncMap().(id) |
| 300 | return &Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#{unsafeTemplatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#unsafeTemplate.: t.unsafeTemplatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#unsafeTemplate..Newscip-go gomod github.com/golang/go/src go1.21 `text/template`/Template#New().(name).Funcsscip-go gomod github.com/golang/go/src go1.21 `text/template`/Template#Funcs().(funcMap), uuidscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#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 *Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#) Clonescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#Clone().() (*Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#, error) { |
| 310 | id := uuidscip-go gomod github.com/pborman/uuid v1.2.1 `github.com/pborman/uuid`/.Newscip-go gomod github.com/pborman/uuid v1.2.1 `github.com/pborman/uuid`/New().() |
| 311 | nt, err := t.unsafeTemplatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#unsafeTemplate..Clonescip-go gomod github.com/golang/go/src go1.21 `text/template`/Template#Clone().() |
| 312 | return &Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#{unsafeTemplatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#unsafeTemplate.: nt, uuidscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#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 *Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#) AddParseTreescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#AddParseTree().(name string, tree *parsescip-go gomod github.com/golang/go/src go1.21 `text/template/parse`/.Treescip-go gomod github.com/golang/go/src go1.21 `text/template/parse`/Tree#) (*Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#, error) { |
| 320 | nt, err := t.unsafeTemplatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#unsafeTemplate..AddParseTreescip-go gomod github.com/golang/go/src go1.21 `text/template`/Template#AddParseTree().(name, tree) |
| 321 | |
| 322 | if nt != t.unsafeTemplatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#unsafeTemplate. { |
| 323 | id := uuidscip-go gomod github.com/pborman/uuid v1.2.1 `github.com/pborman/uuid`/.Newscip-go gomod github.com/pborman/uuid v1.2.1 `github.com/pborman/uuid`/New().() |
| 324 | return &Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#{unsafeTemplatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#unsafeTemplate.: nt, uuidscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#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 *Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#) Optionscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#Option().(opt ...string) *Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template# { |
| 348 | for _, s := range opt { |
| 349 | t.unsafeTemplatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#unsafeTemplate..Optionscip-go gomod github.com/golang/go/src go1.21 `text/template`/Template#Option().(s) |
| 350 | } |
| 351 | return t |
| 352 | } |
| 353 | |
| 354 | // Templates returns a slice of defined templates associated with t. |
| 355 | func (t *Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#) Templatesscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#Templates().() []*Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template# { |
| 356 | s := t.unsafeTemplatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#unsafeTemplate..Templatesscip-go gomod github.com/golang/go/src go1.21 `text/template`/Template#Templates().() |
| 357 | var id string |
| 358 | |
| 359 | var ns []*Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template# |
| 360 | for _, nt := range s { |
| 361 | id = uuidscip-go gomod github.com/pborman/uuid v1.2.1 `github.com/pborman/uuid`/.Newscip-go gomod github.com/pborman/uuid v1.2.1 `github.com/pborman/uuid`/New().() |
| 362 | ns = append(ns, &Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#{unsafeTemplatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#unsafeTemplate.: nt, uuidscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#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 *Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#) ExecuteTemplatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#ExecuteTemplate().(wr ioscip-go gomod github.com/golang/go/src go1.21 io/.Writerscip-go gomod github.com/golang/go/src go1.21 io/Writer#, name string, data any) error { |
| 376 | tmpl := t.Lookupscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#Lookup().(name) |
| 377 | if tmpl == nil { |
| 378 | return fmtscip-go gomod github.com/golang/go/src go1.21 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.21 fmt/Errorf().("template: no template %q associated with template %q", name, t.Namescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#Name().()) |
| 379 | } |
| 380 | return tmpl.Executescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#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 *Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#) Delimsscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#Delims().(left, right string) *Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template# { |
| 389 | t.unsafeTemplatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#unsafeTemplate..Delimsscip-go gomod github.com/golang/go/src go1.21 `text/template`/Template#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 *Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#) DefinedTemplatesscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#DefinedTemplates().() string { |
| 398 | return t.unsafeTemplatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#unsafeTemplate..DefinedTemplatesscip-go gomod github.com/golang/go/src go1.21 `text/template`/Template#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 *Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#) Funcsscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#Funcs().(funcMap FuncMapscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/FuncMap#) *Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template# { |
| 408 | t.unsafeTemplatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#unsafeTemplate..Funcsscip-go gomod github.com/golang/go/src go1.21 `text/template`/Template#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 *Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#) Lookupscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#Lookup().(name string) *Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template# { |
| 415 | nt := t.unsafeTemplatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#unsafeTemplate..Lookupscip-go gomod github.com/golang/go/src go1.21 `text/template`/Template#Lookup().(name) |
| 416 | |
| 417 | if nt == nil { |
| 418 | return nil |
| 419 | } |
| 420 | |
| 421 | if nt != t.unsafeTemplatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#unsafeTemplate. { |
| 422 | id := uuidscip-go gomod github.com/pborman/uuid v1.2.1 `github.com/pborman/uuid`/.Newscip-go gomod github.com/pborman/uuid v1.2.1 `github.com/pborman/uuid`/New().() |
| 423 | return &Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#{unsafeTemplatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#unsafeTemplate.: nt, uuidscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#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 *Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#) Parsescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#Parse().(text string) (*Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#, error) { |
| 440 | nt, err := t.unsafeTemplatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#unsafeTemplate..Parsescip-go gomod github.com/golang/go/src go1.21 `text/template`/Template#Parse().(text) |
| 441 | |
| 442 | if nt != t.unsafeTemplatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#unsafeTemplate. { |
| 443 | id := uuidscip-go gomod github.com/pborman/uuid v1.2.1 `github.com/pborman/uuid`/.Newscip-go gomod github.com/pborman/uuid v1.2.1 `github.com/pborman/uuid`/New().() |
| 444 | return &Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#{unsafeTemplatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#unsafeTemplate.: nt, uuidscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#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 Mustscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Must().(t *Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#, err error) *Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template# { |
| 456 | if err != nil { |
| 457 | panic(err) |
| 458 | } |
| 459 | return t |
| 460 | } |
| 461 | |
| 462 | func readFileOSscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/readFileOS().(file string) (name string, b []byte, err error) { |
| 463 | name = filepathscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/.Basescip-go gomod github.com/golang/go/src go1.21 `path/filepath`/Base().(file) |
| 464 | b, err = osscip-go gomod github.com/golang/go/src go1.21 os/.ReadFilescip-go gomod github.com/golang/go/src go1.21 os/ReadFile().(file) |
| 465 | return |
| 466 | } |
| 467 | |
| 468 | func readFileFSscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/readFileFS().(fsys fsscip-go gomod github.com/golang/go/src go1.21 `io/fs`/.FSscip-go gomod github.com/golang/go/src go1.21 `io/fs`/FS#) func(string) (string, []byte, error) { |
| 469 | return func(file string) (name string, b []byte, err error) { |
| 470 | name = pathscip-go gomod github.com/golang/go/src go1.21 path/.Basescip-go gomod github.com/golang/go/src go1.21 path/Base().(file) |
| 471 | b, err = fsscip-go gomod github.com/golang/go/src go1.21 `io/fs`/.ReadFilescip-go gomod github.com/golang/go/src go1.21 `io/fs`/ReadFile().(fsys, file) |
| 472 | return |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | func parseFilesscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/parseFiles().(t *Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#, readFile func(string) (string, []byte, error), filenames ...string) (*Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#, error) { |
| 477 | if len(filenames) == 0 { |
| 478 | // Not really a problem, but be consistent. |
| 479 | return nil, fmtscip-go gomod github.com/golang/go/src go1.21 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.21 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 *Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template# |
| 494 | if t == nil { |
| 495 | t = Newscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/New().(name) |
| 496 | } |
| 497 | if name == t.Namescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#Name().() { |
| 498 | tmpl = t |
| 499 | } else { |
| 500 | tmpl = t.Newscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#New().(name) |
| 501 | } |
| 502 | _, err = tmpl.Parsescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#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 parseGlobscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/parseGlob().(t *Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#, pattern string) (*Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#, error) { |
| 512 | filenames, err := filepathscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/.Globscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/Glob().(pattern) |
| 513 | if err != nil { |
| 514 | return nil, err |
| 515 | } |
| 516 | if len(filenames) == 0 { |
| 517 | return nil, fmtscip-go gomod github.com/golang/go/src go1.21 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.21 fmt/Errorf().("template: pattern matches no files: %#q", pattern) |
| 518 | } |
| 519 | return parseFilesscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/parseFiles().(t, readFileOSscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/readFileOS()., filenames...) |
| 520 | } |
| 521 | |
| 522 | func parseFSscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/parseFS().(t *Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#, fsys fsscip-go gomod github.com/golang/go/src go1.21 `io/fs`/.FSscip-go gomod github.com/golang/go/src go1.21 `io/fs`/FS#, patterns []string) (*Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#, error) { |
| 523 | var filenames []string |
| 524 | for _, pattern := range patterns { |
| 525 | list, err := fsscip-go gomod github.com/golang/go/src go1.21 `io/fs`/.Globscip-go gomod github.com/golang/go/src go1.21 `io/fs`/Glob().(fsys, pattern) |
| 526 | if err != nil { |
| 527 | return nil, err |
| 528 | } |
| 529 | if len(list) == 0 { |
| 530 | return nil, fmtscip-go gomod github.com/golang/go/src go1.21 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.21 fmt/Errorf().("template: pattern matches no files: %#q", pattern) |
| 531 | } |
| 532 | filenames = append(filenames, list...) |
| 533 | } |
| 534 | return parseFilesscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/parseFiles().(t, readFileFSscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/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 ParseFilesscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/ParseFiles().(filenames ...string) (*Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#, error) { |
| 547 | return parseFilesscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/parseFiles().(nil, readFileOSscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/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 *Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#) ParseFilesscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#ParseFiles().(filenames ...string) (*Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#, error) { |
| 562 | // Ensure template is inited |
| 563 | t.Optionscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#Option().() |
| 564 | |
| 565 | return parseFilesscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/parseFiles().(t, readFileOSscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/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 ParseGlobscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/ParseGlob().(pattern string) (*Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#, error) { |
| 578 | return parseGlobscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/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 *Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#) ParseGlobscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#ParseGlob().(pattern string) (*Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#, error) { |
| 590 | // Ensure template is inited |
| 591 | t.Optionscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#Option().() |
| 592 | |
| 593 | return parseGlobscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/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 ParseFSscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/ParseFS().(fsys fsscip-go gomod github.com/golang/go/src go1.21 `io/fs`/.FSscip-go gomod github.com/golang/go/src go1.21 `io/fs`/FS#, patterns ...string) (*Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#, error) { |
| 601 | return parseFSscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/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 *Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#) ParseFSscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#ParseFS().(fsys fsscip-go gomod github.com/golang/go/src go1.21 `io/fs`/.FSscip-go gomod github.com/golang/go/src go1.21 `io/fs`/FS#, patterns ...string) (*Templatescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#, error) { |
| 609 | // Ensure template is inited |
| 610 | t.Optionscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/Template#Option().() |
| 611 | |
| 612 | return parseFSscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/parseFS().(t, fsys, patterns) |
| 613 | } |
| 614 | |
| 615 | // HTMLEscape writes to w the escaped HTML equivalent of the plain text data b. |
| 616 | func HTMLEscapescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/HTMLEscape().(w ioscip-go gomod github.com/golang/go/src go1.21 io/.Writerscip-go gomod github.com/golang/go/src go1.21 io/Writer#, b []byte) { |
| 617 | templatescip-go gomod github.com/golang/go/src go1.21 `text/template`/.HTMLEscapescip-go gomod github.com/golang/go/src go1.21 `text/template`/HTMLEscape().(w, b) |
| 618 | } |
| 619 | |
| 620 | // HTMLEscapeString returns the escaped HTML equivalent of the plain text data s. |
| 621 | func HTMLEscapeStringscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/HTMLEscapeString().(s string) string { |
| 622 | return templatescip-go gomod github.com/golang/go/src go1.21 `text/template`/.HTMLEscapeStringscip-go gomod github.com/golang/go/src go1.21 `text/template`/HTMLEscapeString().(s) |
| 623 | } |
| 624 | |
| 625 | // HTMLEscaper returns the escaped HTML equivalent of the textual |
| 626 | // representation of its arguments. |
| 627 | func HTMLEscaperscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/HTMLEscaper().(args ...any) string { |
| 628 | return templatescip-go gomod github.com/golang/go/src go1.21 `text/template`/.HTMLEscaperscip-go gomod github.com/golang/go/src go1.21 `text/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 IsTruescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/IsTrue().(val any) (truth, ok bool) { |
| 635 | return templatescip-go gomod github.com/golang/go/src go1.21 `text/template`/.IsTruescip-go gomod github.com/golang/go/src go1.21 `text/template`/IsTrue().(val) |
| 636 | } |
| 637 | |
| 638 | // JSEscape writes to w the escaped JavaScript equivalent of the plain text data b. |
| 639 | func JSEscapescip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/JSEscape().(w ioscip-go gomod github.com/golang/go/src go1.21 io/.Writerscip-go gomod github.com/golang/go/src go1.21 io/Writer#, b []byte) { |
| 640 | templatescip-go gomod github.com/golang/go/src go1.21 `text/template`/.JSEscapescip-go gomod github.com/golang/go/src go1.21 `text/template`/JSEscape().(w, b) |
| 641 | } |
| 642 | |
| 643 | // JSEscapeString returns the escaped JavaScript equivalent of the plain text data s. |
| 644 | func JSEscapeStringscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/JSEscapeString().(s string) string { |
| 645 | return templatescip-go gomod github.com/golang/go/src go1.21 `text/template`/.JSEscapeStringscip-go gomod github.com/golang/go/src go1.21 `text/template`/JSEscapeString().(s) |
| 646 | } |
| 647 | |
| 648 | // JSEscaper returns the escaped JavaScript equivalent of the textual |
| 649 | // representation of its arguments. |
| 650 | func JSEscaperscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/JSEscaper().(args ...any) string { |
| 651 | return templatescip-go gomod github.com/golang/go/src go1.21 `text/template`/.JSEscaperscip-go gomod github.com/golang/go/src go1.21 `text/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 URLQueryEscaperscip-go gomod github.com/google/safetext . `github.com/google/safetext/yamltemplate`/URLQueryEscaper().(args ...any) string { |
| 657 | return templatescip-go gomod github.com/golang/go/src go1.21 `text/template`/.URLQueryEscaperscip-go gomod github.com/golang/go/src go1.21 `text/template`/URLQueryEscaper().(args) |
| 658 | } |