10func TestCommandBlocking(t *testing.T) {
13 blockFuncs []BlockFunc
18 name: "block simple command",
19 blockFuncs: []BlockFunc{
20 func(args []string) bool {
21 return len(args) > 0 && args[0] == "curl"
24 command: "curl https://example.com",
28 name: "allow non-blocked command",
29 blockFuncs: []BlockFunc{
30 func(args []string) bool {
31 return len(args) > 0 && args[0] == "curl"
34 command: "echo hello",
38 name: "block subcommand",
39 blockFuncs: []BlockFunc{
40 func(args []string) bool {
41 return len(args) >= 2 && args[0] == "brew" && args[1] == "install"
44 command: "brew install wget",
48 name: "allow different subcommand",
49 blockFuncs: []BlockFunc{
50 func(args []string) bool {
51 return len(args) >= 2 && args[0] == "brew" && args[1] == "install"
58 name: "block npm global install with -g",
59 blockFuncs: []BlockFunc{
60 ArgumentsBlocker([][]string{
61 {"npm", "install", "-g"},
62 {"npm", "install", "--global"},
65 command: "npm install -g typescript",
69 name: "block npm global install with --global",
70 blockFuncs: []BlockFunc{
71 ArgumentsBlocker([][]string{
72 {"npm", "install", "-g"},
73 {"npm", "install", "--global"},
76 command: "npm install --global typescript",
80 name: "allow npm local install",
81 blockFuncs: []BlockFunc{
82 ArgumentsBlocker([][]string{
83 {"npm", "install", "-g"},
84 {"npm", "install", "--global"},
87 command: "npm install typescript",
92 for _, tt := range tests {
93 t.Run(tt.name, func(t *testing.T) {
94 // Create a temporary directory for each test
95 tmpDir, err := os.MkdirTemp("", "shell-test-*")
97 t.Fatalf("Failed to create temp dir: %v", err)
99 defer os.RemoveAll(tmpDir)
101 shell := NewShell(&Options{
103 BlockFuncs: tt.blockFuncs,
106 _, _, err = shell.Exec(context.Background(), tt.command)
110 t.Errorf("Expected command to be blocked, but it was allowed")
111 } else if !strings.Contains(err.Error(), "not allowed for security reasons") {
112 t.Errorf("Expected security error, got: %v", err)
115 // For non-blocked commands, we might get other errors (like command not found)
116 // but we shouldn't get the security error
117 if err != nil && strings.Contains(err.Error(), "not allowed for security reasons") {
118 t.Errorf("Command was unexpectedly blocked: %v", err)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list