| 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_test |
| 16 | |
| 17 | import ( |
| 18 | "bytes" |
| 19 | "embed" |
| 20 | "fmt" |
| 21 | "strconv" |
| 22 | "strings" |
| 23 | "testing" |
| 24 | |
| 25 | template "github.com/google/safetext/yamltemplate" |
| 26 | ) |
| 27 | |
| 28 | //go:embed list.yaml.tmpl |
| 29 | var eListYamlTmpl embed.FS |
| 30 | |
| 31 | //go:embed nested-template.yaml.tmpl |
| 32 | var nestedTemplateYamlTmpl embed.FS |
| 33 | |
| 34 | //go:embed nested-template-inner.yaml.tmpl |
| 35 | var nestedTemplateInnerYamlTmpl embed.FS |
| 36 | |
| 37 | func TestSafetextYamltemplate(t *testing.T) { |
| 38 | type testCase struct { |
| 39 | tmplText string |
| 40 | replacements map[any]any |
| 41 | err bool |
| 42 | } |
| 43 | |
| 44 | testCases := []testCase{ |
| 45 | // Negative cases |
| 46 | { |
| 47 | tmplText: "{ hello: \"{{ .addressee | js }}\" }", |
| 48 | replacements: map[any]any{ |
| 49 | "addressee": "world\", inject: \"oops", |
| 50 | }, |
| 51 | err: false, |
| 52 | }, |
| 53 | |
| 54 | { |
| 55 | tmplText: ` |
| 56 | --- |
| 57 | - stream: one, |
| 58 | - hello: {{ .addressee }}, |
| 59 | --- |
| 60 | - stream: two, |
| 61 | - hello: {{ .addressee }}, |
| 62 | `, |
| 63 | replacements: nil, |
| 64 | err: false, |
| 65 | }, |
| 66 | |
| 67 | { |
| 68 | tmplText: ` |
| 69 | data: |
| 70 | HTTPS_PROXY: {{.p1}} |
| 71 | NO_PROXY: {{.p2}} |
| 72 | `, |
| 73 | replacements: map[any]any{ |
| 74 | "p1": "1", |
| 75 | "p2": "localhost, 127.0.0.1", |
| 76 | }, |
| 77 | err: false, |
| 78 | }, |
| 79 | |
| 80 | { |
| 81 | tmplText: ` |
| 82 | data: |
| 83 | HTTPS_PROXY: {{.p1}} |
| 84 | NO_PROXY: {{.p2}} |
| 85 | `, |
| 86 | replacements: map[any]any{ |
| 87 | "p1": "", |
| 88 | "p2": "localhost, 127.0.0.1", |
| 89 | }, |
| 90 | err: false, |
| 91 | }, |
| 92 | |
| 93 | { |
| 94 | tmplText: "{ {{ if not .hide }}hello: {{ .addressee }}{{end}} }", |
| 95 | replacements: map[any]any{ |
| 96 | "addressee": "world", |
| 97 | "hide": false, |
| 98 | }, |
| 99 | err: false, |
| 100 | }, |
| 101 | |
| 102 | { |
| 103 | tmplText: "{ {{ if eq .addressee \"world\" }}hello: {{ .addressee }}{{end}} }", |
| 104 | replacements: map[any]any{ |
| 105 | "addressee": "world", |
| 106 | }, |
| 107 | err: false, |
| 108 | }, |
| 109 | |
| 110 | { |
| 111 | tmplText: `{ list: "{{ range .entries }}{{.}}{{ end }}" }`, |
| 112 | replacements: map[any]any{ |
| 113 | "entries": []string{"(special characters to not trigger fast path {})", "two", "three"}, |
| 114 | }, |
| 115 | err: false, |
| 116 | }, |
| 117 | |
| 118 | { |
| 119 | tmplText: ` |
| 120 | list: |
| 121 | {{with .some_field}} |
| 122 | {{if eq . "x"}} |
| 123 | - {{.}} |
| 124 | {{end}} |
| 125 | {{end}} |
| 126 | `, |
| 127 | replacements: map[any]any{ |
| 128 | "some_field": "x", |
| 129 | "slow": "{}", |
| 130 | }, |
| 131 | err: false, |
| 132 | }, |
| 133 | |
| 134 | { |
| 135 | tmplText: "{ test: bla }", |
| 136 | replacements: map[any]any{ |
| 137 | 0: "(special characters to not trigger fast path {})", |
| 138 | }, |
| 139 | err: false, |
| 140 | }, |
| 141 | |
| 142 | // Verify that unused replacements in nested yaml don't cause templates to fail |
| 143 | { |
| 144 | tmplText: `hello: |
| 145 | - to: {{ .addressee }} |
| 146 | next: |
| 147 | - first: test |
| 148 | `, |
| 149 | replacements: map[any]any{ |
| 150 | "addressee": "world", |
| 151 | "unused": "some-thing", |
| 152 | }, |
| 153 | err: false, |
| 154 | }, |
| 155 | |
| 156 | // Verify that valid strings with non-standard characters work in nested yaml |
| 157 | { |
| 158 | tmplText: `hello: |
| 159 | - to: {{ .addressee }} |
| 160 | next: |
| 161 | - first: test |
| 162 | `, |
| 163 | replacements: map[any]any{ |
| 164 | "addressee": "whole-world", |
| 165 | }, |
| 166 | err: false, |
| 167 | }, |
| 168 | |
| 169 | // Verify that internal YAML parser rejects duplicate keys |
| 170 | { |
| 171 | tmplText: "{ hello: {{ .addressee }}, hello: multiple }", |
| 172 | replacements: map[any]any{ |
| 173 | "addressee": "world", |
| 174 | }, |
| 175 | err: true, |
| 176 | }, |
| 177 | |
| 178 | // Verify that internal YAML parsers rejects map keys |
| 179 | { |
| 180 | tmplText: "{ {}: {{ .addressee }} }", |
| 181 | replacements: map[any]any{ |
| 182 | "addressee": "world", |
| 183 | }, |
| 184 | err: true, |
| 185 | }, |
| 186 | |
| 187 | // Verify that internal YAML parsers rejects slice keys |
| 188 | { |
| 189 | tmplText: "{ [1, 2, 3]: {{ .addressee }} }", |
| 190 | replacements: map[any]any{ |
| 191 | "addressee": "world", |
| 192 | }, |
| 193 | err: true, |
| 194 | }, |
| 195 | |
| 196 | // Verify that YAML parses still accepts "non-strict" YAML (whilst rejecting duplicate keys) |
| 197 | { |
| 198 | tmplText: "a: {{ .addressee }}", |
| 199 | replacements: map[any]any{ |
| 200 | "addressee": "world", |
| 201 | }, |
| 202 | err: false, |
| 203 | }, |
| 204 | |
| 205 | // nil type |
| 206 | { |
| 207 | tmplText: `{ a: {{.a}}, b: '{{.b}}' }`, |
| 208 | replacements: map[any]any{ |
| 209 | "a": nil, |
| 210 | "b": "{}", |
| 211 | }, |
| 212 | err: false, |
| 213 | }, |
| 214 | |
| 215 | // Positive cases |
| 216 | { |
| 217 | tmplText: "{ hello: \"{{ .addressee }}\" }", |
| 218 | replacements: map[any]any{ |
| 219 | "addressee": "world\", hello: \"oops_p", |
| 220 | }, |
| 221 | err: true, |
| 222 | }, |
| 223 | |
| 224 | { |
| 225 | tmplText: "{ hello: \"{{ .addressee }}\", parent: [ 1, {{ .s }}, 3 ] }", |
| 226 | replacements: map[any]any{ |
| 227 | "addressee": "world", |
| 228 | "s": "2, 4", |
| 229 | }, |
| 230 | err: true, |
| 231 | }, |
| 232 | |
| 233 | { |
| 234 | tmplText: "{ hello: \"{{ .addressee }}\", parent: [ 1, { a: {{ .s }} }, 3 ] }", |
| 235 | replacements: map[any]any{ |
| 236 | "addressee": "world", |
| 237 | "s": "2 , b : b", |
| 238 | }, |
| 239 | err: true, |
| 240 | }, |
| 241 | |
| 242 | { |
| 243 | tmplText: "{ hello: {{ .addressee }} }", |
| 244 | replacements: map[any]any{ |
| 245 | "addressee": "{}", |
| 246 | }, |
| 247 | err: true, |
| 248 | }, |
| 249 | |
| 250 | { |
| 251 | tmplText: "{ {{ if eq .caddressee \"world\" }}hello: {{ .addressee }}{{end}} }", |
| 252 | replacements: map[any]any{ |
| 253 | "caddressee": "world", |
| 254 | "addressee": "world, inject: true", |
| 255 | }, |
| 256 | err: true, |
| 257 | }, |
| 258 | |
| 259 | { |
| 260 | tmplText: ` |
| 261 | --- |
| 262 | - stream: one |
| 263 | - hello: a |
| 264 | --- |
| 265 | - stream: two |
| 266 | - hello: {{ .addressee }} |
| 267 | `, |
| 268 | replacements: map[any]any{"addressee": "world\n- inject"}, |
| 269 | err: true, |
| 270 | }, |
| 271 | |
| 272 | // Accessing anchors should count as injected YAML syntax |
| 273 | { |
| 274 | tmplText: `{ secret: &secret_label 'test', disclosed: {{ .controlled }} }`, |
| 275 | replacements: map[any]any{ |
| 276 | "controlled": "*secret_label", |
| 277 | }, |
| 278 | err: true, |
| 279 | }, |
| 280 | } |
| 281 | |
| 282 | for _, tc := range testCases { |
| 283 | t.Run(tc.tmplText, func(t *testing.T) { |
| 284 | tmpl := template.Must(template.New("test").Parse(tc.tmplText)) |
| 285 | var buf bytes.Buffer |
| 286 | err := tmpl.Execute(&buf, tc.replacements) |
| 287 | |
| 288 | if (err != nil) != tc.err { |
| 289 | t.Errorf("tmpl.Execute: Expected %v, got %v\n", tc.err, err) |
| 290 | } |
| 291 | }) |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | // Check func maps still work |
| 296 | func sanitize(input any) string { |
| 297 | return fmt.Sprintf("%q", input) |
| 298 | } |
| 299 | |
| 300 | func TestSafetextYamltemplateNegativeFuncMap(t *testing.T) { |
| 301 | var funcMap = map[string]any{ |
| 302 | "sanitize": sanitize, |
| 303 | } |
| 304 | |
| 305 | tmpl := template.Must(template.New("test").Funcs(template.FuncMap(funcMap)).Parse( |
| 306 | "{ a: {{ .a | sanitize }}, b: {{ .b | sanitize }} }", |
| 307 | )) |
| 308 | |
| 309 | replacements := map[string]any{ |
| 310 | "a": "world\", inject: \"oops", |
| 311 | "b": "world, inject: oops", |
| 312 | } |
| 313 | |
| 314 | var buf bytes.Buffer |
| 315 | err := tmpl.Execute(&buf, replacements) |
| 316 | if err != nil { |
| 317 | t.Errorf("tmpl.Execute() error = %v", err) |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | // Check structs, instead of maps |
| 322 | func TestSafetextYamltemplateNegativeStruct(t *testing.T) { |
| 323 | tmpl := template.Must(template.New("test").Parse( |
| 324 | "{ name: {{ .Name }}, age: {{ .Age }} }", |
| 325 | )) |
| 326 | |
| 327 | type person struct { |
| 328 | Name string |
| 329 | Age int |
| 330 | } |
| 331 | |
| 332 | var buf bytes.Buffer |
| 333 | err := tmpl.Execute(&buf, person{Name: "bla", Age: 42}) |
| 334 | if err != nil { |
| 335 | t.Errorf("tmpl.Execute() error = %v", err) |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | func TestSafetextYamltemplatePositiveStruct(t *testing.T) { |
| 340 | tmpl := template.Must(template.New("test").Parse( |
| 341 | "{ name: {{ .Name }}, age: {{ .Age }} }", |
| 342 | )) |
| 343 | |
| 344 | type person struct { |
| 345 | Name string |
| 346 | Age int |
| 347 | } |
| 348 | |
| 349 | var buf bytes.Buffer |
| 350 | err := tmpl.Execute(&buf, person{Name: "bla, age: 31", Age: 42}) |
| 351 | if err != template.ErrYAMLInjection { |
| 352 | t.Errorf("Failed to detect YAML injection (%v)!", err) |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | // Root node being a list instead of a map |
| 357 | func TestSafetextYamltemplateNegativeRootList(t *testing.T) { |
| 358 | tmpl := template.Must(template.New("test").Parse(` |
| 359 | - one: a |
| 360 | - one: b |
| 361 | `)) |
| 362 | |
| 363 | replacements := map[string]any{ |
| 364 | "some_field": "x", |
| 365 | "use_slow_path": "{}", |
| 366 | } |
| 367 | |
| 368 | var buf bytes.Buffer |
| 369 | err := tmpl.Execute(&buf, replacements) |
| 370 | if err != nil { |
| 371 | t.Errorf("tmpl.Execute() error = %v", err) |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | // Check indirect types are followed |
| 376 | func TestSafetextYamltemplatePositiveIndirection(t *testing.T) { |
| 377 | tmpl := template.Must(template.New("test").Parse( |
| 378 | "{ name: {{ .Name }}, age: {{ .Age }} }", |
| 379 | )) |
| 380 | |
| 381 | type person struct { |
| 382 | Name **string |
| 383 | Age int |
| 384 | } |
| 385 | |
| 386 | n := "bla, age 31" |
| 387 | nAddr := &n |
| 388 | |
| 389 | var buf bytes.Buffer |
| 390 | err := tmpl.Execute(&buf, person{Name: &nAddr, Age: 42}) |
| 391 | if err != template.ErrYAMLInjection { |
| 392 | t.Errorf("Failed to detect YAML injection (%v)!", err) |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | // Check parsing files works |
| 397 | func TestSafetextYamltemplateFiles(t *testing.T) { |
| 398 | tmpl, err := template.ParseFS(eListYamlTmpl, "list.yaml.tmpl") |
| 399 | if err != nil { |
| 400 | t.Fatalf("template.ParseFS() failed: %v", err) |
| 401 | } |
| 402 | |
| 403 | replacements := map[string]any{ |
| 404 | "some_field": "x", |
| 405 | "use_slow_path": "{}", |
| 406 | } |
| 407 | |
| 408 | var buf bytes.Buffer |
| 409 | if err := tmpl.Execute(&buf, replacements); err != nil { |
| 410 | t.Errorf("tmpl.Execute() error = %v", err) |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | // Check methods work |
| 415 | type A struct { |
| 416 | } |
| 417 | |
| 418 | func (A) GetName(n int) string { return "n is " + strconv.Itoa(n) } |
| 419 | |
| 420 | func TestSafetextYamltemplateMethod(t *testing.T) { |
| 421 | tmpl := template.Must(template.New("test").Parse(`- {{ (.a.GetName 0x41) | js }}`)) |
| 422 | |
| 423 | replacements := map[string]any{ |
| 424 | "a": A{}, |
| 425 | "use_slow_path": "{}", |
| 426 | } |
| 427 | |
| 428 | var buf bytes.Buffer |
| 429 | err := tmpl.Execute(&buf, replacements) |
| 430 | if err != nil { |
| 431 | t.Errorf("tmpl.Execute() error = %v", err) |
| 432 | } |
| 433 | |
| 434 | if buf.String() != "- n is 65" { |
| 435 | t.Errorf("Got %v, want %v\n", buf.String(), "- n is 65") |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | func TestSafetextYamltemplateOptOut(t *testing.T) { |
| 440 | tmpl := template.Must(template.New("test").Parse("{ Person-{{ (StructuralData .Name) }}: {{ .Age }} }")) |
| 441 | |
| 442 | type person struct { |
| 443 | Name string |
| 444 | Age int |
| 445 | Slow string |
| 446 | } |
| 447 | |
| 448 | var buf bytes.Buffer |
| 449 | err := tmpl.Execute(&buf, person{Name: "bla", Age: 42, Slow: "{}"}) |
| 450 | if err != nil { |
| 451 | t.Errorf("tmpl.Execute() error = %v", err) |
| 452 | } |
| 453 | |
| 454 | if buf.String() != "{ Person-bla: 42 }" { |
| 455 | t.Errorf("Got %v, want { Person-bla: 42 }", buf.String()) |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | func TestCustomTypeWithStringBaseYamltemplatePositiveStruct(t *testing.T) { |
| 460 | yamlTemplate := ` |
| 461 | name: {{ .Name }} |
| 462 | type: {{ .PDType }} |
| 463 | ` |
| 464 | tmpl := template.Must(template.New("test").Parse(yamlTemplate)) |
| 465 | |
| 466 | type PersistentDiskType string |
| 467 | |
| 468 | type StorageClassSpec struct { |
| 469 | Name string |
| 470 | PDType PersistentDiskType |
| 471 | } |
| 472 | |
| 473 | var buf bytes.Buffer |
| 474 | err := tmpl.Execute(&buf, StorageClassSpec{Name: "ssd", PDType: "pd-ssd"}) |
| 475 | if err != nil { |
| 476 | t.Errorf("tmpl.Execute() error = %v", err) |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | // Demonstration of manually applying injection detection to the result of a function call |
| 481 | func readFile(path string) string { |
| 482 | // (Read potentially untrusted file) |
| 483 | |
| 484 | switch path { |
| 485 | case "untrusted.txt": |
| 486 | return ", injection: true" |
| 487 | case "safe.txt": |
| 488 | return "safe" |
| 489 | default: |
| 490 | panic("Unknown file path!") |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | func TestSafetextYamltemplateManualAnnotation(t *testing.T) { |
| 495 | var funcMap = map[string]any{ |
| 496 | "readFile": readFile, |
| 497 | } |
| 498 | |
| 499 | tmpl := template.Must(template.New("test").Funcs(template.FuncMap(funcMap)).Parse( |
| 500 | "{ name: {{ readFile (StructuralData .path) | ApplyInjectionDetection }} }", |
| 501 | )) |
| 502 | |
| 503 | type testCase struct { |
| 504 | name string |
| 505 | replacements map[string]string |
| 506 | err bool |
| 507 | } |
| 508 | |
| 509 | testCases := []testCase{ |
| 510 | { |
| 511 | name: "negative", |
| 512 | replacements: map[string]string{"path": "safe.txt"}, |
| 513 | err: false, |
| 514 | }, |
| 515 | { |
| 516 | name: "positive", |
| 517 | replacements: map[string]string{"path": "untrusted.txt"}, |
| 518 | err: true, |
| 519 | }, |
| 520 | } |
| 521 | |
| 522 | for _, tc := range testCases { |
| 523 | t.Run(tc.name, func(t *testing.T) { |
| 524 | var buf bytes.Buffer |
| 525 | err := tmpl.Execute(&buf, tc.replacements) |
| 526 | |
| 527 | if (err != nil) != tc.err { |
| 528 | t.Errorf("tmpl.Execute: Expected %v, got %v\n", tc.err, err) |
| 529 | } |
| 530 | }) |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | func indent(str string, level int) string { |
| 535 | pad := "\n" + strings.Repeat(" ", level) |
| 536 | return strings.Replace(str, "\n", pad, -1) |
| 537 | } |
| 538 | |
| 539 | var innerData string |
| 540 | |
| 541 | func executeTemplate(tmpl *template.Template, tmplData any, indentLevel int) (string, error) { |
| 542 | sb := new(strings.Builder) |
| 543 | if err := tmpl.Execute(sb, map[string]any{ |
| 544 | "Data": tmplData, |
| 545 | "Inner": innerData, |
| 546 | }); err != nil { |
| 547 | return "", fmt.Errorf("failed to execute %s template: %v", tmpl.Name(), err) |
| 548 | } |
| 549 | return indent(sb.String(), indentLevel), nil |
| 550 | } |
| 551 | |
| 552 | type DaemonSetConfiguration struct { |
| 553 | // Name is the name of the DaemonSet |
| 554 | Name string |
| 555 | } |
| 556 | |
| 557 | func (d *DaemonSetConfiguration) GenerateLabels(indent int) (string, error) { |
| 558 | labelsTmpl, err := template.ParseFS(nestedTemplateInnerYamlTmpl, "nested-template-inner.yaml.tmpl") |
| 559 | if err != nil { |
| 560 | return "", fmt.Errorf("template.ParseFS() failed: %v", err) |
| 561 | } |
| 562 | labelsMap := map[string]string{ |
| 563 | "one": "foo", |
| 564 | "two": "bar", |
| 565 | "three": "baz", |
| 566 | "four": "qux", |
| 567 | } |
| 568 | |
| 569 | labels, err := executeTemplate(labelsTmpl, labelsMap, indent) |
| 570 | if err != nil { |
| 571 | return "", fmt.Errorf("failed to generate Labels clause: %v", err) |
| 572 | } |
| 573 | return labels, nil |
| 574 | } |
| 575 | |
| 576 | func TestSafetextYamltemplateNegativeNestedTemplate(t *testing.T) { |
| 577 | innerData = "Negative" |
| 578 | d := DaemonSetConfiguration{Name: "NestedTest"} |
| 579 | nestedTmpl, err := template.ParseFS(nestedTemplateYamlTmpl, "nested-template.yaml.tmpl") |
| 580 | if err != nil { |
| 581 | t.Fatalf("template.ParseFS() failed: %v", err) |
| 582 | } |
| 583 | var buf bytes.Buffer |
| 584 | err = nestedTmpl.Execute(&buf, &d) |
| 585 | if err != nil { |
| 586 | t.Errorf("tmpl.Execute() error = %v", err) |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | func TestSafetextYamltemplatePositiveNestedTemplate(t *testing.T) { |
| 591 | innerData = "Injection, value: 42" |
| 592 | d := DaemonSetConfiguration{Name: "NestedTest"} |
| 593 | nestedTmpl, err := template.ParseFS(nestedTemplateYamlTmpl, "nested-template.yaml.tmpl") |
| 594 | if err != nil { |
| 595 | t.Fatalf("template.ParseFS() failed: %v", err) |
| 596 | } |
| 597 | var buf bytes.Buffer |
| 598 | err = nestedTmpl.Execute(&buf, &d) |
| 599 | if err == nil { |
| 600 | t.Errorf("tmpl.Execute() error = %v", err) |
| 601 | } |
| 602 | } |