From 75cd52e2032ea9114091705eb75cc962eecf71c0 Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Fri, 6 Nov 2026 17:59:07 +0530 Subject: [PATCH 1/7] fix(hooks): redact captured text before cutting it to length Hooks cut captured text to their length limit before sending it, and the server redacted afterwards. When the cut landed inside a token, the start of that token no longer matched its secret pattern and was stored. truncateText and truncateCaptureOutput now redact first and cut second, so every hook that uses them (task-completed, post-tool-failure, session-end, post-tool-use, subagent-stop) sends no part of a secret. Text within the limit is sent unchanged. --- plugin/scripts/post-tool-failure.mjs | 59 ++++++++++++++++++++++- plugin/scripts/post-tool-use.mjs | 70 ++++++++++++++++++++++++---- plugin/scripts/session-end.mjs | 59 ++++++++++++++++++++++- plugin/scripts/subagent-stop.mjs | 59 ++++++++++++++++++++++- plugin/scripts/task-completed.mjs | 59 ++++++++++++++++++++++- src/hooks/_capture-filter.ts | 27 ++++++----- test/capture-filter.test.ts | 23 +++++++++ test/hook-truncation.test.ts | 11 +++++ 8 files changed, 343 insertions(+), 24 deletions(-) diff --git a/plugin/scripts/post-tool-failure.mjs b/plugin/scripts/post-tool-failure.mjs index 85cf906e5c..c69181db82 100755 --- a/plugin/scripts/post-tool-failure.mjs +++ b/plugin/scripts/post-tool-failure.mjs @@ -3,6 +3,59 @@ import { execSync } from "node:child_process"; import { realpathSync } from "node:fs"; import { basename, dirname, resolve } from "node:path"; import { captureObservation, isDrainChild, runDrainChild, withEventId } from "./_capture.mjs"; +//#region src/functions/privacy.ts +const PRIVATE_TAG_TOKEN_RE = /)[^>]*>|<\/private\s*>/gi; +function redactPrivateBlocks(input) { + const tokens = new RegExp(PRIVATE_TAG_TOKEN_RE.source, PRIVATE_TAG_TOKEN_RE.flags); + let out = ""; + let cursor = 0; + let depth = 0; + for (let match = tokens.exec(input); match; match = tokens.exec(input)) { + const closing = match[0].startsWith(" 0) return `${out}[REDACTED]`; + return out + input.slice(cursor); +} +const SECRET_PATTERN_SOURCES = [ + /(?:api[_-]?key|secret|token|password|credential|auth)[\s]*[=:]\s*["']?[A-Za-z0-9_\-/.+]{20,}["']?/gi, + /Bearer\s+[A-Za-z0-9._\-+/=]{20,}/gi, + /(?]*:[^\s/?#@"'<>]+@/gi; +function stripPrivateData(input) { + let result = redactPrivateBlocks(input); + result = result.replace(new RegExp(PRIVATE_KEY_BLOCK_RE.source, PRIVATE_KEY_BLOCK_RE.flags), "[REDACTED_SECRET]"); + result = result.replace(new RegExp(URL_CREDENTIALS_RE.source, URL_CREDENTIALS_RE.flags), "$1[REDACTED_SECRET]@"); + for (const source of SECRET_PATTERN_SOURCES) { + const pattern = new RegExp(source.source, source.flags); + result = result.replace(pattern, "[REDACTED_SECRET]"); + } + return result; +} +//#endregion //#region src/hooks/_capture-filter.ts const DEFAULT_DENY_PATTERNS = [ "memory_*", @@ -43,11 +96,15 @@ function shouldCaptureTool(toolName) { if (allow) return matchesAny(toolName, allow); return !matchesAny(toolName, [...DEFAULT_DENY_PATTERNS, ...parseEnvList(process.env["AGENTMEMORY_CAPTURE_DENY"]) ?? []]); } -function truncateText(value, max) { +function cut(value, max) { if (value.length <= max) return value; const end = /[\uD800-\uDBFF]/.test(value[max - 1] ?? "") ? max - 1 : max; return value.slice(0, end); } +function truncateText(value, max) { + if (value.length <= max) return value; + return cut(stripPrivateData(value), max); +} //#endregion //#region src/hooks/_project.ts function resolveProject(cwd) { diff --git a/plugin/scripts/post-tool-use.mjs b/plugin/scripts/post-tool-use.mjs index 08c542899a..909c72c329 100755 --- a/plugin/scripts/post-tool-use.mjs +++ b/plugin/scripts/post-tool-use.mjs @@ -3,6 +3,59 @@ import { execSync } from "node:child_process"; import { realpathSync } from "node:fs"; import { basename, dirname, resolve } from "node:path"; import { captureObservation, isDrainChild, runDrainChild, withEventId } from "./_capture.mjs"; +//#region src/functions/privacy.ts +const PRIVATE_TAG_TOKEN_RE = /)[^>]*>|<\/private\s*>/gi; +function redactPrivateBlocks(input) { + const tokens = new RegExp(PRIVATE_TAG_TOKEN_RE.source, PRIVATE_TAG_TOKEN_RE.flags); + let out = ""; + let cursor = 0; + let depth = 0; + for (let match = tokens.exec(input); match; match = tokens.exec(input)) { + const closing = match[0].startsWith(" 0) return `${out}[REDACTED]`; + return out + input.slice(cursor); +} +const SECRET_PATTERN_SOURCES = [ + /(?:api[_-]?key|secret|token|password|credential|auth)[\s]*[=:]\s*["']?[A-Za-z0-9_\-/.+]{20,}["']?/gi, + /Bearer\s+[A-Za-z0-9._\-+/=]{20,}/gi, + /(?]*:[^\s/?#@"'<>]+@/gi; +function stripPrivateData(input) { + let result = redactPrivateBlocks(input); + result = result.replace(new RegExp(PRIVATE_KEY_BLOCK_RE.source, PRIVATE_KEY_BLOCK_RE.flags), "[REDACTED_SECRET]"); + result = result.replace(new RegExp(URL_CREDENTIALS_RE.source, URL_CREDENTIALS_RE.flags), "$1[REDACTED_SECRET]@"); + for (const source of SECRET_PATTERN_SOURCES) { + const pattern = new RegExp(source.source, source.flags); + result = result.replace(pattern, "[REDACTED_SECRET]"); + } + return result; +} +//#endregion //#region src/hooks/_capture-filter.ts const DEFAULT_DENY_PATTERNS = [ "memory_*", @@ -49,22 +102,21 @@ function captureOutputMax() { const parsed = Number.parseInt(raw, 10); return Number.isFinite(parsed) && parsed > 0 ? parsed : 8e3; } -function truncateText(value, max) { +function cut(value, max) { if (value.length <= max) return value; const end = /[\uD800-\uDBFF]/.test(value[max - 1] ?? "") ? max - 1 : max; return value.slice(0, end); } +function truncateWithSuffix(value, max, suffix) { + const safe = stripPrivateData(value); + if (safe.length <= max) return safe; + return max <= suffix.length ? suffix.slice(0, max) : cut(safe, max - suffix.length) + suffix; +} function truncateCaptureOutput(value, max) { - if (typeof value === "string" && value.length > max) { - const suffix = "\n[...truncated]"; - return max <= 15 ? suffix.slice(0, max) : truncateText(value, max - 15) + suffix; - } + if (typeof value === "string" && value.length > max) return truncateWithSuffix(value, max, "\n[...truncated]"); if (typeof value === "object" && value !== null) { const str = JSON.stringify(value); - if (str.length > max) { - const suffix = "...[truncated]"; - return max <= 14 ? suffix.slice(0, max) : truncateText(str, max - 14) + suffix; - } + if (str.length > max) return truncateWithSuffix(str, max, "...[truncated]"); return value; } return value; diff --git a/plugin/scripts/session-end.mjs b/plugin/scripts/session-end.mjs index d64c0b2797..9c7d7bf3ed 100755 --- a/plugin/scripts/session-end.mjs +++ b/plugin/scripts/session-end.mjs @@ -3,12 +3,69 @@ import { readFileSync, realpathSync } from "node:fs"; import { execSync } from "node:child_process"; import { basename, dirname, resolve } from "node:path"; import { REST_URL, authHeaders, captureObservation, isDrainChild, runDrainChild, transportBlocked, withEventId } from "./_capture.mjs"; +//#region src/functions/privacy.ts +const PRIVATE_TAG_TOKEN_RE = /)[^>]*>|<\/private\s*>/gi; +function redactPrivateBlocks(input) { + const tokens = new RegExp(PRIVATE_TAG_TOKEN_RE.source, PRIVATE_TAG_TOKEN_RE.flags); + let out = ""; + let cursor = 0; + let depth = 0; + for (let match = tokens.exec(input); match; match = tokens.exec(input)) { + const closing = match[0].startsWith(" 0) return `${out}[REDACTED]`; + return out + input.slice(cursor); +} +const SECRET_PATTERN_SOURCES = [ + /(?:api[_-]?key|secret|token|password|credential|auth)[\s]*[=:]\s*["']?[A-Za-z0-9_\-/.+]{20,}["']?/gi, + /Bearer\s+[A-Za-z0-9._\-+/=]{20,}/gi, + /(?]*:[^\s/?#@"'<>]+@/gi; +function stripPrivateData(input) { + let result = redactPrivateBlocks(input); + result = result.replace(new RegExp(PRIVATE_KEY_BLOCK_RE.source, PRIVATE_KEY_BLOCK_RE.flags), "[REDACTED_SECRET]"); + result = result.replace(new RegExp(URL_CREDENTIALS_RE.source, URL_CREDENTIALS_RE.flags), "$1[REDACTED_SECRET]@"); + for (const source of SECRET_PATTERN_SOURCES) { + const pattern = new RegExp(source.source, source.flags); + result = result.replace(pattern, "[REDACTED_SECRET]"); + } + return result; +} +//#endregion //#region src/hooks/_capture-filter.ts -function truncateText(value, max) { +function cut(value, max) { if (value.length <= max) return value; const end = /[\uD800-\uDBFF]/.test(value[max - 1] ?? "") ? max - 1 : max; return value.slice(0, end); } +function truncateText(value, max) { + if (value.length <= max) return value; + return cut(stripPrivateData(value), max); +} //#endregion //#region src/hooks/_project.ts function resolveProject(cwd) { diff --git a/plugin/scripts/subagent-stop.mjs b/plugin/scripts/subagent-stop.mjs index a01bff6cd0..0786f45533 100755 --- a/plugin/scripts/subagent-stop.mjs +++ b/plugin/scripts/subagent-stop.mjs @@ -3,12 +3,69 @@ import { execSync } from "node:child_process"; import { realpathSync } from "node:fs"; import { basename, dirname, resolve } from "node:path"; import { captureObservation, isDrainChild, runDrainChild, stripPrivateData, withEventId } from "./_capture.mjs"; +//#region src/functions/privacy.ts +const PRIVATE_TAG_TOKEN_RE = /)[^>]*>|<\/private\s*>/gi; +function redactPrivateBlocks(input) { + const tokens = new RegExp(PRIVATE_TAG_TOKEN_RE.source, PRIVATE_TAG_TOKEN_RE.flags); + let out = ""; + let cursor = 0; + let depth = 0; + for (let match = tokens.exec(input); match; match = tokens.exec(input)) { + const closing = match[0].startsWith(" 0) return `${out}[REDACTED]`; + return out + input.slice(cursor); +} +const SECRET_PATTERN_SOURCES = [ + /(?:api[_-]?key|secret|token|password|credential|auth)[\s]*[=:]\s*["']?[A-Za-z0-9_\-/.+]{20,}["']?/gi, + /Bearer\s+[A-Za-z0-9._\-+/=]{20,}/gi, + /(?]*:[^\s/?#@"'<>]+@/gi; +function stripPrivateData$1(input) { + let result = redactPrivateBlocks(input); + result = result.replace(new RegExp(PRIVATE_KEY_BLOCK_RE.source, PRIVATE_KEY_BLOCK_RE.flags), "[REDACTED_SECRET]"); + result = result.replace(new RegExp(URL_CREDENTIALS_RE.source, URL_CREDENTIALS_RE.flags), "$1[REDACTED_SECRET]@"); + for (const source of SECRET_PATTERN_SOURCES) { + const pattern = new RegExp(source.source, source.flags); + result = result.replace(pattern, "[REDACTED_SECRET]"); + } + return result; +} +//#endregion //#region src/hooks/_capture-filter.ts -function truncateText(value, max) { +function cut(value, max) { if (value.length <= max) return value; const end = /[\uD800-\uDBFF]/.test(value[max - 1] ?? "") ? max - 1 : max; return value.slice(0, end); } +function truncateText(value, max) { + if (value.length <= max) return value; + return cut(stripPrivateData$1(value), max); +} //#endregion //#region src/hooks/_project.ts function resolveProject(cwd) { diff --git a/plugin/scripts/task-completed.mjs b/plugin/scripts/task-completed.mjs index d92bb7240e..baaa8a4f4d 100755 --- a/plugin/scripts/task-completed.mjs +++ b/plugin/scripts/task-completed.mjs @@ -3,12 +3,69 @@ import { execSync } from "node:child_process"; import { realpathSync } from "node:fs"; import { basename, dirname, resolve } from "node:path"; import { captureObservation, isDrainChild, runDrainChild, withEventId } from "./_capture.mjs"; +//#region src/functions/privacy.ts +const PRIVATE_TAG_TOKEN_RE = /)[^>]*>|<\/private\s*>/gi; +function redactPrivateBlocks(input) { + const tokens = new RegExp(PRIVATE_TAG_TOKEN_RE.source, PRIVATE_TAG_TOKEN_RE.flags); + let out = ""; + let cursor = 0; + let depth = 0; + for (let match = tokens.exec(input); match; match = tokens.exec(input)) { + const closing = match[0].startsWith(" 0) return `${out}[REDACTED]`; + return out + input.slice(cursor); +} +const SECRET_PATTERN_SOURCES = [ + /(?:api[_-]?key|secret|token|password|credential|auth)[\s]*[=:]\s*["']?[A-Za-z0-9_\-/.+]{20,}["']?/gi, + /Bearer\s+[A-Za-z0-9._\-+/=]{20,}/gi, + /(?]*:[^\s/?#@"'<>]+@/gi; +function stripPrivateData(input) { + let result = redactPrivateBlocks(input); + result = result.replace(new RegExp(PRIVATE_KEY_BLOCK_RE.source, PRIVATE_KEY_BLOCK_RE.flags), "[REDACTED_SECRET]"); + result = result.replace(new RegExp(URL_CREDENTIALS_RE.source, URL_CREDENTIALS_RE.flags), "$1[REDACTED_SECRET]@"); + for (const source of SECRET_PATTERN_SOURCES) { + const pattern = new RegExp(source.source, source.flags); + result = result.replace(pattern, "[REDACTED_SECRET]"); + } + return result; +} +//#endregion //#region src/hooks/_capture-filter.ts -function truncateText(value, max) { +function cut(value, max) { if (value.length <= max) return value; const end = /[\uD800-\uDBFF]/.test(value[max - 1] ?? "") ? max - 1 : max; return value.slice(0, end); } +function truncateText(value, max) { + if (value.length <= max) return value; + return cut(stripPrivateData(value), max); +} //#endregion //#region src/hooks/_project.ts function resolveProject(cwd) { diff --git a/src/hooks/_capture-filter.ts b/src/hooks/_capture-filter.ts index 762f52420e..de8f826934 100644 --- a/src/hooks/_capture-filter.ts +++ b/src/hooks/_capture-filter.ts @@ -1,3 +1,5 @@ +import { stripPrivateData } from "../functions/privacy.js"; + const DEFAULT_DENY_PATTERNS = [ "memory_*", "toolsearch", @@ -60,27 +62,30 @@ export function captureOutputMax(): number { return Number.isFinite(parsed) && parsed > 0 ? parsed : 8000; } -export function truncateText(value: string, max: number): string { +function cut(value: string, max: number): string { if (value.length <= max) return value; const end = /[\uD800-\uDBFF]/.test(value[max - 1] ?? "") ? max - 1 : max; return value.slice(0, end); } +export function truncateText(value: string, max: number): string { + if (value.length <= max) return value; + return cut(stripPrivateData(value), max); +} + +function truncateWithSuffix(value: string, max: number, suffix: string): string { + const safe = stripPrivateData(value); + if (safe.length <= max) return safe; + return max <= suffix.length ? suffix.slice(0, max) : cut(safe, max - suffix.length) + suffix; +} + export function truncateCaptureOutput(value: unknown, max: number): unknown { if (typeof value === "string" && value.length > max) { - const suffix = "\n[...truncated]"; - return max <= suffix.length - ? suffix.slice(0, max) - : truncateText(value, max - suffix.length) + suffix; + return truncateWithSuffix(value, max, "\n[...truncated]"); } if (typeof value === "object" && value !== null) { const str = JSON.stringify(value); - if (str.length > max) { - const suffix = "...[truncated]"; - return max <= suffix.length - ? suffix.slice(0, max) - : truncateText(str, max - suffix.length) + suffix; - } + if (str.length > max) return truncateWithSuffix(str, max, "...[truncated]"); return value; } return value; diff --git a/test/capture-filter.test.ts b/test/capture-filter.test.ts index 0d576103b7..5a91cf554e 100644 --- a/test/capture-filter.test.ts +++ b/test/capture-filter.test.ts @@ -116,6 +116,29 @@ describe("truncateText", () => { }); }); +describe("truncation redacts before cutting", () => { + const token = "ghp_" + "A".repeat(40); + + it("truncateText leaves text within the limit unchanged", () => { + expect(truncateText(`short ${token}`, 100)).toBe(`short ${token}`); + }); + + it("truncateText does not keep the start of a token the cut lands in", () => { + const out = truncateText("x".repeat(10) + " " + token + " tail", 20); + expect(out).not.toContain("ghp_"); + expect(out.length).toBeLessThanOrEqual(20); + }); + + it("truncateCaptureOutput redacts strings and JSON before cutting", () => { + const str = truncateCaptureOutput("x".repeat(10) + " " + token + "y".repeat(100), 40) as string; + expect(str).not.toContain("ghp_"); + expect(str.length).toBeLessThanOrEqual(40); + const json = truncateCaptureOutput({ out: "x".repeat(10) + " " + token + "y".repeat(100) }, 40) as string; + expect(json).not.toContain("ghp_"); + expect(json.length).toBeLessThanOrEqual(40); + }); +}); + describe("truncateCaptureOutput", () => { it("leaves short strings alone", () => { expect(truncateCaptureOutput("hello", 20)).toBe("hello"); diff --git a/test/hook-truncation.test.ts b/test/hook-truncation.test.ts index b14f3ce11e..8543c78059 100644 --- a/test/hook-truncation.test.ts +++ b/test/hook-truncation.test.ts @@ -159,4 +159,15 @@ describe("hook truncation at a surrogate pair", () => { expect(whole).toContain(SMILE); expect(count(whole, "x")).toBe(kept - 2); }); + + it.each(sites.map((site) => [site.name, site] as const))("%s redacts a secret before cutting through it", async (_name, site) => { + const kept = count(await sent(site, "x".repeat(LONG)), "x"); + const token = await sent(site, "x".repeat(kept - 12) + " ghp_" + "A".repeat(40) + " " + "x".repeat(LONG)); + expect(token).not.toContain("ghp_"); + expect(token).not.toContain("AAAA"); + const bearer = await sent(site, "x".repeat(kept - 12) + " Bearer " + "B".repeat(40) + " " + "x".repeat(LONG)); + expect(bearer).not.toContain("BBBB"); + const block = await sent(site, "x".repeat(kept - 12) + " hidden-value " + "x".repeat(LONG)); + expect(block).not.toContain("hidden"); + }); }); From 6c2fd3d0ae67d850fad3de77809b49a8c7f4d044 Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Sat, 7 Nov 2026 00:09:44 +0530 Subject: [PATCH 2/7] fix(hooks): redact structured values before serializing them JSON escaping turns a line break inside a private tag into a literal backslash-n, so the tag no longer matched after serialization and the private text could survive truncation. Long object values are now redacted field by field before they are serialized and cut, and the post-tool-failure hook uses the same path for tool input and errors. --- plugin/scripts/post-tool-failure.mjs | 32 ++++++++++++++++++++++++++-- plugin/scripts/post-tool-use.mjs | 26 ++++++++++++++++++++-- src/hooks/_capture-filter.ts | 10 +++++++-- src/hooks/post-tool-failure.ts | 6 +++--- test/capture-filter.test.ts | 15 +++++++++++++ 5 files changed, 80 insertions(+), 9 deletions(-) diff --git a/plugin/scripts/post-tool-failure.mjs b/plugin/scripts/post-tool-failure.mjs index c69181db82..43b0565152 100755 --- a/plugin/scripts/post-tool-failure.mjs +++ b/plugin/scripts/post-tool-failure.mjs @@ -55,6 +55,29 @@ function stripPrivateData(input) { } return result; } +function scrubRecord(value, options = {}) { + if (typeof value === "string") return stripPrivateData(value); + if (Array.isArray(value)) return value.map((item) => scrubRecord(item, options)); + if (value !== null && typeof value === "object") { + const proto = Object.getPrototypeOf(value); + if (proto !== Object.prototype && proto !== null) return value; + const entries = Object.entries(value).map(([key, item]) => [options.scrubKeys ? stripPrivateData(key) : key, item]); + const reserved = new Set(entries.map(([key]) => key)); + const used = /* @__PURE__ */ new Set(); + return Object.fromEntries(entries.map(([key, item]) => { + let unique = key; + if (used.has(unique)) { + let suffix = 2; + do + unique = `${key}_${suffix++}`; + while (reserved.has(unique) || used.has(unique)); + } + used.add(unique); + return [unique, scrubRecord(item, options)]; + })); + } + return value; +} //#endregion //#region src/hooks/_capture-filter.ts const DEFAULT_DENY_PATTERNS = [ @@ -105,6 +128,11 @@ function truncateText(value, max) { if (value.length <= max) return value; return cut(stripPrivateData(value), max); } +function truncateJson(value, max) { + const str = JSON.stringify(value); + if (str.length <= max) return str; + return cut(stripPrivateData(JSON.stringify(scrubRecord(value))), max); +} //#endregion //#region src/hooks/_project.ts function resolveProject(cwd) { @@ -178,8 +206,8 @@ async function main() { timestamp: (/* @__PURE__ */ new Date()).toISOString(), data: { tool_name: toolName, - tool_input: typeof toolInput === "string" ? truncateText(toolInput, 4e3) : truncateText(JSON.stringify(toolInput ?? ""), 4e3), - error: typeof error === "string" ? truncateText(error, 4e3) : truncateText(JSON.stringify(error ?? ""), 4e3) + tool_input: typeof toolInput === "string" ? truncateText(toolInput, 4e3) : truncateJson(toolInput ?? "", 4e3), + error: typeof error === "string" ? truncateText(error, 4e3) : truncateJson(error ?? "", 4e3) } }, data), OBSERVE_TIMEOUT_MS); setTimeout(() => process.exit(0), EXIT_CAP_MS).unref(); diff --git a/plugin/scripts/post-tool-use.mjs b/plugin/scripts/post-tool-use.mjs index 909c72c329..e1f5186f75 100755 --- a/plugin/scripts/post-tool-use.mjs +++ b/plugin/scripts/post-tool-use.mjs @@ -55,6 +55,29 @@ function stripPrivateData(input) { } return result; } +function scrubRecord(value, options = {}) { + if (typeof value === "string") return stripPrivateData(value); + if (Array.isArray(value)) return value.map((item) => scrubRecord(item, options)); + if (value !== null && typeof value === "object") { + const proto = Object.getPrototypeOf(value); + if (proto !== Object.prototype && proto !== null) return value; + const entries = Object.entries(value).map(([key, item]) => [options.scrubKeys ? stripPrivateData(key) : key, item]); + const reserved = new Set(entries.map(([key]) => key)); + const used = /* @__PURE__ */ new Set(); + return Object.fromEntries(entries.map(([key, item]) => { + let unique = key; + if (used.has(unique)) { + let suffix = 2; + do + unique = `${key}_${suffix++}`; + while (reserved.has(unique) || used.has(unique)); + } + used.add(unique); + return [unique, scrubRecord(item, options)]; + })); + } + return value; +} //#endregion //#region src/hooks/_capture-filter.ts const DEFAULT_DENY_PATTERNS = [ @@ -115,8 +138,7 @@ function truncateWithSuffix(value, max, suffix) { function truncateCaptureOutput(value, max) { if (typeof value === "string" && value.length > max) return truncateWithSuffix(value, max, "\n[...truncated]"); if (typeof value === "object" && value !== null) { - const str = JSON.stringify(value); - if (str.length > max) return truncateWithSuffix(str, max, "...[truncated]"); + if (JSON.stringify(value).length > max) return truncateWithSuffix(JSON.stringify(scrubRecord(value)), max, "...[truncated]"); return value; } return value; diff --git a/src/hooks/_capture-filter.ts b/src/hooks/_capture-filter.ts index de8f826934..48d75fbce0 100644 --- a/src/hooks/_capture-filter.ts +++ b/src/hooks/_capture-filter.ts @@ -1,4 +1,4 @@ -import { stripPrivateData } from "../functions/privacy.js"; +import { scrubRecord, stripPrivateData } from "../functions/privacy.js"; const DEFAULT_DENY_PATTERNS = [ "memory_*", @@ -73,6 +73,12 @@ export function truncateText(value: string, max: number): string { return cut(stripPrivateData(value), max); } +export function truncateJson(value: unknown, max: number): string { + const str = JSON.stringify(value); + if (str.length <= max) return str; + return cut(stripPrivateData(JSON.stringify(scrubRecord(value))), max); +} + function truncateWithSuffix(value: string, max: number, suffix: string): string { const safe = stripPrivateData(value); if (safe.length <= max) return safe; @@ -85,7 +91,7 @@ export function truncateCaptureOutput(value: unknown, max: number): unknown { } if (typeof value === "object" && value !== null) { const str = JSON.stringify(value); - if (str.length > max) return truncateWithSuffix(str, max, "...[truncated]"); + if (str.length > max) return truncateWithSuffix(JSON.stringify(scrubRecord(value)), max, "...[truncated]"); return value; } return value; diff --git a/src/hooks/post-tool-failure.ts b/src/hooks/post-tool-failure.ts index 372da6b1a7..9dbe015dcb 100644 --- a/src/hooks/post-tool-failure.ts +++ b/src/hooks/post-tool-failure.ts @@ -1,5 +1,5 @@ #!/usr/bin/env node -import { shouldCaptureTool, truncateText } from "./_capture-filter.js"; +import { shouldCaptureTool, truncateJson, truncateText } from "./_capture-filter.js"; import { resolveProject, hookCwd } from "./_project.js"; import { captureObservation, isDrainChild, runDrainChild, withEventId } from "./_capture.js"; @@ -52,11 +52,11 @@ async function main() { tool_input: typeof toolInput === "string" ? truncateText(toolInput, 4000) - : truncateText(JSON.stringify(toolInput ?? ""), 4000), + : truncateJson(toolInput ?? "", 4000), error: typeof error === "string" ? truncateText(error, 4000) - : truncateText(JSON.stringify(error ?? ""), 4000), + : truncateJson(error ?? "", 4000), }, }, data, diff --git a/test/capture-filter.test.ts b/test/capture-filter.test.ts index 5a91cf554e..749b90abad 100644 --- a/test/capture-filter.test.ts +++ b/test/capture-filter.test.ts @@ -5,6 +5,7 @@ import { preCompactBudget, shouldCaptureTool, truncateCaptureOutput, + truncateJson, truncateText, } from "../src/hooks/_capture-filter.js"; @@ -139,6 +140,20 @@ describe("truncation redacts before cutting", () => { }); }); +describe("truncateCaptureOutput redacts structured values before serializing", () => { + it("removes a private block whose tag spans a line break", () => { + const out = truncateCaptureOutput({ log: "hidden-value" + "y".repeat(200) }, 60) as string; + expect(out).not.toContain("hidden"); + }); + + it("truncateJson leaves short JSON unchanged and redacts long JSON before cutting", () => { + expect(truncateJson({ a: 1 }, 100)).toBe('{"a":1}'); + const out = truncateJson({ log: "hidden-value" + "y".repeat(200) }, 60); + expect(out).not.toContain("hidden"); + expect(out.length).toBeLessThanOrEqual(60); + }); +}); + describe("truncateCaptureOutput", () => { it("leaves short strings alone", () => { expect(truncateCaptureOutput("hello", 20)).toBe("hello"); From 942e134d5e800d16b73aaf98a777ff06df2888e3 Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Sat, 7 Nov 2026 00:46:43 +0530 Subject: [PATCH 3/7] fix(hooks): redact the whole serialized value, not each field Redacting each field before serialization removed the opening tag of a private block that started in one field and ended in the next, so the text in the second field was kept. It also left object keys alone, and JSON escaping could hide a credential in a key. Structured values are now serialized once. The JSON escapes for line breaks and tabs are turned back into whitespace, the whole text is redacted, and then the escapes are restored. This covers private blocks that span fields or contain line breaks, and secrets in keys or values. --- plugin/scripts/post-tool-failure.mjs | 33 ++++++++------------------- plugin/scripts/post-tool-use.mjs | 34 ++++++++-------------------- src/hooks/_capture-filter.ts | 13 ++++++++--- test/capture-filter.test.ts | 18 +++++++++++++++ 4 files changed, 47 insertions(+), 51 deletions(-) diff --git a/plugin/scripts/post-tool-failure.mjs b/plugin/scripts/post-tool-failure.mjs index 43b0565152..d10dbf8b3b 100755 --- a/plugin/scripts/post-tool-failure.mjs +++ b/plugin/scripts/post-tool-failure.mjs @@ -55,29 +55,6 @@ function stripPrivateData(input) { } return result; } -function scrubRecord(value, options = {}) { - if (typeof value === "string") return stripPrivateData(value); - if (Array.isArray(value)) return value.map((item) => scrubRecord(item, options)); - if (value !== null && typeof value === "object") { - const proto = Object.getPrototypeOf(value); - if (proto !== Object.prototype && proto !== null) return value; - const entries = Object.entries(value).map(([key, item]) => [options.scrubKeys ? stripPrivateData(key) : key, item]); - const reserved = new Set(entries.map(([key]) => key)); - const used = /* @__PURE__ */ new Set(); - return Object.fromEntries(entries.map(([key, item]) => { - let unique = key; - if (used.has(unique)) { - let suffix = 2; - do - unique = `${key}_${suffix++}`; - while (reserved.has(unique) || used.has(unique)); - } - used.add(unique); - return [unique, scrubRecord(item, options)]; - })); - } - return value; -} //#endregion //#region src/hooks/_capture-filter.ts const DEFAULT_DENY_PATTERNS = [ @@ -128,10 +105,18 @@ function truncateText(value, max) { if (value.length <= max) return value; return cut(stripPrivateData(value), max); } +const JSON_WHITESPACE_ESCAPES = { + n: "\n", + r: "\r", + t: " " +}; +function redactSerialized(serialized) { + return stripPrivateData(serialized.replace(/\\([\\nrt])/g, (escape, c) => JSON_WHITESPACE_ESCAPES[c] ?? escape)).replace(/[\n\r\t]/g, (c) => JSON.stringify(c).slice(1, -1)); +} function truncateJson(value, max) { const str = JSON.stringify(value); if (str.length <= max) return str; - return cut(stripPrivateData(JSON.stringify(scrubRecord(value))), max); + return cut(redactSerialized(str), max); } //#endregion //#region src/hooks/_project.ts diff --git a/plugin/scripts/post-tool-use.mjs b/plugin/scripts/post-tool-use.mjs index e1f5186f75..acb8855983 100755 --- a/plugin/scripts/post-tool-use.mjs +++ b/plugin/scripts/post-tool-use.mjs @@ -55,29 +55,6 @@ function stripPrivateData(input) { } return result; } -function scrubRecord(value, options = {}) { - if (typeof value === "string") return stripPrivateData(value); - if (Array.isArray(value)) return value.map((item) => scrubRecord(item, options)); - if (value !== null && typeof value === "object") { - const proto = Object.getPrototypeOf(value); - if (proto !== Object.prototype && proto !== null) return value; - const entries = Object.entries(value).map(([key, item]) => [options.scrubKeys ? stripPrivateData(key) : key, item]); - const reserved = new Set(entries.map(([key]) => key)); - const used = /* @__PURE__ */ new Set(); - return Object.fromEntries(entries.map(([key, item]) => { - let unique = key; - if (used.has(unique)) { - let suffix = 2; - do - unique = `${key}_${suffix++}`; - while (reserved.has(unique) || used.has(unique)); - } - used.add(unique); - return [unique, scrubRecord(item, options)]; - })); - } - return value; -} //#endregion //#region src/hooks/_capture-filter.ts const DEFAULT_DENY_PATTERNS = [ @@ -130,6 +107,14 @@ function cut(value, max) { const end = /[\uD800-\uDBFF]/.test(value[max - 1] ?? "") ? max - 1 : max; return value.slice(0, end); } +const JSON_WHITESPACE_ESCAPES = { + n: "\n", + r: "\r", + t: " " +}; +function redactSerialized(serialized) { + return stripPrivateData(serialized.replace(/\\([\\nrt])/g, (escape, c) => JSON_WHITESPACE_ESCAPES[c] ?? escape)).replace(/[\n\r\t]/g, (c) => JSON.stringify(c).slice(1, -1)); +} function truncateWithSuffix(value, max, suffix) { const safe = stripPrivateData(value); if (safe.length <= max) return safe; @@ -138,7 +123,8 @@ function truncateWithSuffix(value, max, suffix) { function truncateCaptureOutput(value, max) { if (typeof value === "string" && value.length > max) return truncateWithSuffix(value, max, "\n[...truncated]"); if (typeof value === "object" && value !== null) { - if (JSON.stringify(value).length > max) return truncateWithSuffix(JSON.stringify(scrubRecord(value)), max, "...[truncated]"); + const str = JSON.stringify(value); + if (str.length > max) return truncateWithSuffix(redactSerialized(str), max, "...[truncated]"); return value; } return value; diff --git a/src/hooks/_capture-filter.ts b/src/hooks/_capture-filter.ts index 48d75fbce0..1715ad1341 100644 --- a/src/hooks/_capture-filter.ts +++ b/src/hooks/_capture-filter.ts @@ -1,4 +1,4 @@ -import { scrubRecord, stripPrivateData } from "../functions/privacy.js"; +import { stripPrivateData } from "../functions/privacy.js"; const DEFAULT_DENY_PATTERNS = [ "memory_*", @@ -73,10 +73,17 @@ export function truncateText(value: string, max: number): string { return cut(stripPrivateData(value), max); } +const JSON_WHITESPACE_ESCAPES: Record = { n: "\n", r: "\r", t: "\t" }; + +function redactSerialized(serialized: string): string { + const unescaped = serialized.replace(/\\([\\nrt])/g, (escape, c: string) => JSON_WHITESPACE_ESCAPES[c] ?? escape); + return stripPrivateData(unescaped).replace(/[\n\r\t]/g, (c) => JSON.stringify(c).slice(1, -1)); +} + export function truncateJson(value: unknown, max: number): string { const str = JSON.stringify(value); if (str.length <= max) return str; - return cut(stripPrivateData(JSON.stringify(scrubRecord(value))), max); + return cut(redactSerialized(str), max); } function truncateWithSuffix(value: string, max: number, suffix: string): string { @@ -91,7 +98,7 @@ export function truncateCaptureOutput(value: unknown, max: number): unknown { } if (typeof value === "object" && value !== null) { const str = JSON.stringify(value); - if (str.length > max) return truncateWithSuffix(JSON.stringify(scrubRecord(value)), max, "...[truncated]"); + if (str.length > max) return truncateWithSuffix(redactSerialized(str), max, "...[truncated]"); return value; } return value; diff --git a/test/capture-filter.test.ts b/test/capture-filter.test.ts index 749b90abad..c2ea264ab0 100644 --- a/test/capture-filter.test.ts +++ b/test/capture-filter.test.ts @@ -146,6 +146,24 @@ describe("truncateCaptureOutput redacts structured values before serializing", ( expect(out).not.toContain("hidden"); }); + it("removes a private block that opens in one field and closes in the next", () => { + const value = { a: "start ", b: "hidden-value end", pad: "y".repeat(200) }; + expect(truncateCaptureOutput(value, 80) as string).not.toContain("hidden"); + expect(truncateJson(value, 80)).not.toContain("hidden"); + }); + + it("redacts a credential in a key that a line break separates from its scheme", () => { + const key = "Bearer\n" + "A".repeat(32); + const value = { [key]: "x", pad: "y".repeat(200) }; + expect(truncateJson(value, 100)).not.toContain("A".repeat(32)); + expect(truncateCaptureOutput(value, 100) as string).not.toContain("A".repeat(32)); + }); + + it("keeps escaped backslashes and line breaks in the cut JSON", () => { + const out = truncateJson({ a: "one\ntwo\\nthree", pad: "y".repeat(200) }, 60); + expect(out.startsWith('{"a":"one\\ntwo\\\\nthree"')).toBe(true); + }); + it("truncateJson leaves short JSON unchanged and redacts long JSON before cutting", () => { expect(truncateJson({ a: 1 }, 100)).toBe('{"a":1}'); const out = truncateJson({ log: "hidden-value" + "y".repeat(200) }, 60); From d1c4ea17453a6e3deca3aa4e17eccbaf4852eb78 Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Sat, 7 Nov 2026 00:59:00 +0530 Subject: [PATCH 4/7] fix(hooks): strip secrets from each field before serializing In the serialized text, JSON escapes the quote in secret="TOKEN", and the secret pattern then no longer matches, so the token survived the cut. Secrets are now stripped from each key and string value before serialization, and private blocks are still redacted over the whole serialized text so that blocks spanning fields are removed. stripSecrets is split out of stripPrivateData for this. The hook bundles that include privacy.ts are rebuilt. --- plugin/scripts/_capture.mjs | 6 ++++-- plugin/scripts/post-tool-failure.mjs | 18 +++++++++++++++--- plugin/scripts/post-tool-use.mjs | 19 +++++++++++++++---- plugin/scripts/session-end.mjs | 6 ++++-- plugin/scripts/subagent-stop.mjs | 6 ++++-- plugin/scripts/task-completed.mjs | 6 ++++-- src/functions/privacy.ts | 7 +++++-- src/hooks/_capture-filter.ts | 17 ++++++++++++++--- test/capture-filter.test.ts | 6 ++++++ 9 files changed, 71 insertions(+), 20 deletions(-) diff --git a/plugin/scripts/_capture.mjs b/plugin/scripts/_capture.mjs index 83bc5d0d17..406f16e2ac 100644 --- a/plugin/scripts/_capture.mjs +++ b/plugin/scripts/_capture.mjs @@ -332,8 +332,10 @@ const SECRET_PATTERN_SOURCES = [ const PRIVATE_KEY_BLOCK_RE = /-----BEGIN [A-Z0-9 ]*PRIVATE KEY(?: BLOCK)?-----[\s\S]*?(?:-----END [A-Z0-9 ]*PRIVATE KEY(?: BLOCK)?-----|$)/g; const URL_CREDENTIALS_RE = /\b([a-z][a-z0-9+.-]*:\/\/)[^\s/?#@:"'<>]*:[^\s/?#@"'<>]+@/gi; function stripPrivateData(input) { - let result = redactPrivateBlocks(input); - result = result.replace(new RegExp(PRIVATE_KEY_BLOCK_RE.source, PRIVATE_KEY_BLOCK_RE.flags), "[REDACTED_SECRET]"); + return stripSecrets(redactPrivateBlocks(input)); +} +function stripSecrets(input) { + let result = input.replace(new RegExp(PRIVATE_KEY_BLOCK_RE.source, PRIVATE_KEY_BLOCK_RE.flags), "[REDACTED_SECRET]"); result = result.replace(new RegExp(URL_CREDENTIALS_RE.source, URL_CREDENTIALS_RE.flags), "$1[REDACTED_SECRET]@"); for (const source of SECRET_PATTERN_SOURCES) { const pattern = new RegExp(source.source, source.flags); diff --git a/plugin/scripts/post-tool-failure.mjs b/plugin/scripts/post-tool-failure.mjs index d10dbf8b3b..3fdf23a876 100755 --- a/plugin/scripts/post-tool-failure.mjs +++ b/plugin/scripts/post-tool-failure.mjs @@ -46,8 +46,10 @@ const SECRET_PATTERN_SOURCES = [ const PRIVATE_KEY_BLOCK_RE = /-----BEGIN [A-Z0-9 ]*PRIVATE KEY(?: BLOCK)?-----[\s\S]*?(?:-----END [A-Z0-9 ]*PRIVATE KEY(?: BLOCK)?-----|$)/g; const URL_CREDENTIALS_RE = /\b([a-z][a-z0-9+.-]*:\/\/)[^\s/?#@:"'<>]*:[^\s/?#@"'<>]+@/gi; function stripPrivateData(input) { - let result = redactPrivateBlocks(input); - result = result.replace(new RegExp(PRIVATE_KEY_BLOCK_RE.source, PRIVATE_KEY_BLOCK_RE.flags), "[REDACTED_SECRET]"); + return stripSecrets(redactPrivateBlocks(input)); +} +function stripSecrets(input) { + let result = input.replace(new RegExp(PRIVATE_KEY_BLOCK_RE.source, PRIVATE_KEY_BLOCK_RE.flags), "[REDACTED_SECRET]"); result = result.replace(new RegExp(URL_CREDENTIALS_RE.source, URL_CREDENTIALS_RE.flags), "$1[REDACTED_SECRET]@"); for (const source of SECRET_PATTERN_SOURCES) { const pattern = new RegExp(source.source, source.flags); @@ -110,13 +112,23 @@ const JSON_WHITESPACE_ESCAPES = { r: "\r", t: " " }; +function scrubSecrets(value) { + if (typeof value === "string") return stripSecrets(value); + if (Array.isArray(value)) return value.map(scrubSecrets); + if (value !== null && typeof value === "object") { + const proto = Object.getPrototypeOf(value); + if (proto !== Object.prototype && proto !== null) return value; + return Object.fromEntries(Object.entries(value).map(([key, item]) => [stripSecrets(key), scrubSecrets(item)])); + } + return value; +} function redactSerialized(serialized) { return stripPrivateData(serialized.replace(/\\([\\nrt])/g, (escape, c) => JSON_WHITESPACE_ESCAPES[c] ?? escape)).replace(/[\n\r\t]/g, (c) => JSON.stringify(c).slice(1, -1)); } function truncateJson(value, max) { const str = JSON.stringify(value); if (str.length <= max) return str; - return cut(redactSerialized(str), max); + return cut(redactSerialized(JSON.stringify(scrubSecrets(value))), max); } //#endregion //#region src/hooks/_project.ts diff --git a/plugin/scripts/post-tool-use.mjs b/plugin/scripts/post-tool-use.mjs index acb8855983..f3f9c74678 100755 --- a/plugin/scripts/post-tool-use.mjs +++ b/plugin/scripts/post-tool-use.mjs @@ -46,8 +46,10 @@ const SECRET_PATTERN_SOURCES = [ const PRIVATE_KEY_BLOCK_RE = /-----BEGIN [A-Z0-9 ]*PRIVATE KEY(?: BLOCK)?-----[\s\S]*?(?:-----END [A-Z0-9 ]*PRIVATE KEY(?: BLOCK)?-----|$)/g; const URL_CREDENTIALS_RE = /\b([a-z][a-z0-9+.-]*:\/\/)[^\s/?#@:"'<>]*:[^\s/?#@"'<>]+@/gi; function stripPrivateData(input) { - let result = redactPrivateBlocks(input); - result = result.replace(new RegExp(PRIVATE_KEY_BLOCK_RE.source, PRIVATE_KEY_BLOCK_RE.flags), "[REDACTED_SECRET]"); + return stripSecrets(redactPrivateBlocks(input)); +} +function stripSecrets(input) { + let result = input.replace(new RegExp(PRIVATE_KEY_BLOCK_RE.source, PRIVATE_KEY_BLOCK_RE.flags), "[REDACTED_SECRET]"); result = result.replace(new RegExp(URL_CREDENTIALS_RE.source, URL_CREDENTIALS_RE.flags), "$1[REDACTED_SECRET]@"); for (const source of SECRET_PATTERN_SOURCES) { const pattern = new RegExp(source.source, source.flags); @@ -112,6 +114,16 @@ const JSON_WHITESPACE_ESCAPES = { r: "\r", t: " " }; +function scrubSecrets(value) { + if (typeof value === "string") return stripSecrets(value); + if (Array.isArray(value)) return value.map(scrubSecrets); + if (value !== null && typeof value === "object") { + const proto = Object.getPrototypeOf(value); + if (proto !== Object.prototype && proto !== null) return value; + return Object.fromEntries(Object.entries(value).map(([key, item]) => [stripSecrets(key), scrubSecrets(item)])); + } + return value; +} function redactSerialized(serialized) { return stripPrivateData(serialized.replace(/\\([\\nrt])/g, (escape, c) => JSON_WHITESPACE_ESCAPES[c] ?? escape)).replace(/[\n\r\t]/g, (c) => JSON.stringify(c).slice(1, -1)); } @@ -123,8 +135,7 @@ function truncateWithSuffix(value, max, suffix) { function truncateCaptureOutput(value, max) { if (typeof value === "string" && value.length > max) return truncateWithSuffix(value, max, "\n[...truncated]"); if (typeof value === "object" && value !== null) { - const str = JSON.stringify(value); - if (str.length > max) return truncateWithSuffix(redactSerialized(str), max, "...[truncated]"); + if (JSON.stringify(value).length > max) return truncateWithSuffix(redactSerialized(JSON.stringify(scrubSecrets(value))), max, "...[truncated]"); return value; } return value; diff --git a/plugin/scripts/session-end.mjs b/plugin/scripts/session-end.mjs index 9c7d7bf3ed..8761e46c23 100755 --- a/plugin/scripts/session-end.mjs +++ b/plugin/scripts/session-end.mjs @@ -46,8 +46,10 @@ const SECRET_PATTERN_SOURCES = [ const PRIVATE_KEY_BLOCK_RE = /-----BEGIN [A-Z0-9 ]*PRIVATE KEY(?: BLOCK)?-----[\s\S]*?(?:-----END [A-Z0-9 ]*PRIVATE KEY(?: BLOCK)?-----|$)/g; const URL_CREDENTIALS_RE = /\b([a-z][a-z0-9+.-]*:\/\/)[^\s/?#@:"'<>]*:[^\s/?#@"'<>]+@/gi; function stripPrivateData(input) { - let result = redactPrivateBlocks(input); - result = result.replace(new RegExp(PRIVATE_KEY_BLOCK_RE.source, PRIVATE_KEY_BLOCK_RE.flags), "[REDACTED_SECRET]"); + return stripSecrets(redactPrivateBlocks(input)); +} +function stripSecrets(input) { + let result = input.replace(new RegExp(PRIVATE_KEY_BLOCK_RE.source, PRIVATE_KEY_BLOCK_RE.flags), "[REDACTED_SECRET]"); result = result.replace(new RegExp(URL_CREDENTIALS_RE.source, URL_CREDENTIALS_RE.flags), "$1[REDACTED_SECRET]@"); for (const source of SECRET_PATTERN_SOURCES) { const pattern = new RegExp(source.source, source.flags); diff --git a/plugin/scripts/subagent-stop.mjs b/plugin/scripts/subagent-stop.mjs index 0786f45533..4b92d4c17f 100755 --- a/plugin/scripts/subagent-stop.mjs +++ b/plugin/scripts/subagent-stop.mjs @@ -46,8 +46,10 @@ const SECRET_PATTERN_SOURCES = [ const PRIVATE_KEY_BLOCK_RE = /-----BEGIN [A-Z0-9 ]*PRIVATE KEY(?: BLOCK)?-----[\s\S]*?(?:-----END [A-Z0-9 ]*PRIVATE KEY(?: BLOCK)?-----|$)/g; const URL_CREDENTIALS_RE = /\b([a-z][a-z0-9+.-]*:\/\/)[^\s/?#@:"'<>]*:[^\s/?#@"'<>]+@/gi; function stripPrivateData$1(input) { - let result = redactPrivateBlocks(input); - result = result.replace(new RegExp(PRIVATE_KEY_BLOCK_RE.source, PRIVATE_KEY_BLOCK_RE.flags), "[REDACTED_SECRET]"); + return stripSecrets(redactPrivateBlocks(input)); +} +function stripSecrets(input) { + let result = input.replace(new RegExp(PRIVATE_KEY_BLOCK_RE.source, PRIVATE_KEY_BLOCK_RE.flags), "[REDACTED_SECRET]"); result = result.replace(new RegExp(URL_CREDENTIALS_RE.source, URL_CREDENTIALS_RE.flags), "$1[REDACTED_SECRET]@"); for (const source of SECRET_PATTERN_SOURCES) { const pattern = new RegExp(source.source, source.flags); diff --git a/plugin/scripts/task-completed.mjs b/plugin/scripts/task-completed.mjs index baaa8a4f4d..090cdf2de6 100755 --- a/plugin/scripts/task-completed.mjs +++ b/plugin/scripts/task-completed.mjs @@ -46,8 +46,10 @@ const SECRET_PATTERN_SOURCES = [ const PRIVATE_KEY_BLOCK_RE = /-----BEGIN [A-Z0-9 ]*PRIVATE KEY(?: BLOCK)?-----[\s\S]*?(?:-----END [A-Z0-9 ]*PRIVATE KEY(?: BLOCK)?-----|$)/g; const URL_CREDENTIALS_RE = /\b([a-z][a-z0-9+.-]*:\/\/)[^\s/?#@:"'<>]*:[^\s/?#@"'<>]+@/gi; function stripPrivateData(input) { - let result = redactPrivateBlocks(input); - result = result.replace(new RegExp(PRIVATE_KEY_BLOCK_RE.source, PRIVATE_KEY_BLOCK_RE.flags), "[REDACTED_SECRET]"); + return stripSecrets(redactPrivateBlocks(input)); +} +function stripSecrets(input) { + let result = input.replace(new RegExp(PRIVATE_KEY_BLOCK_RE.source, PRIVATE_KEY_BLOCK_RE.flags), "[REDACTED_SECRET]"); result = result.replace(new RegExp(URL_CREDENTIALS_RE.source, URL_CREDENTIALS_RE.flags), "$1[REDACTED_SECRET]@"); for (const source of SECRET_PATTERN_SOURCES) { const pattern = new RegExp(source.source, source.flags); diff --git a/src/functions/privacy.ts b/src/functions/privacy.ts index e4f4a13d68..9bf99ec476 100644 --- a/src/functions/privacy.ts +++ b/src/functions/privacy.ts @@ -51,8 +51,11 @@ const URL_CREDENTIALS_RE = /\b([a-z][a-z0-9+.-]*:\/\/)[^\s/?#@:"'<>]*:[^\s/?#@"'<>]+@/gi; export function stripPrivateData(input: string): string { - let result = redactPrivateBlocks(input); - result = result.replace(new RegExp(PRIVATE_KEY_BLOCK_RE.source, PRIVATE_KEY_BLOCK_RE.flags), "[REDACTED_SECRET]"); + return stripSecrets(redactPrivateBlocks(input)); +} + +export function stripSecrets(input: string): string { + let result = input.replace(new RegExp(PRIVATE_KEY_BLOCK_RE.source, PRIVATE_KEY_BLOCK_RE.flags), "[REDACTED_SECRET]"); result = result.replace(new RegExp(URL_CREDENTIALS_RE.source, URL_CREDENTIALS_RE.flags), "$1[REDACTED_SECRET]@"); for (const source of SECRET_PATTERN_SOURCES) { const pattern = new RegExp(source.source, source.flags); diff --git a/src/hooks/_capture-filter.ts b/src/hooks/_capture-filter.ts index 1715ad1341..bf4125480f 100644 --- a/src/hooks/_capture-filter.ts +++ b/src/hooks/_capture-filter.ts @@ -1,4 +1,4 @@ -import { stripPrivateData } from "../functions/privacy.js"; +import { stripPrivateData, stripSecrets } from "../functions/privacy.js"; const DEFAULT_DENY_PATTERNS = [ "memory_*", @@ -75,6 +75,17 @@ export function truncateText(value: string, max: number): string { const JSON_WHITESPACE_ESCAPES: Record = { n: "\n", r: "\r", t: "\t" }; +function scrubSecrets(value: unknown): unknown { + if (typeof value === "string") return stripSecrets(value); + if (Array.isArray(value)) return value.map(scrubSecrets); + if (value !== null && typeof value === "object") { + const proto = Object.getPrototypeOf(value); + if (proto !== Object.prototype && proto !== null) return value; + return Object.fromEntries(Object.entries(value).map(([key, item]) => [stripSecrets(key), scrubSecrets(item)])); + } + return value; +} + function redactSerialized(serialized: string): string { const unescaped = serialized.replace(/\\([\\nrt])/g, (escape, c: string) => JSON_WHITESPACE_ESCAPES[c] ?? escape); return stripPrivateData(unescaped).replace(/[\n\r\t]/g, (c) => JSON.stringify(c).slice(1, -1)); @@ -83,7 +94,7 @@ function redactSerialized(serialized: string): string { export function truncateJson(value: unknown, max: number): string { const str = JSON.stringify(value); if (str.length <= max) return str; - return cut(redactSerialized(str), max); + return cut(redactSerialized(JSON.stringify(scrubSecrets(value))), max); } function truncateWithSuffix(value: string, max: number, suffix: string): string { @@ -98,7 +109,7 @@ export function truncateCaptureOutput(value: unknown, max: number): unknown { } if (typeof value === "object" && value !== null) { const str = JSON.stringify(value); - if (str.length > max) return truncateWithSuffix(redactSerialized(str), max, "...[truncated]"); + if (str.length > max) return truncateWithSuffix(redactSerialized(JSON.stringify(scrubSecrets(value))), max, "...[truncated]"); return value; } return value; diff --git a/test/capture-filter.test.ts b/test/capture-filter.test.ts index c2ea264ab0..6b7fee160a 100644 --- a/test/capture-filter.test.ts +++ b/test/capture-filter.test.ts @@ -159,6 +159,12 @@ describe("truncateCaptureOutput redacts structured values before serializing", ( expect(truncateCaptureOutput(value, 100) as string).not.toContain("A".repeat(32)); }); + it("redacts a quoted credential whose quote JSON escapes", () => { + const value = { cmd: 'secret="' + "B".repeat(32) + '"', pad: "y".repeat(200) }; + expect(truncateJson(value, 100)).not.toContain("B".repeat(20)); + expect(truncateCaptureOutput(value, 100) as string).not.toContain("B".repeat(20)); + }); + it("keeps escaped backslashes and line breaks in the cut JSON", () => { const out = truncateJson({ a: "one\ntwo\\nthree", pad: "y".repeat(200) }, 60); expect(out.startsWith('{"a":"one\\ntwo\\\\nthree"')).toBe(true); From 81de0e368c836de5515987feaddaba92c05635a7 Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Sat, 7 Nov 2026 01:12:11 +0530 Subject: [PATCH 5/7] fix(hooks): keep every field when redacted keys collide Two keys that held different secrets both became [REDACTED_SECRET], and the second value replaced the first. The capture filter now uses scrubRecord, which gives repeated keys a unique suffix. scrubRecord takes a strip option so the filter can remove secrets there and leave private blocks to the redaction of the whole serialized text. --- plugin/scripts/_capture.mjs | 5 ++-- plugin/scripts/post-tool-failure.mjs | 36 +++++++++++++++++++++------- plugin/scripts/post-tool-use.mjs | 36 +++++++++++++++++++++------- src/functions/privacy.ts | 10 +++++--- src/hooks/_capture-filter.ts | 11 ++------- test/capture-filter.test.ts | 12 ++++++++++ 6 files changed, 80 insertions(+), 30 deletions(-) diff --git a/plugin/scripts/_capture.mjs b/plugin/scripts/_capture.mjs index 406f16e2ac..5ccd50a168 100644 --- a/plugin/scripts/_capture.mjs +++ b/plugin/scripts/_capture.mjs @@ -344,12 +344,13 @@ function stripSecrets(input) { return result; } function scrubRecord(value, options = {}) { - if (typeof value === "string") return stripPrivateData(value); + const strip = options.strip ?? stripPrivateData; + if (typeof value === "string") return strip(value); if (Array.isArray(value)) return value.map((item) => scrubRecord(item, options)); if (value !== null && typeof value === "object") { const proto = Object.getPrototypeOf(value); if (proto !== Object.prototype && proto !== null) return value; - const entries = Object.entries(value).map(([key, item]) => [options.scrubKeys ? stripPrivateData(key) : key, item]); + const entries = Object.entries(value).map(([key, item]) => [options.scrubKeys ? strip(key) : key, item]); const reserved = new Set(entries.map(([key]) => key)); const used = /* @__PURE__ */ new Set(); return Object.fromEntries(entries.map(([key, item]) => { diff --git a/plugin/scripts/post-tool-failure.mjs b/plugin/scripts/post-tool-failure.mjs index 3fdf23a876..0445060932 100755 --- a/plugin/scripts/post-tool-failure.mjs +++ b/plugin/scripts/post-tool-failure.mjs @@ -57,6 +57,30 @@ function stripSecrets(input) { } return result; } +function scrubRecord(value, options = {}) { + const strip = options.strip ?? stripPrivateData; + if (typeof value === "string") return strip(value); + if (Array.isArray(value)) return value.map((item) => scrubRecord(item, options)); + if (value !== null && typeof value === "object") { + const proto = Object.getPrototypeOf(value); + if (proto !== Object.prototype && proto !== null) return value; + const entries = Object.entries(value).map(([key, item]) => [options.scrubKeys ? strip(key) : key, item]); + const reserved = new Set(entries.map(([key]) => key)); + const used = /* @__PURE__ */ new Set(); + return Object.fromEntries(entries.map(([key, item]) => { + let unique = key; + if (used.has(unique)) { + let suffix = 2; + do + unique = `${key}_${suffix++}`; + while (reserved.has(unique) || used.has(unique)); + } + used.add(unique); + return [unique, scrubRecord(item, options)]; + })); + } + return value; +} //#endregion //#region src/hooks/_capture-filter.ts const DEFAULT_DENY_PATTERNS = [ @@ -113,14 +137,10 @@ const JSON_WHITESPACE_ESCAPES = { t: " " }; function scrubSecrets(value) { - if (typeof value === "string") return stripSecrets(value); - if (Array.isArray(value)) return value.map(scrubSecrets); - if (value !== null && typeof value === "object") { - const proto = Object.getPrototypeOf(value); - if (proto !== Object.prototype && proto !== null) return value; - return Object.fromEntries(Object.entries(value).map(([key, item]) => [stripSecrets(key), scrubSecrets(item)])); - } - return value; + return scrubRecord(value, { + scrubKeys: true, + strip: stripSecrets + }); } function redactSerialized(serialized) { return stripPrivateData(serialized.replace(/\\([\\nrt])/g, (escape, c) => JSON_WHITESPACE_ESCAPES[c] ?? escape)).replace(/[\n\r\t]/g, (c) => JSON.stringify(c).slice(1, -1)); diff --git a/plugin/scripts/post-tool-use.mjs b/plugin/scripts/post-tool-use.mjs index f3f9c74678..20e7035a19 100755 --- a/plugin/scripts/post-tool-use.mjs +++ b/plugin/scripts/post-tool-use.mjs @@ -57,6 +57,30 @@ function stripSecrets(input) { } return result; } +function scrubRecord(value, options = {}) { + const strip = options.strip ?? stripPrivateData; + if (typeof value === "string") return strip(value); + if (Array.isArray(value)) return value.map((item) => scrubRecord(item, options)); + if (value !== null && typeof value === "object") { + const proto = Object.getPrototypeOf(value); + if (proto !== Object.prototype && proto !== null) return value; + const entries = Object.entries(value).map(([key, item]) => [options.scrubKeys ? strip(key) : key, item]); + const reserved = new Set(entries.map(([key]) => key)); + const used = /* @__PURE__ */ new Set(); + return Object.fromEntries(entries.map(([key, item]) => { + let unique = key; + if (used.has(unique)) { + let suffix = 2; + do + unique = `${key}_${suffix++}`; + while (reserved.has(unique) || used.has(unique)); + } + used.add(unique); + return [unique, scrubRecord(item, options)]; + })); + } + return value; +} //#endregion //#region src/hooks/_capture-filter.ts const DEFAULT_DENY_PATTERNS = [ @@ -115,14 +139,10 @@ const JSON_WHITESPACE_ESCAPES = { t: " " }; function scrubSecrets(value) { - if (typeof value === "string") return stripSecrets(value); - if (Array.isArray(value)) return value.map(scrubSecrets); - if (value !== null && typeof value === "object") { - const proto = Object.getPrototypeOf(value); - if (proto !== Object.prototype && proto !== null) return value; - return Object.fromEntries(Object.entries(value).map(([key, item]) => [stripSecrets(key), scrubSecrets(item)])); - } - return value; + return scrubRecord(value, { + scrubKeys: true, + strip: stripSecrets + }); } function redactSerialized(serialized) { return stripPrivateData(serialized.replace(/\\([\\nrt])/g, (escape, c) => JSON_WHITESPACE_ESCAPES[c] ?? escape)).replace(/[\n\r\t]/g, (c) => JSON.stringify(c).slice(1, -1)); diff --git a/src/functions/privacy.ts b/src/functions/privacy.ts index 9bf99ec476..3eece0d48d 100644 --- a/src/functions/privacy.ts +++ b/src/functions/privacy.ts @@ -64,14 +64,18 @@ export function stripSecrets(input: string): string { return result; } -export function scrubRecord(value: T, options: { scrubKeys?: boolean } = {}): T { - if (typeof value === "string") return stripPrivateData(value) as T; +export function scrubRecord( + value: T, + options: { scrubKeys?: boolean; strip?: (input: string) => string } = {}, +): T { + const strip = options.strip ?? stripPrivateData; + if (typeof value === "string") return strip(value) as T; if (Array.isArray(value)) return value.map((item) => scrubRecord(item, options)) as T; if (value !== null && typeof value === "object") { const proto = Object.getPrototypeOf(value); if (proto !== Object.prototype && proto !== null) return value; const entries = Object.entries(value as Record).map(([key, item]) => [ - options.scrubKeys ? stripPrivateData(key) : key, + options.scrubKeys ? strip(key) : key, item, ] as const); const reserved = new Set(entries.map(([key]) => key)); diff --git a/src/hooks/_capture-filter.ts b/src/hooks/_capture-filter.ts index bf4125480f..43fd36bf48 100644 --- a/src/hooks/_capture-filter.ts +++ b/src/hooks/_capture-filter.ts @@ -1,4 +1,4 @@ -import { stripPrivateData, stripSecrets } from "../functions/privacy.js"; +import { scrubRecord, stripPrivateData, stripSecrets } from "../functions/privacy.js"; const DEFAULT_DENY_PATTERNS = [ "memory_*", @@ -76,14 +76,7 @@ export function truncateText(value: string, max: number): string { const JSON_WHITESPACE_ESCAPES: Record = { n: "\n", r: "\r", t: "\t" }; function scrubSecrets(value: unknown): unknown { - if (typeof value === "string") return stripSecrets(value); - if (Array.isArray(value)) return value.map(scrubSecrets); - if (value !== null && typeof value === "object") { - const proto = Object.getPrototypeOf(value); - if (proto !== Object.prototype && proto !== null) return value; - return Object.fromEntries(Object.entries(value).map(([key, item]) => [stripSecrets(key), scrubSecrets(item)])); - } - return value; + return scrubRecord(value, { scrubKeys: true, strip: stripSecrets }); } function redactSerialized(serialized: string): string { diff --git a/test/capture-filter.test.ts b/test/capture-filter.test.ts index 6b7fee160a..fe131c1191 100644 --- a/test/capture-filter.test.ts +++ b/test/capture-filter.test.ts @@ -165,6 +165,18 @@ describe("truncateCaptureOutput redacts structured values before serializing", ( expect(truncateCaptureOutput(value, 100) as string).not.toContain("B".repeat(20)); }); + it("redacts a credential that a vertical tab separates from its scheme", () => { + const value = { header: "Bearer\u000b" + "C".repeat(32), pad: "y".repeat(200) }; + expect(truncateJson(value, 100)).not.toContain("C".repeat(20)); + }); + + it("keeps every field when two keys redact to the same text", () => { + const value = { ["token=" + "A".repeat(24)]: "first", ["token=" + "B".repeat(24)]: "second", pad: "y".repeat(200) }; + const out = truncateJson(value, 150); + expect(out).toContain("first"); + expect(out).toContain("second"); + }); + it("keeps escaped backslashes and line breaks in the cut JSON", () => { const out = truncateJson({ a: "one\ntwo\\nthree", pad: "y".repeat(200) }, 60); expect(out.startsWith('{"a":"one\\ntwo\\\\nthree"')).toBe(true); From 0208b26f19e34c0e9eeb1d897b22d2a3c13d9499 Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Sat, 7 Nov 2026 12:39:39 +0530 Subject: [PATCH 6/7] fix(hooks): redact values under secret-like keys before truncating A value stored under a key such as password or apiKey had no key text in front of it once serialized, so the secret patterns did not match and the cut kept the start of the value. Both truncation helpers now replace such string values first. Short inputs stay unchanged. --- plugin/scripts/post-tool-failure.mjs | 13 ++++++++++++- plugin/scripts/post-tool-use.mjs | 13 ++++++++++++- src/hooks/_capture-filter.ts | 21 ++++++++++++++++++++- test/capture-filter.test.ts | 15 +++++++++++++++ 4 files changed, 59 insertions(+), 3 deletions(-) diff --git a/plugin/scripts/post-tool-failure.mjs b/plugin/scripts/post-tool-failure.mjs index 0445060932..97c79e7145 100755 --- a/plugin/scripts/post-tool-failure.mjs +++ b/plugin/scripts/post-tool-failure.mjs @@ -136,8 +136,19 @@ const JSON_WHITESPACE_ESCAPES = { r: "\r", t: " " }; +const SECRET_KEY_RE = /api[_-]?key|secret|token|password|passwd|credential|^auth(?:orization)?$/i; +function isSecretKeyName(key) { + return /^[\w.-]{1,64}$/.test(key) && SECRET_KEY_RE.test(key); +} +function redactSecretKeys(value) { + if (Array.isArray(value)) return value.map(redactSecretKeys); + if (value === null || typeof value !== "object") return value; + const proto = Object.getPrototypeOf(value); + if (proto !== Object.prototype && proto !== null) return value; + return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, typeof item === "string" && item && isSecretKeyName(key) ? "[REDACTED_SECRET]" : redactSecretKeys(item)])); +} function scrubSecrets(value) { - return scrubRecord(value, { + return scrubRecord(redactSecretKeys(value), { scrubKeys: true, strip: stripSecrets }); diff --git a/plugin/scripts/post-tool-use.mjs b/plugin/scripts/post-tool-use.mjs index 20e7035a19..033e99db3f 100755 --- a/plugin/scripts/post-tool-use.mjs +++ b/plugin/scripts/post-tool-use.mjs @@ -138,8 +138,19 @@ const JSON_WHITESPACE_ESCAPES = { r: "\r", t: " " }; +const SECRET_KEY_RE = /api[_-]?key|secret|token|password|passwd|credential|^auth(?:orization)?$/i; +function isSecretKeyName(key) { + return /^[\w.-]{1,64}$/.test(key) && SECRET_KEY_RE.test(key); +} +function redactSecretKeys(value) { + if (Array.isArray(value)) return value.map(redactSecretKeys); + if (value === null || typeof value !== "object") return value; + const proto = Object.getPrototypeOf(value); + if (proto !== Object.prototype && proto !== null) return value; + return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, typeof item === "string" && item && isSecretKeyName(key) ? "[REDACTED_SECRET]" : redactSecretKeys(item)])); +} function scrubSecrets(value) { - return scrubRecord(value, { + return scrubRecord(redactSecretKeys(value), { scrubKeys: true, strip: stripSecrets }); diff --git a/src/hooks/_capture-filter.ts b/src/hooks/_capture-filter.ts index 43fd36bf48..09249c7de2 100644 --- a/src/hooks/_capture-filter.ts +++ b/src/hooks/_capture-filter.ts @@ -75,8 +75,27 @@ export function truncateText(value: string, max: number): string { const JSON_WHITESPACE_ESCAPES: Record = { n: "\n", r: "\r", t: "\t" }; +const SECRET_KEY_RE = /api[_-]?key|secret|token|password|passwd|credential|^auth(?:orization)?$/i; + +function isSecretKeyName(key: string): boolean { + return /^[\w.-]{1,64}$/.test(key) && SECRET_KEY_RE.test(key); +} + +function redactSecretKeys(value: unknown): unknown { + if (Array.isArray(value)) return value.map(redactSecretKeys); + if (value === null || typeof value !== "object") return value; + const proto = Object.getPrototypeOf(value); + if (proto !== Object.prototype && proto !== null) return value; + return Object.fromEntries( + Object.entries(value as Record).map(([key, item]) => [ + key, + typeof item === "string" && item && isSecretKeyName(key) ? "[REDACTED_SECRET]" : redactSecretKeys(item), + ]), + ); +} + function scrubSecrets(value: unknown): unknown { - return scrubRecord(value, { scrubKeys: true, strip: stripSecrets }); + return scrubRecord(redactSecretKeys(value), { scrubKeys: true, strip: stripSecrets }); } function redactSerialized(serialized: string): string { diff --git a/test/capture-filter.test.ts b/test/capture-filter.test.ts index fe131c1191..3d554a3de3 100644 --- a/test/capture-filter.test.ts +++ b/test/capture-filter.test.ts @@ -141,6 +141,21 @@ describe("truncation redacts before cutting", () => { }); describe("truncateCaptureOutput redacts structured values before serializing", () => { + it("redacts values under secret-like keys in both helpers", () => { + const value = { password: "A".repeat(100), nested: { apiKey: "K".repeat(40) }, note: "kept" }; + for (const out of [truncateJson(value, 90), truncateCaptureOutput(value, 90) as string]) { + expect(out).not.toContain("AAAA"); + expect(out).not.toContain("KKKK"); + expect(out).toContain("[REDACTED_SECRET]"); + } + }); + + it("leaves a short value with a secret-like key unchanged", () => { + const value = { password: "short" }; + expect(truncateJson(value, 100)).toBe(JSON.stringify(value)); + expect(truncateCaptureOutput(value, 100)).toBe(value); + }); + it("removes a private block whose tag spans a line break", () => { const out = truncateCaptureOutput({ log: "hidden-value" + "y".repeat(200) }, 60) as string; expect(out).not.toContain("hidden"); From 4ca5b73bcf55183f80edf410bb7461fa065b2bd5 Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Sat, 7 Nov 2026 13:08:36 +0530 Subject: [PATCH 7/7] fix(hooks): match secret key names by whole word Keys such as tokenization or secretary no longer lose their values. Only key words like token, password, secret or api_key are redacted. --- plugin/scripts/post-tool-failure.mjs | 16 ++++++++++++++-- plugin/scripts/post-tool-use.mjs | 16 ++++++++++++++-- src/hooks/_capture-filter.ts | 6 ++++-- test/capture-filter.test.ts | 8 ++++++++ 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/plugin/scripts/post-tool-failure.mjs b/plugin/scripts/post-tool-failure.mjs index 97c79e7145..aa941c63e3 100755 --- a/plugin/scripts/post-tool-failure.mjs +++ b/plugin/scripts/post-tool-failure.mjs @@ -136,9 +136,21 @@ const JSON_WHITESPACE_ESCAPES = { r: "\r", t: " " }; -const SECRET_KEY_RE = /api[_-]?key|secret|token|password|passwd|credential|^auth(?:orization)?$/i; +const SECRET_KEY_WORDS = new Set([ + "apikey", + "secret", + "token", + "password", + "passwd", + "credential", + "credentials", + "auth", + "authorization" +]); function isSecretKeyName(key) { - return /^[\w.-]{1,64}$/.test(key) && SECRET_KEY_RE.test(key); + if (!/^[\w.-]{1,64}$/.test(key)) return false; + const words = key.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase().split(/[_.-]+/); + return words.some((word, i) => SECRET_KEY_WORDS.has(word) || word === "api" && words[i + 1] === "key"); } function redactSecretKeys(value) { if (Array.isArray(value)) return value.map(redactSecretKeys); diff --git a/plugin/scripts/post-tool-use.mjs b/plugin/scripts/post-tool-use.mjs index 033e99db3f..bda61f130a 100755 --- a/plugin/scripts/post-tool-use.mjs +++ b/plugin/scripts/post-tool-use.mjs @@ -138,9 +138,21 @@ const JSON_WHITESPACE_ESCAPES = { r: "\r", t: " " }; -const SECRET_KEY_RE = /api[_-]?key|secret|token|password|passwd|credential|^auth(?:orization)?$/i; +const SECRET_KEY_WORDS = new Set([ + "apikey", + "secret", + "token", + "password", + "passwd", + "credential", + "credentials", + "auth", + "authorization" +]); function isSecretKeyName(key) { - return /^[\w.-]{1,64}$/.test(key) && SECRET_KEY_RE.test(key); + if (!/^[\w.-]{1,64}$/.test(key)) return false; + const words = key.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase().split(/[_.-]+/); + return words.some((word, i) => SECRET_KEY_WORDS.has(word) || word === "api" && words[i + 1] === "key"); } function redactSecretKeys(value) { if (Array.isArray(value)) return value.map(redactSecretKeys); diff --git a/src/hooks/_capture-filter.ts b/src/hooks/_capture-filter.ts index 09249c7de2..799bcf0e5c 100644 --- a/src/hooks/_capture-filter.ts +++ b/src/hooks/_capture-filter.ts @@ -75,10 +75,12 @@ export function truncateText(value: string, max: number): string { const JSON_WHITESPACE_ESCAPES: Record = { n: "\n", r: "\r", t: "\t" }; -const SECRET_KEY_RE = /api[_-]?key|secret|token|password|passwd|credential|^auth(?:orization)?$/i; +const SECRET_KEY_WORDS = new Set(["apikey", "secret", "token", "password", "passwd", "credential", "credentials", "auth", "authorization"]); function isSecretKeyName(key: string): boolean { - return /^[\w.-]{1,64}$/.test(key) && SECRET_KEY_RE.test(key); + if (!/^[\w.-]{1,64}$/.test(key)) return false; + const words = key.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase().split(/[_.-]+/); + return words.some((word, i) => SECRET_KEY_WORDS.has(word) || (word === "api" && words[i + 1] === "key")); } function redactSecretKeys(value: unknown): unknown { diff --git a/test/capture-filter.test.ts b/test/capture-filter.test.ts index 3d554a3de3..566f96ec47 100644 --- a/test/capture-filter.test.ts +++ b/test/capture-filter.test.ts @@ -150,6 +150,14 @@ describe("truncateCaptureOutput redacts structured values before serializing", ( } }); + it("keeps values whose key only contains a secret word inside another word", () => { + const value = { tokenization: "words", secretary: "office", access_token: "T".repeat(40), pad: "y".repeat(200) }; + const out = truncateJson(value, 120); + expect(out).toContain("words"); + expect(out).toContain("office"); + expect(out).not.toContain("TTTT"); + }); + it("leaves a short value with a secret-like key unchanged", () => { const value = { password: "short" }; expect(truncateJson(value, 100)).toBe(JSON.stringify(value));