返回 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", true},
50 {"git remote -v", true},
51 {"git remote add origin git@example.com:x/y", false},
52 {"git config --global user.name Xinwei", false},
53 {"git stash", false},
54 {"git stash push", false},
55 {"git archive --output repo.tar HEAD", false},
56 {"git bundle create repo.bundle HEAD", false},
57 {"git diff --output changes.patch", false},
58 {"git show --output=changes.patch HEAD", false},
59 {"git diff --output changes.patch 2>/dev/null", false},
60 {"git log > changes.patch", false},
61 {"git log >$nullish", false},
62 {"git log >nul.txt", false},
63 {"git log < /dev/null", false},
64
65 // Go read-only
66 {"go vet ./...", true},
67 {"go doc fmt", true},
68 {"go list ./...", true},
69 {"go env -w GOPROXY=https://proxy.golang.org,direct", false},
70 {"go env -u GOPROXY", false},
71
72 // Not read-only
73 {"rm file.txt", false},
74 {"echo $HOME", false},
75 {"rm -rf /", false},
76 {"env rm -rf /", false},
77 {"git commit -m 'msg'", false},
78 {"git branch", true},
79 {"git branch -a", true},
80 {"git branch feature/new", false},
81 {"git config --edit", false},
82 {"git config --global -e", false},
83 {"git push", false},
84 {"git push --force", false},
85 {"cd /tmp && rm file.txt", false},
86 {"go build ./...", false},
87 {"go fmt ./...", false},
88 {"go test ./...", false},
89 {"ls; rm file.txt", false},
90 {"git status && rm file.txt", false},
91 {"cat main.go | tee copy.go", false},
92 {"cat file > output.txt", false},
93 {"git diff > changes.patch", false},
94 {"git diff >> changes.patch", false},
95 {"cat < input.txt", false},
96 {"diff <(sort a.txt) <(sort b.txt)", false},
97 {"cat >(tee output.txt)", false},
98 {"ls $(touch output.txt)", false},
99 {`basename "$(touch output.txt)"`, false},
100 {`basename $(pwd)`, false},
101 {"ls `touch output.txt`", false},
102 {"ls || rm file.txt", false},
103 {"ls & rm file.txt", false},
104 {"find . -name '*.go' | xargs gofmt -w", false},
105 {"find . -name '*.go' -exec rm {} \\;", false},
106 {"find . -name '*.go' -ok rm {} \\;", false},
107 {"find . -name '*.go' -okdir rm {} \\;", false},
108 {"find . -name '*.tmp' -delete", false},
109 {"find . -name '*.go' -fls files.txt", false},
110 {"find . -name '*.go' -fprint files.txt", false},
111 {"find . -name '*.go' -fprint0 files.txt", false},
112 {"find . -name '*.go' -fprintf files.txt '%p\\n'", false},
113 {"awk '{print $1}' file.txt", false},
114 {"awk 'BEGIN{system(\"touch /tmp/reasonix-awk-test\")}'", false},
115 {"awk 'BEGIN{print \"evil\" > \"/tmp/reasonix-awk-test\"}'", false},
116 {"sed 's/a/b/' file.txt", false},
117 {"sed -e 's/.*/touch \\/tmp\\/reasonix-sed-test/e' file.txt", false},
118 {"sed -i 's/a/b/' file.txt", false},
119 {"sed -i.bak 's/a/b/' file.txt", false},
120 {"sed -ibak 's/a/b/' file.txt", false},
121 {"sed --in-place 's/a/b/' file.txt", false},
122 {"sort -o sorted.txt input.txt", false},
123 {"sort -osorted.txt input.txt", false},
124 {"sort --output=sorted.txt input.txt", false},
125 {"tee out.txt", false},
126 {"xargs rm", false},
127 {"make build", false},
128 {"curl https://example.com", false},
129 {"npm install", false},
130 {"chmod 777 file", false},
131 {"Start-Process mongod", false},
132 {"Stop-Process -Name mongod", false},
133 {"Set-Content style.css bad", false},
134 {"", false},
135 }
136
137 for _, tt := range tests {
138 t.Run(tt.cmd, func(t *testing.T) {
139 if got := isReadOnlyBashSubject(tt.cmd); got != tt.want {
140 t.Errorf("isReadOnlyBashSubject(%q) = %v, want %v", tt.cmd, got, tt.want)
141 }
142 })
143 }
144 }
145
146 func TestBashCommandIsReadOnlyRejectsProcessLifecycleFlags(t *testing.T) {
147 tests := []struct {
148 name string
149 args string
150 want bool
151 }{
152 {name: "foreground reader", args: `{"command":"git status"}`, want: true},
153 {name: "background reader", args: `{"command":"git status","run_in_background":true}`},
154 {name: "preserved reader", args: `{"command":"git status","preserve_background_processes":true}`},
155 {name: "writer", args: `{"command":"rm -rf build"}`},
156 {name: "missing command", args: `{}`},
157 {name: "malformed", args: `{"command":`},
158 }
159 for _, tc := range tests {
160 t.Run(tc.name, func(t *testing.T) {
161 if got := BashCommandIsReadOnly(json.RawMessage(tc.args)); got != tc.want {
162 t.Fatalf("BashCommandIsReadOnly(%s) = %v, want %v", tc.args, got, tc.want)
163 }
164 })
165 }
166 }
167
168 func TestBashDangerWarning(t *testing.T) {
169 tests := []struct {
170 cmd string
171 want string
172 }{
173 {"ls", ""},
174 {"cat main.go", ""},
175 {"rm -rf /tmp/build", "recursive delete"},
176 {"rm -r old_files", "recursive delete"},
177 {"git push --force origin main", "force push"},
178 {"git push -f", "force push"},
179 {"git reset --hard HEAD~1", "hard reset"},
180 {"chmod 777 script.sh", "world-writable"},
181 {"sudo make install", "superuser"},
182 {"git clean -fd", "force clean"},
183 }
184
185 for _, tt := range tests {
186 t.Run(tt.cmd, func(t *testing.T) {
187 if got := BashDangerWarning(tt.cmd); got != tt.want {
188 t.Errorf("BashDangerWarning(%q) = %q, want %q", tt.cmd, got, tt.want)
189 }
190 })
191 }
192 }
193
193 lines GO