-
-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathvalid_paths_test.clj
More file actions
306 lines (253 loc) · 12.9 KB
/
valid_paths_test.clj
File metadata and controls
306 lines (253 loc) · 12.9 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
(ns clojure-mcp.utils.valid-paths-test
(:require [clojure.test :refer [deftest is testing]]
[clojure.java.io :as io]
[clojure.string :as str]
[clojure-mcp.utils.valid-paths :as valid-paths]))
(deftest extract-paths-from-bash-command-test
(testing "Basic path extraction"
(is (= #{"/usr/bin"}
(valid-paths/extract-paths-from-bash-command "ls /usr/bin")))
(is (= #{"./file.txt"}
(valid-paths/extract-paths-from-bash-command "cat ./file.txt")))
(is (= #{"."}
(valid-paths/extract-paths-from-bash-command "find . -name '*.clj'")))
(is (= #{"../other"}
(valid-paths/extract-paths-from-bash-command "cd ../other")))
(is (= #{"~/.bashrc"}
(valid-paths/extract-paths-from-bash-command "ls ~/.bashrc"))))
(testing "Multiple paths"
(is (= #{"/path1" "/path2" "../path3"}
(valid-paths/extract-paths-from-bash-command "ls /path1 /path2 ../path3")))
(is (= #{"/etc" "/home"}
(valid-paths/extract-paths-from-bash-command "tar -czf backup.tar.gz /etc /home"))))
(testing "Quoted paths with spaces"
(is (= #{"/src/file" "/dest with spaces/"}
(valid-paths/extract-paths-from-bash-command "cp /src/file \"/dest with spaces/\"")))
(is (= #{"/path with spaces"}
(valid-paths/extract-paths-from-bash-command "ls '/path with spaces'"))))
(testing "Complex commands with pipes and redirections"
(is (= #{"/usr" "/tmp/out.txt"}
(valid-paths/extract-paths-from-bash-command "find /usr -name '*.txt' | head > /tmp/out.txt")))
(is (= #{"/var/log/app.log" "/tmp/errors.log"}
(valid-paths/extract-paths-from-bash-command "cat /var/log/app.log | grep ERROR | tee /tmp/errors.log"))))
(testing "False positives should be avoided"
(is (= #{}
(valid-paths/extract-paths-from-bash-command "echo 'not/a/path really'")))
(is (= #{}
(valid-paths/extract-paths-from-bash-command "grep 'pattern/with/slashes' file.txt")))
(is (= #{}
(valid-paths/extract-paths-from-bash-command "sed 's/old/new/g' input.txt"))))
(testing "Security-relevant paths"
(is (= #{"../../../../etc/passwd"}
(valid-paths/extract-paths-from-bash-command "cat ../../../../etc/passwd")))
(is (= #{"~/.ssh/"}
(valid-paths/extract-paths-from-bash-command "ls ~/.ssh/")))
(is (= #{"/etc/passwd" "./innocent-file"}
(valid-paths/extract-paths-from-bash-command "ln -s /etc/passwd ./innocent-file"))))
(testing "Edge cases"
(is (= nil
(valid-paths/extract-paths-from-bash-command "")))
(is (= nil
(valid-paths/extract-paths-from-bash-command nil)))
(is (= #{}
(valid-paths/extract-paths-from-bash-command "ps aux")))
(is (= #{}
(valid-paths/extract-paths-from-bash-command "echo hello world")))))
(deftest preprocess-path-test
(testing "Home directory expansion"
(let [home (System/getProperty "user.home")]
(is (= (str home "/config")
(valid-paths/preprocess-path "~/config")))
(is (= (str home "/some/deep/path")
(valid-paths/preprocess-path "~/some/deep/path")))
(is (= home
(valid-paths/preprocess-path "~")))))
(testing "Other paths unchanged"
(is (= "/absolute/path"
(valid-paths/preprocess-path "/absolute/path")))
(is (= "./relative"
(valid-paths/preprocess-path "./relative")))
(is (= "../parent"
(valid-paths/preprocess-path "../parent")))
(is (= "."
(valid-paths/preprocess-path ".")))
(is (= ".."
(valid-paths/preprocess-path "..")))))
(deftest clojure-file?-test
(testing "Detect ClojureDart extension"
(is (valid-paths/clojure-file? "test.cljd"))
(is (valid-paths/clojure-file? "/path/to/file.cljd")))
(testing "Detect Basilisp extension"
(is (valid-paths/clojure-file? "test.lpy"))
(is (valid-paths/clojure-file? "/path/to/file.lpy")))
(testing "Detect Babashka shebang"
(let [tmp (io/file (System/getProperty "java.io.tmpdir") "bb-script.sh")]
(spit tmp "#!/usr/bin/env bb\n(println :hi)")
(is (valid-paths/clojure-file? (.getPath tmp)))
(.delete tmp)))
(testing "Regular bash script not detected"
(let [tmp (io/file (System/getProperty "java.io.tmpdir") "bash-script.sh")]
(spit tmp "#!/bin/bash\necho hi")
(is (not (valid-paths/clojure-file? (.getPath tmp))))
(.delete tmp))))
(deftest dash-to-underscore-correction-test
(let [test-dir (io/file (System/getProperty "java.io.tmpdir") "valid-paths-dash-test")
canonical-dir (.getCanonicalPath test-dir)
make-client (fn []
{:clojure-mcp.config/config
{:nrepl-user-dir canonical-dir
:allowed-directories [canonical-dir]}})]
(try
(.mkdirs test-dir)
(testing "Clojure file with dashes corrected to underscores when underscore version exists"
(let [underscore-file (io/file test-dir "core_stuff.clj")
_ (spit underscore-file "(ns core-stuff)")
dash-path (.getAbsolutePath (io/file test-dir "core-stuff.clj"))
result (valid-paths/validate-path-with-client dash-path (make-client))]
(is (= (.getCanonicalPath underscore-file) result))
(.delete underscore-file)))
(testing "Non-Clojure files (.java) do NOT get corrected"
(let [underscore-file (io/file test-dir "core_stuff.java")
_ (spit underscore-file "public class core_stuff {}")
dash-path (.getAbsolutePath (io/file test-dir "core-stuff.java"))
result (valid-paths/validate-path-with-client dash-path (make-client))]
(is (not= (.getCanonicalPath underscore-file) result))
(.delete underscore-file)))
(testing "Non-Clojure files (.py) do NOT get corrected"
(let [underscore-file (io/file test-dir "my_module.py")
_ (spit underscore-file "def hello(): pass")
dash-path (.getAbsolutePath (io/file test-dir "my-module.py"))
result (valid-paths/validate-path-with-client dash-path (make-client))]
(is (not= (.getCanonicalPath underscore-file) result))
(.delete underscore-file)))
(testing "File where dashed version exists is returned as-is"
(let [dash-file (io/file test-dir "core-stuff.clj")
_ (spit dash-file "(ns core-stuff)")
dash-path (.getAbsolutePath dash-file)
result (valid-paths/validate-path-with-client dash-path (make-client))]
(is (= (.getCanonicalPath dash-file) result))
(.delete dash-file)))
(testing "Directory components with dashes are NOT changed, only filename"
(let [dashed-dir (io/file test-dir "my-cool-dir")
_ (.mkdirs dashed-dir)
underscore-file (io/file dashed-dir "my_file.clj")
_ (spit underscore-file "(ns my-cool-dir.my-file)")
dash-path (.getAbsolutePath (io/file dashed-dir "my-file.clj"))
result (valid-paths/validate-path-with-client dash-path (make-client))]
(is (= (.getCanonicalPath underscore-file) result))
(is (str/includes? result "my-cool-dir")
"Directory dashes should be preserved")
(.delete underscore-file)
(.delete dashed-dir)))
(testing ".cljs extension works"
(let [underscore-file (io/file test-dir "my_component.cljs")
_ (spit underscore-file "(ns my-component)")
dash-path (.getAbsolutePath (io/file test-dir "my-component.cljs"))
result (valid-paths/validate-path-with-client dash-path (make-client))]
(is (= (.getCanonicalPath underscore-file) result))
(.delete underscore-file)))
(testing ".cljc extension works"
(let [underscore-file (io/file test-dir "shared_utils.cljc")
_ (spit underscore-file "(ns shared-utils)")
dash-path (.getAbsolutePath (io/file test-dir "shared-utils.cljc"))
result (valid-paths/validate-path-with-client dash-path (make-client))]
(is (= (.getCanonicalPath underscore-file) result))
(.delete underscore-file)))
(testing ".cljd extension works"
(let [underscore-file (io/file test-dir "my_widget.cljd")
_ (spit underscore-file "(ns my-widget)")
dash-path (.getAbsolutePath (io/file test-dir "my-widget.cljd"))
result (valid-paths/validate-path-with-client dash-path (make-client))]
(is (= (.getCanonicalPath underscore-file) result))
(.delete underscore-file)))
(testing ".bb files are NOT corrected"
(let [underscore-file (io/file test-dir "my_script.bb")
_ (spit underscore-file "(println :hello)")
dash-path (.getAbsolutePath (io/file test-dir "my-script.bb"))
result (valid-paths/validate-path-with-client dash-path (make-client))]
(is (not= (.getCanonicalPath underscore-file) result))
(.delete underscore-file)))
(testing "When neither dashed nor underscored file exists, returns original validated path"
(let [dash-path (.getAbsolutePath (io/file test-dir "no-such-file.clj"))
result (valid-paths/validate-path-with-client dash-path (make-client))]
(is (string? result))
(is (not (valid-paths/path-exists? result)))))
(testing "Case-insensitive extension matching"
(let [underscore-file (io/file test-dir "MY_THING.CLJ")
_ (spit underscore-file "(ns my-thing)")
dash-path (.getAbsolutePath (io/file test-dir "MY-THING.CLJ"))
result (valid-paths/validate-path-with-client dash-path (make-client))]
(is (= (.getCanonicalPath underscore-file) result))
(.delete underscore-file)))
(testing "Reverse direction: underscore requested, only dash exists - should NOT correct"
(let [dash-file (io/file test-dir "core-stuff.clj")
_ (spit dash-file "(ns core-stuff)")
underscore-path (.getAbsolutePath (io/file test-dir "core_stuff.clj"))
result (valid-paths/validate-path-with-client underscore-path (make-client))]
(is (not= (.getCanonicalPath dash-file) result))
(.delete dash-file)))
(testing "Mixed dashes and underscores in filename - corrects all dashes"
(let [underscore-file (io/file test-dir "my_cool_thing.clj")
_ (spit underscore-file "(ns my-cool-thing)")
dash-path (.getAbsolutePath (io/file test-dir "my-cool_thing.clj"))
result (valid-paths/validate-path-with-client dash-path (make-client))]
(is (= (.getCanonicalPath underscore-file) result))
(.delete underscore-file)))
(finally
(when (.exists test-dir)
(doseq [file (reverse (file-seq test-dir))]
(.delete file)))))))
(deftest validate-bash-command-paths-test
(let [test-dir (.getCanonicalPath (io/file (System/getProperty "java.io.tmpdir")))
home-dir (System/getProperty "user.home")]
(testing "Valid paths"
(let [result (valid-paths/validate-bash-command-paths
"ls ."
test-dir
[test-dir])]
(is (set? result))
(is (contains? result test-dir))))
(testing "Home directory expansion"
(let [result (valid-paths/validate-bash-command-paths
"cat ~/.bashrc"
test-dir
[home-dir])]
(is (contains? result (str home-dir "/.bashrc")))))
(testing "Commands with no paths"
(let [result (valid-paths/validate-bash-command-paths
"ps aux"
test-dir
[test-dir])]
(is (= #{} result))))
(testing "Invalid paths throw exceptions"
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Invalid paths in bash command"
(valid-paths/validate-bash-command-paths
"cat /etc/passwd"
test-dir
[test-dir]))))
(testing "Mixed valid and invalid paths"
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Invalid paths in bash command"
(valid-paths/validate-bash-command-paths
"cp ./file /etc/passwd"
test-dir
[test-dir]))))
(testing "Complex commands with quotes"
(let [result (valid-paths/validate-bash-command-paths
"find . -name '*.txt' > \"./results with spaces.txt\""
test-dir
[test-dir])
expected-file (str test-dir "/results with spaces.txt")]
(is (contains? result test-dir))
(is (contains? result expected-file))))
(testing "Directory traversal attempts"
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Invalid paths in bash command"
(valid-paths/validate-bash-command-paths
"cat ../../../../etc/passwd"
test-dir
[test-dir]))))))