guarded-deletion.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. import { describe, expect, it } from "vitest";
  2. import { matchDangerousCommand } from "../src/tool/handlers/guarded/command.js";
  3. import { literalPosixWord } from "../src/tool/handlers/guarded/word.js";
  4. import { analyzeCmdCommand } from "../src/tool/handlers/guarded/cmd.js";
  5. describe("Guarded deletion catalog", () => {
  6. it.each(["rm first --help", "rm first --version", "rm first -f", "rm first --"])(
  7. "counts BSD rm trailing option-shaped filenames: %s",
  8. (command) => {
  9. expect(matchDangerousCommand(command, "posix")).toMatchObject({
  10. status: "matched",
  11. ruleId: "safety.bash.remove-critical-path",
  12. });
  13. },
  14. );
  15. it.each(["posix", "git-bash"] as const)("%s recursive and syntactically batch rm", (dialect) => {
  16. for (const command of [
  17. "rm -r dir",
  18. "rm -R dir",
  19. "rm --recursive dir",
  20. "rm -ri dir",
  21. "rm -r --help",
  22. "rm a b",
  23. 'rm "one file" "two files"',
  24. "rm -- -r -f",
  25. "rm *.log",
  26. "rm file?.txt",
  27. "rm [ab].txt",
  28. 'rm "prefix"*.log',
  29. "rm -f *.log",
  30. "rm -r --unknown dir",
  31. "env A=b rm -r dir",
  32. "time -o timing.txt rm a b",
  33. "sudo -u root rm *.log",
  34. "echo ready; rm a b",
  35. "true && rm -r dir",
  36. "false || rm *.log",
  37. "rm a b | cat",
  38. "rm a b > output",
  39. "rm a b # keep comment",
  40. ])
  41. expect(matchDangerousCommand(command, dialect), command).toMatchObject({
  42. status: "matched",
  43. ruleId: "safety.bash.remove-critical-path",
  44. });
  45. for (const command of [
  46. "rm one",
  47. "rm -f one",
  48. 'rm "one file"',
  49. "rm -- -r",
  50. "rm --help",
  51. 'rm "*.log"',
  52. "rm '*.log'",
  53. "rm \\*.log",
  54. "rm file\\?.txt",
  55. "rm \\[ab].txt",
  56. "rm $TARGET",
  57. 'rm "$TARGET"',
  58. "rm {a,b}",
  59. "rm $(echo target)",
  60. "rm $(echo *)",
  61. "rm ${TARGET:-*}",
  62. 'echo "rm a b"',
  63. "echo ok # rm a b",
  64. "echo ok > rm",
  65. "rmdir empty",
  66. "rmdir /s folder",
  67. "del file",
  68. "xargs rm -r",
  69. "npx rimraf dir",
  70. "sh -c 'rm -r dir'",
  71. ])
  72. expect(matchDangerousCommand(command, dialect).status, command).not.toBe("matched");
  73. });
  74. it.each(["-d", "-x", "-d -x"])("BSD find %s keeps the starting-path phase", (flags) => {
  75. for (const command of [
  76. `find ${flags} . -type f -delete`,
  77. `find ${flags} "space dir" another -delete`,
  78. `time -o timing.txt find ${flags} . -delete`,
  79. ])
  80. expect(matchDangerousCommand(command, "posix"), command).toMatchObject({
  81. status: "matched",
  82. ruleId: "safety.bash.find-delete",
  83. });
  84. for (const command of [
  85. `find ${flags} . -type f -print`,
  86. `find ${flags} . -name '-delete'`,
  87. `find ${flags} . -exec echo -delete \\;`,
  88. ])
  89. expect(matchDangerousCommand(command, "posix"), command).toEqual({ status: "notMatched" });
  90. expect(matchDangerousCommand(`find ${flags} . -unknown -delete`, "posix")).toEqual({
  91. status: "unsupported",
  92. });
  93. });
  94. it.each(["posix", "git-bash"] as const)("%s find action arity", (dialect) => {
  95. for (const command of [
  96. "find . -delete",
  97. "find . -type f -name '*.log' -delete",
  98. "find . \\( -name a -o -name b \\) -delete",
  99. "find . -false -a -delete",
  100. "find . -exec echo {} \\; -delete",
  101. "find . -execdir echo {} + -delete",
  102. "find . -ok echo {} \\; -delete",
  103. "find . -okdir echo {} \\; -delete",
  104. "find -L . -maxdepth 2 -mtime +1 -delete",
  105. "find . -delete -unknown value",
  106. "time -o file find . -delete",
  107. "find . -unknown value; rm -r dir",
  108. ])
  109. expect(matchDangerousCommand(command, dialect).status, command).toBe("matched");
  110. for (const command of [
  111. "find . -name '-delete'",
  112. "find . -path -delete",
  113. "find . -printf '-delete'",
  114. "find . -fprintf out '-delete'",
  115. "find . -fprint -delete",
  116. "find . -exec echo -delete \\;",
  117. "find . -exec rm -rf {} +",
  118. "find . -okdir echo -delete \\;",
  119. "find . -print",
  120. "find . -name '*.log'",
  121. ])
  122. expect(matchDangerousCommand(command, dialect).status, command).not.toBe("matched");
  123. for (const command of ["find . -unknown -delete", "find . -name", "find . -exec echo -delete"])
  124. expect(matchDangerousCommand(command, dialect), command).toEqual({ status: "unsupported" });
  125. });
  126. it.each([
  127. ["rd /s folder", "cmd-remove-tree"],
  128. ['RMDIR /S /Q "space dir"', "cmd-remove-tree"],
  129. ["rmdir folder /s", "cmd-remove-tree"],
  130. ["rd /s folder /?", "cmd-remove-tree"],
  131. ["rd /s/q folder", "cmd-remove-tree"],
  132. ["rmdir /Q/S folder", "cmd-remove-tree"],
  133. ["rd target/s/q", "cmd-remove-tree"],
  134. ["rd/s/q target", "cmd-remove-tree"],
  135. ['RMDIR/Q/S "space dir"', "cmd-remove-tree"],
  136. ["rd target^/s/q", "cmd-remove-tree"],
  137. ["del/q target\\one.txt", "cmd-delete-files"],
  138. ["erase/P file", "cmd-delete-files"],
  139. [">rem rd/s/q target 2>nul", "cmd-remove-tree"],
  140. ["echo ready && rd target/s/q | more", "cmd-remove-tree"],
  141. ["del file", "cmd-delete-files"],
  142. ['ERASE /P "space dir"', "cmd-delete-files"],
  143. ["del /s /q *.txt", "cmd-delete-files"],
  144. ["erase dir /A:-R", "cmd-delete-files"],
  145. ["del file /?", "cmd-delete-files"],
  146. [">rem rd /s dir", "cmd-remove-tree"],
  147. ["echo ready & del file", "cmd-delete-files"],
  148. ["echo ready && rd /s dir", "cmd-remove-tree"],
  149. ["del file 2>nul", "cmd-delete-files"],
  150. ["rd /s dir | more", "cmd-remove-tree"],
  151. ])("CMD deletion: %s", (command, rule) => {
  152. expect(matchDangerousCommand(command, "cmd")).toMatchObject({
  153. status: "matched",
  154. ruleId: `safety.bash.${rule}`,
  155. });
  156. });
  157. it.each([
  158. "rd dir",
  159. "rmdir /?",
  160. "rd /?",
  161. "del",
  162. "del /q /s",
  163. "del /?",
  164. "erase /?",
  165. "rem del file",
  166. 'echo "rd /s dir"',
  167. "find file -delete",
  168. 'rd "target/s/q"',
  169. "rd target > output/s/q",
  170. "rd/?",
  171. "del/q",
  172. "del/?",
  173. "echo rd/s/q target",
  174. "rem rd/s/q target",
  175. ])("CMD non-deletion: %s", (command) => {
  176. expect(matchDangerousCommand(command, "cmd").status).not.toBe("matched");
  177. });
  178. it("keeps CMD builtin switch splitting local and source positions exact", () => {
  179. const command = "echo ready & rd/s/q target";
  180. expect(matchDangerousCommand(command, "cmd")).toMatchObject({
  181. start: command.indexOf("rd/"),
  182. end: command.length,
  183. });
  184. for (const source of ["echo target/s/q", "C:/bin/tool.exe target/s/q", "git -C /s status"])
  185. expect(analyzeCmdCommand(source).commands[0]?.map((word) => word.value)).toEqual(
  186. source.split(" "),
  187. );
  188. expect(analyzeCmdCommand('rd "target/s/q"').commands[0]?.map((word) => word.value)).toEqual([
  189. "rd",
  190. "target/s/q",
  191. ]);
  192. });
  193. it("recognizes Git Bash Robocopy drive paths without weakening switch arity", () => {
  194. for (const command of [
  195. "robocopy /c/src /c/dst /MIR",
  196. "env MSYS2_ARG_CONV_EXCL=/MIR robocopy /c/src /c/dst /MIR",
  197. 'robocopy "/c/space src" "/d/space dst" /PURGE',
  198. "robocopy /MIR /c/src /d/dst /R:0 /W:0",
  199. ])
  200. expect(matchDangerousCommand(command, "git-bash"), command).toMatchObject({
  201. status: "matched",
  202. ruleId: "safety.bash.robocopy-delete",
  203. });
  204. for (const command of [
  205. "robocopy /c/src /c/dst /MIR /L",
  206. "robocopy /c/src /c/dst /PURGE /XD /c/excluded /L",
  207. "robocopy /c/src /c/dst /LOG:/MIR",
  208. "robocopy /c/src /MIR",
  209. ])
  210. expect(matchDangerousCommand(command, "git-bash").status, command).not.toBe("matched");
  211. expect(matchDangerousCommand("robocopy /unknown /c/src /c/dst /MIR", "git-bash")).toEqual({
  212. status: "unsupported",
  213. });
  214. expect(matchDangerousCommand("robocopy /JOB:job /c/src /c/dst /MIR", "git-bash")).toEqual({
  215. status: "unsupported",
  216. });
  217. expect(matchDangerousCommand("robocopy /c/src /c/dst /MIR", "cmd").status).toBe("unsupported");
  218. });
  219. it.each(["cmd", "git-bash"] as const)("%s Robocopy option ownership", (dialect) => {
  220. for (const command of [
  221. "robocopy src dst /MIR",
  222. "robocopy /MIR src dst",
  223. "robocopy src /PURGE dst",
  224. "ROBOCOPY.EXE src dst /purge",
  225. 'robocopy "space src" "space dst" /E /PURGE',
  226. "robocopy src dst /MIR /LOG:out",
  227. "robocopy src dst /MIR /unknown /L",
  228. "robocopy src dst /MIR /JOB:job",
  229. "robocopy src dst /XF /MIR",
  230. ])
  231. expect(matchDangerousCommand(command, dialect), command).toMatchObject({
  232. status: "matched",
  233. ruleId: "safety.bash.robocopy-delete",
  234. });
  235. for (const command of [
  236. "robocopy src dst /E",
  237. "robocopy src dst /MIR /L",
  238. "robocopy src dst /l /PURGE",
  239. "robocopy src dst /LOG:/MIR",
  240. "robocopy src dst /MIR /XF /L",
  241. "robocopy src dst /MIR /XD /L",
  242. "robocopy /MIR dst",
  243. "robocopy src /PURGE",
  244. "robocopy /?",
  245. "robocopy src dst /JOB:job",
  246. ])
  247. expect(matchDangerousCommand(command, dialect).status, command).not.toBe("matched");
  248. expect(matchDangerousCommand("robocopy src dst /unknown /L /MIR", dialect).status).toBe(
  249. "unsupported",
  250. );
  251. });
  252. it("does not treat POSIX slash paths as Windows switches", () => {
  253. expect(matchDangerousCommand("robocopy src dst /MIR", "posix").status).toBe("notMatched");
  254. expect(matchDangerousCommand("rm /s", "posix").status).toBe("notMatched");
  255. });
  256. it.each(["posix", "git-bash", "cmd"] as const)("%s Git clean effective force", (dialect) => {
  257. for (const command of [
  258. "git clean -f",
  259. "git clean -fx",
  260. "git clean -fX",
  261. "git clean --force",
  262. "git clean -nf --no-dry-run",
  263. "git clean -f -e --dry-run",
  264. ])
  265. expect(matchDangerousCommand(command, dialect), command).toMatchObject({
  266. status: "matched",
  267. ruleId: "safety.bash.git-clean-force",
  268. });
  269. for (const command of [
  270. "git clean",
  271. "git clean -fn",
  272. "git clean -f --dry-run",
  273. "git clean -f --help",
  274. "git clean -n --no-dry-run -f -n",
  275. ])
  276. expect(matchDangerousCommand(command, dialect).status, command).not.toBe("matched");
  277. });
  278. it("keeps literal, dynamic and unquoted glob as separate word facts", () => {
  279. for (const [source, dynamic, hasUnquotedGlob] of [
  280. ["*.log", true, true],
  281. ['"*.log"', false, false],
  282. ["\\*.log", false, false],
  283. ["$TARGET", true, false],
  284. ["{a,b}", true, false],
  285. ["file?.txt", true, true],
  286. ] as const)
  287. expect(literalPosixWord(source, 3, 3 + source.length)).toMatchObject({
  288. dynamic,
  289. hasUnquotedGlob,
  290. start: 3,
  291. end: 3 + source.length,
  292. });
  293. });
  294. });