This repository was archived by the owner on Feb 28, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand-blocker.test.ts
More file actions
1998 lines (1723 loc) · 69.2 KB
/
command-blocker.test.ts
File metadata and controls
1998 lines (1723 loc) · 69.2 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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { describe, it, expect, beforeEach } from "vitest";
import { CommandBlocker } from "./command-blocker";
interface PluginHook {
"tool.execute.before": (input: unknown, output: unknown) => Promise<void>;
}
describe("Command Blocker", () => {
describe("checkPythonNodeCommand", () => {
let plugin: any;
let mockApp: any;
let mockClient: any;
let mock$: any;
beforeEach(async () => {
mockApp = {};
mockClient = {};
mock$ = {};
plugin = await CommandBlocker({
app: mockApp,
client: mockClient,
$: mock$,
});
});
it("should block node command", async () => {
const input = { tool: "bash" };
const output = { args: { command: "node --version" } };
await expect(
plugin["tool.execute.before"](input, output)
).rejects.toThrow(
"`node` is blocked to ensure reproducible builds. Use `bun` (faster, more reliable) or `bunx` for running scripts. Example: `bun run dev` instead of `node server.js`"
);
});
it("should block npm command", async () => {
const input = { tool: "bash" };
const output = { args: { command: "npm install" } };
await expect(
plugin["tool.execute.before"](input, output)
).rejects.toThrow(
"`npm` is blocked to ensure reproducible builds. Use `bun` (faster, more reliable) instead. Examples: `bun install` instead of `npm install`, `bun run build` instead of `npm run build`"
);
});
it("should block npx command", async () => {
const input = { tool: "bash" };
const output = { args: { command: "npx create-react-app my-app" } };
await expect(
plugin["tool.execute.before"](input, output)
).rejects.toThrow(
"`npx` is blocked to ensure reproducible builds. Use `bunx` (faster, more reliable) instead. Examples: `bunx create-react-app my-app` instead of `npx create-react-app my-app`"
);
});
it("should block pip command", async () => {
const input = { tool: "bash" };
const output = { args: { command: "pip install requests" } };
await expect(
plugin["tool.execute.before"](input, output)
).rejects.toThrow(
"`pip` is blocked to ensure reproducible builds. Use `uv` or `uvx` for dependency management. Example: `uv add requests` instead of `pip install requests`"
);
});
it("should block python commands", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: "python script.py" } };
await expect(
plugin["tool.execute.before"](input1, output1)
).rejects.toThrow(
"`python` is blocked to ensure environment isolation. Use `uv` for dependency management or `uvx` for running tools. Virtual environment python (e.g., `.venv/bin/python`) is allowed. Example: `uv run python script.py`"
);
const input2 = { tool: "bash" };
const output2 = { args: { command: "python2 script.py" } };
await expect(
plugin["tool.execute.before"](input2, output2)
).rejects.toThrow(
"`python2` is blocked (Python 2 is deprecated). Use `uv` with Python 3 for modern dependency management. Virtual environment python2 commands are allowed if needed. Example: `uv run --python 3.8 python script.py`"
);
const input3 = { tool: "bash" };
const output3 = { args: { command: "python3 script.py" } };
await expect(
plugin["tool.execute.before"](input3, output3)
).rejects.toThrow(
"`python3` is blocked to ensure environment isolation. Use `uv` for dependency management or `uvx` for running tools. Virtual environment python3 (e.g., `.venv/bin/python3`) is allowed. Example: `uv run python3 script.py`"
);
});
it("should allow virtual environment python commands", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: ".venv/bin/python script.py" } };
await expect(async () => {
await plugin["tool.execute.before"](input1, output1);
}).not.toThrow();
const input2 = { tool: "bash" };
const output2 = {
args: { command: ".venv/bin/python3 -c 'print(\"hello\")'" },
};
await expect(async () => {
await plugin["tool.execute.before"](input2, output2);
}).not.toThrow();
const input3 = { tool: "bash" };
const output3 = {
args: { command: "venv/bin/python manage.py runserver" },
};
await expect(async () => {
await plugin["tool.execute.before"](input3, output3);
}).not.toThrow();
const input4 = { tool: "bash" };
const output4 = {
args: { command: "env/bin/python3 -c 'print(\"hello\")'" },
};
await expect(async () => {
await plugin["tool.execute.before"](input4, output4);
}).not.toThrow();
const input5 = { tool: "bash" };
const output5 = { args: { command: "./.venv/bin/python test.py" } };
await expect(async () => {
await plugin["tool.execute.before"](input5, output5);
}).not.toThrow();
const input6 = { tool: "bash" };
const output6 = { args: { command: "../venv/bin/python3 script.py" } };
await expect(async () => {
await plugin["tool.execute.before"](input6, output6);
}).not.toThrow();
const input7 = { tool: "bash" };
const output7 = { args: { command: "cd directory & .venv/bin/python" } };
await expect(async () => {
await plugin["tool.execute.before"](input7, output7);
}).not.toThrow();
const input8 = { tool: "bash" };
const output8 = { args: { command: "uv run python script.py" } };
await expect(async () => {
await plugin["tool.execute.before"](input8, output8);
}).not.toThrow();
const input9 = { tool: "bash" };
const output9 = {
args: { command: "uv run python3 -c 'print(\"hello\")'" },
};
await expect(async () => {
await plugin["tool.execute.before"](input9, output9);
}).not.toThrow();
const input10 = { tool: "bash" };
const output10 = { args: { command: "uvx python manage.py runserver" } };
await expect(async () => {
await plugin["tool.execute.before"](input10, output10);
}).not.toThrow();
const input11 = { tool: "bash" };
const output11 = {
args: {
command:
'cd /home/knoopx/Projects/my-project && uv run python -c "import torchdata; print(dir(torchdata))"',
},
};
await expect(async () => {
await plugin["tool.execute.before"](input11, output11);
}).not.toThrow();
});
it("should allow which/whereis commands", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: "which node" } };
await expect(async () => {
await plugin["tool.execute.before"](input1, output1);
}).not.toThrow();
const input2 = { tool: "bash" };
const output2 = { args: { command: "whereis python" } };
await expect(async () => {
await plugin["tool.execute.before"](input2, output2);
}).not.toThrow();
});
it("should block commands containing blocked words", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: 'echo "node --version"' } };
await expect(
plugin["tool.execute.before"](input1, output1)
).rejects.toThrow();
const input2 = { tool: "bash" };
const output2 = { args: { command: 'bash -c "npm install"' } };
await expect(
plugin["tool.execute.before"](input2, output2)
).rejects.toThrow();
});
it("should allow non-blocked commands", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: "ls -la" } };
await expect(async () => {
await plugin["tool.execute.before"](input1, output1);
}).not.toThrow();
const input2 = { tool: "bash" };
const output2 = { args: { command: "bun install" } };
await expect(async () => {
await plugin["tool.execute.before"](input2, output2);
}).not.toThrow();
});
// Test piping and escape methods
it("should block node in piped commands", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: 'echo "node --version" | bash' } };
await expect(
plugin["tool.execute.before"](input1, output1)
).rejects.toThrow();
const input2 = { tool: "bash" };
const output2 = { args: { command: "cat file.txt | node" } };
await expect(
plugin["tool.execute.before"](input2, output2)
).rejects.toThrow();
const input3 = { tool: "bash" };
const output3 = { args: { command: 'node script.js | grep "output"' } };
await expect(
plugin["tool.execute.before"](input3, output3)
).rejects.toThrow();
});
it("should block npm in piped commands", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: 'echo "npm install" | sh' } };
await expect(
plugin["tool.execute.before"](input1, output1)
).rejects.toThrow();
const input2 = { tool: "bash" };
const output2 = { args: { command: "npm list | grep package" } };
await expect(
plugin["tool.execute.before"](input2, output2)
).rejects.toThrow();
});
it("should block npx in piped commands", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: 'echo "npx create-react-app" | sh' } };
await expect(
plugin["tool.execute.before"](input1, output1)
).rejects.toThrow();
const input2 = { tool: "bash" };
const output2 = { args: { command: "npx eslint | grep error" } };
await expect(
plugin["tool.execute.before"](input2, output2)
).rejects.toThrow();
});
it("should block pip in piped commands", async () => {
const input1 = { tool: "bash" };
const output1 = {
args: { command: 'echo "pip install requests" | python' },
};
await expect(
plugin["tool.execute.before"](input1, output1)
).rejects.toThrow();
const input2 = { tool: "bash" };
const output2 = { args: { command: "pip list | head -5" } };
await expect(
plugin["tool.execute.before"](input2, output2)
).rejects.toThrow();
});
it("should block python in piped commands", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: 'echo "print(1)" | python' } };
await expect(
plugin["tool.execute.before"](input1, output1)
).rejects.toThrow();
const input2 = { tool: "bash" };
const output2 = { args: { command: 'python -c "print(1)" | cat' } };
await expect(
plugin["tool.execute.before"](input2, output2)
).rejects.toThrow();
});
it("should block commands with command substitution", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: "echo $(node --version)" } };
await expect(
plugin["tool.execute.before"](input1, output1)
).rejects.toThrow();
const input2 = { tool: "bash" };
const output2 = { args: { command: "bash -c $(npm install)" } };
await expect(
plugin["tool.execute.before"](input2, output2)
).rejects.toThrow();
const input3 = { tool: "bash" };
const output3 = { args: { command: "sh -c $(pip install requests)" } };
await expect(
plugin["tool.execute.before"](input3, output3)
).rejects.toThrow();
});
it("should block commands with backticks", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: "echo `node --version`" } };
await expect(
plugin["tool.execute.before"](input1, output1)
).rejects.toThrow();
const input2 = { tool: "bash" };
const output2 = { args: { command: "bash -c `npm install`" } };
await expect(
plugin["tool.execute.before"](input2, output2)
).rejects.toThrow();
const input3 = { tool: "bash" };
const output3 = { args: { command: "sh -c `pip install requests`" } };
await expect(
plugin["tool.execute.before"](input3, output3)
).rejects.toThrow();
});
it("should block commands with semicolons", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: "ls; node --version" } };
await expect(
plugin["tool.execute.before"](input1, output1)
).rejects.toThrow();
const input2 = { tool: "bash" };
const output2 = { args: { command: 'echo "test"; npm install' } };
await expect(
plugin["tool.execute.before"](input2, output2)
).rejects.toThrow();
const input3 = { tool: "bash" };
const output3 = { args: { command: "pwd; pip install requests" } };
await expect(
plugin["tool.execute.before"](input3, output3)
).rejects.toThrow();
});
it("should block commands with && and || operators", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: "ls && node --version" } };
await expect(
plugin["tool.execute.before"](input1, output1)
).rejects.toThrow();
const input2 = { tool: "bash" };
const output2 = { args: { command: 'echo "test" && npm install' } };
await expect(
plugin["tool.execute.before"](input2, output2)
).rejects.toThrow();
const input3 = { tool: "bash" };
const output3 = { args: { command: "pwd || pip install requests" } };
await expect(
plugin["tool.execute.before"](input3, output3)
).rejects.toThrow();
});
it("should block commands with background execution", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: "node --version &" } };
await expect(
plugin["tool.execute.before"](input1, output1)
).rejects.toThrow();
const input2 = { tool: "bash" };
const output2 = { args: { command: "npm install &" } };
await expect(
plugin["tool.execute.before"](input2, output2)
).rejects.toThrow();
const input3 = { tool: "bash" };
const output3 = { args: { command: "pip install requests &" } };
await expect(
plugin["tool.execute.before"](input3, output3)
).rejects.toThrow();
});
it("should block commands with redirection", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: "node --version > output.txt" } };
await expect(
plugin["tool.execute.before"](input1, output1)
).rejects.toThrow();
const input2 = { tool: "bash" };
const output2 = { args: { command: "npm install >> log.txt" } };
await expect(
plugin["tool.execute.before"](input2, output2)
).rejects.toThrow();
const input3 = { tool: "bash" };
const output3 = { args: { command: "pip install requests 2>&1" } };
await expect(
plugin["tool.execute.before"](input3, output3)
).rejects.toThrow();
});
it("should block commands with environment variables", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: "NODE_ENV=production node" } };
await expect(
plugin["tool.execute.before"](input1, output1)
).rejects.toThrow();
const input2 = { tool: "bash" };
const output2 = {
args: { command: "npm_config_cache=/tmp npm install" },
};
await expect(
plugin["tool.execute.before"](input2, output2)
).rejects.toThrow();
const input3 = { tool: "bash" };
const output3 = {
args: { command: "PIP_INDEX_URL=https://pypi.org/simple pip install" },
};
await expect(
plugin["tool.execute.before"](input3, output3)
).rejects.toThrow();
});
it("should block commands with escaped characters", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: "n\\ode --version" } };
await expect(
plugin["tool.execute.before"](input1, output1)
).rejects.toThrow();
const input2 = { tool: "bash" };
const output2 = { args: { command: "np\\m install" } };
await expect(
plugin["tool.execute.before"](input2, output2)
).rejects.toThrow();
const input3 = { tool: "bash" };
const output3 = { args: { command: "pi\\p install" } };
await expect(
plugin["tool.execute.before"](input3, output3)
).rejects.toThrow();
});
it("should block commands with quotes containing blocked commands", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: 'bash -c "node --version"' } };
await expect(
plugin["tool.execute.before"](input1, output1)
).rejects.toThrow();
const input2 = { tool: "bash" };
const output2 = { args: { command: "sh -c 'npm install'" } };
await expect(
plugin["tool.execute.before"](input2, output2)
).rejects.toThrow();
const input3 = { tool: "bash" };
const output3 = {
args: {
command:
"python -c \"import subprocess; subprocess.run(['pip', 'install', 'requests'])\"",
},
};
await expect(
plugin["tool.execute.before"](input3, output3)
).rejects.toThrow();
});
it("should block commands with eval", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: 'eval "node --version"' } };
await expect(
plugin["tool.execute.before"](input1, output1)
).rejects.toThrow();
const input2 = { tool: "bash" };
const output2 = { args: { command: "eval 'npm install'" } };
await expect(
plugin["tool.execute.before"](input2, output2)
).rejects.toThrow();
const input3 = { tool: "bash" };
const output3 = { args: { command: 'bash -c "eval pip install"' } };
await expect(
plugin["tool.execute.before"](input3, output3)
).rejects.toThrow();
});
it("should block commands with exec", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: "exec node --version" } };
await expect(
plugin["tool.execute.before"](input1, output1)
).rejects.toThrow();
const input2 = { tool: "bash" };
const output2 = { args: { command: "exec npm install" } };
await expect(
plugin["tool.execute.before"](input2, output2)
).rejects.toThrow();
const input3 = { tool: "bash" };
const output3 = { args: { command: "exec pip install" } };
await expect(
plugin["tool.execute.before"](input3, output3)
).rejects.toThrow();
});
it("should allow safe piped commands", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: "ls -la | grep test" } };
await expect(async () => {
await plugin["tool.execute.before"](input1, output1);
}).not.toThrow();
const input2 = { tool: "bash" };
const output2 = { args: { command: "cat file.txt | head -5" } };
await expect(async () => {
await plugin["tool.execute.before"](input2, output2);
}).not.toThrow();
const input3 = { tool: "bash" };
const output3 = { args: { command: 'echo "hello" | wc -l' } };
await expect(async () => {
await plugin["tool.execute.before"](input3, output3);
}).not.toThrow();
});
it("should allow safe command substitution", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: "echo $(date)" } };
await expect(async () => {
await plugin["tool.execute.before"](input1, output1);
}).not.toThrow();
const input2 = { tool: "bash" };
const output2 = { args: { command: "echo `pwd`" } };
await expect(async () => {
await plugin["tool.execute.before"](input2, output2);
}).not.toThrow();
const input3 = { tool: "bash" };
const output3 = { args: { command: 'ls $(echo "*.txt")' } };
await expect(async () => {
await plugin["tool.execute.before"](input3, output3);
}).not.toThrow();
});
});
describe("checkGitCommand", () => {
let plugin: any;
let mockApp: any;
let mockClient: any;
let mock$: any;
beforeEach(async () => {
mockApp = {};
mockClient = {};
mock$ = {};
plugin = await CommandBlocker({
app: mockApp,
client: mockClient,
$: mock$,
});
});
it("should allow read-only git commands", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: "git status" } };
await expect(async () => {
await plugin["tool.execute.before"](input1, output1);
}).not.toThrow();
const input2 = { tool: "bash" };
const output2 = { args: { command: "git diff" } };
await expect(async () => {
await plugin["tool.execute.before"](input2, output2);
}).not.toThrow();
const input3 = { tool: "bash" };
const output3 = { args: { command: "git show HEAD" } };
await expect(async () => {
await plugin["tool.execute.before"](input3, output3);
}).not.toThrow();
const input4 = { tool: "bash" };
const output4 = { args: { command: "git rev-parse HEAD" } };
await expect(async () => {
await plugin["tool.execute.before"](input4, output4);
}).not.toThrow();
const input5 = { tool: "bash" };
const output5 = { args: { command: "git log --oneline -1" } };
await expect(async () => {
await plugin["tool.execute.before"](input5, output5);
}).not.toThrow();
const input6 = { tool: "bash" };
const output6 = { args: { command: "git ls-files --others --exclude-standard" } };
await expect(async () => {
await plugin["tool.execute.before"](input6, output6);
}).not.toThrow();
});
it("should block write git commands", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: "git add ." } };
await expect(
plugin["tool.execute.before"](input1, output1)
).rejects.toThrow(
"`git` write operations are blocked to prevent agents from managing version control. Only read-only commands are allowed: `git status`, `git diff`, `git show`, `git log`, `git rev-parse`."
);
const input2 = { tool: "bash" };
const output2 = { args: { command: 'git commit -m "test"' } };
await expect(
plugin["tool.execute.before"](input2, output2)
).rejects.toThrow();
const input3 = { tool: "bash" };
const output3 = { args: { command: "git push" } };
await expect(
plugin["tool.execute.before"](input3, output3)
).rejects.toThrow();
const input4 = { tool: "bash" };
const output4 = { args: { command: "git checkout file.txt" } };
await expect(
plugin["tool.execute.before"](input4, output4)
).rejects.toThrow();
const input5 = { tool: "bash" };
const output5 = { args: { command: "git checkout HEAD -- file.txt" } };
await expect(
plugin["tool.execute.before"](input5, output5)
).rejects.toThrow();
const input6 = { tool: "bash" };
const output6 = { args: { command: "git checkout main" } };
await expect(
plugin["tool.execute.before"](input6, output6)
).rejects.toThrow();
});
it("should allow non-git commands", async () => {
const input = { tool: "bash" };
const output = { args: { command: "ls -la" } };
await expect(async () => {
await plugin["tool.execute.before"](input, output);
}).not.toThrow();
});
// Test git command escape methods
it("should block git in piped commands", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: 'echo "git add ." | bash' } };
await expect(
plugin["tool.execute.before"](input1, output1)
).rejects.toThrow();
const input2 = { tool: "bash" };
const output2 = { args: { command: "git status | grep modified" } };
await expect(
plugin["tool.execute.before"](input2, output2)
).resolves.toBeUndefined(); // This should be allowed since git status is allowed
const input3 = { tool: "bash" };
const output3 = { args: { command: 'echo "git commit" | sh' } };
await expect(
plugin["tool.execute.before"](input3, output3)
).rejects.toThrow();
});
it("should block git with command substitution", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: "echo $(git add .)" } };
await expect(
plugin["tool.execute.before"](input1, output1)
).rejects.toThrow();
const input2 = { tool: "bash" };
const output2 = { args: { command: 'bash -c $(git commit -m "test")' } };
await expect(
plugin["tool.execute.before"](input2, output2)
).rejects.toThrow();
});
it("should block git with semicolons", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: "ls; git add ." } };
await expect(
plugin["tool.execute.before"](input1, output1)
).rejects.toThrow();
const input2 = { tool: "bash" };
const output2 = { args: { command: 'echo "test"; git commit' } };
await expect(
plugin["tool.execute.before"](input2, output2)
).rejects.toThrow();
});
it("should block git with && and || operators", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: "ls && git add ." } };
await expect(
plugin["tool.execute.before"](input1, output1)
).rejects.toThrow();
const input2 = { tool: "bash" };
const output2 = { args: { command: 'echo "test" && git commit' } };
await expect(
plugin["tool.execute.before"](input2, output2)
).rejects.toThrow();
const input3 = { tool: "bash" };
const output3 = { args: { command: "pwd || git push" } };
await expect(
plugin["tool.execute.before"](input3, output3)
).rejects.toThrow();
});
it("should block git with background execution", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: "git add . &" } };
await expect(
plugin["tool.execute.before"](input1, output1)
).rejects.toThrow();
const input2 = { tool: "bash" };
const output2 = { args: { command: 'git commit -m "test" &' } };
await expect(
plugin["tool.execute.before"](input2, output2)
).rejects.toThrow();
});
it("should block git with redirection", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: "git add . > /dev/null" } };
await expect(
plugin["tool.execute.before"](input1, output1)
).rejects.toThrow();
const input2 = { tool: "bash" };
const output2 = { args: { command: "git commit >> log.txt" } };
await expect(
plugin["tool.execute.before"](input2, output2)
).rejects.toThrow();
});
it("should block git with environment variables", async () => {
const input1 = { tool: "bash" };
const output1 = {
args: { command: 'GIT_AUTHOR_NAME="Test" git commit' },
};
await expect(
plugin["tool.execute.before"](input1, output1)
).rejects.toThrow();
const input2 = { tool: "bash" };
const output2 = {
args: { command: 'GIT_COMMITTER_EMAIL="test@example.com" git add .' },
};
await expect(
plugin["tool.execute.before"](input2, output2)
).rejects.toThrow();
});
it("should block git with eval", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: 'eval "git add ."' } };
await expect(
plugin["tool.execute.before"](input1, output1)
).rejects.toThrow();
const input2 = { tool: "bash" };
const output2 = { args: { command: "eval 'git commit'" } };
await expect(
plugin["tool.execute.before"](input2, output2)
).rejects.toThrow();
});
it("should block git with exec", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: "exec git add ." } };
await expect(
plugin["tool.execute.before"](input1, output1)
).rejects.toThrow();
const input2 = { tool: "bash" };
const output2 = { args: { command: "exec git commit" } };
await expect(
plugin["tool.execute.before"](input2, output2)
).rejects.toThrow();
});
it("should allow safe git operations with pipes", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: "git status | cat" } };
await expect(async () => {
await plugin["tool.execute.before"](input1, output1);
}).not.toThrow();
const input2 = { tool: "bash" };
const output2 = { args: { command: "git diff | less" } };
await expect(async () => {
await plugin["tool.execute.before"](input2, output2);
}).not.toThrow();
const input3 = { tool: "bash" };
const output3 = { args: { command: "git show HEAD | head -20" } };
await expect(async () => {
await plugin["tool.execute.before"](input3, output3);
}).not.toThrow();
});
});
describe("checkNixCommand", () => {
let plugin: any;
let mockApp: any;
let mockClient: any;
let mock$: any;
beforeEach(async () => {
mockApp = {};
mockClient = {};
mock$ = {};
plugin = await CommandBlocker({
app: mockApp,
client: mockClient,
$: mock$,
});
});
it("should allow nix commands with path: prefix", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: "nix run path:./my-flake#output" } };
await expect(async () => {
await plugin["tool.execute.before"](input1, output1);
}).not.toThrow();
const input2 = { tool: "bash" };
const output2 = { args: { command: "nix build path:../flake#package" } };
await expect(async () => {
await plugin["tool.execute.before"](input2, output2);
}).not.toThrow();
});
it("should allow nix commands with github: prefix", async () => {
const input = { tool: "bash" };
const output = { args: { command: "nix run github:user/repo#output" } };
await expect(async () => {
await plugin["tool.execute.before"](input, output);
}).not.toThrow();
});
it("should allow nix commands with git+https: prefix", async () => {
const input = { tool: "bash" };
const output = {
args: { command: "nix run git+https://github.com/user/repo#output" },
};
await expect(async () => {
await plugin["tool.execute.before"](input, output);
}).not.toThrow();
});
it("should block nix commands without proper prefix", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: "nix run ./my-flake#output" } };
await expect(
plugin["tool.execute.before"](input1, output1)
).rejects.toThrow(
"Local flake paths without `path:` prefix are blocked to ensure reproducible builds. Use `path:` for local flakes (includes uncommitted changes), `github:` for remote repos, or `git+https:` for git URLs. Examples: `nix run path:./my-flake#output`, `nix run github:user/repo#output`"
);
const input2 = { tool: "bash" };
const output2 = { args: { command: "nix run ../flake#package" } };
await expect(
plugin["tool.execute.before"](input2, output2)
).rejects.toThrow();
const input3 = { tool: "bash" };
const output3 = { args: { command: "nix run /absolute/path#output" } };
await expect(
plugin["tool.execute.before"](input3, output3)
).rejects.toThrow();
});
it("should allow nix registry references", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: "nix run nixpkgs#yq" } };
await expect(async () => {
await plugin["tool.execute.before"](input1, output1);
}).not.toThrow();
const input2 = { tool: "bash" };
const output2 = { args: { command: "nix run nixpkgs/unstable#hello" } };
await expect(async () => {
await plugin["tool.execute.before"](input2, output2);
}).not.toThrow();
});
it("should allow non-nix commands", async () => {
const input = { tool: "bash" };
const output = { args: { command: "ls -la" } };
await expect(async () => {
await plugin["tool.execute.before"](input, output);
}).not.toThrow();
});
it("should allow nix commands that are not run or build", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: "nix flake update" } };
await expect(async () => {
await plugin["tool.execute.before"](input1, output1);
}).not.toThrow();
const input2 = { tool: "bash" };
const output2 = { args: { command: "nix develop" } };
await expect(async () => {
await plugin["tool.execute.before"](input2, output2);
}).not.toThrow();
});
// Test nix command escape methods
it("should block nix in piped commands", async () => {
const input1 = { tool: "bash" };
const output1 = {
args: { command: 'echo "nix run ./flake#package" | bash' },
};
await expect(
plugin["tool.execute.before"](input1, output1)
).rejects.toThrow();
const input2 = { tool: "bash" };
const output2 = { args: { command: "cat flake.nix | nix build" } };
await expect(async () => {
await plugin["tool.execute.before"](input2, output2);
}).not.toThrow(); // This should be allowed since nix build is allowed
const input3 = { tool: "bash" };
const output3 = {
args: { command: 'echo "nix run github:user/repo" | sh' },
};
await expect(async () => {
await plugin["tool.execute.before"](input3, output3);
}).not.toThrow(); // This should be allowed since it has github: prefix
});
it("should block nix with command substitution", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: "echo $(nix run ./flake#package)" } };
await expect(
plugin["tool.execute.before"](input1, output1)
).rejects.toThrow();
const input2 = { tool: "bash" };
const output2 = {
args: { command: "bash -c $(nix build ../flake#package)" },
};
await expect(
plugin["tool.execute.before"](input2, output2)
).rejects.toThrow();
});
it("should block nix with semicolons", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: "ls; nix run ./flake#package" } };
await expect(
plugin["tool.execute.before"](input1, output1)
).rejects.toThrow();
const input2 = { tool: "bash" };
const output2 = { args: { command: 'echo "test"; nix build ../flake' } };
await expect(
plugin["tool.execute.before"](input2, output2)
).rejects.toThrow();
});
it("should block nix with && and || operators", async () => {
const input1 = { tool: "bash" };
const output1 = { args: { command: "ls && nix run ./flake#package" } };
await expect(