From 69d44c2590207a52b3af74f426f70ed6c3d38b60 Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Sat, 7 Nov 2026 13:37:50 +0530 Subject: [PATCH] fix(hooks): redact compound secret key names Keys such as accesstoken, clientSecret or apiKey2 now count as secret keys: a key word that ends in a secret word, or that is followed by digits, still matches. Words that only start with one, such as tokenization or secretary, keep their values. --- plugin/scripts/post-tool-failure.mjs | 13 ++++++------- plugin/scripts/post-tool-use.mjs | 13 ++++++------- src/hooks/_capture-filter.ts | 17 ++++++++++++++--- test/capture-filter.test.ts | 6 ++++-- 4 files changed, 30 insertions(+), 19 deletions(-) diff --git a/plugin/scripts/post-tool-failure.mjs b/plugin/scripts/post-tool-failure.mjs index aa941c63e3..3efd1aafc4 100755 --- a/plugin/scripts/post-tool-failure.mjs +++ b/plugin/scripts/post-tool-failure.mjs @@ -136,21 +136,20 @@ const JSON_WHITESPACE_ESCAPES = { r: "\r", t: " " }; -const SECRET_KEY_WORDS = new Set([ +const SECRET_KEY_SUFFIXES = [ "apikey", "secret", "token", "password", "passwd", "credential", - "credentials", - "auth", - "authorization" -]); + "credentials" +]; +const SECRET_KEY_WORDS = new Set(["auth", "authorization"]); function isSecretKeyName(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"); + const words = key.replace(/([a-z])([A-Z])/g, "$1_$2").replace(/([A-Za-z])(\d)/g, "$1_$2").toLowerCase().split(/[_.\-\d]+/).filter(Boolean); + return words.some((word, i) => SECRET_KEY_WORDS.has(word) || SECRET_KEY_SUFFIXES.some((suffix) => word.endsWith(suffix)) || 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 bda61f130a..50ba26d83b 100755 --- a/plugin/scripts/post-tool-use.mjs +++ b/plugin/scripts/post-tool-use.mjs @@ -138,21 +138,20 @@ const JSON_WHITESPACE_ESCAPES = { r: "\r", t: " " }; -const SECRET_KEY_WORDS = new Set([ +const SECRET_KEY_SUFFIXES = [ "apikey", "secret", "token", "password", "passwd", "credential", - "credentials", - "auth", - "authorization" -]); + "credentials" +]; +const SECRET_KEY_WORDS = new Set(["auth", "authorization"]); function isSecretKeyName(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"); + const words = key.replace(/([a-z])([A-Z])/g, "$1_$2").replace(/([A-Za-z])(\d)/g, "$1_$2").toLowerCase().split(/[_.\-\d]+/).filter(Boolean); + return words.some((word, i) => SECRET_KEY_WORDS.has(word) || SECRET_KEY_SUFFIXES.some((suffix) => word.endsWith(suffix)) || 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 799bcf0e5c..29c3df7fa1 100644 --- a/src/hooks/_capture-filter.ts +++ b/src/hooks/_capture-filter.ts @@ -75,12 +75,23 @@ export function truncateText(value: string, max: number): string { const JSON_WHITESPACE_ESCAPES: Record = { n: "\n", r: "\r", t: "\t" }; -const SECRET_KEY_WORDS = new Set(["apikey", "secret", "token", "password", "passwd", "credential", "credentials", "auth", "authorization"]); +const SECRET_KEY_SUFFIXES = ["apikey", "secret", "token", "password", "passwd", "credential", "credentials"]; +const SECRET_KEY_WORDS = new Set(["auth", "authorization"]); function isSecretKeyName(key: string): boolean { 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")); + const words = key + .replace(/([a-z])([A-Z])/g, "$1_$2") + .replace(/([A-Za-z])(\d)/g, "$1_$2") + .toLowerCase() + .split(/[_.\-\d]+/) + .filter(Boolean); + return words.some( + (word, i) => + SECRET_KEY_WORDS.has(word) || + SECRET_KEY_SUFFIXES.some((suffix) => word.endsWith(suffix)) || + (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 566f96ec47..a75f5292e6 100644 --- a/test/capture-filter.test.ts +++ b/test/capture-filter.test.ts @@ -151,11 +151,13 @@ 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); + const value = { tokenization: "words", secretary: "office", access_token: "T".repeat(40), accesstoken: "U".repeat(40), apiKey2: "V".repeat(40), pad: "y".repeat(200) }; + const out = truncateJson(value, 200); expect(out).toContain("words"); expect(out).toContain("office"); expect(out).not.toContain("TTTT"); + expect(out).not.toContain("UUUU"); + expect(out).not.toContain("VVVV"); }); it("leaves a short value with a secret-like key unchanged", () => {