Skip to content

Commit 3f78e93

Browse files
committed
fix(security): use argv arrays for docker and curl to prevent shell injection
1 parent 6e1208c commit 3f78e93

6 files changed

Lines changed: 264 additions & 118 deletions

File tree

‎bin/lib/local-inference.js‎

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ function getLocalProviderBaseUrl(provider) {
2121
function getLocalProviderHealthCheck(provider) {
2222
switch (provider) {
2323
case "vllm-local":
24-
return "curl -sf http://localhost:8000/v1/models 2>/dev/null";
24+
return ["curl", "-sf", "http://localhost:8000/v1/models"];
2525
case "ollama-local":
26-
return "curl -sf http://localhost:11434/api/tags 2>/dev/null";
26+
return ["curl", "-sf", "http://localhost:11434/api/tags"];
2727
default:
2828
return null;
2929
}
@@ -32,9 +32,27 @@ function getLocalProviderHealthCheck(provider) {
3232
function getLocalProviderContainerReachabilityCheck(provider) {
3333
switch (provider) {
3434
case "vllm-local":
35-
return `docker run --rm --add-host host.openshell.internal:host-gateway ${CONTAINER_REACHABILITY_IMAGE} -sf http://host.openshell.internal:8000/v1/models 2>/dev/null`;
35+
return [
36+
"docker",
37+
"run",
38+
"--rm",
39+
"--add-host",
40+
"host.openshell.internal:host-gateway",
41+
CONTAINER_REACHABILITY_IMAGE,
42+
"-sf",
43+
"http://host.openshell.internal:8000/v1/models",
44+
];
3645
case "ollama-local":
37-
return `docker run --rm --add-host host.openshell.internal:host-gateway ${CONTAINER_REACHABILITY_IMAGE} -sf http://host.openshell.internal:11434/api/tags 2>/dev/null`;
46+
return [
47+
"docker",
48+
"run",
49+
"--rm",
50+
"--add-host",
51+
"host.openshell.internal:host-gateway",
52+
CONTAINER_REACHABILITY_IMAGE,
53+
"-sf",
54+
"http://host.openshell.internal:11434/api/tags",
55+
];
3856
default:
3957
return null;
4058
}
@@ -103,7 +121,7 @@ function parseOllamaList(output) {
103121
}
104122

105123
function getOllamaModelOptions(runCapture) {
106-
const output = runCapture("ollama list 2>/dev/null", { ignoreError: true });
124+
const output = runCapture(["ollama", "list"], { ignoreError: true });
107125
const parsed = parseOllamaList(output);
108126
if (parsed.length > 0) {
109127
return parsed;
@@ -123,7 +141,11 @@ function getOllamaWarmupCommand(model, keepAlive = "15m") {
123141
stream: false,
124142
keep_alive: keepAlive,
125143
});
126-
return `nohup curl -s http://localhost:11434/api/generate -H 'Content-Type: application/json' -d ${shellQuote(payload)} >/dev/null 2>&1 &`;
144+
return [
145+
"bash",
146+
"-c",
147+
`nohup curl -s http://localhost:11434/api/generate -H 'Content-Type: application/json' -d ${shellQuote(payload)} >/dev/null 2>&1 &`,
148+
];
127149
}
128150

129151
function getOllamaProbeCommand(model, timeoutSeconds = 120, keepAlive = "15m") {
@@ -133,7 +155,17 @@ function getOllamaProbeCommand(model, timeoutSeconds = 120, keepAlive = "15m") {
133155
stream: false,
134156
keep_alive: keepAlive,
135157
});
136-
return `curl -sS --max-time ${timeoutSeconds} http://localhost:11434/api/generate -H 'Content-Type: application/json' -d ${shellQuote(payload)} 2>/dev/null`;
158+
return [
159+
"curl",
160+
"-sS",
161+
"--max-time",
162+
String(timeoutSeconds),
163+
"http://localhost:11434/api/generate",
164+
"-H",
165+
"Content-Type: application/json",
166+
"-d",
167+
payload,
168+
];
137169
}
138170

139171
function validateOllamaModel(model, runCapture) {

‎bin/lib/nim.js‎

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function detectGpu() {
2727
// Try NVIDIA first — query VRAM
2828
try {
2929
const output = runCapture(
30-
"nvidia-smi --query-gpu=memory.total --format=csv,noheader,nounits",
30+
["nvidia-smi", "--query-gpu=memory.total", "--format=csv,noheader,nounits"],
3131
{ ignoreError: true }
3232
);
3333
if (output) {
@@ -49,15 +49,21 @@ function detectGpu() {
4949
// Fallback: DGX Spark (GB10) — VRAM not queryable due to unified memory architecture
5050
try {
5151
const nameOutput = runCapture(
52-
"nvidia-smi --query-gpu=name --format=csv,noheader,nounits",
52+
["nvidia-smi", "--query-gpu=name", "--format=csv,noheader,nounits"],
5353
{ ignoreError: true }
5454
);
5555
if (nameOutput && nameOutput.includes("GB10")) {
5656
// GB10 has 128GB unified memory shared with Grace CPU — use system RAM
5757
let totalMemoryMB = 0;
5858
try {
59-
const memLine = runCapture("free -m | awk '/Mem:/ {print $2}'", { ignoreError: true });
60-
if (memLine) totalMemoryMB = parseInt(memLine.trim(), 10) || 0;
59+
const freeOut = runCapture(["free", "-m"], { ignoreError: true });
60+
if (freeOut) {
61+
const memLine = freeOut.split("\n").find((l) => l.includes("Mem:"));
62+
if (memLine) {
63+
const parts = memLine.split(/\s+/);
64+
totalMemoryMB = parseInt(parts[1], 10) || 0;
65+
}
66+
}
6167
} catch {}
6268
return {
6369
type: "nvidia",
@@ -74,7 +80,7 @@ function detectGpu() {
7480
if (process.platform === "darwin") {
7581
try {
7682
const spOutput = runCapture(
77-
"system_profiler SPDisplaysDataType 2>/dev/null",
83+
["system_profiler", "SPDisplaysDataType"],
7884
{ ignoreError: true }
7985
);
8086
if (spOutput) {
@@ -92,7 +98,7 @@ function detectGpu() {
9298
} else {
9399
// Apple Silicon shares system RAM — read total memory
94100
try {
95-
const memBytes = runCapture("sysctl -n hw.memsize", { ignoreError: true });
101+
const memBytes = runCapture(["sysctl", "-n", "hw.memsize"], { ignoreError: true });
96102
if (memBytes) memoryMB = Math.floor(parseInt(memBytes, 10) / 1024 / 1024);
97103
} catch {}
98104
}
@@ -121,7 +127,7 @@ function pullNimImage(model) {
121127
process.exit(1);
122128
}
123129
console.log(` Pulling NIM image: ${image}`);
124-
run(`docker pull ${shellQuote(image)}`);
130+
run(["docker", "pull", image]);
125131
return image;
126132
}
127133

@@ -134,13 +140,23 @@ function startNimContainer(sandboxName, model, port = 8000) {
134140
}
135141

136142
// Stop any existing container with same name
137-
const qn = shellQuote(name);
138-
run(`docker rm -f ${qn} 2>/dev/null || true`, { ignoreError: true });
143+
run(["docker", "rm", "-f", name], { ignoreError: true });
139144

140145
console.log(` Starting NIM container: ${name}`);
141-
run(
142-
`docker run -d --gpus all -p ${Number(port)}:8000 --name ${qn} --shm-size 16g ${shellQuote(image)}`
143-
);
146+
run([
147+
"docker",
148+
"run",
149+
"-d",
150+
"--gpus",
151+
"all",
152+
"-p",
153+
`${port}:8000`,
154+
"--name",
155+
name,
156+
"--shm-size",
157+
"16g",
158+
image,
159+
]);
144160
return name;
145161
}
146162

@@ -152,7 +168,7 @@ function waitForNimHealth(port = 8000, timeout = 300) {
152168

153169
while ((Date.now() - start) / 1000 < timeout) {
154170
try {
155-
const result = runCapture(`curl -sf http://localhost:${safePort}/v1/models`, {
171+
const result = runCapture(["curl", "-sf", `http://localhost:${port}/v1/models`], {
156172
ignoreError: true,
157173
});
158174
if (result) {
@@ -169,24 +185,23 @@ function waitForNimHealth(port = 8000, timeout = 300) {
169185

170186
function stopNimContainer(sandboxName) {
171187
const name = containerName(sandboxName);
172-
const qn = shellQuote(name);
173188
console.log(` Stopping NIM container: ${name}`);
174-
run(`docker stop ${qn} 2>/dev/null || true`, { ignoreError: true });
175-
run(`docker rm ${qn} 2>/dev/null || true`, { ignoreError: true });
189+
run(["docker", "stop", name], { ignoreError: true });
190+
run(["docker", "rm", name], { ignoreError: true });
176191
}
177192

178193
function nimStatus(sandboxName) {
179194
const name = containerName(sandboxName);
180195
try {
181196
const state = runCapture(
182-
`docker inspect --format '{{.State.Status}}' ${shellQuote(name)} 2>/dev/null`,
197+
["docker", "inspect", "--format", "{{.State.Status}}", name],
183198
{ ignoreError: true }
184199
);
185200
if (!state) return { running: false, container: name };
186201

187202
let healthy = false;
188203
if (state === "running") {
189-
const health = runCapture(`curl -sf http://localhost:8000/v1/models 2>/dev/null`, {
204+
const health = runCapture(["curl", "-sf", "http://localhost:8000/v1/models"], {
190205
ignoreError: true,
191206
});
192207
healthy = !!health;

0 commit comments

Comments
 (0)