|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// |
| 4 | +// Security regression test: credential values must never appear in --credential |
| 5 | +// CLI arguments. OpenShell reads credential values from the environment when |
| 6 | +// only the env-var name is passed (e.g. --credential "NVIDIA_API_KEY"), so |
| 7 | +// there is no reason to pass the secret itself on the command line where it |
| 8 | +// would be visible in `ps aux` output. |
| 9 | + |
| 10 | +"use strict"; |
| 11 | + |
| 12 | +const fs = require("node:fs"); |
| 13 | +const path = require("node:path"); |
| 14 | + |
| 15 | +const ONBOARD_JS = path.join(__dirname, "..", "bin", "lib", "onboard.js"); |
| 16 | +const RUNNER_PY = path.join( |
| 17 | + __dirname, |
| 18 | + "..", |
| 19 | + "nemoclaw-blueprint", |
| 20 | + "orchestrator", |
| 21 | + "runner.py", |
| 22 | +); |
| 23 | + |
| 24 | +// Matches --credential followed by a value containing "=" (i.e. KEY=VALUE). |
| 25 | +// This catches patterns like: |
| 26 | +// --credential "NVIDIA_API_KEY=" + process.env.NVIDIA_API_KEY |
| 27 | +// --credential "OPENAI_API_KEY=somevalue" |
| 28 | +// --credential f"OPENAI_API_KEY={credential}" |
| 29 | +const JS_EXPOSURE_RE = /--credential\s+[^"]*"[A-Z_]+=/; |
| 30 | +const JS_CREDENTIAL_CONCAT_RE = /--credential.*=.*process\.env\./; |
| 31 | +const PY_EXPOSURE_RE = /--credential.*=.*\{/; |
| 32 | + |
| 33 | +describe("credential exposure in process arguments", () => { |
| 34 | + it("onboard.js must not pass KEY=VALUE to --credential", () => { |
| 35 | + const src = fs.readFileSync(ONBOARD_JS, "utf-8"); |
| 36 | + const lines = src.split("\n"); |
| 37 | + |
| 38 | + const violations = lines.filter( |
| 39 | + (line, i) => |
| 40 | + (JS_EXPOSURE_RE.test(line) || JS_CREDENTIAL_CONCAT_RE.test(line)) && |
| 41 | + // Allow comments that describe the old pattern |
| 42 | + !line.trimStart().startsWith("//"), |
| 43 | + ); |
| 44 | + |
| 45 | + expect(violations).toEqual([]); |
| 46 | + }); |
| 47 | + |
| 48 | + it("runner.py must not pass KEY=VALUE to --credential", () => { |
| 49 | + const src = fs.readFileSync(RUNNER_PY, "utf-8"); |
| 50 | + const lines = src.split("\n"); |
| 51 | + |
| 52 | + const violations = lines.filter( |
| 53 | + (line) => |
| 54 | + PY_EXPOSURE_RE.test(line) && |
| 55 | + line.includes("--credential") && |
| 56 | + !line.trimStart().startsWith("#"), |
| 57 | + ); |
| 58 | + |
| 59 | + expect(violations).toEqual([]); |
| 60 | + }); |
| 61 | + |
| 62 | + it("onboard.js --credential flags pass env var names only", () => { |
| 63 | + const src = fs.readFileSync(ONBOARD_JS, "utf-8"); |
| 64 | + |
| 65 | + // Find all --credential arguments and verify they contain only a key name |
| 66 | + // (no "=" sign in the credential value) |
| 67 | + const credentialArgs = src.match(/--credential\s+"([^"]+)"/g) || []; |
| 68 | + const credentialShellQuote = |
| 69 | + src.match(/--credential\s+\$\{shellQuote\("([^"]+)"\)\}/g) || []; |
| 70 | + |
| 71 | + const allArgs = [...credentialArgs, ...credentialShellQuote]; |
| 72 | + expect(allArgs.length).toBeGreaterThan(0); |
| 73 | + |
| 74 | + for (const arg of allArgs) { |
| 75 | + // Extract the credential value from the match |
| 76 | + const valueMatch = |
| 77 | + arg.match(/--credential\s+"([^"]+)"/) || |
| 78 | + arg.match(/--credential\s+\$\{shellQuote\("([^"]+)"\)\}/); |
| 79 | + if (valueMatch) { |
| 80 | + expect(valueMatch[1]).not.toContain("="); |
| 81 | + } |
| 82 | + } |
| 83 | + }); |
| 84 | +}); |
0 commit comments