Skip to content

#1 Persistent memory for AI coding agents based on real-world benchmarks

fix: preserve identifiers during secret redaction (#1540)

Rohit Ghumare35813c5194aa
diff --git a/plugin/scripts/_capture.mjs b/plugin/scripts/_capture.mjs
index d4b91d7f..efa2cc79 100644
--- a/plugin/scripts/_capture.mjs
+++ b/plugin/scripts/_capture.mjs
@@ -221,9 +221,9 @@ const PRIVATE_TAG_RE = /<private>[\s\S]*?<\/private>/gi;
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,
- /sk-proj-[A-Za-z0-9\-_]{20,}/g,
- /(?:sk|pk|rk|ak)-[A-Za-z0-9][A-Za-z0-9\-_]{19,}/g,
- /sk-ant-[A-Za-z0-9\-_]{20,}/g,
+ /(?<![\p{L}\p{N}\p{M}_])sk-proj-[A-Za-z0-9\-_]{20,}/gu,
+ /(?<![\p{L}\p{N}\p{M}_])(?:sk|pk|rk|ak)-[A-Za-z0-9][A-Za-z0-9\-_]{19,}/gu,
+ /(?<![\p{L}\p{N}\p{M}_])sk-ant-[A-Za-z0-9\-_]{20,}/gu,
/gh[pus]_[A-Za-z0-9]{36,}/g,
/github_pat_[A-Za-z0-9_]{22,}/g,
/xoxb-[A-Za-z0-9\-]+/g,
diff --git a/src/functions/privacy.ts b/src/functions/privacy.ts
index 538453e9..4aff36c0 100644
--- a/src/functions/privacy.ts
+++ b/src/functions/privacy.ts
@@ -5,9 +5,9 @@ const PRIVATE_TAG_RE = /<private>[\s\S]*?<\/private>/gi;
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,
- /sk-proj-[A-Za-z0-9\-_]{20,}/g,
- /(?:sk|pk|rk|ak)-[A-Za-z0-9][A-Za-z0-9\-_]{19,}/g,
- /sk-ant-[A-Za-z0-9\-_]{20,}/g,
+ /(?<![\p{L}\p{N}\p{M}_])sk-proj-[A-Za-z0-9\-_]{20,}/gu,
+ /(?<![\p{L}\p{N}\p{M}_])(?:sk|pk|rk|ak)-[A-Za-z0-9][A-Za-z0-9\-_]{19,}/gu,
+ /(?<![\p{L}\p{N}\p{M}_])sk-ant-[A-Za-z0-9\-_]{20,}/gu,
/gh[pus]_[A-Za-z0-9]{36,}/g,
/github_pat_[A-Za-z0-9_]{22,}/g,
/xoxb-[A-Za-z0-9\-]+/g,
diff --git a/test/privacy.test.ts b/test/privacy.test.ts
index 450e2091..0fa17b68 100644
--- a/test/privacy.test.ts
+++ b/test/privacy.test.ts
@@ -104,4 +104,37 @@ describe("stripPrivateData", () => {
expect(stripPrivateData(input)).toBe("[REDACTED_SECRET]");
expect(stripPrivateData(input)).toBe("[REDACTED_SECRET]");
});
+
+ it.each([
+ "task-runner-that-does-something-long",
+ "bookmark-this-long-reference-identifier",
+ "break-this-long-background-operation",
+ "task-proj-abcdefghijklmnopqrstuvwxyz",
+ "task-ant-abcdefghijklmnopqrstuvwxyz",
+ "prefix_pk-abcdefghijklmnopqrstuvwxyz",
+ "42rk-abcdefghijklmnopqrstuvwxyz",
+ "éak-abcdefghijklmnopqrstuvwxyz",
+ "漢sk-abcdefghijklmnopqrstuvwxyz",
+ "e\u0301sk-abcdefghijklmnopqrstuvwxyz",
+ ])("preserves key-like suffixes inside identifiers: %s", (identifier) => {
+ expect(stripPrivateData(identifier)).toBe(identifier);
+ });
+
+ it.each(["sk", "pk", "rk", "ak", "sk-proj", "sk-ant"])(
+ "still redacts standalone %s keys at punctuation and whitespace boundaries",
+ (prefix) => {
+ const key = `${prefix}-abcdefghijklmnopqrstuvwxyz`;
+ for (const [before, after] of [["", ""], ["'", "'"], ['"', '"'], ["(", ")"], ["/", "."], ["\n", "\t"], ["🔑", "!"]]) {
+ expect(stripPrivateData(`${before}${key}${after}`)).toBe(`${before}[REDACTED_SECRET]${after}`);
+ }
+ },
+ );
+
+ it("preserves identifiers while redacting adjacent keys on repeated passes", () => {
+ const input = '{"task":"task-runner-that-does-something-long","key":"sk-abcdefghijklmnopqrstuvwxyz"}';
+ const expected = '{"task":"task-runner-that-does-something-long","key":"[REDACTED_SECRET]"}';
+ expect(stripPrivateData(input)).toBe(expected);
+ expect(stripPrivateData(input)).toBe(expected);
+ expect(stripPrivateData(expected)).toBe(expected);
+ });
});