repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

fix(leaks): reduce noisy provider redactions

973dbf7e8037d73dda2dcefe1682916e09f4b95c

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (7)

CHANGELOG.md

Modified
177 177 and notifications without exposing secrets
178 178 - Report leaks provider redaction paths, labels, and placeholders in safe
179 179 metadata and show unbounded path/label details in redaction notifications
180 +- Suppress leaks assignment-span false positives on common programmer-noun keys
181 + (`seed`, `id`, `name`, `count`, `type`, `value`, `path`, `key`, `ts`, `time`,
182 + `date`, `idx`, `mode`, `flag`, `opts`, `params`, `args`, `options`, `label`,
183 + `field`, `tag`); heuristic only, does not affect dotenv/env learning
184 +- Deduplicate leaks `ctx.ui.notify` per-process via `notifiedPlaceholders` so
185 + re-notification of the same redacted value across requests is suppressed
181 187 ## 0.0.3

extensions/leaks/__tests__/detectors.test.ts

Modified
72 72 "ROUTING_KEY",
73 73 "ENCRYPTION_KEY",
74 74 "SECRET_KEY_BASE",
75 - "SEED",
76 75 "UNSEAL_KEY",
77 76 "MNEMONIC",

extensions/leaks/__tests__/structures.test.ts

