返回 DeepSeek-Reasonix
bash_readonly_test.go
根目录 / internal / permission / bash_readonly_test.go
1 package permission
2
3 import (
4 "encoding/json"
5 "testing"
6 )
7
8 func TestIsReadOnlyBashSubject(t *testing.T) {
9 tests := []struct {
10 cmd string
11 want bool
12 }{
13 // Read-only commands
14 {"ls", true},
15 {"ls /tmp", true},
16 {"cat main.go", true},
17 {"head -n 5 file.txt", true},
18 {"find . -name '*.go'", true},
19 {"grep TODO *.go", true},
20 {"grep 'a|b' file", true},
21 {"rg pattern", true},
22 {"echo hello", true},
23 {"pwd", true},
24 {"cd /tmp/project", true},
25 {"whoami", true},
26 {"wc -l file.txt", true},
27 {"stat main.go", true},
28 {"du -sh .", true},
29 {"diff a.go b.go", true},
30 {"printenv PATH", true},
31 {"Test-NetConnection -ComputerName localhost -Port 27017", false},
32 {"Get-Process -Name mongod", true},
33 {"Get-ChildItem -Path .", true},
34 {"Get-NetTCPConnection -LocalPort 6379", true},
35 {`basename "$(pwd)"`, true},
36
37 // Git read-only
38 {"git log", true},
39 {"git status", true},
40 {"git diff", true},
41 {"git show HEAD", true},
42 {"git blame main.go", true},
43 {"git log 2>/dev/null", true},
44 {"git log >/dev/null", true},
45 {"git log >$null", true},
46 {"git log >NUL", true},
47 {"git log 2>&1", true},
48 {"git log &>/dev/null", true},
49 {"git remote", false},
50 {"git remote add origin git@example.com:x/y", false},
51 {"git config --global user.name Xinwei", false},
52 {"git stash", false},
53 {"git stash push", false},
54 {"git archive --output repo.tar HEAD", false},
55 {"git bundle create repo.bundle HEAD", false},
56 {"git diff --output changes.patch", false},
57 {"git show --output=changes.patch HEAD", false},
58 {"git diff --output changes.patch 2>/dev/null", false},
59 {"git log > changes.patch", false},
60 {"git log >$nullish", false},
61 {"git log >nul.txt", false},
62 {"git log < /dev/null", false},
63
64 // Go read-only
65 {"go vet ./...", true},
66 {"go doc fmt", true},
67 {"go list ./...", true},
68 {"go env -w GOPROXY=https://proxy.golang.org,direct", false},
69 {"go env -u GOPROXY", false},
70
71 // Not read-only
72 {"rm file.txt", false},
73 {"echo $HOME", false},
74 {"rm -rf /", false},
75 {"env rm -rf /", false},
76 {"git commit -m 'msg'", false},
77 {"git branch", false},
78 {"git branch feature/new", false},
79 {"git push", false},
80 {"git push --force", false},
81 {"cd /tmp && rm file.txt", false},
82 {"go build ./...", false},
83 {"go fmt ./...", false},
84 {"go test ./...", false},
85 {"ls; rm file.txt", false},
86 {"git status && rm file.txt", false},
87 {"cat main.go | tee copy.go", false},
88 {"cat file > output.txt", false},
89 {"git diff > changes.patch", false},
90 {"git diff >> changes.patch", false},
91 {"cat < input.txt", false},
92 {"diff <(sort a.txt) <(sort b.txt)", false},
93 {"cat >(tee output.txt)", false},
94 {"ls $(touch output.txt)", false},
95 {`basename "$(touch output.txt)"`, false},
96 {`basename $(pwd)`, false},
97 {"ls `touch output.txt`", false},
98 {"ls || rm file.txt", false},
99 {"ls & rm file.txt", false},
100 {"find . -name '*.go' | xargs gofmt -w", false},
101 {"find . -name '*.go' -exec rm {} \\;", false},
102 {"find . -name '*.go' -ok rm {} \\;", false},
103 {"find . -name '*.go' -okdir rm {} \\;", false},
104 {"find . -name '*.tmp' -delete", false},
105 {"find . -name '*.go' -fls files.txt", false},
106 {"find . -name '*.go' -fprint files.txt", false},
107 {"find . -name '*.go' -fprint0 files.txt", false},
108 {"find . -name '*.go' -fprintf files.txt '%p\\n'", false},
109 {"awk '{print $1}' file.txt", false},
110 {"awk 'BEGIN{system(\"touch /tmp/reasonix-awk-test\")}'", false},
111 {"awk 'BEGIN{print \"evil\" > \"/tmp/reasonix-awk-test\"}'", false},
112 {"sed 's/a/b/' file.txt", false},
113 {"sed -e 's/.*/touch \\/tmp\\/reasonix-sed-test/e' file.txt", false},
114 {"sed -i 's/a/b/' file.txt", false},
115 {"sed -i.bak 's/a/b/' file.txt", false},
116 {"sed -ibak 's/a/b/' file.txt", false},
117 {"sed --in-place 's/a/b/' file.txt", false},
118 {"sort -o sorted.txt input.txt", false},
119 {"sort -osorted.txt input.txt", false},
120 {"sort --output=sorted.txt input.txt", false},
121 {"tee out.txt", false},
122 {"xargs rm", false},
123 {"make build", false},
124 {"curl https://example.com", false},
125 {"npm install", false},
126 {"chmod 777 file", false},
127 {"Start-Process mongod", false},
128 {"Stop-Process -Name mongod", false},
129 {"Set-Content style.css bad", false},
130 {"", false},
131 }
132
133 for _, tt := range tests {
134 t.Run(tt.cmd, func(t *testing.T) {
135 if got := isReadOnlyBashSubject(tt.cmd); got != tt.want {
136 t.Errorf("isReadOnlyBashSubject(%q) = %v, want %v", tt.cmd, got, tt.want)
137 }
138 })
139 }
140 }
141
142 func TestBashCommandIsReadOnlyRejectsProcessLifecycleFlags(t *testing.T) {
143 tests := []struct {
144 name string
145 args string
146 want bool
147 }{
148 {name: "foreground reader", args: `{"command":"git status"}`, want: true},
149 {name: "background reader", args: `{"command":"git status","run_in_background":true}`},
150 {name: "preserved reader", args: `{"command":"git status","preserve_background_processes":true}`},
151 {name: "writer", args: `{"command":"rm -rf build"}`},
152 {name: "missing command", args: `{}`},
153 {name: "malformed", args: `{"command":`},
154 }
155 for _, tc := range tests {
156 t.Run(tc.name, func(t *testing.T) {
157 if got := BashCommandIsReadOnly(json.RawMessage(tc.args)); got != tc.want {
158 t.Fatalf("BashCommandIsReadOnly(%s) = %v, want %v", tc.args, got, tc.want)
159 }
160 })
161 }
162 }
163
164 func TestBashDangerWarning(t *testing.T) {
165 tests := []struct {
166 cmd string
167 want string
168 }{
169 {"ls", ""},
170 {"cat main.go", ""},
171 {"rm -rf /tmp/build", "recursive delete"},
172 {"rm -r old_files", "recursive delete"},
173 {"git push --force origin main", "force push"},
174 {"git push -f", "force push"},
175 {"git reset --hard HEAD~1", "hard reset"},
176 {"chmod 777 script.sh", "world-writable"},
177 {"sudo make install", "superuser"},
178 {"git clean -fd", "force clean"},
179 }
180
181 for _, tt := range tests {
182 t.Run(tt.cmd, func(t *testing.T) {
183 if got := BashDangerWarning(tt.cmd); got != tt.want {
184 t.Errorf("BashDangerWarning(%q) = %q, want %q", tt.cmd, got, tt.want)
185 }
186 })
187 }
188 }
189
189 lines GO