| 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 shsprintf is a drop-in-replacement for using sprintf |
| 16 | // to produce shell scripts, that adds automatic detection for command / argument injection. |
| 17 | package shsprintf |
| 18 | |
| 19 | import ( |
| 20 | "bytes" |
| 21 | "errors" |
| 22 | "fmt" |
| 23 | "reflect" |
| 24 | "regexp" |
| 25 | "strings" |
| 26 | "unicode/utf8" |
| 27 | |
| 28 | "github.com/google/safetext/common" |
| 29 | "mvdan.cc/sh/v3/syntax" |
| 30 | ) |
| 31 | |
| 32 | // ErrInvalidShTemplate indicates the requested template is not a valid script |
| 33 | var ErrInvalidShTemplate error = errors.New("Invalid Shell Template") |
| 34 | |
| 35 | // ErrShInjection indicates the inputs resulted in shell injection. |
| 36 | var ErrShInjection error = errors.New("Shell Injection Detected") |
| 37 | |
| 38 | const replaceableIndicator string = "REPLACEABLE" |
| 39 | |
| 40 | const ( |
| 41 | specialChars = "\\'\"`${[|&;<>()*?!+@" + |
| 42 | // extraSpecialChars |
| 43 | " \t\r\n" + |
| 44 | // prefixChars |
| 45 | "~" |
| 46 | ) |
| 47 | |
| 48 | // EscapeDefaultContext escapes special characters in a context where there is no existing quoting, such as --arg=%s |
| 49 | func EscapeDefaultContext(in string) string { |
| 50 | var buf bytes.Buffer |
| 51 | |
| 52 | cur := in |
| 53 | for len(cur) > 0 { |
| 54 | c, l := utf8.DecodeRuneInString(cur) |
| 55 | cur = cur[l:] |
| 56 | |
| 57 | if strings.ContainsRune(specialChars, c) { |
| 58 | buf.WriteByte('\\') |
| 59 | } |
| 60 | |
| 61 | buf.WriteRune(c) |
| 62 | } |
| 63 | |
| 64 | return buf.String() |
| 65 | } |
| 66 | |
| 67 | func commentsArrayMatch(a, b []syntax.Comment) bool { |
| 68 | if len(b) != len(a) { |
| 69 | return false |
| 70 | } |
| 71 | |
| 72 | for i := range a { |
| 73 | if !verifyStrings(a[i].Text, b[i].Text, matchStructure) { |
| 74 | return false |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | return true |
| 79 | } |
| 80 | |
| 81 | func paramExprMatch(a, b *syntax.ParamExp) bool { |
| 82 | if b.Short != a.Short { |
| 83 | return false |
| 84 | } |
| 85 | |
| 86 | if b.Excl != a.Excl { |
| 87 | return false |
| 88 | } |
| 89 | |
| 90 | if b.Length != a.Length { |
| 91 | return false |
| 92 | } |
| 93 | |
| 94 | if b.Width != a.Width { |
| 95 | return false |
| 96 | } |
| 97 | |
| 98 | if (b.Param == nil) != (a.Param == nil) { |
| 99 | return false |
| 100 | } |
| 101 | |
| 102 | if a.Param != nil && |
| 103 | (b.Param.Value != a.Param.Value) { |
| 104 | return false |
| 105 | } |
| 106 | |
| 107 | if (b.Index == nil) != (a.Index == nil) { |
| 108 | return false |
| 109 | } |
| 110 | |
| 111 | if a.Index != nil && !arithmExprMatch(a.Index, b.Index) { |
| 112 | return false |
| 113 | } |
| 114 | |
| 115 | if (b.Slice == nil) != (a.Slice == nil) { |
| 116 | return false |
| 117 | } |
| 118 | |
| 119 | if a.Slice != nil && !slicesMatch(a.Slice, b.Slice) { |
| 120 | return false |
| 121 | } |
| 122 | |
| 123 | if (b.Repl == nil) != (a.Repl == nil) { |
| 124 | return false |
| 125 | } |
| 126 | |
| 127 | if a.Repl != nil && !replaceMatch(a.Repl, b.Repl) { |
| 128 | return false |
| 129 | } |
| 130 | |
| 131 | if b.Names != a.Names { |
| 132 | return false |
| 133 | } |
| 134 | |
| 135 | if (b.Exp == nil) != (a.Exp == nil) { |
| 136 | return false |
| 137 | } |
| 138 | |
| 139 | if a.Exp != nil && !wordsMatch(a.Exp.Word.Parts, b.Exp.Word.Parts, matchStructure) { |
| 140 | return false |
| 141 | } |
| 142 | |
| 143 | return true |
| 144 | } |
| 145 | |
| 146 | func cmdSubstMatch(a, b *syntax.CmdSubst) bool { |
| 147 | if b.Backquotes != a.Backquotes { |
| 148 | return false |
| 149 | } |
| 150 | |
| 151 | if b.TempFile != a.TempFile { |
| 152 | return false |
| 153 | } |
| 154 | |
| 155 | if b.ReplyVar != a.ReplyVar { |
| 156 | return false |
| 157 | } |
| 158 | |
| 159 | if !statementsArrayMatch(a.Stmts, b.Stmts) { |
| 160 | return false |
| 161 | } |
| 162 | |
| 163 | if !commentsArrayMatch(a.Last, b.Last) { |
| 164 | return false |
| 165 | } |
| 166 | |
| 167 | return true |
| 168 | } |
| 169 | |
| 170 | func slicesMatch(a, b *syntax.Slice) bool { |
| 171 | return arithmExprMatch(a.Offset, b.Offset) && arithmExprMatch(a.Length, b.Length) |
| 172 | } |
| 173 | |
| 174 | func replaceMatch(a, b *syntax.Replace) bool { |
| 175 | if b.All != a.All { |
| 176 | return false |
| 177 | } |
| 178 | |
| 179 | if !wordsMatch(a.Orig.Parts, b.Orig.Parts, matchStructure) { |
| 180 | return false |
| 181 | } |
| 182 | |
| 183 | if !wordsMatch(a.With.Parts, b.With.Parts, matchStructure) { |
| 184 | return false |
| 185 | } |
| 186 | |
| 187 | return true |
| 188 | } |
| 189 | |
| 190 | func arithmExpMatch(a, b syntax.ArithmExp) bool { |
| 191 | if b.Bracket != a.Bracket { |
| 192 | return false |
| 193 | } |
| 194 | |
| 195 | if b.Unsigned != a.Unsigned { |
| 196 | return false |
| 197 | } |
| 198 | |
| 199 | return arithmExprMatch(a.X, b.X) |
| 200 | } |
| 201 | |
| 202 | func arithmExprArrayMatch(a, b []syntax.ArithmExpr) bool { |
| 203 | if len(b) != len(a) { |
| 204 | return false |
| 205 | } |
| 206 | |
| 207 | for i := range a { |
| 208 | if !arithmExprMatch(a[i], b[i]) { |
| 209 | return false |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | return true |
| 214 | } |
| 215 | |
| 216 | func arithmExprMatch(a, b syntax.ArithmExpr) bool { |
| 217 | if reflect.TypeOf(b) != reflect.TypeOf(a) { |
| 218 | return false |
| 219 | } |
| 220 | |
| 221 | switch a := a.(type) { |
| 222 | case *syntax.BinaryArithm: |
| 223 | if b.(*syntax.BinaryArithm).Op != a.Op { |
| 224 | return false |
| 225 | } |
| 226 | if !arithmExprMatch(a.X, b.(*syntax.BinaryArithm).X) { |
| 227 | return false |
| 228 | } |
| 229 | if !arithmExprMatch(a.Y, b.(*syntax.BinaryArithm).Y) { |
| 230 | return false |
| 231 | } |
| 232 | case *syntax.UnaryArithm: |
| 233 | if b.(*syntax.UnaryArithm).Op != a.Op { |
| 234 | return false |
| 235 | } |
| 236 | if b.(*syntax.UnaryArithm).Post != a.Post { |
| 237 | return false |
| 238 | } |
| 239 | if !arithmExprMatch(a.X, b.(*syntax.UnaryArithm).X) { |
| 240 | return false |
| 241 | } |
| 242 | case *syntax.ParenArithm: |
| 243 | if !arithmExprMatch(a.X, b.(*syntax.ParenArithm).X) { |
| 244 | return false |
| 245 | } |
| 246 | case *syntax.Word: |
| 247 | if !wordsMatch(a.Parts, b.(*syntax.Word).Parts, matchStructure) { |
| 248 | return false |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | return true |
| 253 | } |
| 254 | |
| 255 | func assignArrayMatch(a, b []*syntax.Assign) bool { |
| 256 | if len(b) != len(a) { |
| 257 | return false |
| 258 | } |
| 259 | |
| 260 | for i := range a { |
| 261 | if !assignMatch(a[i], b[i]) { |
| 262 | return false |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | return true |
| 267 | } |
| 268 | |
| 269 | func assignMatch(a, b *syntax.Assign) bool { |
| 270 | if b.Append != a.Append { |
| 271 | return false |
| 272 | } |
| 273 | |
| 274 | if b.Naked != a.Naked { |
| 275 | return false |
| 276 | } |
| 277 | |
| 278 | if (b.Name == nil) != (a.Name == nil) { |
| 279 | return false |
| 280 | } |
| 281 | |
| 282 | if a.Name != nil && !verifyStrings(a.Name.Value, b.Name.Value, matchStructure) { |
| 283 | return false |
| 284 | } |
| 285 | |
| 286 | if (b.Index == nil) != (a.Index == nil) { |
| 287 | return false |
| 288 | } |
| 289 | |
| 290 | if a.Index != nil && !arithmExprMatch(a.Index, b.Index) { |
| 291 | return false |
| 292 | } |
| 293 | |
| 294 | if (b.Value == nil) != (a.Value == nil) { |
| 295 | return false |
| 296 | } |
| 297 | |
| 298 | if a.Value != nil && !wordsMatch(a.Value.Parts, b.Value.Parts, matchStructure) { |
| 299 | return false |
| 300 | } |
| 301 | |
| 302 | if (b.Array == nil) != (a.Array == nil) { |
| 303 | return false |
| 304 | } |
| 305 | |
| 306 | if a.Array != nil && !arrayExprMatch(a.Array, b.Array) { |
| 307 | return false |
| 308 | } |
| 309 | |
| 310 | return true |
| 311 | } |
| 312 | |
| 313 | func arrayExprMatch(a, b *syntax.ArrayExpr) bool { |
| 314 | if len(b.Elems) != len(a.Elems) { |
| 315 | return false |
| 316 | } |
| 317 | |
| 318 | for i := range a.Elems { |
| 319 | if !arrayElemMatch(a.Elems[i], b.Elems[i]) { |
| 320 | return false |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | if !commentsArrayMatch(a.Last, b.Last) { |
| 325 | return false |
| 326 | } |
| 327 | |
| 328 | return true |
| 329 | } |
| 330 | |
| 331 | func arrayElemMatch(a, b *syntax.ArrayElem) bool { |
| 332 | if (b.Index == nil) != (a.Index == nil) { |
| 333 | return false |
| 334 | } |
| 335 | |
| 336 | if a.Index != nil && !arithmExprMatch(a.Index, b.Index) { |
| 337 | return false |
| 338 | } |
| 339 | |
| 340 | if (b.Value == nil) != (a.Value == nil) { |
| 341 | return false |
| 342 | } |
| 343 | |
| 344 | if a.Value != nil && !wordsMatch(a.Value.Parts, b.Value.Parts, matchStructure) { |
| 345 | return false |
| 346 | } |
| 347 | |
| 348 | if !commentsArrayMatch(a.Comments, b.Comments) { |
| 349 | return false |
| 350 | } |
| 351 | |
| 352 | return true |
| 353 | } |
| 354 | |
| 355 | func caseItemArrayMatch(a, b []*syntax.CaseItem) bool { |
| 356 | if len(b) != len(a) { |
| 357 | return false |
| 358 | } |
| 359 | |
| 360 | for i := range a { |
| 361 | if !caseItemMatch(a[i], b[i]) { |
| 362 | return false |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | return true |
| 367 | } |
| 368 | |
| 369 | func caseItemMatch(a, b *syntax.CaseItem) bool { |
| 370 | if b.Op != a.Op { |
| 371 | return false |
| 372 | } |
| 373 | |
| 374 | if !wordsArrayMatch(a.Patterns, b.Patterns, matchStructure) { |
| 375 | return false |
| 376 | } |
| 377 | |
| 378 | if !statementsArrayMatch(a.Stmts, b.Stmts) { |
| 379 | return false |
| 380 | } |
| 381 | |
| 382 | if !commentsArrayMatch(a.Comments, b.Comments) { |
| 383 | return false |
| 384 | } |
| 385 | |
| 386 | if !commentsArrayMatch(a.Last, b.Last) { |
| 387 | return false |
| 388 | } |
| 389 | |
| 390 | return true |
| 391 | } |
| 392 | |
| 393 | func loopMatch(a, b syntax.Loop) bool { |
| 394 | if reflect.TypeOf(b) != reflect.TypeOf(a) { |
| 395 | return false |
| 396 | } |
| 397 | |
| 398 | switch a := a.(type) { |
| 399 | case *syntax.WordIter: |
| 400 | if (b.(*syntax.WordIter).Name == nil) != (a.Name == nil) { |
| 401 | return false |
| 402 | } |
| 403 | |
| 404 | if a.Name != nil && !verifyStrings(a.Name.Value, b.(*syntax.WordIter).Name.Value, matchStructure) { |
| 405 | return false |
| 406 | } |
| 407 | |
| 408 | if !wordsArrayMatch(a.Items, b.(*syntax.WordIter).Items, matchStructure) { |
| 409 | return false |
| 410 | } |
| 411 | case *syntax.CStyleLoop: |
| 412 | if (b.(*syntax.CStyleLoop).Init == nil) != (a.Init == nil) { |
| 413 | return false |
| 414 | } |
| 415 | |
| 416 | if a.Init != nil && !arithmExprMatch(a.Init, b.(*syntax.CStyleLoop).Init) { |
| 417 | return false |
| 418 | } |
| 419 | |
| 420 | if (b.(*syntax.CStyleLoop).Cond == nil) != (a.Cond == nil) { |
| 421 | return false |
| 422 | } |
| 423 | |
| 424 | if a.Cond != nil && !arithmExprMatch(a.Cond, b.(*syntax.CStyleLoop).Cond) { |
| 425 | return false |
| 426 | } |
| 427 | |
| 428 | if (b.(*syntax.CStyleLoop).Post == nil) != (a.Post == nil) { |
| 429 | return false |
| 430 | } |
| 431 | |
| 432 | if a.Post != nil && !arithmExprMatch(a.Post, b.(*syntax.CStyleLoop).Post) { |
| 433 | return false |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | return true |
| 438 | } |
| 439 | |
| 440 | func testExprMatch(a, b syntax.TestExpr) bool { |
| 441 | if reflect.TypeOf(b) != reflect.TypeOf(a) { |
| 442 | return false |
| 443 | } |
| 444 | |
| 445 | switch a := a.(type) { |
| 446 | case *syntax.BinaryTest: |
| 447 | if b.(*syntax.BinaryTest).Op != a.Op { |
| 448 | return false |
| 449 | } |
| 450 | |
| 451 | if !testExprMatch(a.X, b.(*syntax.BinaryTest).X) { |
| 452 | return false |
| 453 | } |
| 454 | |
| 455 | if !testExprMatch(a.Y, b.(*syntax.BinaryTest).Y) { |
| 456 | return false |
| 457 | } |
| 458 | case *syntax.UnaryTest: |
| 459 | if b.(*syntax.UnaryTest).Op != a.Op { |
| 460 | return false |
| 461 | } |
| 462 | |
| 463 | if !testExprMatch(a.X, b.(*syntax.UnaryTest).X) { |
| 464 | return false |
| 465 | } |
| 466 | case *syntax.ParenTest: |
| 467 | if !testExprMatch(a.X, b.(*syntax.ParenTest).X) { |
| 468 | return false |
| 469 | } |
| 470 | case *syntax.Word: |
| 471 | if !wordsMatch(a.Parts, b.(*syntax.Word).Parts, matchStructure) { |
| 472 | return false |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | return true |
| 477 | } |
| 478 | |
| 479 | type wordComparisonContext int |
| 480 | |
| 481 | const ( |
| 482 | matchStructure wordComparisonContext = 0 |
| 483 | forbidFlagInjection wordComparisonContext = 1 << 0 |
| 484 | literal wordComparisonContext = 1 << 1 |
| 485 | ) |
| 486 | |
| 487 | func wordsArrayMatch(a, b []*syntax.Word, ctx wordComparisonContext) bool { |
| 488 | if len(b) != len(a) { |
| 489 | return false |
| 490 | } |
| 491 | |
| 492 | for i := range a { |
| 493 | if !wordsMatch(a[i].Parts, b[i].Parts, ctx) { |
| 494 | return false |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | return true |
| 499 | } |
| 500 | |
| 501 | func wordsMatch(a, b []syntax.WordPart, ctx wordComparisonContext) bool { |
| 502 | if len(b) != len(a) { |
| 503 | return false |
| 504 | } |
| 505 | |
| 506 | for i, p := range a { |
| 507 | if !wordPartsMatch(p, b[i], ctx) { |
| 508 | return false |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | return true |
| 513 | } |
| 514 | |
| 515 | func verifyStrings(a, b string, ctx wordComparisonContext) bool { |
| 516 | aPat := "(?s)^" + strings.Replace(regexp.QuoteMeta(a), replaceableIndicator, "(.*)", -1) + "$" |
| 517 | |
| 518 | matched, err := regexp.Match(aPat, []byte(b)) |
| 519 | if err != nil { |
| 520 | return false |
| 521 | } |
| 522 | |
| 523 | if !matched { |
| 524 | return false |
| 525 | } |
| 526 | |
| 527 | if ctx&forbidFlagInjection != 0 { |
| 528 | if flagInjected(a, b) { |
| 529 | return false |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | if ctx&literal != 0 { |
| 534 | // Repeat matching to capture subgroups (untrusted insertions) |
| 535 | re := regexp.MustCompile(aPat) |
| 536 | insertedContent := re.FindStringSubmatch(b)[1:] |
| 537 | |
| 538 | for _, g := range insertedContent { |
| 539 | if literalInjection(g) { |
| 540 | return false |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | return true |
| 546 | } |
| 547 | |
| 548 | func flagInjected(a, b string) bool { |
| 549 | return !strings.HasPrefix(a, "-") && strings.HasPrefix(b, "-") |
| 550 | } |
| 551 | |
| 552 | // Check for unescaped ? * + @ ! characters |
| 553 | func literalInjection(a string) bool { |
| 554 | literalSpecials := map[rune]struct{}{'?': {}, '*': {}, '+': {}, '@': {}, '!': {}} |
| 555 | |
| 556 | escaped := false |
| 557 | for _, c := range a { |
| 558 | if !escaped { |
| 559 | if c == '\\' { |
| 560 | escaped = true |
| 561 | } else if _, special := literalSpecials[c]; special { |
| 562 | return true |
| 563 | } |
| 564 | } else { |
| 565 | escaped = false |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | // Unpaired trailing \ character would be incorrect escaping |
| 570 | return escaped |
| 571 | } |
| 572 | |
| 573 | func wordPartsMatch(a, b syntax.WordPart, ctx wordComparisonContext) bool { |
| 574 | if reflect.TypeOf(b) != reflect.TypeOf(a) { |
| 575 | return false |
| 576 | } |
| 577 | |
| 578 | switch a := a.(type) { |
| 579 | case *syntax.Lit: |
| 580 | if !verifyStrings(a.Value, b.(*syntax.Lit).Value, ctx|literal) { |
| 581 | return false |
| 582 | } |
| 583 | case *syntax.SglQuoted: |
| 584 | if !verifyStrings(a.Value, b.(*syntax.SglQuoted).Value, ctx) { |
| 585 | return false |
| 586 | } |
| 587 | case *syntax.DblQuoted: |
| 588 | if !wordsMatch(a.Parts, b.(*syntax.DblQuoted).Parts, ctx) { |
| 589 | return false |
| 590 | } |
| 591 | case *syntax.ParamExp: |
| 592 | if !paramExprMatch(a, b.(*syntax.ParamExp)) { |
| 593 | return false |
| 594 | } |
| 595 | case *syntax.CmdSubst: |
| 596 | if !cmdSubstMatch(a, b.(*syntax.CmdSubst)) { |
| 597 | return false |
| 598 | } |
| 599 | case *syntax.ArithmExp: |
| 600 | if !arithmExpMatch(*a, *b.(*syntax.ArithmExp)) { |
| 601 | return false |
| 602 | } |
| 603 | case *syntax.ProcSubst: |
| 604 | if b.(*syntax.ProcSubst).Op != a.Op { |
| 605 | return false |
| 606 | } |
| 607 | |
| 608 | if !statementsArrayMatch(a.Stmts, b.(*syntax.ProcSubst).Stmts) { |
| 609 | return false |
| 610 | } |
| 611 | |
| 612 | if !commentsArrayMatch(a.Last, b.(*syntax.ProcSubst).Last) { |
| 613 | return false |
| 614 | } |
| 615 | case *syntax.ExtGlob: |
| 616 | if b.(*syntax.ExtGlob).Op != a.Op { |
| 617 | return false |
| 618 | } |
| 619 | |
| 620 | if b.(*syntax.ExtGlob).Pattern.Value != a.Pattern.Value { |
| 621 | return false |
| 622 | } |
| 623 | case *syntax.BraceExp: |
| 624 | if !wordsArrayMatch(a.Elems, b.(*syntax.BraceExp).Elems, ctx) { |
| 625 | return false |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | return true |
| 630 | } |
| 631 | |
| 632 | func commandsMatch(a, b syntax.Command) bool { |
| 633 | if reflect.TypeOf(b) != reflect.TypeOf(a) { |
| 634 | return false |
| 635 | } |
| 636 | |
| 637 | switch cmd := a.(type) { |
| 638 | case *syntax.CallExpr: |
| 639 | if !assignArrayMatch(cmd.Assigns, b.(*syntax.CallExpr).Assigns) { |
| 640 | return false |
| 641 | } |
| 642 | |
| 643 | if len(b.(*syntax.CallExpr).Args) != len(cmd.Args) { |
| 644 | return false |
| 645 | } |
| 646 | |
| 647 | // First "argument" is command name |
| 648 | context := matchStructure |
| 649 | for i := 0; i < len(cmd.Args); i++ { |
| 650 | if !wordsMatch(cmd.Args[i].Parts, b.(*syntax.CallExpr).Args[i].Parts, context) { |
| 651 | return false |
| 652 | } |
| 653 | |
| 654 | // For subsequent arguments, also check for flag injection |
| 655 | context = forbidFlagInjection |
| 656 | } |
| 657 | case *syntax.IfClause: |
| 658 | if !statementsArrayMatch(cmd.Cond, b.(*syntax.IfClause).Cond) { |
| 659 | return false |
| 660 | } |
| 661 | |
| 662 | if !statementsArrayMatch(cmd.Then, b.(*syntax.IfClause).Then) { |
| 663 | return false |
| 664 | } |
| 665 | |
| 666 | if (b.(*syntax.IfClause).Else == nil) != (cmd.Else == nil) { |
| 667 | return false |
| 668 | } |
| 669 | |
| 670 | if cmd.Else != nil && !commandsMatch(cmd.Else, b.(*syntax.IfClause).Else) { |
| 671 | return false |
| 672 | } |
| 673 | |
| 674 | if !commentsArrayMatch(cmd.CondLast, b.(*syntax.IfClause).CondLast) { |
| 675 | return false |
| 676 | } |
| 677 | |
| 678 | if !commentsArrayMatch(cmd.ThenLast, b.(*syntax.IfClause).ThenLast) { |
| 679 | return false |
| 680 | } |
| 681 | |
| 682 | if !commentsArrayMatch(cmd.Last, b.(*syntax.IfClause).Last) { |
| 683 | return false |
| 684 | } |
| 685 | case *syntax.WhileClause: |
| 686 | if b.(*syntax.WhileClause).Until != cmd.Until { |
| 687 | return false |
| 688 | } |
| 689 | |
| 690 | if !statementsArrayMatch(cmd.Cond, b.(*syntax.WhileClause).Cond) { |
| 691 | return false |
| 692 | } |
| 693 | |
| 694 | if !statementsArrayMatch(cmd.Do, b.(*syntax.WhileClause).Do) { |
| 695 | return false |
| 696 | } |
| 697 | |
| 698 | if !commentsArrayMatch(cmd.CondLast, b.(*syntax.WhileClause).CondLast) { |
| 699 | return false |
| 700 | } |
| 701 | |
| 702 | if !commentsArrayMatch(cmd.DoLast, b.(*syntax.WhileClause).DoLast) { |
| 703 | return false |
| 704 | } |
| 705 | case *syntax.ForClause: |
| 706 | if b.(*syntax.ForClause).Select != cmd.Select { |
| 707 | return false |
| 708 | } |
| 709 | |
| 710 | if !loopMatch(cmd.Loop, b.(*syntax.ForClause).Loop) { |
| 711 | return false |
| 712 | } |
| 713 | |
| 714 | if !statementsArrayMatch(cmd.Do, b.(*syntax.ForClause).Do) { |
| 715 | return false |
| 716 | } |
| 717 | |
| 718 | if !commentsArrayMatch(cmd.DoLast, b.(*syntax.ForClause).DoLast) { |
| 719 | return false |
| 720 | } |
| 721 | case *syntax.CaseClause: |
| 722 | if !wordsMatch(cmd.Word.Parts, b.(*syntax.CaseClause).Word.Parts, matchStructure) { |
| 723 | return false |
| 724 | } |
| 725 | |
| 726 | if !caseItemArrayMatch(cmd.Items, b.(*syntax.CaseClause).Items) { |
| 727 | return false |
| 728 | } |
| 729 | |
| 730 | if !commentsArrayMatch(cmd.Last, b.(*syntax.CaseClause).Last) { |
| 731 | return false |
| 732 | } |
| 733 | case *syntax.Block: |
| 734 | if !statementsArrayMatch(cmd.Stmts, b.(*syntax.Block).Stmts) { |
| 735 | return false |
| 736 | } |
| 737 | |
| 738 | if !commentsArrayMatch(cmd.Last, b.(*syntax.Block).Last) { |
| 739 | return false |
| 740 | } |
| 741 | case *syntax.Subshell: |
| 742 | if !statementsArrayMatch(cmd.Stmts, b.(*syntax.Subshell).Stmts) { |
| 743 | return false |
| 744 | } |
| 745 | |
| 746 | if !commentsArrayMatch(cmd.Last, b.(*syntax.Subshell).Last) { |
| 747 | return false |
| 748 | } |
| 749 | case *syntax.BinaryCmd: |
| 750 | if b.(*syntax.BinaryCmd).Op != cmd.Op { |
| 751 | return false |
| 752 | } |
| 753 | |
| 754 | if !statementsMatch(cmd.X, b.(*syntax.BinaryCmd).X) { |
| 755 | return false |
| 756 | } |
| 757 | |
| 758 | if !statementsMatch(cmd.Y, b.(*syntax.BinaryCmd).Y) { |
| 759 | return false |
| 760 | } |
| 761 | |
| 762 | case *syntax.FuncDecl: |
| 763 | if b.(*syntax.FuncDecl).RsrvWord != cmd.RsrvWord { |
| 764 | return false |
| 765 | } |
| 766 | |
| 767 | if (cmd.Name == nil) != (b.(*syntax.FuncDecl).Name != nil) { |
| 768 | return false |
| 769 | } |
| 770 | |
| 771 | if cmd.Name != nil && !verifyStrings(cmd.Name.Value, b.(*syntax.FuncDecl).Name.Value, matchStructure) { |
| 772 | return false |
| 773 | } |
| 774 | |
| 775 | if !statementsMatch(cmd.Body, b.(*syntax.FuncDecl).Body) { |
| 776 | return false |
| 777 | } |
| 778 | case *syntax.ArithmCmd: |
| 779 | if b.(*syntax.ArithmCmd).Unsigned != cmd.Unsigned { |
| 780 | return false |
| 781 | } |
| 782 | |
| 783 | if !arithmExprMatch(cmd.X, b.(*syntax.ArithmCmd).X) { |
| 784 | return false |
| 785 | } |
| 786 | case *syntax.TestClause: |
| 787 | if !testExprMatch(cmd.X, b.(*syntax.TestClause).X) { |
| 788 | return false |
| 789 | } |
| 790 | case *syntax.DeclClause: |
| 791 | if (b.(*syntax.DeclClause).Variant == nil) != (cmd.Variant == nil) { |
| 792 | return false |
| 793 | } |
| 794 | |
| 795 | if cmd.Variant != nil && |
| 796 | (b.(*syntax.DeclClause).Variant.Value != cmd.Variant.Value) { |
| 797 | return false |
| 798 | } |
| 799 | |
| 800 | if !assignArrayMatch(cmd.Args, b.(*syntax.DeclClause).Args) { |
| 801 | return false |
| 802 | } |
| 803 | case *syntax.LetClause: |
| 804 | if !arithmExprArrayMatch(cmd.Exprs, b.(*syntax.LetClause).Exprs) { |
| 805 | return false |
| 806 | } |
| 807 | case *syntax.TimeClause: |
| 808 | if !statementsMatch(cmd.Stmt, b.(*syntax.TimeClause).Stmt) { |
| 809 | return false |
| 810 | } |
| 811 | case *syntax.CoprocClause: |
| 812 | if !wordsMatch(cmd.Name.Parts, b.(*syntax.CoprocClause).Name.Parts, matchStructure) { |
| 813 | return false |
| 814 | } |
| 815 | |
| 816 | if !statementsMatch(cmd.Stmt, b.(*syntax.CoprocClause).Stmt) { |
| 817 | return false |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | return true |
| 822 | } |
| 823 | |
| 824 | func redirectsMatch(a, b *syntax.Redirect) bool { |
| 825 | if b.Op != a.Op { |
| 826 | return false |
| 827 | } |
| 828 | |
| 829 | if (b.N == nil) != (a.N == nil) { |
| 830 | return false |
| 831 | } |
| 832 | |
| 833 | if a.N != nil && !verifyStrings(a.N.Value, b.N.Value, matchStructure) { |
| 834 | return false |
| 835 | } |
| 836 | |
| 837 | if (b.Word == nil) != (a.Word == nil) { |
| 838 | return false |
| 839 | } |
| 840 | |
| 841 | if a.Word != nil && !wordsMatch(a.Word.Parts, b.Word.Parts, matchStructure) { |
| 842 | return false |
| 843 | } |
| 844 | |
| 845 | if (b.Hdoc == nil) != (a.Hdoc == nil) { |
| 846 | return false |
| 847 | } |
| 848 | |
| 849 | if a.Hdoc != nil && !wordsMatch(a.Hdoc.Parts, b.Hdoc.Parts, matchStructure) { |
| 850 | return false |
| 851 | } |
| 852 | |
| 853 | return true |
| 854 | } |
| 855 | |
| 856 | func redirectsArrayMatch(a, b []*syntax.Redirect) bool { |
| 857 | if len(b) != len(a) { |
| 858 | return false |
| 859 | } |
| 860 | |
| 861 | for i := range a { |
| 862 | if !redirectsMatch(a[i], b[i]) { |
| 863 | return false |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | return true |
| 868 | } |
| 869 | |
| 870 | func statementsMatch(a, b *syntax.Stmt) bool { |
| 871 | if b.Negated != a.Negated { |
| 872 | return false |
| 873 | } |
| 874 | |
| 875 | if b.Background != a.Background { |
| 876 | return false |
| 877 | } |
| 878 | |
| 879 | if b.Coprocess != a.Coprocess { |
| 880 | return false |
| 881 | } |
| 882 | |
| 883 | if !redirectsArrayMatch(a.Redirs, b.Redirs) { |
| 884 | return false |
| 885 | } |
| 886 | |
| 887 | if (b.Cmd == nil) != (a.Cmd == nil) { |
| 888 | return false |
| 889 | } |
| 890 | |
| 891 | if a.Cmd != nil && !commandsMatch(a.Cmd, b.Cmd) { |
| 892 | return false |
| 893 | } |
| 894 | |
| 895 | if !commentsArrayMatch(a.Comments, b.Comments) { |
| 896 | return false |
| 897 | } |
| 898 | |
| 899 | return true |
| 900 | } |
| 901 | |
| 902 | func statementsArrayMatch(a, b []*syntax.Stmt) bool { |
| 903 | if len(b) != len(a) { |
| 904 | return false |
| 905 | } |
| 906 | |
| 907 | for i := range a { |
| 908 | if !statementsMatch(a[i], b[i]) { |
| 909 | return false |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | return true |
| 914 | } |
| 915 | |
| 916 | func scriptsMatch(a, b *syntax.File) bool { |
| 917 | if !statementsArrayMatch(a.Stmts, b.Stmts) { |
| 918 | return false |
| 919 | } |
| 920 | |
| 921 | if !commentsArrayMatch(a.Last, b.Last) { |
| 922 | return false |
| 923 | } |
| 924 | |
| 925 | return true |
| 926 | } |
| 927 | |
| 928 | var truncationSpecifierPattern = regexp.MustCompile(`([^%]|^)%([+-0 #]*)(\d*\.\d*)(s|v)`) |
| 929 | |
| 930 | // Sprintf is a fmt.Sprintf replacement specific for generating bash scripts, with protection against any string substitution from escaping its context, or from injecting new flag arguments. |
| 931 | func Sprintf(format string, a ...any) (string, error) { |
| 932 | return SprintfLang(format, syntax.LangBash, a...) |
| 933 | } |
| 934 | |
| 935 | // MustSprintf is a wrapper to Sprintf that panics upon error. |
| 936 | func MustSprintf(format string, a ...any) string { |
| 937 | res, err := Sprintf(format, a...) |
| 938 | if err != nil { |
| 939 | panic(err) |
| 940 | } |
| 941 | return res |
| 942 | } |
| 943 | |
| 944 | // SprintfLang is a fmt.Sprintf replacement specific for generating shell scripts, with protection against any string substitution from escaping its context, or from injecting new flag arguments. |
| 945 | func SprintfLang(format string, lang syntax.LangVariant, a ...any) (string, error) { |
| 946 | baselineArgs := common.DeepCopyMutateStrings(a, func(in string) string { return replaceableIndicator }) |
| 947 | |
| 948 | // Disable string truncation so that strings are always directly substituted |
| 949 | baselineFormat := string(truncationSpecifierPattern.ReplaceAll([]byte(format), []byte("${1}%${2}${4}"))) |
| 950 | baselineResult := fmt.Sprintf(baselineFormat, baselineArgs.([]any)...) |
| 951 | |
| 952 | parsedBaselineResult, err := syntax.NewParser(syntax.Variant(lang), syntax.KeepComments(true)).Parse(strings.NewReader(baselineResult), "template.sh") |
| 953 | if err != nil { |
| 954 | return "", ErrInvalidShTemplate |
| 955 | } |
| 956 | |
| 957 | requestedResult := fmt.Sprintf(format, a...) |
| 958 | parsedRequestedResult, err := syntax.NewParser(syntax.Variant(lang), syntax.KeepComments(true)).Parse(strings.NewReader(requestedResult), "template.sh") |
| 959 | if err != nil { |
| 960 | return "", ErrShInjection |
| 961 | } |
| 962 | |
| 963 | if !scriptsMatch(parsedBaselineResult, parsedRequestedResult) { |
| 964 | return "", ErrShInjection |
| 965 | } |
| 966 | |
| 967 | return requestedResult, nil |
| 968 | } |