62159
Sy
symb / files
symb / internal / shell / block.go
4.2 KB
1// Package shell provides an in-process POSIX shell interpreter with command 2// blocking for safe LLM-driven execution. 3package shell 4 5import "strings" 6 7// BlockFunc returns true if the given command args should be blocked. 8type BlockFunc func(args []string) bool 9 10// CommandsBlocker returns a BlockFunc that blocks exact command name matches. 11func CommandsBlocker(cmds []string) BlockFunc { 12 blocked := make(map[string]struct{}, len(cmds)) 13 for _, c := range cmds { 14 blocked[c] = struct{}{} 15 } 16 return func(args []string) bool { 17 if len(args) == 0 { 18 return false 19 } 20 _, ok := blocked[args[0]] 21 return ok 22 } 23} 24 25// ArgumentsBlocker returns a BlockFunc that blocks a command when specific 26// subcommand args and/or flags are present. 27// 28// For example, ArgumentsBlocker("npm", []string{"install"}, []string{"-g"}) 29// blocks "npm install -g <pkg>" but allows "npm install <pkg>". 30func ArgumentsBlocker(cmd string, subArgs, flags []string) BlockFunc { 31 return func(args []string) bool { 32 if len(args) == 0 || args[0] != cmd { 33 return false 34 } 35 posArgs, posFlags := splitArgsFlags(args[1:]) 36 if !prefixMatch(posArgs, subArgs) { 37 return false 38 } 39 if len(flags) > 0 && !flagsPresent(posFlags, flags) { 40 return false 41 } 42 return true 43 } 44} 45 46// splitArgsFlags separates positional arguments from flags (anything 47// starting with '-'). 48func splitArgsFlags(args []string) (positional, flags []string) { 49 for _, a := range args { 50 if strings.HasPrefix(a, "-") { 51 flags = append(flags, a) 52 } else { 53 positional = append(positional, a) 54 } 55 } 56 return 57} 58 59// prefixMatch returns true if haystack starts with all elements of needle. 60func prefixMatch(haystack, needle []string) bool { 61 if len(haystack) < len(needle) { 62 return false 63 } 64 for i, n := range needle { 65 if haystack[i] != n { 66 return false 67 } 68 } 69 return true 70} 71 72// flagsPresent returns true if all required flags appear in the actual flags. 73func flagsPresent(actual, required []string) bool { 74 have := make(map[string]struct{}, len(actual)) 75 for _, f := range actual { 76 have[f] = struct{}{} 77 } 78 for _, r := range required { 79 if _, ok := have[r]; !ok { 80 return false 81 } 82 } 83 return true 84} 85 86// BannedCommands is the default set of commands blocked for security. 87var BannedCommands = []string{ 88 // Bypass vectors — block shells, interpreters, and indirection commands 89 // that could re-exec blocked commands or run arbitrary network code. 90 "bash", "sh", "zsh", "fish", "csh", "tcsh", "ksh", "dash", 91 "env", "nohup", "xargs", "strace", "ltrace", 92 "python", "python3", "python2", "node", "ruby", "perl", 93 "php", "lua", "tclsh", "wish", 94 // Network / download 95 "aria2c", "axel", "curl", "curlie", "http-prompt", "httpie", 96 "links", "lynx", "nc", "ncat", "scp", "sftp", "ssh", 97 "telnet", "w3m", "wget", "xh", 98 // Privilege escalation 99 "doas", "su", "sudo", 100 // Package managers 101 "apk", "apt", "apt-cache", "apt-get", "dnf", "dpkg", "emerge", 102 "home-manager", "makepkg", "opkg", "pacman", "paru", "pkg", 103 "pkg_add", "pkg_delete", "portage", "rpm", "yay", "yum", "zypper", 104 // System modification 105 "at", "batch", "chkconfig", "crontab", "fdisk", "mkfs", "mount", 106 "parted", "service", "systemctl", "umount", 107 // Network configuration 108 "firewall-cmd", "ifconfig", "ip", "iptables", "netstat", "pfctl", 109 "route", "ufw", 110 // Directory traversal is handled by cwd clamping in updateFromRunner, 111 // not by command blocking (cd is a shell builtin, invisible to ExecHandlers). 112} 113 114// DefaultBlockFuncs returns the standard set of block functions. 115func DefaultBlockFuncs() []BlockFunc { 116 return []BlockFunc{ 117 CommandsBlocker(BannedCommands), 118 // Block global package installs 119 ArgumentsBlocker("npm", []string{"install"}, []string{"-g"}), 120 ArgumentsBlocker("npm", []string{"install"}, []string{"--global"}), 121 ArgumentsBlocker("pnpm", []string{"add"}, []string{"-g"}), 122 ArgumentsBlocker("pnpm", []string{"add"}, []string{"--global"}), 123 ArgumentsBlocker("yarn", []string{"global"}, nil), 124 ArgumentsBlocker("pip", []string{"install"}, nil), 125 ArgumentsBlocker("pip3", []string{"install"}, nil), 126 ArgumentsBlocker("gem", []string{"install"}, nil), 127 ArgumentsBlocker("cargo", []string{"install"}, nil), 128 ArgumentsBlocker("go", []string{"install"}, nil), 129 // Block go test -exec (code execution escape) 130 ArgumentsBlocker("go", []string{"test"}, []string{"-exec"}), 131 } 132}