9func TestCommandBlocking(t *testing.T) {
12 blockFuncs []BlockFunc
17 name: "block simple command",
18 blockFuncs: []BlockFunc{
19 func(args []string) bool {
20 return len(args) > 0 && args[0] == "curl"
23 command: "curl https://example.com",
27 name: "allow non-blocked command",
28 blockFuncs: []BlockFunc{
29 func(args []string) bool {
30 return len(args) > 0 && args[0] == "curl"
33 command: "echo hello",
37 name: "block subcommand",
38 blockFuncs: []BlockFunc{
39 func(args []string) bool {
40 return len(args) >= 2 && args[0] == "brew" && args[1] == "install"
43 command: "brew install wget",
47 name: "allow different subcommand",
48 blockFuncs: []BlockFunc{
49 func(args []string) bool {
50 return len(args) >= 2 && args[0] == "brew" && args[1] == "install"
57 name: "block npm global install with -g",
58 blockFuncs: []BlockFunc{
59 ArgumentsBlocker([][]string{
60 {"npm", "install", "-g"},
61 {"npm", "install", "--global"},
64 command: "npm install -g typescript",
68 name: "block npm global install with --global",
69 blockFuncs: []BlockFunc{
70 ArgumentsBlocker([][]string{
71 {"npm", "install", "-g"},
72 {"npm", "install", "--global"},
75 command: "npm install --global typescript",
79 name: "allow npm local install",
80 blockFuncs: []BlockFunc{
81 ArgumentsBlocker([][]string{
82 {"npm", "install", "-g"},
83 {"npm", "install", "--global"},
86 command: "npm install typescript",
91 for _, tt := range tests {
92 t.Run(tt.name, func(t *testing.T) {
93 // Create a temporary directory for each test
96 shell := NewShell(&Options{
98 BlockFuncs: tt.blockFuncs,
101 _, _, err := shell.Exec(context.Background(), tt.command)
105 t.Errorf("Expected command to be blocked, but it was allowed")
106 } else if !strings.Contains(err.Error(), "not allowed for security reasons") {
107 t.Errorf("Expected security error, got: %v", err)
110 // For non-blocked commands, we might get other errors (like command not found)
111 // but we shouldn't get the security error
112 if err != nil && strings.Contains(err.Error(), "not allowed for security reasons") {
113 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