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({
13 - secretFiles: [
".env",
".env.local",
".env.*.local"],
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({
12 + customRegexCount:
0,
15 + lastProviderRedactionCount:
0,
16 + totalProviderRedactionCount:
0,
17 - __test.
normalizeSettings({
20 - keywords: [
" prod ",
1],
26 - regex: [{ pattern:
"foo" }],
18 + expect(engine.
redactDeep(
1,
"provider")).
toEqual({
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 });
35 - path.
join(cwd,
".pi",
"settings.json"),
36 - JSON.
stringify({ leaks: { keywords: [
"from-file"], secretFiles: [] } }),
40 - expect(__test.
parseSettings({}
as never, cwd)).
toMatchObject({
41 - keywords: [
"from-file"],
23 + expect(engine.
redactDeep(
null,
"provider")).
toEqual({
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", ()
=> {
49 - "A=short\nB=true\nC=abc123456789\nexport D='quoted-secret-123'\n# nope",
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);
54 - it(
"redacts secret file values exactly", ()
=> {
55 - const cwd
= mkdtempSync(path.
join(
tmpdir(),
"leaks-unit-"));
57 - path.
join(cwd,
".env"),
58 - "VALUE=dotenv-secret-123456789\nSMALL=123\n",
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({
43 + throw new Error(
"detector broke");
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}`,
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"] }),
73 - engine.
reload(
process.
cwd(), engine.settings);
59 + it(
"keeps warning text secret-free", ()
=> {
60 + const engine
= new __test.
LeaksEngine({
63 + throw new Error(
`detector saw ${fixtureSecret}`);
77 - .
redactText(
"prod product reproduce Project RavenX Project Raven")
78 - .matches.
map((m)
=> m.original),
79 - ).
toEqual([
"prod",
"Project Raven"]);
68 + engine.
redactText(
`send ${fixtureSecret}`,
"provider");
82 - it(
"redacts builtin AI/provider tokens", ()
=> {
83 - const engine
= new __test.
LeaksEngine(
84 - __test.
normalizeSettings({ secretFiles: [] }),
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);
96 - it(
"rejects generic false positives", ()
=> {
97 - expect(__test.
isGenericReject(
"550e8400-e29b-41d4-a716-446655440000")).
toBe(
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);
108 - it(
"redacts generic assignment only when random enough", ()
=> {
109 - const engine
= new __test.
LeaksEngine(
110 - __test.
normalizeSettings({ secretFiles: [] }),
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}`,
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",
129 - it(
"restores placeholders deeply", ()
=> {
130 - const engine
= new __test.
LeaksEngine(
131 - __test.
normalizeSettings({ secretFiles: [] }),
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}`);
144 - it(
"dry-run test placeholders do not persist", ()
=> {
145 - const engine
= new __test.
LeaksEngine(
146 - __test.
normalizeSettings({ secretFiles: [] }),
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";
7 -}
from "@earendil-works/pi-coding-agent";
9 -type SettingsAPI = ExtensionAPI & {
10 - settingsManager
?: {
getSettings()
: Record<
string,
unknown>
| undefined };
13 -type LeaksSettings = {
16 - regex
: UserRegexRule[];
17 - secretFiles
: string[];
21 -type UserRegexRule = { pattern
: string; flags
?: string; label
?: string };
22 -type CompiledRule = {
25 - secretGroup
?: number;
26 - minEntropy
?: number;
36 - placeholder
?: string;
38 -type RedactResult = { text
: string; matches
: Match[] };
43 - secretValues
: number;
44 - lastProviderRedactions
: number;
45 - totalProviderRedactions
: number;
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 }
58 - pattern:
String.raw`\bsk-(?:(?:proj|svcacct|service|admin)-[A-Za-z0-9_-]+|[A-Za-z0-9]+)T3BlbkFJ[A-Za-z0-9_-]+\b`,
62 - pattern:
String.raw`\bsk-ant-(?:admin01|api03)-[A-Za-z0-9_-]{93}AA\b`,
66 - pattern:
String.raw`\bsk-or-v1-[0-9a-f]{64}\b`,
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" },
77 - pattern:
String.raw`\bnvapi-[A-Za-z0-9_-]{60,70}\b`,
80 - { id:
"vercel-ai-gateway", pattern:
String.raw`\bvck_[A-Za-z0-9_-]{56}\b` },
84 - pattern:
String.raw`\bdeepseek[\s\S]{0,80}?(sk-[a-f0-9]{32})\b`,
90 - pattern:
String.raw`\bmistral[\s\S]{0,80}?([A-Za-z0-9]{32})\b`,
97 - pattern:
String.raw`\bstability[\s\S]{0,80}?(sk-[A-Za-z0-9]{48})\b`,
103 - pattern:
String.raw`\bassemblyai[\s\S]{0,80}?([a-z0-9]{32})\b`,
110 - pattern:
String.raw`\bdeepgram[\s\S]{0,80}?([a-f0-9]{40})\b`,
116 - pattern:
String.raw`\belevenlabs[\s\S]{0,80}?(sk_[0-9a-f]{48})\b`,
120 - { id:
"wandb", pattern:
String.raw`\bwandb_v1_[A-Za-z0-9_]{77}\b` },
122 - id:
"wandb-context",
123 - pattern:
String.raw`\b(?:wandb|weightsandbiases)[\s\S]{0,80}?([a-f0-9]{40})\b`,
130 - pattern:
String.raw`\b(?:(?:ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9]{36}|github_pat_[A-Za-z0-9_]{82})\b`,
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`,
138 - pattern:
String.raw`\bxox[baprs]-[A-Za-z0-9-]{10,250}\b`,
141 - id:
"slack-webhook",
142 - pattern:
String.raw`https?:\/\/hooks\.slack\.com\/(?:services|workflows|triggers)\/[A-Za-z0-9+/]{43,80}`,
146 - pattern:
String.raw`\b[rs]k_(?:test|live|prod)_[A-Za-z0-9]{10,99}\b`,
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` },
152 - id:
"aws-access-key-id",
153 - pattern:
String.raw`\b(?:A3T[A-Z0-9]|AKIA|ASIA|ABIA|ACCA)[A-Z2-7]{16}\b`,
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`,
165 - pattern:
String.raw`\beyJ[A-Za-z0-9_-]{10,}\.eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}={0,2}\b`,
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)?-----`,
174 - pattern:
String.raw`mongodb(?:\+srv)?:\/\/[^/\s:@]+:[^@\s]+@[^/\s]+`,
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})`,
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)
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) {
213 - this.forward.
set(ph, original);
214 - this.reverse.
set(original, ph);
218 - lookup(placeholder
: string)
: string | undefined {
219 - return this.forward.
get(placeholder);
223 - this.forward.
clear();
224 - this.reverse.
clear();
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;
242 - reload(cwd
: string, settings
: LeaksSettings)
: void {
243 - this.settings
= settings;
244 - this.keywords
= unique(
245 - settings.keywords.
map((s)
=> s.
trim()).
filter(
Boolean),
247 - this.exclude
= new Set(
248 - [
...DEFAULT_EXCLUDE,
...settings.exclude].
filter(
Boolean),
250 - this.secretValues
= loadSecretValues(
252 - settings.secretFiles,
255 - this.
rules = compileRules(settings.regex);
258 - stats()
: EngineStats {
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,
269 - this.session.
clear();
270 - this.lastProviderRedactions
= 0;
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);
286 - for (
const m
of [
...selected].
sort((a, b)
=> b.
start - a.
start)) {
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);
293 - return { text: out, matches: selected };
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);
305 - )
: { value
: T; count
: number; changed
: boolean } {
306 - const cloned
= cloneJson(value);
308 - const walk = (node
: unknown)
: unknown => {
309 - if (
typeof node
=== "string") {
310 - const res
= this.
redactText(node, persist);
311 - count
+= res.matches.
length;
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]);
320 - const value2
= walk(cloned)
as T;
321 - return { value: value2, count, changed: count
> 0 };
324 - restoreDeepInPlace(value
: unknown)
: number {
325 - const seen
= new WeakSet<
object>();
327 - const walk = (node
: unknown)
: void => {
328 - if (
!node
|| typeof node
!== "object")
return;
329 - if (seen.
has(node))
return;
331 - if (
Array.
isArray(node)) {
332 - for (
let i
= 0; i
< node.
length; i
++) {
334 - if (
typeof v
=== "string") {
335 - const after
= this.
restoreText(v);
336 - if (after
!== v) count
++;
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
++;
357 -function builtinRules()
: CompiledRule[] {
358 - return BUILTIN_RULES.
map((r)
=> ({
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,
368 -function compileRules(rules
: UserRegexRule[])
: CompiledRule[] {
369 - const out
: CompiledRule[]
= [];
370 - for (
const r
of rules) {
373 - id: r.
label?.
trim()
|| "user-regex",
374 - re:
new RegExp(r.pattern,
flagsWithGlobal(r.flags
?? "")),
384 -function flagsWithGlobal(flags
: string)
: string {
386 - for (
const ch
of flags)
387 - if (
"gimsuy".
includes(ch)
&& !out.
includes(ch)) out
+= ch;
388 - if (
!out.
includes(
"g")) out
+= "g";
392 -function addSecretMatches(
396 - exclude
: Set<
string>,
398 - for (
const value
of values) {
399 - if (
!value
|| exclude.
has(value))
continue;
402 - const start
= text.
indexOf(value, idx);
403 - if (start
< 0)
break;
404 - const end
= start
+ value.
length;
406 - if (
isPlaceholder(value))
continue;
411 - label:
"secret-file",
418 -function addKeywordMatches(
422 - exclude
: Set<
string>,
424 - for (
const value
of values) {
425 - if (
!value
|| exclude.
has(value))
continue;
428 - const start
= text.
indexOf(value, idx);
429 - if (start
< 0)
break;
430 - const end
= start
+ value.
length;
432 - if (
!hasSmartBoundary(text, start, end, value))
continue;
444 -function addRegexMatches(
447 - rules
: CompiledRule[],
448 - exclude
: Set<
string>,
450 - for (
const rule
of rules) {
451 - rule.re.
lastIndex = 0;
452 - for (
const m
of text.
matchAll(rule.re)) {
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;
469 - original: captured,
471 - priority: rule.priority,
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;
486 - for (
const m
of sorted) {
487 - if (selected.
some((s)
=> m.
start < s.end
&& s.
start < m.end))
continue;
490 - return selected.
sort((a, b)
=> a.
start - b.
start);
493 -function hasSmartBoundary(
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]
?? ""))
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;
521 - /^\$\{\{\s*(?:secrets|env|vars|github)\.[A-Za-z_][A-Za-z0-9_.-]*\s*}}$/.
test(
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(
532 - if (
/^https?:\/\//i.
test(v))
return true;
533 - if (
/^(?:\.{0,2}\/|~\/|[A-Za-z]:\\|\/)[\w./\\-]+$/.
test(v))
return true;
535 - /^[a-f0-9]{32,128}$/i.
test(v)
&& 536 - !/\b(?:password|passwd|secret(?:[_-]?key)?|token|credential|auth(?:[_-]?token)?)\b/i.
test(
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);
549 - for (
const n
of freq.
values()) {
550 - const p
= n
/ input.
length;
551 - ent
-= p
* Math.
log2(p);
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);
569 - ...(
isRecord(fromRuntime)
? fromRuntime
: {}),
571 - const leaks
= isRecord(merged.leaks)
? merged.leaks
: {};
572 - return normalizeSettings(leaks);
575 -function readSettingsFiles(cwd
: string)
: Record<
string,
unknown> {
577 - process.
env.PI_CODING_AGENT_DIR
|| 578 - path.
join(
process.
env.HOME
|| "",
".pi",
"agent");
579 - return Object.
assign(
582 - path.
join(agentDir,
"settings.json"),
583 - path.
join(cwd,
".pi",
"settings.json"),
584 - ].
map(readJsonObject),
588 -function readJsonObject(file
: string)
: Record<
string,
unknown> {
590 - const parsed
= JSON.
parse(
readFileSync(file,
"utf8"));
591 - return isRecord(parsed)
? parsed
: {};
597 -function normalizeSettings(raw
: Record<
string,
unknown>)
: LeaksSettings {
599 - enabled: raw.enabled
!== false,
600 - keywords:
stringArray(raw.keywords),
601 - regex:
regexArray(raw.regex),
603 - raw.secretFiles
=== undefined 604 - ? DEFAULT_SECRET_FILES
605 - : stringArray(raw.secretFiles),
606 - exclude:
stringArray(raw.exclude),
610 -function stringArray(v
: unknown)
: string[] {
611 - return Array.
isArray(v)
613 - .
filter((x)
: x
is string => typeof x
=== "string")
614 - .
map((s)
=> s.
trim())
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() });
627 - typeof item.pattern
=== "string" && 628 - item.pattern.
trim()
631 - pattern: item.pattern.
trim(),
632 - flags:
typeof item.flags
=== "string" ? item.flags
: undefined,
633 - label:
typeof item.
label === "string" ? item.
label : undefined,
640 -function loadSecretValues(
643 - exclude
: Set<
string>,
645 - const values
: string[]
= [];
646 - for (
const spec
of specs) {
647 - for (
const file
of expandSecretSpec(cwd, spec)) {
650 - text
= readFileSync(file,
"utf8");
654 - values.
push(
...parseDotenv(text));
658 - values.
map(cleanSecretValue).
filter((v)
=> keepSecretValue(v, exclude)),
662 -function expandSecretSpec(cwd
: string, spec
: string)
: string[] {
663 - if (
!spec.
includes(
"*")) {
664 - const abs
= path.
resolve(cwd, spec);
665 - return existsSync(abs)
? [abs]
: [];
667 - if (spec
!== ".env.*.local")
return [];
669 - return readdirSync(cwd)
670 - .
filter((name)
=> /^\.env\..+\.local$/.
test(name))
671 - .
map((name)
=> path.
join(cwd, name));
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);
684 - out.
push(
stripInlineComment(m[
1]
?? ""));
689 -function stripInlineComment(value
: string)
: string {
690 - let v
= value.
trim();
692 - (v.
startsWith(
'"')
&& v.
endsWith(
'"'))
|| 693 - (v.
startsWith(
"'")
&& v.
endsWith(
"'"))
695 - v
= v.
slice(
1,
-1);
696 - else v
= v.
replace(
/\s+#.*$/,
"");
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;
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);
723 - ctx
: ExtensionContext,
725 - level
: "info" | "warning" = "info",
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));
742 - engine.settings.enabled
? "leaks:on" : "leaks:off",
746 - pi.
on(
"session_start",
async (_event, ctx)
=> {
751 - pi.
on(
"context",
async (event, ctx)
=> {
753 - const res
= engine.
redactDeep(
event.messages,
true);
754 - if (
!res.changed)
return;
755 - return { messages: res.
value };
758 - pi.
on(
"before_provider_request",
async (event, ctx)
=> {
760 - const res
= engine.
redactDeep(
event.payload,
true);
761 - engine.lastProviderRedactions
= res.count;
762 - engine.totalProviderRedactions
+= res.count;
766 - engine.settings.enabled
? `leaks:${res.count}` : "leaks:off",
768 - if (
!res.changed)
return;
772 - pi.
on(
"tool_call",
async (event, ctx)
=> {
774 - engine.
restoreDeepInPlace(
event.
input);
777 - pi.
on(
"message_end",
async (event, 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 };
786 - pi.
registerCommand(
"leaks", {
787 - description:
"Inspect and control provider-bound secret redaction",
788 - async handler(args, ctx) {
790 - const [cmd,
...rest]
= args.
trim().
split(
/\s+/).
filter(
Boolean);
791 - if (
!cmd
|| cmd
=== "status") {
792 - const s
= engine.
stats();
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}`,
799 - if (cmd
=== "reload") {
800 - engine.
reload(ctx.cwd,
parseSettings(pi, ctx.cwd));
801 - const s
= engine.
stats();
804 - `leaks reloaded\nrules: ${s.rules}\nsecret files: ${s.secretValues}`,
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.`,
818 - notify(ctx,
`leaks cleared ${n} mapping(s)`);
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);
827 - Object.
entries(counts)
828 - .
map(([k, v])
=> `${k}: ${v}`)
829 - .
join(
"\n")
|| "no matches";
830 - notify(ctx,
`${head}\n${res.text}`);
833 - notify(ctx,
"usage: /leaks status|reload|test <text>|clear",
"warning");
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;
1 +import { LeaksEngine }
from "./src/engine.ts";
3 + isExistingPlaceholder,
8 +}
from "./src/safety.ts";
10 +export const RUNTIME_SETTING_KEYS
= [
"regex"]
as const;
845 12 export const __test
= {
847 - PlaceholderSession,
14 + RUNTIME_SETTING_KEYS,
15 + isExistingPlaceholder,
858 -export default createLeaksExtension();
22 +export default function leaks(_pi
: unknown) {}
extensions/leaks/src/engine.ts
Addednew file mode 100644 11 +export type LeaksEngineOptions = {
12 + settings
?: Partial<
LeaksSettings>;
13 + env
?: Record<
string,
string | undefined>;
14 + detectors
?: Detector[];
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
?? [];
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)
=> {
39 + this.
warnOnce(
"detector.failure",
"detector failure; redaction pass left unchanged");
43 + const result
= { text, count:
0, labels: [], placeholders: [] };
44 + if (origin
=== "provider") this.lastProviderRedactionCount
= 0;
45 + if (failed)
return result;
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);
53 + value: result.
text as T,
54 + changed: result.
text !== value,
55 + count: result.count,
59 + restoreText(text
: string) {
63 + restoreDeep<
T>(value
: T)
: DeepRedactionResult<
T> {
64 + return { value, changed:
false, count:
0 };
68 + this.lastProviderRedactionCount
= 0;
71 + stats()
: EngineStats {
74 + builtInRuleCount:
0,
75 + customRegexCount: this.settings.regex.
length,
76 + regexWarningCount:
0,
77 + warningCount: this.warnings.
length,
78 + invalidRegexCount:
0,
81 + lastProviderRedactionCount: this.lastProviderRedactionCount,
82 + totalProviderRedactionCount: this.totalProviderRedactionCount,
85 + warnings: [
...this.warnings],
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" });
extensions/leaks/src/safety.ts
Addednew 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;
9 +export function sanitizeTag(raw
: string)
: string {
12 + .
replace(
/[^A-Z0-9]+/g,
"_")
13 + .
replace(
/^_+|_+$/g,
"")
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)) {
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)) {
41 + if (
/^(?:[a-z0-9-]+\.)+[a-z]{2,}$/i.
test(text))
return true;
43 + const url
= new URL(text);
44 + if (
!url.password
&& !/[?&](?:token|key|secret|password|auth)=/i.
test(url.search)) {
48 + if (
/^(?:\.\/|\.\.\/|\/|~\/)[^\s]+$/.
test(text))
return true;
extensions/leaks/src/types.ts
Addednew file mode 100644 1 +export type CustomRegexInput = {
4 + [key
: string]
: unknown;
7 +export type LeaksSettings = {
8 + regex
: CustomRegexInput[];
11 +export type WarningRecord = {
14 + severity
: "warning" | "error";
17 +export type SpanLabel = {
20 + source
: "dotenv" | "env" | "structure" | "custom" | "builtin" | "generic";
23 +export type SecretSpan = {
27 + labels
: SpanLabel[];
28 + kind
: "structure" | "exact" | "custom" | "builtin" | "generic" | "private-key" | "jwt";
33 +export type RedactionOrigin = "context" | "provider" | "diagnostic";
35 +export type RedactionResult = {
39 + placeholders
: string[];
42 +export type DeepRedactionResult<
T = unknown>
= {
48 +export type EngineStats = {
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[];
64 +export type Detector = (text
: string)
=> SecretSpan[];