repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

refactor(leaks): add spec-shaped engine shell

4f77d88e9e353ff9d3a7d2df681aeaf6010e2d7f

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (5)

extensions/leaks/__tests__/unit.test.ts

Modified
1 -import fs, { mkdtempSync, writeFileSync } from "node:fs";
2 -import { tmpdir } from "node:os";
3 -import path from "node:path";
4 1 import { describe, expect, it } from "vitest";
5 2 import { __test } from "../index.ts";
7 -describe("leaks engine", () => {
8 - it("normalizes minimal settings", () => {
9 - expect(__test.normalizeSettings({})).toMatchObject({
10 - enabled: true,
11 - keywords: [],
12 - regex: [],
13 - secretFiles: [".env", ".env.local", ".env.*.local"],
14 - exclude: [],
4 +const fixtureSecret = "shell-fixture-secret-value-123456";
6 +describe("leaks engine shell", () => {
7 + it("creates active engine with empty volatile state", () => {
8 + const engine = new __test.LeaksEngine();
10 + expect(engine.stats()).toMatchObject({
11 + active: true,
12 + customRegexCount: 0,
13 + learnedCount: 0,
14 + mappingCount: 0,
15 + lastProviderRedactionCount: 0,
16 + totalProviderRedactionCount: 0,
16 - expect(
17 - __test.normalizeSettings({
18 - enabled: false,
19 - secretFiles: [],
20 - keywords: [" prod ", 1],
21 - regex: ["foo"],
22 - }),
23 - ).toMatchObject({
24 - enabled: false,
25 - keywords: ["prod"],
26 - regex: [{ pattern: "foo" }],
27 - secretFiles: [],
18 + expect(engine.redactDeep(1, "provider")).toEqual({
19 + value: 1,
20 + changed: false,
21 + count: 0,
29 - });
31 - it("loads project settings.json when runtime settings API is absent", () => {
32 - const cwd = mkdtempSync(path.join(tmpdir(), "leaks-settings-"));
33 - fs.mkdirSync(path.join(cwd, ".pi"), { recursive: true });
34 - writeFileSync(
35 - path.join(cwd, ".pi", "settings.json"),
36 - JSON.stringify({ leaks: { keywords: ["from-file"], secretFiles: [] } }),
37 - "utf8",
38 - );
40 - expect(__test.parseSettings({} as never, cwd)).toMatchObject({
41 - keywords: ["from-file"],
42 - secretFiles: [],
23 + expect(engine.redactDeep(null, "provider")).toEqual({
24 + value: null,
25 + changed: false,
26 + count: 0,
28 + const image = { type: "image", data: fixtureSecret };
29 + expect(engine.redactDeep(image, "provider").value).toEqual(image);
46 - it("loads dotenv values and ignores weak values", () => {
47 - expect(
48 - __test.parseDotenv(
49 - "A=short\nB=true\nC=abc123456789\nexport D='quoted-secret-123'\n# nope",
50 - ),
51 - ).toEqual(["short", "true", "abc123456789", "quoted-secret-123"]);
32 + it("does not implement rejected compatibility settings", () => {
33 + expect(__test.RUNTIME_SETTING_KEYS).toEqual(["regex"]);
34 + for (const key of ["enabled", "keywords", "exclude", "secretFiles"]) {
35 + expect(__test.RUNTIME_SETTING_KEYS).not.toContain(key);
36 + }
54 - it("redacts secret file values exactly", () => {
55 - const cwd = mkdtempSync(path.join(tmpdir(), "leaks-unit-"));
56 - writeFileSync(
57 - path.join(cwd, ".env"),
58 - "VALUE=dotenv-secret-123456789\nSMALL=123\n",
59 - "utf8",
60 - );
61 - const engine = new __test.LeaksEngine(__test.normalizeSettings({}));
62 - engine.reload(cwd, __test.normalizeSettings({}));
39 + it("fails open when a detector throws", () => {
40 + const engine = new __test.LeaksEngine({
41 + detectors: [
42 + () => {
43 + throw new Error("detector broke");
44 + },
45 + ],
46 + });
64 - const res = engine.redactText("send dotenv-secret-123456789 upstream");
65 - expect(res.text).toMatch(/send __LEAK_[a-f0-9]{12}__ upstream/);
66 - expect(res.matches).toHaveLength(1);
48 + const result = engine.redactText(`send ${fixtureSecret}`, "provider");
50 + expect(result).toMatchObject({
51 + text: `send ${fixtureSecret}`,
52 + count: 0,
53 + labels: [],
54 + placeholders: [],
55 + });
56 + expect(engine.stats().warningCount).toBe(1);
69 - it("uses smart boundaries for keywords", () => {
70 - const engine = new __test.LeaksEngine(
71 - __test.normalizeSettings({ keywords: ["prod", "Project Raven"] }),
72 - );
73 - engine.reload(process.cwd(), engine.settings);
59 + it("keeps warning text secret-free", () => {
60 + const engine = new __test.LeaksEngine({
61 + detectors: [
62 + () => {
63 + throw new Error(`detector saw ${fixtureSecret}`);
64 + },
65 + ],
66 + });
75 - expect(
76 - engine
77 - .redactText("prod product reproduce Project RavenX Project Raven")
78 - .matches.map((m) => m.original),
79 - ).toEqual(["prod", "Project Raven"]);
80 - });
68 + engine.redactText(`send ${fixtureSecret}`, "provider");
82 - it("redacts builtin AI/provider tokens", () => {
83 - const engine = new __test.LeaksEngine(
84 - __test.normalizeSettings({ secretFiles: [] }),
85 - );
86 - engine.reload(process.cwd(), engine.settings);
87 - const openrouter = "sk-or-v1-" + "a".repeat(64);
88 - const github = "ghp_" + "A".repeat(36);
89 - const res = engine.redactText(`keys ${openrouter} and ${github}`);
91 - expect(res.matches.map((m) => m.label)).toEqual(["openrouter", "github"]);
92 - expect(res.text).not.toContain(openrouter);
93 - expect(res.text).not.toContain(github);
94 - });
96 - it("rejects generic false positives", () => {
97 - expect(__test.isGenericReject("550e8400-e29b-41d4-a716-446655440000")).toBe(
98 - true,
99 - );
100 - expect(__test.isGenericReject("https://example.com/callback")).toBe(true);
101 - expect(__test.isGenericReject("./config/secrets.json")).toBe(true);
102 - expect(__test.isGenericReject("${API_VALUE}")).toBe(true);
103 - expect(__test.isGenericReject("${{ secrets.PROD_VALUE }}")).toBe(true);
104 - expect(__test.isGenericReject("true")).toBe(true);
105 - expect(__test.isGenericReject("aB9_qP2xZ7mN4rT8vL0s")).toBe(false);
106 - });
108 - it("redacts generic assignment only when random enough", () => {
109 - const engine = new __test.LeaksEngine(
110 - __test.normalizeSettings({ secretFiles: [] }),
111 - );
112 - engine.reload(process.cwd(), engine.settings);
113 - const strong = "aB9_qP2xZ7mN4rT8vL0s";
114 - const hex = "4f9c2a7b8d1e3f604a9c2a7b8d1e3f60";
115 - const res = engine.redactText(
116 - `password = changeme token = ${strong} api_key = ${hex} secret_key = ${hex}`,
117 - );
119 - expect(res.text).toContain("password = changeme");
120 - expect(res.text).toContain(`api_key = ${hex}`);
121 - expect(res.text).not.toContain(strong);
122 - expect(res.matches).toHaveLength(2);
123 - expect(res.matches.map((m) => m.label)).toEqual([
124 - "generic-assignment-secret",
125 - "generic-assignment-secret",
126 - ]);
127 - });
129 - it("restores placeholders deeply", () => {
130 - const engine = new __test.LeaksEngine(
131 - __test.normalizeSettings({ secretFiles: [] }),
132 - );
133 - engine.reload(process.cwd(), engine.settings);
134 - const value = "sk-or-v1-" + "b".repeat(64);
135 - const redacted = engine.redactText(value).text;
136 - const obj = { args: [redacted, { nested: `run ${redacted}` }] };
138 - const count = engine.restoreDeepInPlace(obj);
139 - expect(count).toBe(2);
140 - expect(obj.args[0]).toBe(value);
141 - expect((obj.args[1] as { nested: string }).nested).toBe(`run ${value}`);
142 - });
144 - it("dry-run test placeholders do not persist", () => {
145 - const engine = new __test.LeaksEngine(
146 - __test.normalizeSettings({ secretFiles: [] }),
147 - );
148 - engine.reload(process.cwd(), engine.settings);
149 - const value = "sk-or-v1-" + "c".repeat(64);
150 - const redacted = engine.redactText(value, false).text;
152 - expect(redacted).toMatch(/^__LEAK_[a-f0-9]{12}__$/);
153 - expect(engine.stats().mappings).toBe(0);
154 - expect(engine.restoreText(redacted)).toBe(redacted);
70 + const warnings = engine.stats().warnings.map((warning) => warning.message);
71 + expect(warnings.join("\n")).not.toContain(fixtureSecret);
72 + expect(warnings).toEqual(["detector failure; redaction pass left unchanged"]);

extensions/leaks/index.ts

Modified
1 -import { createHmac, randomBytes } from "node:crypto";
2 -import { existsSync, readdirSync, readFileSync } from "node:fs";
3 -import path from "node:path";
4 -import type {
5 - ExtensionAPI,
6 - ExtensionContext,
7 -} from "@earendil-works/pi-coding-agent";
9 -type SettingsAPI = ExtensionAPI & {
10 - settingsManager?: { getSettings(): Record<string, unknown> | undefined };
11 -};
13 -type LeaksSettings = {
14 - enabled: boolean;
15 - keywords: string[];
16 - regex: UserRegexRule[];
17 - secretFiles: string[];
18 - exclude: string[];
19 -};
21 -type UserRegexRule = { pattern: string; flags?: string; label?: string };
22 -type CompiledRule = {
23 - id: string;
24 - re: RegExp;
25 - secretGroup?: number;
26 - minEntropy?: number;
27 - generic?: boolean;
28 - priority: number;
29 -};
30 -type Match = {
31 - start: number;
32 - end: number;
33 - original: string;
34 - label: string;
35 - priority: number;
36 - placeholder?: string;
37 -};
38 -type RedactResult = { text: string; matches: Match[] };
40 -type EngineStats = {
41 - mappings: number;
42 - rules: number;
43 - secretValues: number;
44 - lastProviderRedactions: number;
45 - totalProviderRedactions: number;
46 -};
48 -const PREFIX = "__LEAK_";
49 -const DEFAULT_SECRET_FILES = [".env", ".env.local", ".env.*.local"];
50 -const DEFAULT_EXCLUDE = ["localhost", "127.0.0.1", "0.0.0.0", "example.com"];
51 -const PLACEHOLDER_RE = /__LEAK_[a-f0-9]{12}(?:_\d+)?__/g;
53 -const BUILTIN_RULES: Array<
54 - Omit<CompiledRule, "re" | "priority"> & { pattern: string; flags?: string }
55 -> = [
56 - {
57 - id: "openai",
58 - pattern: String.raw`\bsk-(?:(?:proj|svcacct|service|admin)-[A-Za-z0-9_-]+|[A-Za-z0-9]+)T3BlbkFJ[A-Za-z0-9_-]+\b`,
59 - },
60 - {
61 - id: "anthropic",
62 - pattern: String.raw`\bsk-ant-(?:admin01|api03)-[A-Za-z0-9_-]{93}AA\b`,
63 - },
64 - {
65 - id: "openrouter",
66 - pattern: String.raw`\bsk-or-v1-[0-9a-f]{64}\b`,
67 - flags: "i",
68 - },
69 - { id: "xai", pattern: String.raw`\bxai-[A-Za-z0-9_-]{70,120}\b` },
70 - { id: "groq", pattern: String.raw`\bgsk_[A-Za-z0-9]{52}\b`, flags: "i" },
71 - { id: "cerebras", pattern: String.raw`\bcsk-[a-z0-9]{48}\b`, flags: "i" },
72 - { id: "togetherai", pattern: String.raw`\btgp_v1_[A-Za-z0-9_-]{43}\b` },
73 - { id: "replicate", pattern: String.raw`\br8_[A-Za-z0-9]{37}\b` },
74 - { id: "huggingface", pattern: String.raw`\bhf_[A-Za-z]{34}\b`, flags: "i" },
75 - {
76 - id: "nvidia",
77 - pattern: String.raw`\bnvapi-[A-Za-z0-9_-]{60,70}\b`,
78 - flags: "i",
79 - },
80 - { id: "vercel-ai-gateway", pattern: String.raw`\bvck_[A-Za-z0-9_-]{56}\b` },
82 - {
83 - id: "deepseek",
84 - pattern: String.raw`\bdeepseek[\s\S]{0,80}?(sk-[a-f0-9]{32})\b`,
85 - flags: "i",
86 - secretGroup: 1,
87 - },
88 - {
89 - id: "mistral",
90 - pattern: String.raw`\bmistral[\s\S]{0,80}?([A-Za-z0-9]{32})\b`,
91 - flags: "i",
92 - secretGroup: 1,
93 - minEntropy: 3.0,
94 - },
95 - {
96 - id: "stability",
97 - pattern: String.raw`\bstability[\s\S]{0,80}?(sk-[A-Za-z0-9]{48})\b`,
98 - flags: "i",
99 - secretGroup: 1,
100 - },
101 - {
102 - id: "assemblyai",
103 - pattern: String.raw`\bassemblyai[\s\S]{0,80}?([a-z0-9]{32})\b`,
104 - flags: "i",
105 - secretGroup: 1,
106 - minEntropy: 3.0,
107 - },
108 - {
109 - id: "deepgram",
110 - pattern: String.raw`\bdeepgram[\s\S]{0,80}?([a-f0-9]{40})\b`,
111 - flags: "i",
112 - secretGroup: 1,
113 - },
114 - {
115 - id: "elevenlabs",
116 - pattern: String.raw`\belevenlabs[\s\S]{0,80}?(sk_[0-9a-f]{48})\b`,
117 - flags: "i",
118 - secretGroup: 1,
119 - },
120 - { id: "wandb", pattern: String.raw`\bwandb_v1_[A-Za-z0-9_]{77}\b` },
121 - {
122 - id: "wandb-context",
123 - pattern: String.raw`\b(?:wandb|weightsandbiases)[\s\S]{0,80}?([a-f0-9]{40})\b`,
124 - flags: "i",
125 - secretGroup: 1,
126 - },
128 - {
129 - id: "github",
130 - pattern: String.raw`\b(?:(?:ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9]{36}|github_pat_[A-Za-z0-9_]{82})\b`,
131 - },
132 - {
133 - id: "gitlab",
134 - pattern: String.raw`\b(?:glpat-[\w-]{20}|glpat-[A-Za-z0-9_-]{27,300}\.[0-9a-z]{9}|gldt-[A-Za-z0-9_-]{20}|glrt-[A-Za-z0-9_-]{20}|glptt-[0-9a-f]{40}|glagent-[A-Za-z0-9_-]{50})\b`,
135 - },
136 - {
137 - id: "slack-token",
138 - pattern: String.raw`\bxox[baprs]-[A-Za-z0-9-]{10,250}\b`,
139 - },
140 - {
141 - id: "slack-webhook",
142 - pattern: String.raw`https?:\/\/hooks\.slack\.com\/(?:services|workflows|triggers)\/[A-Za-z0-9+/]{43,80}`,
143 - },
144 - {
145 - id: "stripe",
146 - pattern: String.raw`\b[rs]k_(?:test|live|prod)_[A-Za-z0-9]{10,99}\b`,
147 - },
148 - { id: "npm", pattern: String.raw`\bnpm_[A-Za-z0-9]{36}\b`, flags: "i" },
150 - { id: "google-api-key", pattern: String.raw`\bAIza[0-9A-Za-z_-]{35}\b` },
151 - {
152 - id: "aws-access-key-id",
153 - pattern: String.raw`\b(?:A3T[A-Z0-9]|AKIA|ASIA|ABIA|ACCA)[A-Z2-7]{16}\b`,
154 - },
155 - {
156 - id: "aws-secret-access-key",
157 - pattern: String.raw`\b(?:aws[\s_.-]{0,20})?(?:secret|access)[\s_.-]{0,20}key[\s'"\`:=>-]{0,20}([A-Za-z0-9/+=]{40})\b`,
158 - flags: "i",
159 - secretGroup: 1,
160 - minEntropy: 4.0,
161 - },
163 - {
164 - id: "jwt",
165 - pattern: String.raw`\beyJ[A-Za-z0-9_-]{10,}\.eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}={0,2}\b`,
166 - },
167 - {
168 - id: "private-key",
169 - pattern: String.raw`-----BEGIN[ A-Z0-9_-]{0,100}PRIVATE KEY(?: BLOCK)?-----[\s\S]{64,}?-----END[ A-Z0-9_-]{0,100}PRIVATE KEY(?: BLOCK)?-----`,
170 - flags: "i",
171 - },
172 - {
173 - id: "mongodb-url",
174 - pattern: String.raw`mongodb(?:\+srv)?:\/\/[^/\s:@]+:[^@\s]+@[^/\s]+`,
175 - flags: "i",
176 - },
178 - {
179 - id: "generic-assignment-secret",
180 - pattern: String.raw`\b(?:api[_-]?key|access[_-]?token|auth[_-]?token|client[_-]?secret|secret[_-]?key|password|passwd|token)\b[\s'"\`:=>-]{0,20}([A-Za-z0-9_./+=-]{16,150})`,
181 - flags: "i",
182 - secretGroup: 1,
183 - minEntropy: 3.5,
184 - generic: true,
185 - },
186 -];
188 -class PlaceholderSession {
189 - readonly secret = randomBytes(32);
190 - readonly forward = new Map<string, string>();
191 - readonly reverse = new Map<string, string>();
193 - getOrCreate(original: string): string {
194 - const existing = this.reverse.get(original);
195 - if (existing) return existing;
197 - const h = createHmac("sha256", this.secret)
198 - .update(original)
199 - .digest("hex")
200 - .slice(0, 12);
201 - let ph = `${PREFIX}${h}__`;
202 - const current = this.forward.get(ph);
203 - if (current !== undefined && current !== original) {
204 - for (let i = 2; ; i++) {
205 - const next = `${PREFIX}${h}_${i}__`;
206 - const prev = this.forward.get(next);
207 - if (prev === undefined || prev === original) {
208 - ph = next;
209 - break;
210 - }
211 - }
212 - }
213 - this.forward.set(ph, original);
214 - this.reverse.set(original, ph);
215 - return ph;
216 - }
218 - lookup(placeholder: string): string | undefined {
219 - return this.forward.get(placeholder);
220 - }
222 - clear(): void {
223 - this.forward.clear();
224 - this.reverse.clear();
225 - }
228 -class LeaksEngine {
229 - readonly session = new PlaceholderSession();
230 - settings: LeaksSettings;
231 - rules: CompiledRule[] = [];
232 - keywords: string[] = [];
233 - secretValues: string[] = [];
234 - exclude = new Set<string>();
235 - lastProviderRedactions = 0;
236 - totalProviderRedactions = 0;
238 - constructor(settings: LeaksSettings) {
239 - this.settings = settings;
240 - }
242 - reload(cwd: string, settings: LeaksSettings): void {
243 - this.settings = settings;
244 - this.keywords = unique(
245 - settings.keywords.map((s) => s.trim()).filter(Boolean),
246 - );
247 - this.exclude = new Set(
248 - [...DEFAULT_EXCLUDE, ...settings.exclude].filter(Boolean),
249 - );
250 - this.secretValues = loadSecretValues(
251 - cwd,
252 - settings.secretFiles,
253 - this.exclude,
254 - );
255 - this.rules = compileRules(settings.regex);
256 - }
258 - stats(): EngineStats {
259 - return {
260 - mappings: this.session.forward.size,
261 - rules: this.rules.length + BUILTIN_RULES.length + this.keywords.length,
262 - secretValues: this.secretValues.length,
263 - lastProviderRedactions: this.lastProviderRedactions,
264 - totalProviderRedactions: this.totalProviderRedactions,
265 - };
266 - }
268 - clear(): void {
269 - this.session.clear();
270 - this.lastProviderRedactions = 0;
271 - }
273 - redactText(input: string, persist = true): RedactResult {
274 - const text = String(input ?? "");
275 - if (!this.settings.enabled || !text) return { text, matches: [] };
277 - const found: Match[] = [];
278 - addSecretMatches(found, text, this.secretValues, this.exclude);
279 - addKeywordMatches(found, text, this.keywords, this.exclude);
280 - addRegexMatches(found, text, this.rules, this.exclude);
281 - addRegexMatches(found, text, builtinRules(), this.exclude);
283 - if (found.length === 0) return { text, matches: [] };
284 - const selected = selectMatches(found);
285 - let out = text;
286 - for (const m of [...selected].sort((a, b) => b.start - a.start)) {
287 - const ph = persist
288 - ? this.session.getOrCreate(m.original)
289 - : `${PREFIX}${hashPreview(m.original)}__`;
290 - m.placeholder = ph;
291 - out = out.slice(0, m.start) + ph + out.slice(m.end);
292 - }
293 - return { text: out, matches: selected };
294 - }
296 - restoreText(input: string): string {
297 - const text = String(input ?? "");
298 - if (!text) return text;
299 - return text.replace(PLACEHOLDER_RE, (ph) => this.session.lookup(ph) ?? ph);
300 - }
302 - redactDeep<T>(
303 - value: T,
304 - persist = true,
305 - ): { value: T; count: number; changed: boolean } {
306 - const cloned = cloneJson(value);
307 - let count = 0;
308 - const walk = (node: unknown): unknown => {
309 - if (typeof node === "string") {
310 - const res = this.redactText(node, persist);
311 - count += res.matches.length;
312 - return res.text;
313 - }
314 - if (Array.isArray(node)) return node.map(walk);
315 - if (isRecord(node)) {
316 - for (const key of Object.keys(node)) node[key] = walk(node[key]);
317 - }
318 - return node;
319 - };
320 - const value2 = walk(cloned) as T;
321 - return { value: value2, count, changed: count > 0 };
322 - }
324 - restoreDeepInPlace(value: unknown): number {
325 - const seen = new WeakSet<object>();
326 - let count = 0;
327 - const walk = (node: unknown): void => {
328 - if (!node || typeof node !== "object") return;
329 - if (seen.has(node)) return;
330 - seen.add(node);
331 - if (Array.isArray(node)) {
332 - for (let i = 0; i < node.length; i++) {
333 - const v = node[i];
334 - if (typeof v === "string") {
335 - const after = this.restoreText(v);
336 - if (after !== v) count++;
337 - node[i] = after;
338 - } else walk(v);
339 - }
340 - return;
341 - }
342 - if (!isRecord(node)) return;
343 - for (const key of Object.keys(node)) {
344 - const v = node[key];
345 - if (typeof v === "string") {
346 - const after = this.restoreText(v);
347 - if (after !== v) count++;
348 - node[key] = after;
349 - } else walk(v);
350 - }
351 - };
352 - walk(value);
353 - return count;
354 - }
357 -function builtinRules(): CompiledRule[] {
358 - return BUILTIN_RULES.map((r) => ({
359 - id: r.id,
360 - re: new RegExp(r.pattern, flagsWithGlobal(r.flags ?? "")),
361 - secretGroup: r.secretGroup,
362 - minEntropy: r.minEntropy,
363 - generic: r.generic,
364 - priority: r.generic ? 10 : 50,
365 - }));
368 -function compileRules(rules: UserRegexRule[]): CompiledRule[] {
369 - const out: CompiledRule[] = [];
370 - for (const r of rules) {
371 - try {
372 - out.push({
373 - id: r.label?.trim() || "user-regex",
374 - re: new RegExp(r.pattern, flagsWithGlobal(r.flags ?? "")),
375 - priority: 80,
376 - });
377 - } catch {
378 - // Invalid user regex: ignore. /leaks test makes absence obvious without breaking Pi startup.
379 - }
380 - }
381 - return out;
384 -function flagsWithGlobal(flags: string): string {
385 - let out = "";
386 - for (const ch of flags)
387 - if ("gimsuy".includes(ch) && !out.includes(ch)) out += ch;
388 - if (!out.includes("g")) out += "g";
389 - return out;
392 -function addSecretMatches(
393 - found: Match[],
394 - text: string,
395 - values: string[],
396 - exclude: Set<string>,
397 -): void {
398 - for (const value of values) {
399 - if (!value || exclude.has(value)) continue;
400 - let idx = 0;
401 - for (;;) {
402 - const start = text.indexOf(value, idx);
403 - if (start < 0) break;
404 - const end = start + value.length;
405 - idx = end;
406 - if (isPlaceholder(value)) continue;
407 - found.push({
408 - start,
409 - end,
410 - original: value,
411 - label: "secret-file",
412 - priority: 100,
413 - });
414 - }
415 - }
418 -function addKeywordMatches(
419 - found: Match[],
420 - text: string,
421 - values: string[],
422 - exclude: Set<string>,
423 -): void {
424 - for (const value of values) {
425 - if (!value || exclude.has(value)) continue;
426 - let idx = 0;
427 - for (;;) {
428 - const start = text.indexOf(value, idx);
429 - if (start < 0) break;
430 - const end = start + value.length;
431 - idx = end;
432 - if (!hasSmartBoundary(text, start, end, value)) continue;
433 - found.push({
434 - start,
435 - end,
436 - original: value,
437 - label: "keyword",
438 - priority: 90,
439 - });
440 - }
441 - }
444 -function addRegexMatches(
445 - found: Match[],
446 - text: string,
447 - rules: CompiledRule[],
448 - exclude: Set<string>,
449 -): void {
450 - for (const rule of rules) {
451 - rule.re.lastIndex = 0;
452 - for (const m of text.matchAll(rule.re)) {
453 - const full = m[0];
454 - if (!full) continue;
455 - const base = m.index ?? -1;
456 - if (base < 0) continue;
457 - const captured = rule.secretGroup ? m[rule.secretGroup] : full;
458 - if (!captured) continue;
459 - const rel = rule.secretGroup ? full.indexOf(captured) : 0;
460 - if (rel < 0) continue;
461 - const start = base + rel;
462 - const end = start + captured.length;
463 - if (exclude.has(captured) || isPlaceholder(captured)) continue;
464 - if (rule.minEntropy && shannon(captured) < rule.minEntropy) continue;
465 - if (rule.generic && isGenericReject(captured, full)) continue;
466 - found.push({
467 - start,
468 - end,
469 - original: captured,
470 - label: rule.id,
471 - priority: rule.priority,
472 - });
473 - }
474 - }
477 -function selectMatches(matches: Match[]): Match[] {
478 - const selected: Match[] = [];
479 - const sorted = matches.toSorted((a, b) => {
480 - if (a.priority !== b.priority) return b.priority - a.priority;
481 - const al = a.end - a.start;
482 - const bl = b.end - b.start;
483 - if (al !== bl) return bl - al;
484 - return a.start - b.start;
485 - });
486 - for (const m of sorted) {
487 - if (selected.some((s) => m.start < s.end && s.start < m.end)) continue;
488 - selected.push(m);
489 - }
490 - return selected.sort((a, b) => a.start - b.start);
493 -function hasSmartBoundary(
494 - text: string,
495 - start: number,
496 - end: number,
497 - value: string,
498 -): boolean {
499 - const first = value[0] ?? "";
500 - const last = value[value.length - 1] ?? "";
501 - if (isWord(first) && start > 0 && isWord(text[start - 1] ?? "")) return false;
502 - if (isWord(last) && end < text.length && isWord(text[end] ?? ""))
503 - return false;
504 - return true;
507 -function isWord(ch: string): boolean {
508 - return /^[A-Za-z0-9_]$/.test(ch);
511 -function isPlaceholder(s: string): boolean {
512 - return /^__LEAK_[a-f0-9]{12}(?:_\d+)?__$/.test(s);
515 -function isGenericReject(s: string, context = ""): boolean {
516 - const v = s.trim();
517 - if (/^(?:true|false|null|undefined)$/i.test(v)) return true;
518 - if (/^\$[A-Za-z_][A-Za-z0-9_]*$/.test(v)) return true;
519 - if (/^\$\{[A-Za-z_][A-Za-z0-9_]*}$/.test(v)) return true;
520 - if (
521 - /^\$\{\{\s*(?:secrets|env|vars|github)\.[A-Za-z_][A-Za-z0-9_.-]*\s*}}$/.test(
522 - v,
523 - )
524 - )
525 - return true;
526 - if (
527 - /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(
528 - v,
529 - )
530 - )
531 - return true;
532 - if (/^https?:\/\//i.test(v)) return true;
533 - if (/^(?:\.{0,2}\/|~\/|[A-Za-z]:\\|\/)[\w./\\-]+$/.test(v)) return true;
534 - if (
535 - /^[a-f0-9]{32,128}$/i.test(v) &&
536 - !/\b(?:password|passwd|secret(?:[_-]?key)?|token|credential|auth(?:[_-]?token)?)\b/i.test(
537 - context,
538 - )
539 - )
540 - return true;
541 - return false;
544 -function shannon(input: string): number {
545 - if (!input) return 0;
546 - const freq = new Map<string, number>();
547 - for (const ch of input) freq.set(ch, (freq.get(ch) ?? 0) + 1);
548 - let ent = 0;
549 - for (const n of freq.values()) {
550 - const p = n / input.length;
551 - ent -= p * Math.log2(p);
552 - }
553 - return ent;
556 -function hashPreview(value: string): string {
557 - return createHmac("sha256", "test").update(value).digest("hex").slice(0, 12);
560 -function unique(values: string[]): string[] {
561 - return [...new Set(values)];
564 -function parseSettings(pi: ExtensionAPI, cwd = process.cwd()): LeaksSettings {
565 - const fromRuntime = (pi as SettingsAPI).settingsManager?.getSettings?.();
566 - const fromFiles = readSettingsFiles(cwd);
567 - const merged = {
568 - ...fromFiles,
569 - ...(isRecord(fromRuntime) ? fromRuntime : {}),
570 - };
571 - const leaks = isRecord(merged.leaks) ? merged.leaks : {};
572 - return normalizeSettings(leaks);
575 -function readSettingsFiles(cwd: string): Record<string, unknown> {
576 - const agentDir =
577 - process.env.PI_CODING_AGENT_DIR ||
578 - path.join(process.env.HOME || "", ".pi", "agent");
579 - return Object.assign(
580 - {},
581 - ...[
582 - path.join(agentDir, "settings.json"),
583 - path.join(cwd, ".pi", "settings.json"),
584 - ].map(readJsonObject),
585 - );
588 -function readJsonObject(file: string): Record<string, unknown> {
589 - try {
590 - const parsed = JSON.parse(readFileSync(file, "utf8"));
591 - return isRecord(parsed) ? parsed : {};
592 - } catch {
593 - return {};
594 - }
597 -function normalizeSettings(raw: Record<string, unknown>): LeaksSettings {
598 - return {
599 - enabled: raw.enabled !== false,
600 - keywords: stringArray(raw.keywords),
601 - regex: regexArray(raw.regex),
602 - secretFiles:
603 - raw.secretFiles === undefined
604 - ? DEFAULT_SECRET_FILES
605 - : stringArray(raw.secretFiles),
606 - exclude: stringArray(raw.exclude),
607 - };
610 -function stringArray(v: unknown): string[] {
611 - return Array.isArray(v)
612 - ? v
613 - .filter((x): x is string => typeof x === "string")
614 - .map((s) => s.trim())
615 - .filter(Boolean)
616 - : [];
619 -function regexArray(v: unknown): UserRegexRule[] {
620 - if (!Array.isArray(v)) return [];
621 - const out: UserRegexRule[] = [];
622 - for (const item of v) {
623 - if (typeof item === "string" && item.trim())
624 - out.push({ pattern: item.trim() });
625 - else if (
626 - isRecord(item) &&
627 - typeof item.pattern === "string" &&
628 - item.pattern.trim()
629 - ) {
630 - out.push({
631 - pattern: item.pattern.trim(),
632 - flags: typeof item.flags === "string" ? item.flags : undefined,
633 - label: typeof item.label === "string" ? item.label : undefined,
634 - });
635 - }
636 - }
637 - return out;
640 -function loadSecretValues(
641 - cwd: string,
642 - specs: string[],
643 - exclude: Set<string>,
644 -): string[] {
645 - const values: string[] = [];
646 - for (const spec of specs) {
647 - for (const file of expandSecretSpec(cwd, spec)) {
648 - let text = "";
649 - try {
650 - text = readFileSync(file, "utf8");
651 - } catch {
652 - continue;
653 - }
654 - values.push(...parseDotenv(text));
655 - }
656 - }
657 - return unique(
658 - values.map(cleanSecretValue).filter((v) => keepSecretValue(v, exclude)),
659 - );
662 -function expandSecretSpec(cwd: string, spec: string): string[] {
663 - if (!spec.includes("*")) {
664 - const abs = path.resolve(cwd, spec);
665 - return existsSync(abs) ? [abs] : [];
666 - }
667 - if (spec !== ".env.*.local") return [];
668 - try {
669 - return readdirSync(cwd)
670 - .filter((name) => /^\.env\..+\.local$/.test(name))
671 - .map((name) => path.join(cwd, name));
672 - } catch {
673 - return [];
674 - }
677 -function parseDotenv(text: string): string[] {
678 - const out: string[] = [];
679 - for (const raw of text.split(/\r?\n/)) {
680 - const line = raw.trim();
681 - if (!line || line.startsWith("#")) continue;
682 - const m = /^(?:export\s+)?[A-Za-z_][A-Za-z0-9_]*\s*=\s*(.*)$/.exec(line);
683 - if (!m) continue;
684 - out.push(stripInlineComment(m[1] ?? ""));
685 - }
686 - return out;
689 -function stripInlineComment(value: string): string {
690 - let v = value.trim();
691 - if (
692 - (v.startsWith('"') && v.endsWith('"')) ||
693 - (v.startsWith("'") && v.endsWith("'"))
694 - )
695 - v = v.slice(1, -1);
696 - else v = v.replace(/\s+#.*$/, "");
697 - return v.trim();
700 -function cleanSecretValue(value: string): string {
701 - return value.trim();
704 -function keepSecretValue(value: string, exclude: Set<string>): boolean {
705 - if (value.length < 8) return false;
706 - if (exclude.has(value)) return false;
707 - if (/^(?:true|false|null|undefined|none)$/i.test(value)) return false;
708 - if (/^-?\d+(?:\.\d+)?$/.test(value)) return false;
709 - return true;
712 -function cloneJson<T>(value: T): T {
713 - if (value === undefined || value === null) return value;
714 - if (typeof value !== "object") return value;
715 - return JSON.parse(JSON.stringify(value)) as T;
718 -function isRecord(v: unknown): v is Record<string, unknown> {
719 - return !!v && typeof v === "object" && !Array.isArray(v);
722 -function notify(
723 - ctx: ExtensionContext,
724 - message: string,
725 - level: "info" | "warning" = "info",
726 -): void {
727 - if (ctx.hasUI) ctx.ui.notify(message, level);
730 -function createLeaksExtension() {
731 - return function leaks(pi: ExtensionAPI) {
732 - const engine = new LeaksEngine(parseSettings(pi));
733 - let loadedCwd = "";
735 - const ensure = (ctx: ExtensionContext) => {
736 - if (loadedCwd === ctx.cwd) return;
737 - loadedCwd = ctx.cwd;
738 - engine.reload(ctx.cwd, parseSettings(pi, ctx.cwd));
739 - if (ctx.hasUI)
740 - ctx.ui.setStatus(
741 - "leaks",
742 - engine.settings.enabled ? "leaks:on" : "leaks:off",
743 - );
744 - };
746 - pi.on("session_start", async (_event, ctx) => {
747 - loadedCwd = "";
748 - ensure(ctx);
749 - });
751 - pi.on("context", async (event, ctx) => {
752 - ensure(ctx);
753 - const res = engine.redactDeep(event.messages, true);
754 - if (!res.changed) return;
755 - return { messages: res.value };
756 - });
758 - pi.on("before_provider_request", async (event, ctx) => {
759 - ensure(ctx);
760 - const res = engine.redactDeep(event.payload, true);
761 - engine.lastProviderRedactions = res.count;
762 - engine.totalProviderRedactions += res.count;
763 - if (ctx.hasUI)
764 - ctx.ui.setStatus(
765 - "leaks",
766 - engine.settings.enabled ? `leaks:${res.count}` : "leaks:off",
767 - );
768 - if (!res.changed) return;
769 - return res.value;
770 - });
772 - pi.on("tool_call", async (event, ctx) => {
773 - ensure(ctx);
774 - engine.restoreDeepInPlace(event.input);
775 - });
777 - pi.on("message_end", async (event, ctx) => {
778 - ensure(ctx);
779 - if (!isRecord(event.message)) return;
780 - const msg = cloneJson(event.message);
781 - const n = engine.restoreDeepInPlace(msg);
782 - if (n <= 0) return;
783 - return { message: msg };
784 - });
786 - pi.registerCommand("leaks", {
787 - description: "Inspect and control provider-bound secret redaction",
788 - async handler(args, ctx) {
789 - ensure(ctx);
790 - const [cmd, ...rest] = args.trim().split(/\s+/).filter(Boolean);
791 - if (!cmd || cmd === "status") {
792 - const s = engine.stats();
793 - notify(
794 - ctx,
795 - `leaks ${engine.settings.enabled ? "on" : "off"}\nrules: ${s.rules}\nsecret files: ${s.secretValues}\nmappings: ${s.mappings}\nlast provider redactions: ${s.lastProviderRedactions}\ntotal provider redactions: ${s.totalProviderRedactions}`,
796 - );
797 - return;
798 - }
799 - if (cmd === "reload") {
800 - engine.reload(ctx.cwd, parseSettings(pi, ctx.cwd));
801 - const s = engine.stats();
802 - notify(
803 - ctx,
804 - `leaks reloaded\nrules: ${s.rules}\nsecret files: ${s.secretValues}`,
805 - );
806 - return;
807 - }
808 - if (cmd === "clear") {
809 - const n = engine.stats().mappings;
810 - if (ctx.hasUI && n > 0) {
811 - const ok = await ctx.ui.confirm(
812 - "Clear leaks mappings?",
813 - `Clear ${n} placeholder mapping(s). Old placeholders become unrestorable.`,
814 - );
815 - if (!ok) return;
816 - }
817 - engine.clear();
818 - notify(ctx, `leaks cleared ${n} mapping(s)`);
819 - return;
820 - }
821 - if (cmd === "test") {
822 - const text = rest.join(" ");
823 - if (!text) return notify(ctx, "usage: /leaks test <text>", "warning");
824 - const res = engine.redactText(text, false);
825 - const counts = countLabels(res.matches);
826 - const head =
827 - Object.entries(counts)
828 - .map(([k, v]) => `${k}: ${v}`)
829 - .join("\n") || "no matches";
830 - notify(ctx, `${head}\n${res.text}`);
831 - return;
832 - }
833 - notify(ctx, "usage: /leaks status|reload|test <text>|clear", "warning");
834 - },
835 - });
836 - };
839 -function countLabels(matches: Match[]): Record<string, number> {
840 - const out: Record<string, number> = {};
841 - for (const m of matches) out[m.label] = (out[m.label] ?? 0) + 1;
842 - return out;
1 +import { LeaksEngine } from "./src/engine.ts";
2 +import {
3 + isExistingPlaceholder,
4 + isPlainObject,
5 + isWeakValue,
6 + maskDiagnostic,
7 + sanitizeTag,
8 +} from "./src/safety.ts";
10 +export const RUNTIME_SETTING_KEYS = ["regex"] as const;
845 12 export const __test = {
846 13 LeaksEngine,
847 - PlaceholderSession,
848 - normalizeSettings,
849 - parseSettings,
850 - parseDotenv,
851 - shannon,
852 - isGenericReject,
853 - hasSmartBoundary,
854 - selectMatches,
855 - builtinRules,
14 + RUNTIME_SETTING_KEYS,
15 + isExistingPlaceholder,
16 + isPlainObject,
17 + isWeakValue,
18 + maskDiagnostic,
19 + sanitizeTag,
858 -export default createLeaksExtension();
22 +export default function leaks(_pi: unknown) {}

extensions/leaks/src/engine.ts

Added
new file mode 100644
1 +import type {
2 + DeepRedactionResult,
3 + Detector,
4 + EngineStats,
5 + LeaksSettings,
6 + RedactionOrigin,
7 + RedactionResult,
8 + WarningRecord,
9 +} from "./types.ts";
11 +export type LeaksEngineOptions = {
12 + settings?: Partial<LeaksSettings>;
13 + env?: Record<string, string | undefined>;
14 + detectors?: Detector[];
15 +};
17 +export class LeaksEngine {
18 + readonly settings: LeaksSettings;
19 + private readonly detectors: Detector[];
20 + private readonly warnings: WarningRecord[] = [];
21 + private readonly warningKeys = new Set<string>();
22 + private lastProviderRedactionCount = 0;
23 + private totalProviderRedactionCount = 0;
25 + constructor(options: LeaksEngineOptions = {}) {
26 + this.settings = { regex: options.settings?.regex ?? [] };
27 + this.detectors = options.detectors ?? [];
28 + void options.env;
29 + }
31 + reload(_cwd = process.cwd(), _settings = this.settings, _env = process.env) {}
33 + redactText(text: string, origin: RedactionOrigin = "provider"): RedactionResult {
34 + const failed = this.detectors.some((detector) => {
35 + try {
36 + detector(text);
37 + return false;
38 + } catch {
39 + this.warnOnce("detector.failure", "detector failure; redaction pass left unchanged");
40 + return true;
41 + }
42 + });
43 + const result = { text, count: 0, labels: [], placeholders: [] };
44 + if (origin === "provider") this.lastProviderRedactionCount = 0;
45 + if (failed) return result;
46 + return result;
47 + }
49 + redactDeep<T>(value: T, origin: RedactionOrigin = "provider"): DeepRedactionResult<T> {
50 + if (typeof value !== "string") return { value, changed: false, count: 0 };
51 + const result = this.redactText(value, origin);
52 + return {
53 + value: result.text as T,
54 + changed: result.text !== value,
55 + count: result.count,
56 + };
57 + }
59 + restoreText(text: string) {
60 + return text;
61 + }
63 + restoreDeep<T>(value: T): DeepRedactionResult<T> {
64 + return { value, changed: false, count: 0 };
65 + }
67 + clear() {
68 + this.lastProviderRedactionCount = 0;
69 + }
71 + stats(): EngineStats {
72 + return {
73 + active: true,
74 + builtInRuleCount: 0,
75 + customRegexCount: this.settings.regex.length,
76 + regexWarningCount: 0,
77 + warningCount: this.warnings.length,
78 + invalidRegexCount: 0,
79 + learnedCount: 0,
80 + mappingCount: 0,
81 + lastProviderRedactionCount: this.lastProviderRedactionCount,
82 + totalProviderRedactionCount: this.totalProviderRedactionCount,
83 + recentLabels: [],
84 + latestTags: [],
85 + warnings: [...this.warnings],
86 + };
87 + }
89 + private warnOnce(code: string, message: string) {
90 + if (this.warningKeys.has(code)) return;
91 + this.warningKeys.add(code);
92 + this.warnings.push({ code, message, severity: "warning" });
93 + }

extensions/leaks/src/safety.ts

Added
new file mode 100644
1 +const PLACEHOLDER_RE = /^__LEAK(?:_[A-Z0-9]{1,64})*_[a-f0-9]{12}(?:_\d+)?__$/;
3 +export function isPlainObject(value: unknown): value is Record<string, unknown> {
4 + if (value === null || typeof value !== "object") return false;
5 + const prototype = Object.getPrototypeOf(value);
6 + return prototype === Object.prototype || prototype === null;
7 +}
9 +export function sanitizeTag(raw: string): string {
10 + const tag = raw
11 + .toUpperCase()
12 + .replace(/[^A-Z0-9]+/g, "_")
13 + .replace(/^_+|_+$/g, "")
14 + .slice(0, 64);
15 + return tag || "SECRET";
18 +export function isExistingPlaceholder(text: string): boolean {
19 + return PLACEHOLDER_RE.test(text);
22 +export function maskDiagnostic(input: string): string {
23 + const chars = [...input].length;
24 + const lines = input.length === 0 ? 0 : input.split(/\r?\n/).length;
25 + return `text chars=${chars} lines=${lines}`;
28 +export function isWeakValue(value: string): boolean {
29 + const text = value.trim();
30 + if (text.length < 8) return true;
31 + if (/^(?:true|false|null|nil|none|undefined|yes|no|on|off)$/i.test(text)) {
32 + return true;
33 + }
34 + if (/^[+-]?\d+(?:\.\d+)?$/.test(text)) return true;
35 + if (/^\$\{?[A-Z_][A-Z0-9_]*\}?$/i.test(text)) return true;
36 + if (/^\$\{\{\s*secrets\.[A-Z0-9_]+\s*\}\}$/i.test(text)) return true;
37 + if (/^process\.env\.[A-Z_][A-Z0-9_]*$/i.test(text)) return true;
38 + if (/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(text)) {
39 + return true;
40 + }
41 + if (/^(?:[a-z0-9-]+\.)+[a-z]{2,}$/i.test(text)) return true;
42 + try {
43 + const url = new URL(text);
44 + if (!url.password && !/[?&](?:token|key|secret|password|auth)=/i.test(url.search)) {
45 + return true;
46 + }
47 + } catch {}
48 + if (/^(?:\.\/|\.\.\/|\/|~\/)[^\s]+$/.test(text)) return true;
49 + return false;

extensions/leaks/src/types.ts

Added
new file mode 100644
1 +export type CustomRegexInput = {
2 + pattern?: unknown;
3 + label?: unknown;
4 + [key: string]: unknown;
5 +};
7 +export type LeaksSettings = {
8 + regex: CustomRegexInput[];
9 +};
11 +export type WarningRecord = {
12 + code: string;
13 + message: string;
14 + severity: "warning" | "error";
15 +};
17 +export type SpanLabel = {
18 + tag: string;
19 + priority: number;
20 + source: "dotenv" | "env" | "structure" | "custom" | "builtin" | "generic";
21 +};
23 +export type SecretSpan = {
24 + start: number;
25 + end: number;
26 + value: string;
27 + labels: SpanLabel[];
28 + kind: "structure" | "exact" | "custom" | "builtin" | "generic" | "private-key" | "jwt";
29 + confidence: number;
30 + wholeSpan: boolean;
31 +};
33 +export type RedactionOrigin = "context" | "provider" | "diagnostic";
35 +export type RedactionResult = {
36 + text: string;
37 + count: number;
38 + labels: string[];
39 + placeholders: string[];
40 +};
42 +export type DeepRedactionResult<T = unknown> = {
43 + value: T;
44 + changed: boolean;
45 + count: number;
46 +};
48 +export type EngineStats = {
49 + active: true;
50 + builtInRuleCount: number;
51 + customRegexCount: number;
52 + regexWarningCount: number;
53 + warningCount: number;
54 + invalidRegexCount: number;
55 + learnedCount: number;
56 + mappingCount: number;
57 + lastProviderRedactionCount: number;
58 + totalProviderRedactionCount: number;
59 + recentLabels: string[];
60 + latestTags: string[];
61 + warnings: WarningRecord[];
62 +};
64 +export type Detector = (text: string) => SecretSpan[];