internal/codescan/shell_test.go

97 lines · 3180 bytes

3 symbols in this file
 1package codescan
 2
 3import (
 4	"errors"
 5	"testing"
 6
 7	"gitbay.org/gitbay/internal/store"
 8)
 9
10func TestSplitLines(t *testing.T) {
11	for _, tc := range []struct {
12		name, src string
13		want      bool
14	}{
15		{"arithmetic in double quotes", `text() { printf '×'; printf "%$(($1 - 1))s" '' | tr ' ' x; }`, false},
16		{"bare arithmetic", `echo $(( $n + 1 ))`, false},
17		{"nested arithmetic parens", `echo "$(( ($a + $b) * 2 ))"`, false},
18		{"double quoted", `echo "$x ${y:-a b}"`, false},
19		{"single quoted", `echo '$x'`, false},
20		{"ansi-c quoted", `echo $'a\'$x'`, false},
21		{"escaped dollar", `echo \$x`, false},
22		{"special parameters", `echo $# $? $$ $! $-`, false},
23		{"comment", `echo ok # $x`, false},
24		{"comment line", "# the repo's own\necho \"$x\"", false},
25		{"quoted heredoc", "cat <<'EOF'\n$(id) $x\nEOF", false},
26		{"unquoted", `echo $x`, true},
27		{"unquoted braces", `echo ${x}`, true},
28		{"positional", `rm $1`, true},
29		{"all arguments", `f $@`, true},
30		{"command substitution in quotes", `echo "$(ls $dir)"`, true},
31		{"command substitution in arithmetic", `echo $(( $(wc -l <$f) + 1 ))`, true},
32		{"backticks in quotes", "echo \"`ls $dir`\"", true},
33		{"after quoted arithmetic", `printf "%$(($1 - 1))s" $x`, true},
34	} {
35		lines, ok := splitLines([]byte(tc.src))
36		if !ok {
37			t.Errorf("%s: not read", tc.name)
38			continue
39		}
40		if lines[1] != tc.want {
41			t.Errorf("%s: split = %v, want %v", tc.name, lines[1], tc.want)
42		}
43	}
44}
45
46func TestSplitLinesAcrossLines(t *testing.T) {
47	src := "a=\"$(($n\n+ 1))\"\necho \\\n  $x\ncat <<EOF\n$(id)\nEOF\necho \"$y\"\n"
48	lines, ok := splitLines([]byte(src))
49	want := map[int]bool{3: true, 4: true, 5: true, 6: true, 7: true}
50	if !ok || len(lines) != len(want) {
51		t.Fatalf("lines = %v ok %v", lines, ok)
52	}
53	for l := range want {
54		if !lines[l] {
55			t.Errorf("line %d not marked: %v", l, lines)
56		}
57	}
58	for _, src := range []string{`echo "$x`, `echo '$x`, `echo $((1 + 2)`, `echo $(ls`, `echo ${x`, "cat <<EOF\nbody\n", `echo $'x`} {
59		if _, ok := splitLines([]byte(src)); ok {
60			t.Errorf("%q read", src)
61		}
62	}
63}
64
65func TestScopeChecksShellSplitting(t *testing.T) {
66	rule := "opt.opengrep-rules.bash.lang.correctness.unquoted-variable-expansion-in-command"
67	files := map[string]string{
68		"quoted.sh":   "#!/bin/sh\nprintf \"%$(($1 - 1))s\" ''\n",
69		"unquoted.sh": "rm $1\n",
70		"broken.sh":   "echo \"$x\n",
71	}
72	read := func(p string) ([]byte, error) {
73		if src, ok := files[p]; ok {
74			return []byte(src), nil
75		}
76		return nil, errors.New("missing")
77	}
78	findings := []store.SecurityFinding{
79		{Rule: rule, Path: "quoted.sh", Line: 2},
80		{Rule: rule, Path: "unquoted.sh", Line: 1},
81		{Rule: rule, Path: "broken.sh", Line: 1},
82		{Rule: rule, Path: "gone.sh", Line: 1},
83		{Rule: "other", Path: "quoted.sh", Line: 2},
84	}
85	kept, aside, _ := Scope{Read: read}.Apply(findings)
86	if aside.Unsplit != 1 || len(kept) != 4 {
87		t.Fatalf("kept %+v aside %+v", kept, aside)
88	}
89	for _, f := range kept {
90		if f.Path == "quoted.sh" && f.Rule == rule {
91			t.Errorf("unsplit hit kept")
92		}
93	}
94	if kept, aside, _ := (Scope{}).Apply(findings); aside.Unsplit != 0 || len(kept) != 5 {
95		t.Errorf("without Read: kept %d aside %+v", len(kept), aside)
96	}
97}