Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 25 additions & 25 deletions src/madengine/tools/run_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,13 @@ def run_model_impl(
print(f"MAD_CONTAINER_IMAGE is {run_details.docker_image}")
print(f"Warning: User override MAD_CONTAINER_IMAGE. Model support on image not guaranteed.")

persistent_hf_cache_dir = self.context.ctx.get("persistent_hf_cache_dir", None)
hf_models_cache_volume_name = "hf_models_cache_volume"

# Create Docker volume for models caching
if persistent_hf_cache_dir:
self.console.sh(f"docker volume create {hf_models_cache_volume_name}")

# prepare docker run options
gpu_vendor = self.context.ctx["gpu_vendor"]
docker_options = ""
Expand Down Expand Up @@ -695,13 +702,17 @@ def run_model_impl(
# Must set env vars and mounts at the end
docker_options += self.get_env_arg(run_env)
docker_options += self.get_mount_arg(mount_datapaths)

if persistent_hf_cache_dir:
docker_options += f" -v {hf_models_cache_volume_name}:{persistent_hf_cache_dir} "
Copy link

Copilot AI Nov 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The persistent_hf_cache_dir value from context is used directly in the Docker mount without validation. This could lead to arbitrary directory mounting if the context contains malicious input. Add validation to ensure the path is safe and doesn't contain dangerous characters or paths like /, /etc, or similar sensitive directories.

Copilot uses AI. Check for mistakes.

docker_options += f" {run_details.additional_docker_run_options}"

# if --shm-size is set, remove --ipc=host
if "SHM_SIZE" in self.context.ctx:
docker_options = docker_options.replace("--ipc=host", "")

print(docker_options)
print(f"Docker options: {docker_options}")

# get machine name
run_details.machine_name = self.console.sh("hostname")
Expand Down Expand Up @@ -746,6 +757,10 @@ def run_model_impl(

model_docker.sh("rm -rf " + model_dir, timeout=240)

if persistent_hf_cache_dir:
print("HF models cache directory content:")
model_docker.sh(f"ls -la {persistent_hf_cache_dir}")

# set safe.directory for workspace
model_docker.sh("git config --global --add safe.directory /myworkspace")

Expand Down Expand Up @@ -841,34 +856,19 @@ def run_model_impl(

# run model
test_start_time = time.time()
model_args = self.context.ctx.get("model_args", info["args"])

if persistent_hf_cache_dir:
model_args += f" --hf_cache_dir {persistent_hf_cache_dir} "
Copy link

Copilot AI Nov 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The persistent_hf_cache_dir is concatenated directly into the command string without shell escaping. If the path contains spaces or special characters, it could break the command or introduce command injection vulnerabilities. Use proper shell escaping (e.g., shlex.quote()) when building the command string.

Copilot uses AI. Check for mistakes.

run_command = f"cd {model_dir} && {script_name} {model_args}"

if not self.args.skip_model_run:
print("Running model...")
if "model_args" in self.context.ctx:
model_docker.sh(
"cd "
+ model_dir
+ " && "
+ script_name
+ " "
+ self.context.ctx["model_args"],
timeout=None,
)
else:
model_docker.sh(
"cd " + model_dir + " && " + script_name + " " + info["args"],
timeout=None,
)
model_docker.sh(run_command, timeout=None)
else:
print("Skipping model run")
print(
"To run model: "
+ "cd "
+ model_dir
+ " && "
+ script_name
+ " "
+ info["args"]
)
print(f"To run model: {run_command}")

run_details.test_duration = time.time() - test_start_time
print("Test Duration: {} seconds".format(run_details.test_duration))
Expand Down