Skip to content

Commit a1b2e96

Browse files
committed
fix: validate and quote instance name in deploy shell commands
Extends the RFC 1123 validation and double-quoting pattern from #170 (sandbox names) to the deploy() function. All 7 unquoted ${name} interpolations in ssh/scp/rsync commands are now quoted, and instance names are validated at entry to prevent command injection.
1 parent 7d4c8e4 commit a1b2e96

2 files changed

Lines changed: 123 additions & 12 deletions

File tree

‎bin/nemoclaw.js‎

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,17 @@ async function deploy(instanceName) {
105105
if (isRepoPrivate("NVIDIA/OpenShell")) {
106106
await ensureGithubToken();
107107
}
108-
const name = instanceName;
108+
const name = instanceName.trim();
109+
110+
// Validate: same RFC 1123 subdomain rules used for sandbox names — lowercase
111+
// alphanumeric and hyphens, must start and end with alphanumeric.
112+
if (!/^[a-z0-9]([a-z0-9-]*[a-z0-9])?$/.test(name) || name.length > 253) {
113+
console.error(` Invalid instance name: '${name}'`);
114+
console.error(" Names must be lowercase, contain only letters, numbers, and hyphens,");
115+
console.error(" and must start and end with a letter or number (max 253 chars).");
116+
process.exit(1);
117+
}
118+
109119
const gpu = process.env.NEMOCLAW_GPU || "a2-highgpu-1g:nvidia-tesla-a100:1";
110120

111121
console.log("");
@@ -127,7 +137,7 @@ async function deploy(instanceName) {
127137

128138
if (!exists) {
129139
console.log(` Creating Brev instance '${name}' (${gpu})...`);
130-
run(`brev create ${name} --gpu "${gpu}"`);
140+
run(`brev create "${name}" --gpu "${gpu}"`);
131141
} else {
132142
console.log(` Brev instance '${name}' already exists.`);
133143
}
@@ -137,7 +147,7 @@ async function deploy(instanceName) {
137147
console.log(" Waiting for SSH...");
138148
for (let i = 0; i < 60; i++) {
139149
try {
140-
execSync(`ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no ${name} 'echo ok' 2>/dev/null`, { encoding: "utf-8", stdio: "pipe" });
150+
execSync(`ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no "${name}" 'echo ok' 2>/dev/null`, { encoding: "utf-8", stdio: "pipe" });
141151
break;
142152
} catch {
143153
if (i === 59) {
@@ -149,31 +159,35 @@ async function deploy(instanceName) {
149159
}
150160

151161
console.log(" Syncing NemoClaw to VM...");
152-
run(`ssh -o StrictHostKeyChecking=no -o LogLevel=ERROR ${name} 'mkdir -p /home/ubuntu/nemoclaw'`);
153-
run(`rsync -az --delete --exclude node_modules --exclude .git --exclude src -e "ssh -o StrictHostKeyChecking=no -o LogLevel=ERROR" "${ROOT}/scripts" "${ROOT}/Dockerfile" "${ROOT}/nemoclaw" "${ROOT}/nemoclaw-blueprint" "${ROOT}/bin" "${ROOT}/package.json" ${name}:/home/ubuntu/nemoclaw/`);
162+
run(`ssh -o StrictHostKeyChecking=no -o LogLevel=ERROR "${name}" 'mkdir -p /home/ubuntu/nemoclaw'`);
163+
run(`rsync -az --delete --exclude node_modules --exclude .git --exclude src -e "ssh -o StrictHostKeyChecking=no -o LogLevel=ERROR" "${ROOT}/scripts" "${ROOT}/Dockerfile" "${ROOT}/nemoclaw" "${ROOT}/nemoclaw-blueprint" "${ROOT}/bin" "${ROOT}/package.json" "${name}":/home/ubuntu/nemoclaw/`);
154164

155165
const envLines = [`NVIDIA_API_KEY=${process.env.NVIDIA_API_KEY}`];
156166
const ghToken = process.env.GITHUB_TOKEN;
157167
if (ghToken) envLines.push(`GITHUB_TOKEN=${ghToken}`);
158168
const tgToken = getCredential("TELEGRAM_BOT_TOKEN");
159169
if (tgToken) envLines.push(`TELEGRAM_BOT_TOKEN=${tgToken}`);
160-
const envTmp = path.join(os.tmpdir(), `nemoclaw-env-${Date.now()}`);
161-
fs.writeFileSync(envTmp, envLines.join("\n") + "\n", { mode: 0o600 });
162-
run(`scp -q -o StrictHostKeyChecking=no -o LogLevel=ERROR "${envTmp}" ${name}:/home/ubuntu/nemoclaw/.env`);
163-
fs.unlinkSync(envTmp);
170+
const envDir = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-env-"));
171+
const envTmp = path.join(envDir, ".env");
172+
try {
173+
fs.writeFileSync(envTmp, envLines.join("\n") + "\n", { mode: 0o600, flag: "wx" });
174+
run(`scp -q -o StrictHostKeyChecking=no -o LogLevel=ERROR "${envTmp}" "${name}":/home/ubuntu/nemoclaw/.env`);
175+
} finally {
176+
fs.rmSync(envDir, { recursive: true, force: true });
177+
}
164178

165179
console.log(" Running setup...");
166-
runInteractive(`ssh -t -o StrictHostKeyChecking=no -o LogLevel=ERROR ${name} 'cd /home/ubuntu/nemoclaw && set -a && . .env && set +a && bash scripts/brev-setup.sh'`);
180+
runInteractive(`ssh -t -o StrictHostKeyChecking=no -o LogLevel=ERROR "${name}" 'cd /home/ubuntu/nemoclaw && set -a && . .env && set +a && bash scripts/brev-setup.sh'`);
167181

168182
if (tgToken) {
169183
console.log(" Starting services...");
170-
run(`ssh -o StrictHostKeyChecking=no -o LogLevel=ERROR ${name} 'cd /home/ubuntu/nemoclaw && set -a && . .env && set +a && bash scripts/start-services.sh'`);
184+
run(`ssh -o StrictHostKeyChecking=no -o LogLevel=ERROR "${name}" 'cd /home/ubuntu/nemoclaw && set -a && . .env && set +a && bash scripts/start-services.sh'`);
171185
}
172186

173187
console.log("");
174188
console.log(" Connecting to sandbox...");
175189
console.log("");
176-
runInteractive(`ssh -t -o StrictHostKeyChecking=no -o LogLevel=ERROR ${name} 'cd /home/ubuntu/nemoclaw && set -a && . .env && set +a && openshell sandbox connect nemoclaw'`);
190+
runInteractive(`ssh -t -o StrictHostKeyChecking=no -o LogLevel=ERROR "${name}" 'cd /home/ubuntu/nemoclaw && set -a && . .env && set +a && openshell sandbox connect nemoclaw'`);
177191
}
178192

179193
async function start() {

‎test/deploy-instance-name.test.js‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
//
4+
// Verify that deploy() validates and quotes the instance name in shell
5+
// commands to prevent command injection.
6+
7+
const { describe, it } = require("node:test");
8+
const assert = require("node:assert/strict");
9+
const fs = require("node:fs");
10+
const path = require("node:path");
11+
12+
const ROOT = path.resolve(__dirname, "..");
13+
const source = fs.readFileSync(path.join(ROOT, "bin/nemoclaw.js"), "utf-8");
14+
15+
// Extract the full deploy() function body using brace-depth counting
16+
// so we don't stop at the first inner closing brace.
17+
function extractDeploy(src) {
18+
const start = src.indexOf("async function deploy(");
19+
if (start === -1) return null;
20+
const open = src.indexOf("{", start);
21+
if (open === -1) return null;
22+
23+
let depth = 0;
24+
let end = -1;
25+
for (let i = open; i < src.length; i++) {
26+
if (src[i] === "{") depth++;
27+
if (src[i] === "}") depth--;
28+
if (depth === 0) {
29+
end = i + 1;
30+
break;
31+
}
32+
}
33+
if (end === -1) return null;
34+
return src.slice(start, end);
35+
}
36+
37+
const deployBody = extractDeploy(source);
38+
39+
describe("deploy instance name hardening", () => {
40+
it("can extract deploy() function body", () => {
41+
assert.ok(deployBody, "Could not find deploy() function");
42+
});
43+
44+
it("enforces RFC 1123 validation inside deploy()", () => {
45+
assert.match(
46+
deployBody,
47+
/\^\[a-z0-9\]/,
48+
"deploy() must validate the instance name against RFC 1123 subdomain rules"
49+
);
50+
});
51+
52+
it("rejects names longer than 253 characters inside deploy()", () => {
53+
assert.match(
54+
deployBody,
55+
/name\.length\s*>\s*253/,
56+
"deploy() must enforce a 253-character limit on instance names"
57+
);
58+
});
59+
60+
it("does not interpolate unquoted instance name in shell commands", () => {
61+
// Every ${name} in a shell command string must be exactly "${name}".
62+
// Skip console.log/error lines which are display-only.
63+
const lines = deployBody.split("\n");
64+
for (const line of lines) {
65+
if (line.includes("${name}") && !line.match(/console\.(log|error)/)) {
66+
const all = [...line.matchAll(/\$\{name\}/g)].length;
67+
const quoted = [...line.matchAll(/"\$\{name\}"/g)].length;
68+
assert.equal(
69+
quoted,
70+
all,
71+
`Unquoted \${name} in shell command: ${line.trim()}`
72+
);
73+
}
74+
}
75+
});
76+
77+
it("quotes instance name in brev create", () => {
78+
assert.ok(
79+
deployBody.includes('brev create "${name}"'),
80+
'brev create must quote the instance name: brev create "${name}"'
81+
);
82+
});
83+
84+
it("quotes instance name in rsync destination", () => {
85+
assert.ok(
86+
deployBody.includes('"${name}":/home/ubuntu/nemoclaw/'),
87+
'rsync destination must quote the instance name: "${name}":/path'
88+
);
89+
});
90+
91+
it("quotes instance name in scp destination", () => {
92+
assert.ok(
93+
deployBody.includes('"${name}":/home/ubuntu/nemoclaw/.env'),
94+
'scp destination must quote the instance name: "${name}":/path'
95+
);
96+
});
97+
});

0 commit comments

Comments
 (0)