Modified
103 103 /^__LEAK_OPENSSH_PRIVATE_KEY_[a-f0-9]{12}__$/,
107 + it("skips assignment keys that are common programming nouns", () => {
108 + const longValue = "abcdef1234567890";
109 + for (const key of [
110 + "seed",
111 + "id",
112 + "name",
113 + "count",
114 + "type",
115 + "value",
116 + "path",
117 + "ts",
118 + "time",
119 + "date",
120 + "idx",
121 + "mode",
122 + "flag",
123 + "opts",
124 + "params",
125 + "args",
126 + "options",
127 + "label",
128 + "field",
129 + "tag",
130 + "node",
131 + "host",
132 + "port",
133 + "version",
134 + ]) {
135 + const text = `${key}=${longValue}`;
136 + expect(
137 + __test.findAssignmentSpans(text),
138 + `expected ${key}=<value> to produce zero spans`,
139 + ).toEqual([]);
141 + });
143 + it("stoplist does not block keys that overlap with SENSITIVE", () => {
144 + const longValue = "abcdef1234567890";
145 + // 'key' is in dotenv.SENSITIVE; assignment detection must still match.
146 + expect(
147 + __test.findAssignmentSpans(`KEY=${longValue}`).length,
148 + ).toBeGreaterThan(0);
149 + // 'token' is in dotenv.SENSITIVE; assignment detection must still match.
150 + expect(
151 + __test.findAssignmentSpans(`TOKEN=${longValue}`).length,
152 + ).toBeGreaterThan(0);
153 + });
155 + it("stoplist is case-insensitive for common programming nouns", () => {
156 + const longValue = "abcdef1234567890";
157 + for (const key of ["Seed", "SEED", "Id", "ID", "Name", "Type"]) {
158 + const text = `${key}=${longValue}`;
159 + expect(
160 + __test.findAssignmentSpans(text),
161 + `expected ${key}=<value> to produce zero spans`,
162 + ).toEqual([]);
164 + });

extensions/leaks/__tests__/ux.test.ts

Modified
30 30 ).toHaveLength(1);
33 + it("does not re-notify the same redacted value across provider turns", async () => {
34 + t = await createTestSession({ extensions: [extPath()] });
35 + await t.session.extensionRunner.emitBeforeProviderRequest({ text: secret });
36 + await t.session.extensionRunner.emitBeforeProviderRequest({ text: secret });
37 + await t.session.extensionRunner.emitBeforeProviderRequest({ text: secret });
39 + const notices = t.events
40 + .uiCallsFor("notify")
41 + .map((call) => String(call.args[0]));
42 + expect(
43 + notices.filter((notice) => notice.includes("leaks redacted")),
44 + ).toHaveLength(1);
45 + });
47 + it("re-notifies after /leaks clear and after reload", async () => {
48 + t = await createTestSession({
49 + extensions: [extPath()],
50 + mockUI: { confirm: true },
51 + });
52 + const command =
53 + t.session.extensionRunner.extensions[0].commands.get("leaks");
54 + const ctx = t.session.extensionRunner.createContext();
56 + await t.session.extensionRunner.emitBeforeProviderRequest({ text: secret });
57 + await t.session.extensionRunner.emitBeforeProviderRequest({ text: secret });
58 + await command.handler("clear", ctx);
59 + await t.session.extensionRunner.emitBeforeProviderRequest({ text: secret });
61 + const notices = t.events
62 + .uiCallsFor("notify")
63 + .map((call) => String(call.args[0]));
64 + expect(
65 + notices.filter((notice) => notice.includes("leaks redacted")),
66 + ).toHaveLength(2);
67 + });
69 + it("notice only lists new redactions when an old secret is repeated alongside a new one", async () => {
70 + const oldSecret = "tdd-old-secret-value-" + "o".repeat(32);
71 + const newSecret = "tdd-new-secret-value-" + "n".repeat(32);
72 + t = await createTestSession({
73 + extensions: [extPath()],
74 + env: {
75 + ["OLD_" + "SECRET"]: oldSecret,
76 + ["NEW_" + "SECRET"]: newSecret,
77 + },
78 + });
80 + await t.session.extensionRunner.emitBeforeProviderRequest({
81 + text: `first ${oldSecret}`,
82 + });
83 + await t.session.extensionRunner.emitBeforeProviderRequest({
84 + text: `again ${oldSecret} and now ${newSecret}`,
85 + });
87 + const leakNotices = t.events
88 + .uiCallsFor("notify")
89 + .map((call) => String(call.args[0]))
90 + .filter((notice) => notice.includes("leaks redacted"));
91 + expect(leakNotices).toHaveLength(2);
92 + expect(leakNotices[1]).toContain("leaks redacted 1 provider value(s)");
93 + expect(leakNotices[1]).toContain("ENV_KEY");
94 + expect(leakNotices[1]).not.toContain(oldSecret);
95 + expect(leakNotices[1]).not.toContain(newSecret);
96 + });
33 98 it("notification reports every provider redaction path and label", async () => {
34 99 const sensitiveValue = "tdd-secret-value-" + "x".repeat(32);
35 100 t = await createTestSession({

extensions/leaks/src/engine.ts

Modified
47 47 private totalProviderRedactionCount = 0;
48 48 private recentLabels: string[] = [];
49 49 private latestTags: string[] = [];
50 + readonly notifiedPlaceholders = new Set<string>();
51 52 constructor(options: LeaksEngineOptions = {}) {
52 53 this.settings = { regex: options.settings?.regex ?? [] };
59 60 this.settings = { regex: settings.regex };
60 61 this.compileSettings();
61 62 this.learn(cwd, env);
63 + this.notifiedPlaceholders.clear();
64 66 redactText(
144 146 this.lastProviderRedactionCount = 0;
145 147 this.recentLabels = [];
146 148 this.latestTags = [];
149 + this.notifiedPlaceholders.clear();
149 152 stats(): EngineStats {

extensions/leaks/src/pi.ts

Modified
20 20 const result = engine.redactDeep(event.payload, "provider");
21 21 if (ctx.hasUI) {
22 22 ctx.ui.setStatus("leaks", `leaks on | last ${result.count}`);
23 - if (result.count > 0) ctx.ui.notify(formatNotice(result), "info");
23 + if (result.count > 0) {
24 + const newPlaceholders = result.redactions
25 + .flatMap((r) => r.placeholders)
26 + .filter((p) => !engine.notifiedPlaceholders.has(p));
27 + if (newPlaceholders.length > 0) {
28 + for (const p of newPlaceholders) engine.notifiedPlaceholders.add(p);
29 + const freshRedactions = result.redactions.filter((r) =>
30 + r.placeholders.some((p) => newPlaceholders.includes(p)),
31 + );
32 + ctx.ui.notify(
33 + formatNotice({
34 + count: newPlaceholders.length,
35 + redactions: freshRedactions,
36 + }),
37 + "info",
38 + );
39 + }
40 + }
25 42 return result.value;
88 105 count: number;
89 106 redactions: Array<{ path: string; labels: string[] }>;
108 + const grouped = new Map<string, number>();
109 + for (const redaction of result.redactions) {
110 + const key = `${redaction.path}: ${redaction.labels.join(", ")}`;
111 + grouped.set(key, (grouped.get(key) ?? 0) + 1);
91 113 return [
92 114 `leaks redacted ${result.count} provider value(s)`,
93 - ...result.redactions.map(
94 - (redaction) => `${redaction.path}: ${redaction.labels.join(", ")}`,
115 + ...[...grouped.entries()].map(([line, n]) =>
116 + n > 1 ? `${line} (×${n})` : line,
96 118 ].join("\n");

extensions/leaks/src/structures.ts

Modified
9 9 const AUTH_RE = /Authorization:\s*(Bearer|Basic)\s+([^\s'"`]+)/dgi;
10 10 const X_API_RE = /\b(X-API-Key|X-Api-Key|Api-Key):\s*([^\s'"`]+)/dg;
11 11 const COOKIE_LINE_RE = /Cookie:\s*([^\n]+)/dgi;
12 +// Keys that look like assignments but are common programming nouns, not secrets.
13 +// Scoped to findAssignmentSpans only — does NOT affect dotenv/env learning
14 +// or structure non-assignment detectors. Excludes any key in dotenv.SENSITIVE
15 +// (TOKEN, KEY, SECRET, …) since those remain real secret indicators. 'seed' is
16 +// the lone SENSITIVE key in this list: it dominates as a programming term in
17 +// tool-arg JSON, and users with real SEED secrets can use a custom regex.
18 +const ASSIGNMENT_STOP_KEYS = new Set([
19 + "seed",
20 + "id",
21 + "name",
22 + "count",
23 + "type",
24 + "value",
25 + "path",
26 + "ts",
27 + "time",
28 + "date",
29 + "idx",
30 + "mode",
31 + "flag",
32 + "opts",
33 + "params",
34 + "args",
35 + "options",
36 + "label",
37 + "field",
38 + "tag",
39 + "node",
40 + "host",
41 + "port",
42 + "version",
43 +]);
12 45 const ASSIGN_RE =
13 46 /(?:^|[\n\r\s{,;])(?:export\s+)?["']?([A-Za-z_][A-Za-z0-9_-]*)["']?\s*(?:=|:|=>)\s*["']?([^\s'",;}]{1,512})["']?/dgi;
14 47 const XML_RE = /<([A-Za-z_][A-Za-z0-9_-]*)>([^<]{1,512})<\/\1>/dgi;
119 152 for (const match of text.matchAll(regex)) {
120 153 const key = match[1] as string;
121 154 const value = match[2] as string;
155 + if (ASSIGNMENT_STOP_KEYS.has(key.toLowerCase())) continue;
122 156 if (!isSensitiveKey(key)) continue;
123 157 if (isWeakValue(value) && !highConfidenceSecretShape(value)) continue;
124 158 spans.push(