77import subprocess
88import sys
99import time
10-
10+ import shlex
1111from azure .ai .ml .exceptions import ErrorCategory , ErrorTarget , MlException
1212
1313
@@ -24,39 +24,40 @@ def run_cli_command(
2424 do_not_print = True ,
2525 stderr_to_stdout = True ,
2626):
27+ # Ensure cmd_arguments is always a list for shell=False safety.
28+ # Some callers may pass a pre-joined string; split it to maintain
29+ # compatibility while keeping shell=False.
30+ if isinstance (cmd_arguments , str ):
31+ cmd_arguments = shlex .split (cmd_arguments )
32+
2733 if not custom_environment :
2834 custom_environment = os .environ
2935
30- # We do this join to construct a command because "shell=True" flag, used below, doesn't work with the vector
31- # argv form on a mac OS.
32- command_to_execute = " " .join (cmd_arguments )
33-
3436 if not do_not_print : # Avoid printing the az login service principal password, for example
35- print ("Preparing to run CLI command: \n {}\n " .format (command_to_execute ))
37+ print ("Preparing to run CLI command: \n {}\n " .format (" " . join ( cmd_arguments ) ))
3638 print ("Current directory: {}" .format (os .getcwd ()))
3739
3840 start_time = time .time ()
3941 try :
4042 # We redirect stderr to stdout, so that in the case of an error, especially in negative tests,
4143 # we get the error reply back to check if the error is expected or not.
42- # We need "shell=True" flag so that the "az" wrapper works.
4344
4445 # We also pass the environment variables, because for some tests we modify
4546 # the environment variables.
4647
4748 subprocess_args = {
48- "shell" : True ,
49+ "shell" : False ,
4950 "stderr" : subprocess .STDOUT ,
5051 "env" : custom_environment ,
5152 }
5253
5354 if not stderr_to_stdout :
54- subprocess_args = {"shell" : True , "env" : custom_environment }
55+ subprocess_args = {"shell" : False , "env" : custom_environment }
5556
5657 if sys .version_info [0 ] != 2 :
5758 subprocess_args ["timeout" ] = timeout
5859
59- output = subprocess .check_output (command_to_execute , ** subprocess_args ).decode (encoding = "UTF-8" )
60+ output = subprocess .check_output (cmd_arguments , ** subprocess_args ).decode (encoding = "UTF-8" )
6061
6162 time_taken = time .time () - start_time
6263 if not do_not_print :
0 commit comments