@@ -243,15 +243,27 @@ def execute_shell(
243
243
"""Executes a shell command and returns the output."""
244
244
shell = get_shell ()
245
245
assert not args
246
+ whitelist_commands = ["ls" , "stat" , "cd" , "cat" , "pwd" , "echo" ]
247
+ whitelisted = True
246
248
247
249
cmd = code .strip ()
248
250
if cmd .startswith ("$ " ):
249
251
cmd = cmd [len ("$ " ) :]
250
252
251
- print_preview (cmd , "bash" )
252
- if not confirm ("Run command?" ):
253
- yield Message ("system" , "Command not run" )
254
- return
253
+ #NOTE: This does not handle control flow words like if, for, while.
254
+ regex = r"(?:^|[|&;]|\|\||&&|\n)\s*([^\s|&;]+)"
255
+
256
+ for match in re .finditer (regex , cmd ):
257
+ for group in match .groups ():
258
+ if group and group not in whitelist_commands :
259
+ whitelisted = False
260
+ break
261
+
262
+ if not whitelisted :
263
+ print_preview (cmd , "bash" )
264
+ if not confirm ("Run command?" ):
265
+ yield Message ("system" , "Command not run" )
266
+ return
255
267
256
268
try :
257
269
returncode , stdout , stderr = shell .run (cmd )
@@ -261,7 +273,7 @@ def execute_shell(
261
273
stdout = _shorten_stdout (stdout .strip (), pre_tokens = 2000 , post_tokens = 8000 )
262
274
stderr = _shorten_stdout (stderr .strip (), pre_tokens = 2000 , post_tokens = 2000 )
263
275
264
- msg = _format_block_smart ("Ran command" , cmd , lang = "bash" ) + "\n \n "
276
+ msg = _format_block_smart (f "Ran { 'whitelisted ' if whitelisted else '' } command" , cmd , lang = "bash" ) + "\n \n "
265
277
if stdout :
266
278
msg += _format_block_smart ("" , stdout , "stdout" ) + "\n \n "
267
279
if stderr :
0 commit comments