Skip to content

Commit 0e77030

Browse files
authored
Merge pull request #739 from NVIDIA/yashraj/fix-pi-redaction-runtime-20261005
fix(pi): make scanner output redaction linear
2 parents 95528a7 + 1d908c2 commit 0e77030

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

‎extensions/skillspector.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ function redactSecrets(value: string): string {
4141
return value
4242
.replace(/(sk-ant-[A-Za-z0-9_-]{12,})/g, "[REDACTED_ANTHROPIC_KEY]")
4343
.replace(/(sk-[A-Za-z0-9_-]{20,})/g, "[REDACTED_OPENAI_KEY]")
44-
.replace(/([A-Za-z0-9_]*API_KEY[=:]\s*)[^\s]+/gi, "$1[REDACTED]")
45-
.replace(/([A-Za-z0-9_]*TOKEN[=:]\s*)[^\s]+/gi, "$1[REDACTED]");
44+
// Start only at a word boundary; retrying at every character is quadratic.
45+
.replace(/\b([A-Za-z0-9_]*(?:API_KEY|TOKEN)[=:]\s*)[^\s]+/gi, "$1[REDACTED]");
4646
}
4747

4848
function truncateText(value: string, maxChars = 12000): { text: string; truncated: boolean } {

‎tests/unit/test_pi_extension.mjs‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { copyFileSync, existsSync, linkSync, mkdirSync, mkdtempSync, readFileSyn
99
import { registerHooks } from "node:module";
1010
import { tmpdir } from "node:os";
1111
import { dirname, join, resolve } from "node:path";
12+
import { performance } from "node:perf_hooks";
1213
import test from "node:test";
1314
import { pathToFileURL } from "node:url";
1415

@@ -74,6 +75,37 @@ test("uses installed absolute executable and preserves scan arguments without ou
7475
assert.equal(ctx.calls[0].options.cwd, ctx.workspace);
7576
});
7677

78+
test("redacts long scanner output without retrying every word character", { timeout: 5000 }, async (t) => {
79+
const ctx = await setup(t, () => ({
80+
code: 0,
81+
stdout: "A".repeat(100_000),
82+
stderr: "B".repeat(100_000),
83+
}));
84+
const started = performance.now();
85+
const result = await ctx.scan();
86+
const elapsed = performance.now() - started;
87+
assert.ok(elapsed < 1000, `redaction took ${elapsed.toFixed(1)} ms`);
88+
assert.equal(result.details.stdoutTruncated, true);
89+
assert.equal(result.details.stderrTruncated, true);
90+
assert.ok(result.content[0].text.length < 19_000);
91+
});
92+
93+
test("redacts complete secrets before truncating at display boundaries", async (t) => {
94+
const ctx = await setup(t, () => ({
95+
code: 0,
96+
stdout: " ".repeat(11_990) + "sk-" + "x".repeat(32),
97+
stderr: "NPM_TOKEN=synthetic-token\nCUSTOM_API_KEY: synthetic-key\napi_key=lower-key",
98+
}));
99+
const result = await ctx.scan();
100+
const text = result.content[0].text;
101+
for (const secret of ["sk-", "synthetic-token", "synthetic-key", "lower-key"]) {
102+
assert.equal(text.includes(secret), false);
103+
}
104+
assert.match(text, /NPM_TOKEN=\[REDACTED\]/);
105+
assert.match(text, /CUSTOM_API_KEY: \[REDACTED\]/);
106+
assert.match(text, /api_key=\[REDACTED\]/);
107+
});
108+
77109
test("finds the Windows virtualenv executable without a PATH fallback", async (t) => {
78110
const ctx = await setup(t);
79111
rmSync(ctx.bin);

0 commit comments

Comments
 (0)