|
2 | 2 |
|
3 | 3 | // npx jest src/utils/__tests__/command-validation.test.ts |
4 | 4 |
|
5 | | -import { parseCommand, isAllowedSingleCommand, validateCommand } from "../command-validation" |
| 5 | +import { |
| 6 | + parseCommand, |
| 7 | + isAllowedSingleCommand, |
| 8 | + isBlacklistedSingleCommand, |
| 9 | + validateCommand, |
| 10 | +} from "../command-validation" |
6 | 11 |
|
7 | 12 | describe("Command Validation", () => { |
8 | 13 | describe("parseCommand", () => { |
@@ -68,6 +73,33 @@ describe("Command Validation", () => { |
68 | 73 | }) |
69 | 74 | }) |
70 | 75 |
|
| 76 | + describe("isBlacklistedSingleCommand", () => { |
| 77 | + const blacklistedCommands = ["rm ", "sudo ", "del "] |
| 78 | + |
| 79 | + it("matches blacklisted commands case-insensitively", () => { |
| 80 | + expect(isBlacklistedSingleCommand("RM -rf /", blacklistedCommands)).toBe(true) |
| 81 | + expect(isBlacklistedSingleCommand("sudo apt install", blacklistedCommands)).toBe(true) |
| 82 | + expect(isBlacklistedSingleCommand("DEL file.txt", blacklistedCommands)).toBe(true) |
| 83 | + }) |
| 84 | + |
| 85 | + it("matches blacklisted command prefixes", () => { |
| 86 | + expect(isBlacklistedSingleCommand("rm -rf /home", blacklistedCommands)).toBe(true) |
| 87 | + expect(isBlacklistedSingleCommand("sudo rm file", blacklistedCommands)).toBe(true) |
| 88 | + expect(isBlacklistedSingleCommand("del *.txt", blacklistedCommands)).toBe(true) |
| 89 | + }) |
| 90 | + |
| 91 | + it("allows non-blacklisted commands", () => { |
| 92 | + expect(isBlacklistedSingleCommand("npm test", blacklistedCommands)).toBe(false) |
| 93 | + expect(isBlacklistedSingleCommand("echo hello", blacklistedCommands)).toBe(false) |
| 94 | + expect(isBlacklistedSingleCommand("git status", blacklistedCommands)).toBe(false) |
| 95 | + }) |
| 96 | + |
| 97 | + it("handles empty inputs", () => { |
| 98 | + expect(isBlacklistedSingleCommand("", blacklistedCommands)).toBe(false) |
| 99 | + expect(isBlacklistedSingleCommand("rm file", [])).toBe(false) |
| 100 | + }) |
| 101 | + }) |
| 102 | + |
71 | 103 | describe("validateCommand", () => { |
72 | 104 | const allowedCommands = ["npm test", "npm run", "echo", "Select-String"] |
73 | 105 |
|
@@ -121,6 +153,33 @@ describe("Command Validation", () => { |
121 | 153 | expect(validateCommand("npm test $(echo dangerous)", wildcardAllowedCommands)).toBe(true) |
122 | 154 | expect(validateCommand("npm test `rm -rf /`", wildcardAllowedCommands)).toBe(true) |
123 | 155 | }) |
| 156 | + |
| 157 | + it("blocks blacklisted commands even if allowed", () => { |
| 158 | + const allowedWithBlacklisted = ["npm test", "npm run", "echo", "rm "] |
| 159 | + const blacklistedCommands = ["rm ", "sudo ", "del "] |
| 160 | + expect(validateCommand("rm -rf /", allowedWithBlacklisted, blacklistedCommands)).toBe(false) |
| 161 | + expect(validateCommand("sudo apt install", allowedWithBlacklisted, blacklistedCommands)).toBe(false) |
| 162 | + expect(validateCommand("npm test", allowedWithBlacklisted, blacklistedCommands)).toBe(true) |
| 163 | + }) |
| 164 | + |
| 165 | + it("validates chained commands with blacklist", () => { |
| 166 | + const blacklistedCommands = ["rm ", "sudo ", "del "] |
| 167 | + expect(validateCommand("npm test && npm run build", allowedCommands, blacklistedCommands)).toBe(true) |
| 168 | + expect(validateCommand("npm test && rm file", allowedCommands, blacklistedCommands)).toBe(false) |
| 169 | + expect(validateCommand("npm test && dangerous", allowedCommands, blacklistedCommands)).toBe(false) |
| 170 | + }) |
| 171 | + |
| 172 | + it("handles wildcard allowed commands with blacklist", () => { |
| 173 | + const blacklistedCommands = ["rm ", "sudo ", "del "] |
| 174 | + expect(validateCommand("any command", ["*"], blacklistedCommands)).toBe(true) |
| 175 | + expect(validateCommand("rm -rf /", ["*"], blacklistedCommands)).toBe(false) // Still blocked by blacklist |
| 176 | + expect(validateCommand("sudo apt install", ["*"], blacklistedCommands)).toBe(false) // Still blocked by blacklist |
| 177 | + }) |
| 178 | + |
| 179 | + it("works without blacklist parameter (backward compatibility)", () => { |
| 180 | + expect(validateCommand("npm test", allowedCommands)).toBe(true) |
| 181 | + expect(validateCommand("dangerous", allowedCommands)).toBe(false) |
| 182 | + }) |
124 | 183 | }) |
125 | 184 | }) |
126 | 185 |
|
|
0 commit comments