Skip to content

Commit 347b00e

Browse files
injection fix using shell=false (#45740)
* injection fix using shell=false * original change * ubuntu pipeline fix * cosmetic change * tox formatting --------- Co-authored-by: Ayushh Garg <ayushhgarg@microsoft.com>
1 parent 4120c02 commit 347b00e

2 files changed

Lines changed: 19 additions & 11 deletions

File tree

sdk/ml/azure-ai-ml/azure/ai/ml/_local_endpoints/utilities/commandline_utility.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import subprocess
88
import sys
99
import time
10-
10+
import shlex
1111
from 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:

sdk/ml/azure-ai-ml/azure/ai/ml/operations/_local_endpoint_helper.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,14 @@ def list(self) -> Iterable[OnlineEndpoint]:
121121
endpoint_stubs = self._endpoint_stub.list()
122122
# Iterate through all cached endpoint files
123123
for endpoint_file in endpoint_stubs:
124-
endpoint_json = json.loads(endpoint_file.read_text())
124+
try:
125+
contents = endpoint_file.read_text()
126+
if not contents.strip():
127+
continue
128+
endpoint_json = json.loads(contents)
129+
except (json.JSONDecodeError, OSError):
130+
# Skip files being written concurrently or otherwise unreadable
131+
continue
125132
container = self._docker_client.get_endpoint_container(
126133
endpoint_name=endpoint_json.get("name"), include_stopped=True
127134
)

0 commit comments

Comments
 (0)