Repository navigation
security: redact secret patterns from CLI error output #1247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
BenediktSchackenberg
wants to merge
4
commits into
NVIDIA:main
from
BenediktSchackenberg:fix/redact-secrets-794
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
79313ec
fix(security): redact secret patterns from CLI log and error output
BenediktSchackenberg 7054f83
fix(security): redact err.output array in runCapture error handler
BenediktSchackenberg c644b49
Merge branch 'main' into fix/redact-secrets-794
BenediktSchackenberg 2997be9
Merge branch 'main' into fix/redact-secrets-794
cv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { describe, it, expect } from "vitest"; | ||
| import { createRequire } from "node:module"; | ||
| import path from "node:path"; | ||
| import { spawnSync } from "node:child_process"; | ||
|
|
||
| const require = createRequire(import.meta.url); | ||
| const { redactSecrets, runCapture } = require("../bin/lib/runner"); | ||
|
|
||
| describe("redactSecrets", () => { | ||
| it("redacts NVIDIA_API_KEY=value", () => { | ||
| const input = 'openshell provider create --credential "NVIDIA_API_KEY=nvapi-abc123XYZ"'; | ||
| const result = redactSecrets(input); | ||
| expect(result).not.toContain("nvapi-abc123XYZ"); | ||
| expect(result).toContain("NVIDIA_API_KEY=***"); | ||
| }); | ||
|
|
||
| it("redacts bare nvapi- tokens", () => { | ||
| const input = "Bearer nvapi-SomeSecretToken123"; | ||
| const result = redactSecrets(input); | ||
| expect(result).not.toContain("nvapi-SomeSecretToken123"); | ||
| expect(result).toContain("nvapi-So***"); | ||
| }); | ||
|
|
||
| it("redacts GITHUB_TOKEN=value", () => { | ||
| const result = redactSecrets("GITHUB_TOKEN=ghp_1234567890abcdef"); | ||
| expect(result).not.toContain("ghp_1234567890abcdef"); | ||
| expect(result).toContain("GITHUB_TOKEN=***"); | ||
| }); | ||
|
|
||
| it("redacts TELEGRAM_BOT_TOKEN=value", () => { | ||
| const result = redactSecrets("TELEGRAM_BOT_TOKEN=123456:ABC-DEF"); | ||
| expect(result).not.toContain("123456:ABC-DEF"); | ||
| expect(result).toContain("TELEGRAM_BOT_TOKEN=***"); | ||
| }); | ||
|
|
||
| it("redacts OPENAI_API_KEY=value", () => { | ||
| const result = redactSecrets("OPENAI_API_KEY=sk-proj-abc123"); | ||
| expect(result).not.toContain("sk-proj-abc123"); | ||
| expect(result).toContain("OPENAI_API_KEY=***"); | ||
| }); | ||
|
|
||
| it("redacts SLACK_BOT_TOKEN=value", () => { | ||
| const result = redactSecrets("SLACK_BOT_TOKEN=xoxb-1234"); | ||
| expect(result).not.toContain("xoxb-1234"); | ||
| expect(result).toContain("SLACK_BOT_TOKEN=***"); | ||
| }); | ||
|
|
||
| it("redacts DISCORD_BOT_TOKEN=value", () => { | ||
| const result = redactSecrets("DISCORD_BOT_TOKEN=MTk4NjIy"); | ||
| expect(result).not.toContain("MTk4NjIy"); | ||
| expect(result).toContain("DISCORD_BOT_TOKEN=***"); | ||
| }); | ||
|
|
||
| it("returns input unchanged when no secrets present", () => { | ||
| const input = "openshell sandbox create --name my-assistant"; | ||
| expect(redactSecrets(input)).toBe(input); | ||
| }); | ||
|
|
||
| it("redacts multiple different secrets in one string", () => { | ||
| const input = "NVIDIA_API_KEY=nvapi-secret GITHUB_TOKEN=ghp_token123"; | ||
| const result = redactSecrets(input); | ||
| expect(result).not.toContain("nvapi-secret"); | ||
| expect(result).not.toContain("ghp_token123"); | ||
| expect(result).toContain("NVIDIA_API_KEY=***"); | ||
| expect(result).toContain("GITHUB_TOKEN=***"); | ||
| }); | ||
|
|
||
| it("redacts double-quoted secret values", () => { | ||
| const result = redactSecrets('GITHUB_TOKEN="ghp_secretValue123"'); | ||
| expect(result).not.toContain("ghp_secretValue123"); | ||
| expect(result).toBe("GITHUB_TOKEN=***"); | ||
| }); | ||
|
|
||
| it("redacts single-quoted secret values", () => { | ||
| const result = redactSecrets("NVIDIA_API_KEY='nvapi-secretValue123'"); | ||
| expect(result).not.toContain("nvapi-secretValue123"); | ||
| expect(result).toBe("NVIDIA_API_KEY=***"); | ||
| }); | ||
|
|
||
| it("handles empty string", () => { | ||
| expect(redactSecrets("")).toBe(""); | ||
| }); | ||
|
|
||
| it("handles null and undefined without throwing", () => { | ||
| expect(redactSecrets(null)).toBe(""); | ||
| expect(redactSecrets(undefined)).toBe(""); | ||
| }); | ||
|
|
||
| it("produces identical results on consecutive calls", () => { | ||
| const input = "NVIDIA_API_KEY=nvapi-test123"; | ||
| const r1 = redactSecrets(input); | ||
| const r2 = redactSecrets(input); | ||
| expect(r1).toBe(r2); | ||
| expect(r1).not.toContain("nvapi-test123"); | ||
| }); | ||
|
|
||
| it("redacts generic env-style secret names", () => { | ||
| const result = redactSecrets('TEST_API_KEY="super-secret-value"'); | ||
| expect(result).toBe("TEST_API_KEY=***"); | ||
| expect(result).not.toContain("super-secret-value"); | ||
| }); | ||
|
|
||
| it("run() error output redacts secrets (integration)", () => { | ||
| // Spawn a child that requires runner.js and calls run() with a command | ||
| // that will fail, then captures stderr to verify redaction. | ||
| const runnerPath = path.join(import.meta.dirname, "..", "bin", "lib", "runner"); | ||
| const script = ` | ||
| const { run } = require(${JSON.stringify(runnerPath)}); | ||
| // Override process.exit so the child doesn't exit before we capture output | ||
| process.exit = () => {}; | ||
| run("false NVIDIA_API_KEY=nvapi-realSecretValue123"); | ||
| `; | ||
| const result = spawnSync("node", ["-e", script], { | ||
| cwd: path.join(import.meta.dirname, ".."), | ||
| encoding: "utf-8", | ||
| timeout: 10000, | ||
| }); | ||
| expect(result.stderr).toContain("NVIDIA_API_KEY=***"); | ||
| expect(result.stderr).not.toContain("nvapi-realSecretValue123"); | ||
| }); | ||
|
BenediktSchackenberg marked this conversation as resolved.
|
||
|
|
||
| it("run() redacts secrets when command is truncated before closing quote (regression)", () => { | ||
| const runnerPath = path.join(import.meta.dirname, "..", "bin", "lib", "runner"); | ||
| const script = ` | ||
| const { run } = require(${JSON.stringify(runnerPath)}); | ||
| process.exit = () => {}; | ||
| const longPadding = ${JSON.stringify("x".repeat(5000))}; | ||
| run('false "NVIDIA_API_KEY=nvapi-truncatedSecret' + longPadding + '"'); | ||
| `; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| const result = spawnSync("node", ["-e", script], { | ||
| cwd: path.join(import.meta.dirname, ".."), | ||
| encoding: "utf-8", | ||
| timeout: 10000, | ||
| }); | ||
| expect(result.stderr).toContain("NVIDIA_API_KEY=***"); | ||
| expect(result.stderr).not.toContain("nvapi-truncatedSecret"); | ||
| }); | ||
|
|
||
| it("runInteractive() error output redacts secrets (integration)", () => { | ||
| const runnerPath = path.join(import.meta.dirname, "..", "bin", "lib", "runner"); | ||
| const script = ` | ||
| const { runInteractive } = require(${JSON.stringify(runnerPath)}); | ||
| process.exit = () => {}; | ||
| runInteractive("false GITHUB_TOKEN=ghp_realSecretValue123"); | ||
| `; | ||
| const result = spawnSync("node", ["-e", script], { | ||
| cwd: path.join(import.meta.dirname, ".."), | ||
| encoding: "utf-8", | ||
| timeout: 10000, | ||
| }); | ||
| expect(result.stderr).toContain("GITHUB_TOKEN=***"); | ||
| expect(result.stderr).not.toContain("ghp_realSecretValue123"); | ||
| }); | ||
|
|
||
| it("runCapture() thrown error redacts message/stdout/stderr", () => { | ||
| try { | ||
| runCapture("bash -lc 'echo NVIDIA_API_KEY=nvapi-realSecret >&2; exit 1'"); | ||
| throw new Error("expected runCapture to throw"); | ||
| } catch (err) { | ||
| expect(String(err.message)).toContain("NVIDIA_API_KEY=***"); | ||
| expect(String(err.message)).not.toContain("nvapi-realSecret"); | ||
| expect(String(err.stderr || "")).toContain("NVIDIA_API_KEY=***"); | ||
| expect(String(err.stderr || "")).not.toContain("nvapi-realSecret"); | ||
| } | ||
| }); | ||
|
|
||
| it("runCapture() thrown error redacts err.output array", () => { | ||
| try { | ||
| runCapture("bash -lc 'echo GITHUB_TOKEN=ghp_secretOutputValue >&2; exit 1'"); | ||
| throw new Error("expected runCapture to throw"); | ||
| } catch (err) { | ||
| if (Array.isArray(err.output)) { | ||
| for (const chunk of err.output) { | ||
| if (chunk != null) { | ||
| expect(String(chunk)).not.toContain("ghp_secretOutputValue"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.