-
Notifications
You must be signed in to change notification settings - Fork 355
Expand file tree
/
Copy pathsecurity.test.ts
More file actions
591 lines (501 loc) · 19.1 KB
/
security.test.ts
File metadata and controls
591 lines (501 loc) · 19.1 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/**
* Security Module — Pattern Matching Tests
*
* Tests for parseBashPattern, globToRegex, matchesAnyPattern,
* chained command splitting, shell-escape scanning, and file path evaluation.
*/
import { describe, test, beforeAll, afterAll } from "vitest";
import { strict as assert } from "node:assert";
import { writeFileSync, mkdirSync, rmSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
import {
parseBashPattern,
globToRegex,
matchesAnyPattern,
splitChainedCommands,
readBashPolicies,
evaluateCommand,
evaluateCommandDenyOnly,
parseToolPattern,
readToolDenyPatterns,
fileGlobToRegex,
evaluateFilePath,
extractShellCommands,
} from "../build/security.js";
describe("parseBashPattern", () => {
test("parseBashPattern: extracts glob from Bash(glob)", () => {
assert.equal(parseBashPattern("Bash(sudo *)"), "sudo *");
});
test("parseBashPattern: handles colon format", () => {
assert.equal(parseBashPattern("Bash(tree:*)"), "tree:*");
});
test("parseBashPattern: returns null for non-Bash", () => {
assert.equal(parseBashPattern("Read(.env)"), null);
});
test("parseBashPattern: returns null for malformed", () => {
assert.equal(parseBashPattern("Bash("), null);
assert.equal(parseBashPattern("notapattern"), null);
});
});
describe("globToRegex: word boundary tests from SECURITY.md", () => {
test("glob: 'ls *' matches 'ls -la'", () => {
assert.ok(globToRegex("ls *").test("ls -la"));
});
test("glob: 'ls *' does NOT match 'lsof -i'", () => {
assert.ok(!globToRegex("ls *").test("lsof -i"));
});
test("glob: 'ls*' matches 'lsof -i' (prefix)", () => {
assert.ok(globToRegex("ls*").test("lsof -i"));
});
test("glob: 'ls*' matches 'ls -la'", () => {
assert.ok(globToRegex("ls*").test("ls -la"));
});
test("glob: 'git *' matches 'git commit -m msg'", () => {
assert.ok(globToRegex("git *").test('git commit -m "msg"'));
});
test("glob: '* commit *' matches 'git commit -m msg'", () => {
assert.ok(globToRegex("* commit *").test('git commit -m "msg"'));
});
});
describe("globToRegex: colon separator", () => {
test("glob: 'tree:*' matches 'tree' (no args)", () => {
assert.ok(globToRegex("tree:*").test("tree"));
});
test("glob: 'tree:*' matches 'tree -a'", () => {
assert.ok(globToRegex("tree:*").test("tree -a"));
});
test("glob: 'tree:*' does NOT match 'treemap'", () => {
assert.ok(!globToRegex("tree:*").test("treemap"));
});
});
describe("globToRegex: real-world deny patterns", () => {
test("glob: 'sudo *' matches 'sudo apt install'", () => {
assert.ok(globToRegex("sudo *").test("sudo apt install"));
});
test("glob: 'sudo *' does NOT match 'sudoedit'", () => {
assert.ok(!globToRegex("sudo *").test("sudoedit"));
});
test("glob: 'rm -rf /*' matches 'rm -rf /etc'", () => {
assert.ok(globToRegex("rm -rf /*").test("rm -rf /etc"));
});
test("glob: 'chmod -R 777 *' matches 'chmod -R 777 /tmp'", () => {
assert.ok(globToRegex("chmod -R 777 *").test("chmod -R 777 /tmp"));
});
});
describe("globToRegex: case sensitivity", () => {
test("glob: case-insensitive 'dir *' matches 'DIR /W'", () => {
assert.ok(globToRegex("dir *", true).test("DIR /W"));
});
test("glob: case-sensitive 'dir *' does NOT match 'DIR /W'", () => {
assert.ok(!globToRegex("dir *", false).test("DIR /W"));
});
});
describe("matchesAnyPattern", () => {
test("matchesAnyPattern: returns matching pattern on hit", () => {
const result = matchesAnyPattern(
"sudo apt install",
["Bash(git:*)", "Bash(sudo *)"],
false,
);
assert.equal(result, "Bash(sudo *)");
});
test("matchesAnyPattern: returns null on miss", () => {
const result = matchesAnyPattern(
"npm install",
["Bash(sudo *)", "Bash(rm -rf /*)"],
false,
);
assert.equal(result, null);
});
});
describe("Chained Command Splitting", () => {
test("splitChainedCommands: simple && chain", () => {
const parts = splitChainedCommands("echo hello && sudo rm -rf /");
assert.deepEqual(parts, ["echo hello", "sudo rm -rf /"]);
});
test("splitChainedCommands: || chain", () => {
const parts = splitChainedCommands("test -f /tmp/x || sudo reboot");
assert.deepEqual(parts, ["test -f /tmp/x", "sudo reboot"]);
});
test("splitChainedCommands: semicolon chain", () => {
const parts = splitChainedCommands("cd /tmp; sudo rm -rf /");
assert.deepEqual(parts, ["cd /tmp", "sudo rm -rf /"]);
});
test("splitChainedCommands: pipe chain", () => {
const parts = splitChainedCommands("cat /etc/passwd | sudo tee /tmp/out");
assert.deepEqual(parts, ["cat /etc/passwd", "sudo tee /tmp/out"]);
});
test("splitChainedCommands: multiple operators", () => {
const parts = splitChainedCommands("echo a && echo b; sudo rm -rf /");
assert.deepEqual(parts, ["echo a", "echo b", "sudo rm -rf /"]);
});
test("splitChainedCommands: respects double quotes", () => {
const parts = splitChainedCommands('echo "hello && world"');
assert.deepEqual(parts, ['echo "hello && world"']);
});
test("splitChainedCommands: respects single quotes", () => {
const parts = splitChainedCommands("echo 'test; value'");
assert.deepEqual(parts, ["echo 'test; value'"]);
});
test("splitChainedCommands: single command unchanged", () => {
const parts = splitChainedCommands("git status");
assert.deepEqual(parts, ["git status"]);
});
});
describe("Chained Command Evaluation", () => {
let chainTmpBase: string;
let chainGlobalPath: string;
beforeAll(() => {
chainTmpBase = join(tmpdir(), `chain-test-${Date.now()}`);
const chainGlobalDir = join(chainTmpBase, "global-home", ".claude");
chainGlobalPath = join(chainGlobalDir, "settings.json");
mkdirSync(chainGlobalDir, { recursive: true });
writeFileSync(
chainGlobalPath,
JSON.stringify({
permissions: {
deny: ["Bash(sudo *)", "Bash(rm -rf /*)"],
allow: ["Bash(echo:*)", "Bash(git:*)"],
},
}),
);
});
afterAll(() => {
rmSync(chainTmpBase, { recursive: true, force: true });
});
test("evaluateCommand: detects 'sudo' in 'echo ok && sudo rm -rf /'", () => {
const policies = readBashPolicies(undefined, chainGlobalPath);
const result = evaluateCommand("echo ok && sudo rm -rf /", policies, false);
assert.equal(result.decision, "deny");
assert.equal(result.matchedPattern, "Bash(sudo *)");
});
test("evaluateCommand: detects 'sudo' after semicolon", () => {
const policies = readBashPolicies(undefined, chainGlobalPath);
const result = evaluateCommand("cd /tmp; sudo apt install vim", policies, false);
assert.equal(result.decision, "deny");
});
test("evaluateCommand: detects 'rm -rf' in piped chain", () => {
const policies = readBashPolicies(undefined, chainGlobalPath);
const result = evaluateCommand("cat file | rm -rf /etc", policies, false);
assert.equal(result.decision, "deny");
});
test("evaluateCommandDenyOnly: detects chained deny", () => {
const policies = readBashPolicies(undefined, chainGlobalPath);
const result = evaluateCommandDenyOnly("echo hello && sudo rm -rf /", policies, false);
assert.equal(result.decision, "deny");
});
test("evaluateCommandDenyOnly: allows safe chained commands", () => {
const policies = readBashPolicies(undefined, chainGlobalPath);
const result = evaluateCommandDenyOnly("echo hello && git status", policies, false);
assert.equal(result.decision, "allow");
});
});
describe("Settings Reader", () => {
let tmpBase: string;
let globalSettingsPath: string;
let projectDir: string;
beforeAll(() => {
tmpBase = join(tmpdir(), `security-test-${Date.now()}`);
const globalDir = join(tmpBase, "global-home", ".claude");
globalSettingsPath = join(globalDir, "settings.json");
projectDir = join(tmpBase, "project");
const projectClaudeDir = join(projectDir, ".claude");
mkdirSync(globalDir, { recursive: true });
mkdirSync(projectClaudeDir, { recursive: true });
writeFileSync(
globalSettingsPath,
JSON.stringify({
permissions: {
allow: ["Bash(npm:*)", "Read(.env)"],
deny: ["Bash(sudo *)"],
},
}),
);
writeFileSync(
join(projectClaudeDir, "settings.json"),
JSON.stringify({
permissions: {
deny: ["Bash(npm publish)"],
allow: [],
},
}),
);
});
afterAll(() => {
rmSync(tmpBase, { recursive: true, force: true });
});
test("readBashPolicies: reads global only when no projectDir", () => {
const policies = readBashPolicies(undefined, globalSettingsPath);
assert.equal(policies.length, 1, "should have 1 policy (global)");
assert.deepEqual(policies[0].allow, ["Bash(npm:*)"]);
assert.deepEqual(policies[0].deny, ["Bash(sudo *)"]);
});
test("readBashPolicies: reads project + global with precedence", () => {
const policies = readBashPolicies(projectDir, globalSettingsPath);
assert.equal(policies.length, 2, "should have 2 policies");
assert.deepEqual(policies[0].deny, ["Bash(npm publish)"]);
assert.deepEqual(policies[1].allow, ["Bash(npm:*)"]);
assert.deepEqual(policies[1].deny, ["Bash(sudo *)"]);
});
test("readBashPolicies: missing files produce empty policies", () => {
const policies = readBashPolicies("/nonexistent/path", globalSettingsPath);
assert.equal(policies.length, 1);
});
test("evaluateCommand: global allow matches", () => {
const policies = readBashPolicies(undefined, globalSettingsPath);
const result = evaluateCommand("npm install", policies, false);
assert.equal(result.decision, "allow");
assert.equal(result.matchedPattern, "Bash(npm:*)");
});
test("evaluateCommand: global deny beats allow", () => {
const policies = readBashPolicies(undefined, globalSettingsPath);
const result = evaluateCommand("sudo npm install", policies, false);
assert.equal(result.decision, "deny");
assert.equal(result.matchedPattern, "Bash(sudo *)");
});
test("evaluateCommand: local deny overrides global allow", () => {
const policies = readBashPolicies(projectDir, globalSettingsPath);
const result = evaluateCommand("npm publish", policies, false);
assert.equal(result.decision, "deny");
assert.equal(result.matchedPattern, "Bash(npm publish)");
});
test("evaluateCommand: no match returns ask", () => {
const policies = readBashPolicies(projectDir, globalSettingsPath);
const result = evaluateCommand("python script.py", policies, false);
assert.equal(result.decision, "ask");
assert.equal(result.matchedPattern, undefined);
});
test("evaluateCommandDenyOnly: denied command", () => {
const policies = readBashPolicies(undefined, globalSettingsPath);
const result = evaluateCommandDenyOnly("sudo rm -rf /", policies, false);
assert.equal(result.decision, "deny");
assert.equal(result.matchedPattern, "Bash(sudo *)");
});
test("evaluateCommandDenyOnly: non-denied returns allow", () => {
const policies = readBashPolicies(undefined, globalSettingsPath);
const result = evaluateCommandDenyOnly("npm install", policies, false);
assert.equal(result.decision, "allow");
assert.equal(result.matchedPattern, undefined);
});
});
describe("Tool Pattern Parsing", () => {
test("parseToolPattern: Read(.env)", () => {
const result = parseToolPattern("Read(.env)");
assert.deepEqual(result, { tool: "Read", glob: ".env" });
});
test("parseToolPattern: Grep(**/*.ts)", () => {
const result = parseToolPattern("Grep(**/*.ts)");
assert.deepEqual(result, { tool: "Grep", glob: "**/*.ts" });
});
test("parseToolPattern: Bash(sudo *)", () => {
const result = parseToolPattern("Bash(sudo *)");
assert.deepEqual(result, { tool: "Bash", glob: "sudo *" });
});
test("parseToolPattern: returns null for bare string", () => {
assert.equal(parseToolPattern("notapattern"), null);
});
});
describe("readToolDenyPatterns", () => {
let toolDenyTmpBase: string;
let toolDenyGlobalPath: string;
beforeAll(() => {
toolDenyTmpBase = join(tmpdir(), `tool-deny-test-${Date.now()}`);
const toolDenyGlobalDir = join(toolDenyTmpBase, "global-home", ".claude");
toolDenyGlobalPath = join(toolDenyGlobalDir, "settings.json");
mkdirSync(toolDenyGlobalDir, { recursive: true });
writeFileSync(
toolDenyGlobalPath,
JSON.stringify({
permissions: {
deny: [
"Read(.env)",
"Read(**/.env)",
"Read(**/*credentials*)",
"Bash(sudo *)",
"Bash(rm -rf /*)",
],
allow: [],
},
}),
);
});
afterAll(() => {
rmSync(toolDenyTmpBase, { recursive: true, force: true });
});
test("readToolDenyPatterns: returns only Read globs for Read", () => {
const result = readToolDenyPatterns("Read", undefined, toolDenyGlobalPath);
assert.equal(result.length, 1, "should have 1 settings file");
assert.deepEqual(result[0], [".env", "**/.env", "**/*credentials*"]);
});
test("readToolDenyPatterns: returns only Bash globs for Bash", () => {
const result = readToolDenyPatterns("Bash", undefined, toolDenyGlobalPath);
assert.equal(result.length, 1);
assert.deepEqual(result[0], ["sudo *", "rm -rf /*"]);
});
test("readToolDenyPatterns: returns empty for Grep (no patterns)", () => {
const result = readToolDenyPatterns("Grep", undefined, toolDenyGlobalPath);
assert.equal(result.length, 1);
assert.deepEqual(result[0], []);
});
});
describe("File Glob Matching", () => {
test("fileGlobToRegex: '.env' matches exactly '.env'", () => {
assert.ok(fileGlobToRegex(".env").test(".env"));
});
test("fileGlobToRegex: '.env' does not match 'src/.env'", () => {
assert.ok(!fileGlobToRegex(".env").test("src/.env"));
});
test("fileGlobToRegex: '**/.env' matches 'deep/nested/.env'", () => {
assert.ok(fileGlobToRegex("**/.env").test("deep/nested/.env"));
});
test("fileGlobToRegex: '**/.env' matches '.env' at root", () => {
assert.ok(fileGlobToRegex("**/.env").test(".env"));
});
test("fileGlobToRegex: '**/*credentials*' matches nested path", () => {
assert.ok(fileGlobToRegex("**/*credentials*").test("secrets/credentials.json"));
});
test("fileGlobToRegex: '**/*credentials*' does not match 'readme.md'", () => {
assert.ok(!fileGlobToRegex("**/*credentials*").test("readme.md"));
});
});
describe("evaluateFilePath", () => {
test("evaluateFilePath: .env denied by ['.env']", () => {
const result = evaluateFilePath(".env", [[".env"]], false);
assert.equal(result.denied, true);
assert.equal(result.matchedPattern, ".env");
});
test("evaluateFilePath: src/config.ts not denied by ['.env']", () => {
const result = evaluateFilePath("src/config.ts", [[".env"]], false);
assert.equal(result.denied, false);
assert.equal(result.matchedPattern, undefined);
});
test("evaluateFilePath: deep/nested/.env denied by ['**/.env']", () => {
const result = evaluateFilePath("deep/nested/.env", [["**/.env"]], false);
assert.equal(result.denied, true);
assert.equal(result.matchedPattern, "**/.env");
});
test("evaluateFilePath: credentials file denied by ['**/*credentials*']", () => {
const result = evaluateFilePath(
"secrets/credentials.json",
[["**/*credentials*"]],
false,
);
assert.equal(result.denied, true);
assert.equal(result.matchedPattern, "**/*credentials*");
});
test("evaluateFilePath: readme.md not denied by ['**/*credentials*']", () => {
const result = evaluateFilePath("readme.md", [["**/*credentials*"]], false);
assert.equal(result.denied, false);
});
test("evaluateFilePath: Windows path with backslashes", () => {
const result = evaluateFilePath(
"C:\\Users\\.env",
[["**/.env"]],
true,
);
assert.equal(result.denied, true);
assert.equal(result.matchedPattern, "**/.env");
});
});
describe("Shell-Escape Scanner", () => {
test("extractShellCommands: Python os.system", () => {
const result = extractShellCommands(
'os.system("sudo rm -rf /")',
"python",
);
assert.deepEqual(result, ["sudo rm -rf /"]);
});
test("extractShellCommands: Python subprocess.run string", () => {
const result = extractShellCommands(
'subprocess.run("sudo apt install vim")',
"python",
);
assert.deepEqual(result, ["sudo apt install vim"]);
});
test("extractShellCommands: Python subprocess.run list args", () => {
const result = extractShellCommands(
'subprocess.run(["rm", "-rf", "/"])',
"python",
);
assert.ok(result.length > 0, "should extract commands from list form");
assert.ok(
result.some((cmd) => cmd.includes("rm") && cmd.includes("-rf")),
`should join list args into command string, got: ${JSON.stringify(result)}`,
);
});
test("extractShellCommands: Python subprocess.call list args", () => {
const result = extractShellCommands(
'subprocess.call(["sudo", "reboot"])',
"python",
);
assert.ok(result.some((cmd) => cmd.includes("sudo") && cmd.includes("reboot")));
});
test("extractShellCommands: JS execSync", () => {
const cmds = extractShellCommands(
'const r = execSync("sudo apt update")',
"javascript",
);
assert.deepEqual(cmds, ["sudo apt update"]);
});
test("extractShellCommands: JS spawnSync", () => {
const cmds = extractShellCommands(
'spawnSync("sudo", ["rm", "-rf"])',
"javascript",
);
assert.ok(cmds.length > 0, "should detect spawnSync");
assert.ok(cmds[0].includes("sudo"));
});
test("extractShellCommands: Ruby system()", () => {
const result = extractShellCommands(
'system("sudo rm -rf /tmp")',
"ruby",
);
assert.deepEqual(result, ["sudo rm -rf /tmp"]);
});
test("extractShellCommands: Go exec.Command", () => {
const result = extractShellCommands(
'exec.Command("sudo", "rm", "-rf")',
"go",
);
assert.ok(result.length > 0, "should detect Go exec.Command");
assert.ok(result[0].includes("sudo"));
});
test("extractShellCommands: PHP shell_exec", () => {
const result = extractShellCommands(
'shell_exec("sudo rm -rf /tmp")',
"php",
);
assert.ok(result.length > 0, "should detect PHP shell_exec");
assert.ok(result[0].includes("sudo"));
});
test("extractShellCommands: PHP system()", () => {
const result = extractShellCommands(
'system("sudo reboot")',
"php",
);
assert.ok(result.length > 0, "should detect PHP system()");
});
test("extractShellCommands: Rust Command::new", () => {
const result = extractShellCommands(
'Command::new("sudo").arg("reboot")',
"rust",
);
assert.ok(result.length > 0, "should detect Rust Command::new");
assert.ok(result[0].includes("sudo"));
});
test("extractShellCommands: safe JS code returns empty", () => {
const result = extractShellCommands(
'console.log("hello")',
"javascript",
);
assert.deepEqual(result, []);
});
test("extractShellCommands: unknown language returns empty", () => {
const result = extractShellCommands(
'os.system("rm -rf /")',
"haskell",
);
assert.deepEqual(result, []);
});
});