Skip to content

Commit e3ccf7c

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 e3ccf7c

2 files changed

Lines changed: 97 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: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
describe("deploy instance name hardening", () => {
16+
it("validates instance name with RFC 1123 regex", () => {
17+
assert.ok(
18+
source.includes("[a-z0-9-]") && source.includes("deploy"),
19+
"deploy() must validate the instance name against RFC 1123 subdomain rules"
20+
);
21+
});
22+
23+
it("rejects names longer than 253 characters", () => {
24+
assert.ok(
25+
source.includes("253"),
26+
"deploy() must enforce a 253-character limit on instance names"
27+
);
28+
});
29+
30+
it("does not interpolate unquoted instance name in ssh commands", () => {
31+
// Extract lines inside deploy() that call ssh/scp/rsync with the instance name.
32+
// After the fix, every ${name} in a shell string must be wrapped in double quotes.
33+
const deployMatch = source.match(/async function deploy\([\s\S]*?\n\}/);
34+
assert.ok(deployMatch, "Could not find deploy() function");
35+
const deployBody = deployMatch[0];
36+
37+
// Find all shell-interpolated ${name} occurrences (skip console.log/error lines)
38+
const lines = deployBody.split("\n");
39+
for (const line of lines) {
40+
if (line.includes("${name}") && !line.match(/console\.(log|error)/)) {
41+
// In shell command strings, ${name} must be inside double quotes: "${name}"
42+
const unquoted = line.match(/[^"]\$\{name\}[^"]/);
43+
assert.ok(
44+
!unquoted,
45+
`Unquoted \${name} in shell command: ${line.trim()}`
46+
);
47+
}
48+
}
49+
});
50+
51+
it("quotes instance name in brev create", () => {
52+
assert.ok(
53+
source.includes('brev create "${name}"'),
54+
'brev create must quote the instance name: brev create "${name}"'
55+
);
56+
});
57+
58+
it("quotes instance name in rsync destination", () => {
59+
assert.ok(
60+
source.includes('"${name}":/home/ubuntu/nemoclaw/'),
61+
'rsync destination must quote the instance name: "${name}":/path'
62+
);
63+
});
64+
65+
it("quotes instance name in scp destination", () => {
66+
assert.ok(
67+
source.includes('"${name}":/home/ubuntu/nemoclaw/.env'),
68+
'scp destination must quote the instance name: "${name}":/path'
69+
);
70+
});
71+
});

0 commit comments

Comments
 (0)