@@ -48,6 +48,261 @@ const FILES = new Set([
4848const FLAGS = new Set ( [ "-destination" , "-literalpath" , "-path" ] )
4949const SWITCHES = new Set ( [ "-confirm" , "-debug" , "-force" , "-nonewline" , "-recurse" , "-verbose" , "-whatif" ] )
5050
51+ // Commands that are inherently safe to auto-approve (read-only, no side effects)
52+ const SAFE_COMMANDS = new Set ( [
53+ "ls" ,
54+ "dir" ,
55+ "find" ,
56+ "locate" ,
57+ "which" ,
58+ "where" ,
59+ "type" ,
60+ "file" ,
61+ "stat" ,
62+ "wc" ,
63+ "head" ,
64+ "tail" ,
65+ "less" ,
66+ "more" ,
67+ "strings" ,
68+ "grep" ,
69+ "egrep" ,
70+ "fgrep" ,
71+ "zgrep" ,
72+ "awk" ,
73+ "sed" ,
74+ "cut" ,
75+ "sort" ,
76+ "uniq" ,
77+ "tr" ,
78+ "tee" ,
79+ "diff" ,
80+ "cmp" ,
81+ "comm" ,
82+ "tree" ,
83+ "du" ,
84+ "df" ,
85+ "free" ,
86+ "top" ,
87+ "ps" ,
88+ "uptime" ,
89+ "whoami" ,
90+ "id" ,
91+ "uname" ,
92+ "date" ,
93+ "time" ,
94+ "echo" ,
95+ "printf" ,
96+ "jq" ,
97+ "yq" ,
98+ "xmllint" ,
99+ "md5sum" ,
100+ "sha1sum" ,
101+ "sha256sum" ,
102+ "shasum" ,
103+ "base64" ,
104+ "xxd" ,
105+ "hexdump" ,
106+ "od" ,
107+ "man" ,
108+ "info" ,
109+ "help" ,
110+ "true" ,
111+ "false" ,
112+ "test" ,
113+ "seq" ,
114+ "shuf" ,
115+ "paste" ,
116+ "join" ,
117+ "xargs" ,
118+ "env" ,
119+ "printenv" ,
120+ "set" ,
121+ "pwd" ,
122+ "dirs" ,
123+ "popd" ,
124+ "readlink" ,
125+ "realpath" ,
126+ "basename" ,
127+ "dirname" ,
128+ "lsblk" ,
129+ "lscpu" ,
130+ "lsmem" ,
131+ "lsusb" ,
132+ "lspci" ,
133+ "ip" ,
134+ "ifconfig" ,
135+ "netstat" ,
136+ "ss" ,
137+ "route" ,
138+ "cat" ,
139+ "bat" ,
140+ "rg" ,
141+ "fd" ,
142+ "exa" ,
143+ "eza" ,
144+ "ping" ,
145+ "dig" ,
146+ "nslookup" ,
147+ "host" ,
148+ // PowerShell read-only equivalents
149+ "get-childitem" ,
150+ "get-content" ,
151+ "select-string" ,
152+ "get-location" ,
153+ "get-process" ,
154+ "get-service" ,
155+ "get-eventlog" ,
156+ "measure-object" ,
157+ "format-list" ,
158+ "format-table" ,
159+ "out-null" ,
160+ "write-host" ,
161+ "write-output" ,
162+ ] )
163+
164+ // Commands that are never safe to auto-approve
165+ const DANGEROUS_COMMANDS = new Set ( [
166+ "rm" ,
167+ "del" ,
168+ "remove-item" ,
169+ "rmdir" ,
170+ "dd" ,
171+ "mkfs" ,
172+ "fdisk" ,
173+ "parted" ,
174+ "shutdown" ,
175+ "reboot" ,
176+ "halt" ,
177+ "poweroff" ,
178+ "kill" ,
179+ "killall" ,
180+ "pkill" ,
181+ "chmod" ,
182+ "chown" ,
183+ "chgrp" ,
184+ "format" ,
185+ "diskpart" ,
186+ ] )
187+
188+ // Patterns that make any command unsafe regardless of the command name
189+ const DANGEROUS_PATTERNS = [
190+ / \| \s * ( s h | b a s h | z s h | f i s h | p w s h | p o w e r s h e l l | c m d ) \b / ,
191+ / - - n o - p r e s e r v e - r o o t / ,
192+ / - r f \s + \/ \s * $ / ,
193+ / > \s * \/ d e v \/ s d / ,
194+ / > \s * \/ d e v \/ h d / ,
195+ ]
196+
197+ /**
198+ * Classify whether a bash command is safe to auto-approve.
199+ * Uses the tree-sitter parsed AST for accurate command identification.
200+ * Returns true only if all commands in the input are read-only and
201+ * contain no dangerous patterns.
202+ */
203+ function classify ( root : Node , ps : boolean ) : boolean {
204+ const cmds = commands ( root )
205+ if ( cmds . length === 0 ) return true
206+
207+ for ( const node of cmds ) {
208+ const command = parts ( node )
209+ const tokens = command . map ( ( item ) => item . text )
210+ const cmd = ps ? tokens [ 0 ] ?. toLowerCase ( ) : tokens [ 0 ]
211+ if ( ! cmd ) continue
212+
213+ const sourceText = source ( node )
214+
215+ // Check dangerous patterns first — these override everything
216+ for ( const pattern of DANGEROUS_PATTERNS ) {
217+ if ( pattern . test ( sourceText ) ) return false
218+ }
219+
220+ // Explicitly dangerous commands are never auto-approved
221+ if ( DANGEROUS_COMMANDS . has ( ps ? cmd . toLowerCase ( ) : cmd ) ) return false
222+
223+ // Command must be in the safe whitelist
224+ if ( ! SAFE_COMMANDS . has ( ps ? cmd . toLowerCase ( ) : cmd ) ) return false
225+
226+ // Context-aware subcommand checks
227+ if ( cmd === "git" && tokens . length > 1 ) {
228+ const sub = tokens [ 1 ]
229+ if (
230+ ! [
231+ "status" ,
232+ "log" ,
233+ "diff" ,
234+ "show" ,
235+ "branch" ,
236+ "tag" ,
237+ "remote" ,
238+ "stash" ,
239+ "reflog" ,
240+ "describe" ,
241+ "shortlog" ,
242+ "blame" ,
243+ "ls-files" ,
244+ "ls-tree" ,
245+ "cat-file" ,
246+ "rev-parse" ,
247+ "rev-list" ,
248+ "merge-base" ,
249+ "name-rev" ,
250+ "for-each-ref" ,
251+ ] . includes ( sub )
252+ )
253+ return false
254+ }
255+
256+ if ( [ "npm" , "pnpm" , "yarn" , "bun" ] . includes ( cmd ) && tokens . length > 1 ) {
257+ if (
258+ ! [
259+ "list" ,
260+ "ls" ,
261+ "info" ,
262+ "view" ,
263+ "search" ,
264+ "explain" ,
265+ "fund" ,
266+ "outdated" ,
267+ "version" ,
268+ "--version" ,
269+ "--help" ,
270+ "run" ,
271+ "exec" ,
272+ ] . includes ( tokens [ 1 ] )
273+ )
274+ return false
275+ }
276+
277+ if ( cmd === "go" && tokens . length > 1 ) {
278+ if ( ! [ "version" , "env" , "list" , "doc" , "help" ] . includes ( tokens [ 1 ] ) ) return false
279+ }
280+
281+ if ( cmd === "cargo" && tokens . length > 1 ) {
282+ if (
283+ ! [
284+ "version" ,
285+ "help" ,
286+ "search" ,
287+ "doc" ,
288+ "metadata" ,
289+ "tree" ,
290+ "verify-project" ,
291+ "read-manifest" ,
292+ "generate-lockfile" ,
293+ "pkgid" ,
294+ ] . includes ( tokens [ 1 ] )
295+ )
296+ return false
297+ }
298+
299+ // curl/wget are safe only if output is not piped to a shell
300+ if ( [ "curl" , "wget" ] . includes ( cmd ) && / \| \s * ( s h | b a s h | z s h ) / . test ( sourceText ) ) return false
301+ }
302+
303+ return true
304+ }
305+
51306type Part = {
52307 type : string
53308 text : string
@@ -262,7 +517,13 @@ async function parse(command: string, ps: boolean) {
262517 return tree . rootNode
263518}
264519
265- async function ask ( ctx : Tool . Context , scan : Scan ) {
520+ async function ask ( ctx : Tool . Context , scan : Scan , root : Node , ps : boolean ) {
521+ // Auto-approve commands classified as safe (read-only, no dangerous patterns)
522+ if ( classify ( root , ps ) ) {
523+ log . info ( "auto-approved safe command" , { command : source ( root ) } )
524+ return
525+ }
526+
266527 if ( scan . dirs . size > 0 ) {
267528 const globs = Array . from ( scan . dirs ) . map ( ( dir ) => {
268529 if ( process . platform === "win32" ) return Filesystem . normalizePathPattern ( path . join ( dir , "*" ) )
@@ -481,7 +742,7 @@ export const BashTool = Tool.define("bash", async () => {
481742 const root = await parse ( params . command , ps )
482743 const scan = await collect ( root , cwd , ps , shell )
483744 if ( ! Instance . containsPath ( cwd ) ) scan . dirs . add ( cwd )
484- await ask ( ctx , scan )
745+ await ask ( ctx , scan , root , ps )
485746
486747 return run (
487748 {
0 commit comments