Skip to content

Commit 8562ecc

Browse files
authored
Merge pull request #745 from NVIDIA/yashraj/redact-short-opencode-credentials
fix(opencode): redact short credential values from scan output
2 parents 4a9f7b1 + c745ff4 commit 8562ecc

2 files changed

Lines changed: 223 additions & 16 deletions

File tree

‎.opencode/tools/skillspector_scan_lib.ts‎

Lines changed: 85 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,94 @@ export function truncate(text: string, max: number): string {
4747
export function redact(text: string, env: Env = process.env): string {
4848
const credentialValues = [...new Set(
4949
CREDENTIAL_ENV_NAMES.map((name) => env[name]?.trim()).filter(
50-
(value): value is string => Boolean(value && value.length >= 4),
50+
(value): value is string => Boolean(value),
5151
),
5252
)].sort((left, right) => right.length - left.length)
53-
const credentialPattern = credentialValues.length
54-
? new RegExp(
55-
credentialValues
56-
.map((value) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))
57-
.join("|"),
58-
"g",
59-
)
53+
const valuePatterns = credentialValues.map((value) => {
54+
const literal = value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
55+
// Short values are usually local-provider placeholders. Treat them as
56+
// tokens so they cannot change words, JSON fields, or longer credentials.
57+
return value.length < 4 ? `(?<![A-Za-z0-9_-])${literal}(?![A-Za-z0-9_-])` : literal
58+
})
59+
// Look ahead so overlapping configured values are all covered. Matching
60+
// still advances through the original text, never through replacement markers.
61+
const credentialPattern = valuePatterns.length
62+
? new RegExp(`(?=(${valuePatterns.join("|")}))`, "g")
6063
: undefined
61-
const redacted = credentialPattern ? text.replace(credentialPattern, "[REDACTED]") : text
62-
return redacted
63-
.replace(/sk-ant-[A-Za-z0-9_-]+/g, "[REDACTED]")
64-
.replace(/\bsk-[A-Za-z0-9_-]{6,}\b/g, "[REDACTED]")
65-
.replace(
66-
/\b([A-Z][A-Z0-9_]*(?:API_KEY|TOKEN|ACCESS_KEY_ID|SECRET_ACCESS_KEY|INFERENCE_KEY))(\s*[:=]\s*["']?)[^"'\s,}]+/g,
67-
"$1$2[REDACTED]",
68-
)
64+
// A full configured credential can act as the separator before another key.
65+
// Match on the original text so overlapping values never split that key.
66+
// Short values cannot end here unless their existing token boundary allows it.
67+
const longValues = valuePatterns.filter((_, index) => credentialValues[index].length >= 4)
68+
const keyBoundary = longValues.length ? `(?:\\b|(?<=${longValues.join("|")}))` : "\\b"
69+
const keyPattern = new RegExp([
70+
"sk-ant-[A-Za-z0-9_-]+",
71+
`${keyBoundary}sk-[A-Za-z0-9_-]{6,}\\b`,
72+
// Consume each uppercase name once, even when it is not an assignment.
73+
// Otherwise a known value like AAAA permits a costly retry at every A.
74+
`${keyBoundary}([A-Z][A-Z0-9_]*)`,
75+
].join("|"), "g")
76+
const assignmentName = /[A-Z0-9_](?:API_KEY|TOKEN|ACCESS_KEY_ID|SECRET_ACCESS_KEY|INFERENCE_KEY)$/
77+
const assignmentValue = /(\s*[:=]\s*["']?)[^"'\s,}]+/y
78+
const scrub = (value: string): string => {
79+
function* spans(pattern: RegExp | undefined, literal: boolean): Generator<[number, number]> {
80+
if (!pattern) return
81+
for (const match of value.matchAll(pattern)) {
82+
if (!literal && match[1]) {
83+
if (!assignmentName.test(match[1])) continue
84+
assignmentValue.lastIndex = match.index + match[0].length
85+
const assigned = assignmentValue.exec(value)
86+
if (assigned) {
87+
yield [assigned.index + assigned[1].length, assigned.index + assigned[0].length]
88+
}
89+
} else {
90+
yield [match.index, match.index + (literal ? match[1].length : match[0].length)]
91+
}
92+
}
93+
}
94+
const known = spans(credentialPattern, true)
95+
const shaped = spans(keyPattern, false)
96+
let nextKnown = known.next().value
97+
let nextShaped = shaped.next().value
98+
let cursor = 0
99+
let start = -1
100+
let end = -1
101+
const parts: string[] = []
102+
// Merge two ordered span streams in linear time. A shorter key/assignment
103+
// match cannot expose the tail of a longer configured credential.
104+
while (nextKnown || nextShaped) {
105+
let span: [number, number]
106+
if (nextKnown && (!nextShaped || nextKnown[0] <= nextShaped[0])) {
107+
span = nextKnown
108+
nextKnown = known.next().value
109+
} else {
110+
span = nextShaped!
111+
nextShaped = shaped.next().value
112+
}
113+
if (span[0] > end) {
114+
if (start >= 0) {
115+
parts.push(value.slice(cursor, start), "[REDACTED]")
116+
cursor = end
117+
}
118+
start = span[0]
119+
}
120+
end = Math.max(end, span[1])
121+
}
122+
if (start >= 0) {
123+
parts.push(value.slice(cursor, start), "[REDACTED]")
124+
cursor = end
125+
}
126+
parts.push(value.slice(cursor))
127+
return parts.join("")
128+
}
129+
try {
130+
const report = JSON.parse(text)
131+
if (report === null || typeof report !== "object") return scrub(text)
132+
// Preserve JSON numbers and booleans, including a score equal to a short
133+
// placeholder. Credentials inside JSON strings still receive full scrubbing.
134+
return text.replace(/"(?:[^"\\]|\\.)*"/g, (token) => JSON.stringify(scrub(JSON.parse(token))))
135+
} catch {
136+
return scrub(text)
137+
}
69138
}
70139

71140
export function isScpGitTarget(target: string): boolean {

‎tests/opencode/skillspector_scan_lib.test.ts‎

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,109 @@ describe("redact", () => {
8989
assert.equal(out.match(/\[REDACTED\]/g)?.length, CREDENTIAL_ENV_NAMES.length)
9090
})
9191

92+
it("redacts one-, two-, and three-character values for every supported credential name", () => {
93+
for (const name of CREDENTIAL_ENV_NAMES) {
94+
for (const value of ["7", "7x", "7xy"]) {
95+
assert.equal(redact(`before ${value} after`, { [name]: value }), "before [REDACTED] after")
96+
}
97+
}
98+
})
99+
100+
it("redacts whole key-shaped tokens before short or overlapping configured values", () => {
101+
for (const value of ["x", "Q7wE"]) {
102+
assert.equal(redact(
103+
"sk-proj-Q7wErTyxUiOp9AsDfGhJkL sk-ant-Q7wErTyxUiOp9AsDfGhJkL OPENAI_API_KEY=Q7wErTyxUiOp",
104+
{ OPENAI_API_KEY: value },
105+
), "[REDACTED] [REDACTED] OPENAI_API_KEY=[REDACTED]")
106+
}
107+
})
108+
109+
it("merges overlapping known values with shorter key and assignment matches", () => {
110+
for (const [secret, input, expected] of [
111+
["alpha,beta", "OPENAI_API_KEY=alpha,beta", "OPENAI_API_KEY=[REDACTED]"],
112+
["alpha,beta", "OPENAI_API_KEY=prefix-alpha,beta", "OPENAI_API_KEY=[REDACTED]"],
113+
["sk-ant-alpha.beta", "sk-ant-alpha.beta", "[REDACTED]"],
114+
]) {
115+
const env = { OPENAI_API_KEY: secret }
116+
assert.equal(redact(input, env), expected)
117+
assert.deepEqual(JSON.parse(redact(JSON.stringify({ code_snippet: input, risk_score: 12 }), env)), {
118+
code_snippet: expected,
119+
risk_score: 12,
120+
})
121+
}
122+
assert.equal(redact("abcdef", { OPENAI_API_KEY: "abcd", ANTHROPIC_API_KEY: "cdef" }), "[REDACTED]")
123+
})
124+
125+
it("keeps JSON metadata readable and parseable with one-character placeholders", () => {
126+
for (const value of ["1", "e", "x"]) {
127+
const report = {
128+
risk_score: 12,
129+
start_line: 1,
130+
safe_to_install: true,
131+
severity: "medium",
132+
code_snippet: `credential ${value} and sk-proj-Q7wErTyxUiOp9AsDfGhJkL`,
133+
}
134+
assert.deepEqual(JSON.parse(redact(JSON.stringify(report), { OPENAI_API_KEY: value })), {
135+
...report,
136+
code_snippet: "credential [REDACTED] and [REDACTED]",
137+
})
138+
}
139+
assert.equal(redact("example xyz x", { OPENAI_API_KEY: "x" }), "example xyz [REDACTED]")
140+
})
141+
142+
it("redacts unrelated credentials directly after a configured value", () => {
143+
const env = { NVIDIA_INFERENCE_KEY: "nvapi-ABCD1234efgh" }
144+
for (const [tail, expected] of [
145+
["sk-abcdef123456", "[REDACTED]"],
146+
["sk-ant-abcdef123456", "[REDACTED]"],
147+
["OPENAI_API_KEY=second-secret", "[REDACTED]OPENAI_API_KEY=[REDACTED]"],
148+
]) {
149+
const input = env.NVIDIA_INFERENCE_KEY + tail
150+
assert.equal(redact(input, env), expected)
151+
assert.deepEqual(JSON.parse(redact(JSON.stringify({ code_snippet: input, risk_score: 12 }), env)), {
152+
code_snippet: expected,
153+
risk_score: 12,
154+
})
155+
}
156+
assert.equal(redact("abcdefsk-abcdef123456", {
157+
OPENAI_API_KEY: "abcd", ANTHROPIC_API_KEY: "cdef",
158+
}), "[REDACTED]")
159+
for (const prefix of ["AAAA", "lowercase", "0000", "-_._"]) {
160+
assert.equal(redact(`${prefix}OPENAI_API_KEY=second-secret`, {
161+
NVIDIA_INFERENCE_KEY: prefix,
162+
}), "[REDACTED]OPENAI_API_KEY=[REDACTED]")
163+
}
164+
assert.equal(redact("abcdefOPENAI_API_KEY=abcdef", {
165+
OPENAI_API_KEY: "abcd", ANTHROPIC_API_KEY: "cdef",
166+
}), "[REDACTED]OPENAI_API_KEY=[REDACTED]")
167+
})
168+
169+
it("bounds assignment scanning with many overlapping credential boundaries", () => {
170+
const input = "A".repeat(128 * 1024) + "!"
171+
const started = performance.now()
172+
assert.equal(redact(input, { OPENAI_API_KEY: "AAAA" }), "[REDACTED]!")
173+
assert.ok(performance.now() - started < 2000, "redaction repeatedly rescanned an uppercase run")
174+
})
175+
176+
it("ignores empty and unset credentials without matching every output position", () => {
177+
assert.equal(redact("ordinary output", {
178+
OPENAI_API_KEY: "",
179+
ANTHROPIC_API_KEY: undefined,
180+
NVIDIA_INFERENCE_KEY: " \t\n",
181+
}), "ordinary output")
182+
})
183+
184+
it("redacts trimmed, overlapping, and regex-bearing short values in one pass", () => {
185+
assert.equal(redact("xyz xy x .* [ R", {
186+
OPENAI_API_KEY: " xyz ",
187+
ANTHROPIC_API_KEY: "xy",
188+
NVIDIA_INFERENCE_KEY: "x",
189+
AWS_SECRET_ACCESS_KEY: ".*",
190+
AWS_SESSION_TOKEN: "[",
191+
AWS_SECURITY_TOKEN: "R",
192+
}), "[REDACTED] [REDACTED] [REDACTED] [REDACTED] [REDACTED] [REDACTED]")
193+
})
194+
92195
it("redacts NVIDIA and AWS assignments even when the environment is unavailable", () => {
93196
const out = redact(
94197
"NVIDIA_INFERENCE_KEY=nvapi-secret AWS_SECRET_ACCESS_KEY: aws-secret",
@@ -220,6 +323,27 @@ describe("formatExecError", () => {
220323
})
221324
assert.ok(!out.includes("hunter2"))
222325
})
326+
327+
it("redacts short credentials from every failure output path before truncation", () => {
328+
for (const value of ["7", "7x", "7xy"]) {
329+
const env = { OPENAI_API_KEY: value }
330+
const stdout = `${"o".repeat(MAX_STDOUT - 2)} ${value} suffix`
331+
const stderr = `${"e".repeat(MAX_STDERR - 2)} ${value} suffix`
332+
for (const failure of [
333+
{ code: 1, stdout, stderr },
334+
{ code: 2, stdout, stderr },
335+
{ killed: true, stdout, stderr },
336+
{ name: "AbortError", stdout, stderr },
337+
{ code: 9, stdout, stderr },
338+
{ code: 9, message: stderr },
339+
stderr,
340+
{ code: 2, stderr: `Usage: skillspector\nError: rejected ${value}` },
341+
]) {
342+
const out = formatExecError("bin", failure, env)
343+
assert.ok(!out.includes("7"), `${value} leaked from ${JSON.stringify(failure).slice(0, 40)}`)
344+
}
345+
}
346+
})
223347
})
224348

225349
describe("formatSuccess", () => {
@@ -242,6 +366,20 @@ describe("formatSuccess", () => {
242366
assert.ok(out.includes("stderr:\nbaseline detected"))
243367
assert.ok(out.includes("[truncated "))
244368
})
369+
370+
it("redacts short credentials in both successful streams before truncation", () => {
371+
for (const value of ["7", "7x", "7xy"]) {
372+
const out = formatSuccess(
373+
undefined,
374+
`${"o".repeat(MAX_STDOUT - 2)} ${value} suffix`,
375+
`${"e".repeat(MAX_STDERR - 2)} ${value} suffix`,
376+
{ OPENAI_API_KEY: value },
377+
)
378+
assert.ok(!out.includes("7"))
379+
assert.ok(out.includes("stderr:\n"))
380+
assert.ok(out.includes("[truncated "))
381+
}
382+
})
245383
})
246384

247385
describe("permissioned execution", () => {

0 commit comments

Comments
 (0)