|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// |
| 4 | +// Verify that real API keys are never passed as literal values in --credential |
| 5 | +// CLI arguments. Secrets must use the env-lookup form (--credential KEY) so |
| 6 | +// they don't appear in `ps aux`. |
| 7 | +// |
| 8 | +// See: https://github.com/NVIDIA/NemoClaw/issues/325 |
| 9 | + |
| 10 | +const { describe, it } = require("node:test"); |
| 11 | +const assert = require("node:assert/strict"); |
| 12 | +const fs = require("node:fs"); |
| 13 | +const path = require("node:path"); |
| 14 | + |
| 15 | +const ROOT = path.resolve(__dirname, ".."); |
| 16 | + |
| 17 | +// Patterns that indicate a real secret is being passed as a literal value |
| 18 | +// in a --credential argument. These MUST NOT appear in the codebase. |
| 19 | +// |
| 20 | +// Safe (env-lookup form): --credential "NVIDIA_API_KEY" |
| 21 | +// Safe (dummy value): --credential "OPENAI_API_KEY=dummy" |
| 22 | +// Safe (ollama value): --credential "OPENAI_API_KEY=ollama" |
| 23 | +// UNSAFE (leaks real key): --credential "NVIDIA_API_KEY=${process.env.NVIDIA_API_KEY}" |
| 24 | +// UNSAFE (leaks real key): --credential f"OPENAI_API_KEY={credential}" |
| 25 | + |
| 26 | +// Allowlist: these KEY=VALUE forms are safe because the values are not secrets |
| 27 | +const ALLOWED_CREDENTIAL_VALUES = [ |
| 28 | + "OPENAI_API_KEY=dummy", |
| 29 | + "OPENAI_API_KEY=ollama", |
| 30 | + "OPENAI_API_KEY=not-needed", |
| 31 | +]; |
| 32 | + |
| 33 | +describe("credential exposure: no secrets in --credential CLI args", () => { |
| 34 | + const filesToCheck = [ |
| 35 | + { path: "bin/lib/onboard.js", lang: "js" }, |
| 36 | + { path: "nemoclaw/src/commands/onboard.ts", lang: "ts" }, |
| 37 | + { path: "nemoclaw-blueprint/orchestrator/runner.py", lang: "py" }, |
| 38 | + ]; |
| 39 | + |
| 40 | + for (const file of filesToCheck) { |
| 41 | + it(`${file.path}: no secret values in --credential args`, () => { |
| 42 | + const fullPath = path.join(ROOT, file.path); |
| 43 | + if (!fs.existsSync(fullPath)) { |
| 44 | + // File may not exist in all branches; skip gracefully |
| 45 | + return; |
| 46 | + } |
| 47 | + const content = fs.readFileSync(fullPath, "utf-8"); |
| 48 | + const lines = content.split("\n"); |
| 49 | + |
| 50 | + for (let i = 0; i < lines.length; i++) { |
| 51 | + const line = lines[i]; |
| 52 | + // Skip comments |
| 53 | + if (line.trim().startsWith("//") || line.trim().startsWith("#")) continue; |
| 54 | + |
| 55 | + // Look for --credential with a KEY=VALUE pattern |
| 56 | + const credMatch = line.match(/--credential[",\s]+["'`]?([A-Z_]+)=(.+?)["'`]/); |
| 57 | + if (!credMatch) continue; |
| 58 | + |
| 59 | + const key = credMatch[1]; |
| 60 | + const fullValue = `${key}=${credMatch[2].replace(/["'`}\s]/g, "")}`; |
| 61 | + |
| 62 | + // Check if this is in the allowlist |
| 63 | + const isAllowed = ALLOWED_CREDENTIAL_VALUES.some((allowed) => |
| 64 | + fullValue.startsWith(allowed.replace(/["']/g, "")) |
| 65 | + ); |
| 66 | + |
| 67 | + if (!isAllowed) { |
| 68 | + assert.fail( |
| 69 | + `${file.path}:${i + 1}: --credential passes secret as literal value: "${fullValue}". ` + |
| 70 | + `Use env-lookup form instead: --credential "${key}" ` + |
| 71 | + `(set the env var before the call so openshell reads it internally)` |
| 72 | + ); |
| 73 | + } |
| 74 | + } |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + it("onboard.js: nvidia-nim provider uses env-lookup form", () => { |
| 79 | + const content = fs.readFileSync(path.join(ROOT, "bin/lib/onboard.js"), "utf-8"); |
| 80 | + // Find the nvidia-nim provider create block |
| 81 | + const nimBlock = content.match(/provider === "nvidia-nim"[\s\S]*?(?=\} else if|\} else \{)/); |
| 82 | + assert.ok(nimBlock, "Could not find nvidia-nim provider block"); |
| 83 | + |
| 84 | + // Must contain --credential "NVIDIA_API_KEY" (env-lookup, no =value) |
| 85 | + assert.ok( |
| 86 | + nimBlock[0].includes('--credential "NVIDIA_API_KEY"') || |
| 87 | + nimBlock[0].includes("--credential NVIDIA_API_KEY"), |
| 88 | + 'nvidia-nim block must use env-lookup form: --credential "NVIDIA_API_KEY"' |
| 89 | + ); |
| 90 | + |
| 91 | + // Must NOT contain --credential "NVIDIA_API_KEY=${ |
| 92 | + assert.ok( |
| 93 | + !nimBlock[0].includes("NVIDIA_API_KEY=${"), |
| 94 | + "nvidia-nim block must NOT interpolate the key value into the credential arg" |
| 95 | + ); |
| 96 | + }); |
| 97 | + |
| 98 | + it("onboard.ts: credential passed as env var name only", () => { |
| 99 | + const tsPath = path.join(ROOT, "nemoclaw/src/commands/onboard.ts"); |
| 100 | + if (!fs.existsSync(tsPath)) return; |
| 101 | + const content = fs.readFileSync(tsPath, "utf-8"); |
| 102 | + |
| 103 | + // Must set env var before execOpenShell |
| 104 | + assert.ok( |
| 105 | + content.includes("process.env[credentialEnv] = apiKey"), |
| 106 | + "onboard.ts must set process.env[credentialEnv] before execOpenShell" |
| 107 | + ); |
| 108 | + |
| 109 | + // The --credential arg in the provider create/update must NOT contain = |
| 110 | + // Find lines with "--credential" followed by a value containing ${ |
| 111 | + const dangerousPattern = /["']--credential["'],\s*[`"']?\$\{credentialEnv\}=\$\{apiKey\}/; |
| 112 | + assert.ok( |
| 113 | + !dangerousPattern.test(content), |
| 114 | + "onboard.ts must not pass credentialEnv=apiKey as --credential value" |
| 115 | + ); |
| 116 | + }); |
| 117 | + |
| 118 | + it("runner.py: credential passed as env var name only", () => { |
| 119 | + const pyPath = path.join(ROOT, "nemoclaw-blueprint/orchestrator/runner.py"); |
| 120 | + if (!fs.existsSync(pyPath)) return; |
| 121 | + const content = fs.readFileSync(pyPath, "utf-8"); |
| 122 | + |
| 123 | + // Must set os.environ before run_cmd |
| 124 | + assert.ok( |
| 125 | + content.includes("os.environ[target_cred_env] = credential"), |
| 126 | + "runner.py must set os.environ[target_cred_env] before run_cmd" |
| 127 | + ); |
| 128 | + |
| 129 | + // Must NOT have f"OPENAI_API_KEY={credential}" pattern |
| 130 | + assert.ok( |
| 131 | + !content.includes('f"OPENAI_API_KEY={credential}"'), |
| 132 | + 'runner.py must not pass f"OPENAI_API_KEY={credential}" as --credential value' |
| 133 | + ); |
| 134 | + }); |
| 135 | +}); |
0 commit comments