repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

fix(leaks): require durable first-turn mappings

f2a25daa04353d4737fd8e6c5ff7504221318295

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (18)

extensions/leaks/CHANGELOG.md

Modified
4 4 # Changelog
6 6 ## Unreleased
8 8 - Parse structured detectors conservatively and keep ambiguous values visible.
9 9 - Persist HMAC32 placeholder mappings as authenticated AES-GCM session entries.
10 +- Fail open until new-session mapping entries are durably flushed.
10 11 - Rebuild mappings from the active branch on start, reload, and tree navigation.
11 12 - Block every tool on unknown placeholders or incomplete input traversal.
12 13 - Add deterministic regressions, fast-check suites, tracked-source scanning, and the CI fuzz gate.

extensions/leaks/README.md

Modified
22 22 HMAC32 is derived from a random generation root key.
23 23 Mappings use AES-256-GCM with a fresh nonce and authenticated generation metadata.
24 -A mapping is appended before its placeholder can reach the provider.
24 +A mapping must be durably appended before its placeholder can reach the provider.
25 +Pi buffers new-session entries until the first assistant message, so the first request remains visible and warns; run `/reload` afterward to activate durable redaction.
25 26 Persistence, encryption, collision, or capacity failure leaves the original value visible and warns.
27 28 Only authenticated mappings on `sessionManager.getBranch()` hydrate.

extensions/leaks/SPEC.md

Modified
38 38 Bound learned exact values and restorable placeholder mappings to 8192 each.
39 39 Bound deep traversal by object identity and depth 64.
40 40 On learned-value cap overflow, stop learning new dotenv/env exact values and warn once.
41 -Every emitted placeholder must have an authenticated active-generation mapping appended before provider use.
41 +Every emitted placeholder must have an authenticated active-generation mapping durably appended before provider use.
42 +A buffered new-session append is a failure: leave the original value visible, warn, and require extension reload after Pi flushes the first assistant message.
42 43 Mapping, collision, capacity, encryption, or append failure leaves the original value visible and warns.
44 45 `leaks` must tolerate faulty user input.

extensions/leaks/__fuzz__/builtins.fuzz.test.ts

Modified
7 7 ..."ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
9 9 const body = fc.string({ unit: alphaNum, minLength: 20, maxLength: 24 });
10 +const base64UrlBody = fc
11 + .tuple(
12 + fc.string({ unit: alphaNum, minLength: 19, maxLength: 23 }),
13 + fc.constantFrom("-", "_"),
14 + )
15 + .map(([prefix, suffix]) => prefix + suffix);
10 16 function ageKey(value: string) {
11 17 const hrp = "age-secret-key-";
12 18 const charset = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";
64 70 "GOOGLE_API_KEY",
65 - (v: string) => ["AIza", v.repeat(2).slice(0, 35)].join(""),
71 + (v: string) => ["AIza", v.repeat(2).slice(0, 34), v.at(-1)].join(""),
68 74 "STRIPE_LIVE_SECRET_KEY",
110 116 ] as const;
118 +const base64UrlTags = new Set([
119 + "OPENAI_TOKEN",
120 + "ANTHROPIC_TOKEN",
121 + "GOOGLE_API_KEY",
122 +]);
112 124 for (const [index, [tag, build]] of cases.entries()) {
125 + const generatedBody = base64UrlTags.has(tag) ? base64UrlBody : body;
114 127 `builtin-${index}-positive`,
116 - body,
129 + body: generatedBody,
117 130 left: fc.constantFrom("", "\n"),
118 131 right: fc.constantFrom("", "\n"),
132 145 `builtin-${index}-negative`,
133 - { body },
146 + { body: generatedBody },
135 148 )(`${tag} rejects one-field-invalid values`, ({ body }) => {
136 149 const built = build(body);

extensions/leaks/__tests__/corpus.test.ts

Modified
13 13 .split("\0")
14 14 .filter(Boolean)
15 15 .filter((file) =>
16 - /\.(?:ts|js|mjs|qml|json|jsonc|kdl|toml|ya?ml|sh|nu)$/.test(file),
16 + /(?:\.(?:ts|js|mjs|qml|lua|html|css|md|json|jsonc|kdl|toml|ya?ml|sh|nu)|(?:^|\/)package-lock\.json)$/.test(
17 + file,
18 + ),
18 20 .filter(
19 21 (file) =>
20 - !/(?:^test\/|^docs\/|\/__tests__\/|\/__fixtures__\/|\/__e2e__\/|\/static\/)/.test(
22 + !/(?:^test\/|^docs\/|\/__tests__\/|\/__fixtures__\/|\/__e2e__\/|\/static\/|(?:^|\/)(?:README|CHANGELOG|SPEC)\.md$)/.test(
21 23 file,

extensions/leaks/__tests__/detectors.test.ts

Modified
124 124 for (const block of blocks) expectRedacts(block, block.slice(0, 16));
127 + it("accepts Base64URL tokens ending in alphabet punctuation", () => {
128 + expect(__test.findBuiltinSpans(`sk-proj-${"A".repeat(31)}-`)).toHaveLength(
129 + 1,
130 + );
131 + expect(
132 + __test.findBuiltinSpans(`sk-ant-api03-${"A".repeat(31)}_`),
133 + ).toHaveLength(1);
134 + expect(__test.findBuiltinSpans(`AIza${"A".repeat(34)}-`)).toHaveLength(1);
135 + });
127 137 it("validates age checksums and minisign payload encoding", () => {
128 138 const validAge = validAgeKey();
129 139 const invalidAge = `${validAge.slice(0, -1)}q`;

extensions/leaks/__tests__/direct-modules.test.ts

Modified
137 137 expect(isPlainObject(new Date())).toBe(false);
138 138 expect(sanitizeTag("a/b")).toBe("A_B");
139 139 expect(isExistingPlaceholder("__LEAK_A_abcdef123456_2__")).toBe(true);
140 + expect(isExistingPlaceholder(`__LEAK_V1_${"_".repeat(10_000)}`)).toBe(
141 + false,
142 + );
140 143 expect(maskDiagnostic("a\nb")).toBe("text chars=3 lines=2");
141 144 expect(isWeakValue("https://example.test/path")).toBe(true);

extensions/leaks/__tests__/dotenv.test.ts

Modified
116 116 expect(__test.isSensitiveKey("CLIENT_SECRET_2")).toBe(true);
117 117 expect(__test.isSensitiveKey("prod-token-backup")).toBe(true);
118 118 expect(__test.isSensitiveKey("my_token_value")).toBe(true);
119 + expect(__test.isSensitiveKey("APP_AUTH_MODE")).toBe(true);
120 + expect(__test.isSensitiveKey("APP_KEY_BACKUP")).toBe(true);
119 121 expect(__test.isSensitiveKey("lineage")).toBe(false);
126 128 SESSION_MANAGER: ordinary,
127 129 PROD_KEY: ordinary,
128 130 APP_AUTH: ordinary,
131 + APP_AUTH_MODE: "auth-mode-value-123456",
132 + APP_KEY_BACKUP: "key-backup-value-123456",
129 133 USER_SESSION: ordinary,
130 134 PROD_ACCESS_TOKEN: ordinary,
143 147 expect(learned.values.map((item) => item.value)).not.toContain(url);
150 + it("warns when dotenv and environment exceed the combined cap", () => {
151 + const cwd = mkdtempSync(path.join(tmpdir(), "leaks-combined-cap-"));
152 + writeFileSync(
153 + path.join(cwd, ".env"),
154 + Array.from(
155 + { length: 8192 },
156 + (_, index) => `TOKEN_${index}=combined-cap-${index}-123456`,
157 + ).join("\n"),
158 + );
159 + const engine = new __test.LeaksEngine();
160 + engine.reload(cwd, { regex: [] }, { TOKEN: "overflow-value-123456" });
161 + expect(engine.stats().warnings).toContainEqual(
162 + expect.objectContaining({
163 + code: "learned.cap",
164 + message: expect.stringContaining("combined"),
165 + }),
166 + );
167 + });
146 169 it("enforces learned exact value cap and warns once", () => {
147 170 const learned = __test.learnEnvironmentValues(

extensions/leaks/__tests__/pi-direct.test.ts

Modified
238 + it("fails open before first-turn entries are flushed", async () => {
239 + const view = ui();
240 + const sessionFile = path.join(view.ctx.cwd, "buffered-session.jsonl");
241 + writeFileSync(sessionFile, "");
242 + const branch: Array<Record<string, unknown>> = [];
243 + const runtime = fakePi((customType, data) => {
244 + branch.push({
245 + id: `buffered-${branch.length}`,
246 + type: "custom",
247 + customType,
248 + data,
249 + });
250 + });
251 + leaks(runtime.pi as never);
252 + const ctx = {
253 + ...view.ctx,
254 + sessionManager: {
255 + getBranch: () => branch,
256 + getSessionFile: () => sessionFile,
257 + },
258 + };
259 + await runtime.handlers.get("session_start")?.({}, ctx);
260 + const secret = "ghp_" + "B".repeat(36);
261 + const result = await runtime.handlers.get("before_provider_request")?.(
262 + { payload: { text: secret } },
263 + ctx,
264 + );
265 + expect(JSON.stringify(result)).toContain(secret);
266 + });
238 268 it("rejects failed-append ghosts after a fresh runtime reload", async () => {
239 269 const view = ui();
240 270 const sessionFile = path.join(view.ctx.cwd, "session.jsonl");
426 456 await runtime.handlers.get("message_update")?.({ message }, view.ctx);
427 457 expect(message.content[0].arguments.content).toBe(placeholder);
458 + const ended = await runtime.handlers.get("message_end")?.(
459 + { message },
460 + view.ctx,
461 + );
462 + expect(ended).toBeUndefined();
463 + expect(message.content[0].arguments.content).toBe(placeholder);
428 464 for (const hook of [
429 465 "tool_call",
430 466 "tool_execution_start",

extensions/leaks/__tests__/unit.test.ts

Modified
103 103 expect(restored.count).toBe(2);
106 + it("bounds status tag history", () => {
107 + const engine = new __test.LeaksEngine();
108 + const values = Array.from(
109 + { length: 20 },
110 + (_, index) => `ghp_${String(index).padStart(2, "0")}${"A".repeat(34)}`,
111 + );
112 + engine.redactText(values.join(" "));
113 + expect(engine.stats().latestTags).toHaveLength(12);
114 + });
106 116 it("reports property-name redactions", () => {
107 117 const sensitiveKey = "property-name-secret-123456";
108 118 const engine = new __test.LeaksEngine({

extensions/leaks/src/detectors.ts

Modified
18 18 "ai",
19 19 /\bsk-or-v1-[A-Za-z0-9]{32,}\b/dg,
21 - rule("openai", "OPENAI_TOKEN", "ai", /\bsk-proj-[A-Za-z0-9_-]{32,}\b/dg),
21 + rule(
22 + "openai",
23 + "OPENAI_TOKEN",
24 + "ai",
25 + /(?<![A-Za-z0-9_-])sk-proj-[A-Za-z0-9_-]{32,}(?![A-Za-z0-9_-])/dg,
26 + ),
22 27 rule(
23 28 "anthropic",
24 29 "ANTHROPIC_TOKEN",
25 30 "ai",
26 - /\bsk-ant-api03-[A-Za-z0-9_-]{32,}\b/dg,
31 + /(?<![A-Za-z0-9_-])sk-ant-api03-[A-Za-z0-9_-]{32,}(?![A-Za-z0-9_-])/dg,
28 33 rule("huggingface", "HUGGINGFACE_TOKEN", "ai", /\bhf_[A-Za-z0-9]{32,}\b/dg),
29 34 rule("github", "GITHUB_TOKEN", "vcs", /\bgh[pousr]_[A-Za-z0-9]{30,}\b/dg),
38 43 "google-api-key",
39 44 "GOOGLE_API_KEY",
40 45 "cloud",
41 - /\bAIza[A-Za-z0-9_-]{35}\b/dg,
46 + /(?<![A-Za-z0-9_-])AIza[A-Za-z0-9_-]{35}(?![A-Za-z0-9_-])/dg,
43 48 rule(
44 49 "stripe-live",
50 55 "slack",
51 56 "SLACK_TOKEN",
52 57 "messaging",
53 - /\bxox[baprs]-[A-Za-z0-9-]{20,}\b/dg,
58 + /(?<![A-Za-z0-9-])xox[baprs]-[A-Za-z0-9-]{20,}(?![A-Za-z0-9-])/dg,
55 60 rule(
56 61 "slack-webhook",

extensions/leaks/src/dotenv.ts

Modified
50 50 "MONGO_URL",
51 51 "REDIS_URL",
53 +const LEARNABLE = new Set(
54 + [...SENSITIVE].filter((sensitive) => !CONTEXT_ONLY.has(sensitive)),
55 +);
54 57 export type LearnedValue = {
55 58 value: string;
141 144 export function isSensitiveKey(key: string): boolean {
142 - const tokens = normalizeKey(key.replace(PUBLIC_PREFIX_RE, "")).split("_");
143 - return [...SENSITIVE].some((sensitive) => {
144 - const expected = sensitive.split("_");
145 - return tokens.some((_, start) =>
146 - expected.every((token, offset) => tokens[start + offset] === token),
147 - );
148 - });
145 + return hasTokens(
146 + normalizeKey(key.replace(PUBLIC_PREFIX_RE, "")).split("_"),
147 + SENSITIVE,
148 + );
151 151 export function highConfidenceSecretShape(value: string): boolean {
249 249 if (PUBLIC_PREFIX_RE.test(key)) return false;
250 250 const normalized = normalizeKey(key);
251 251 if (METADATA_SUFFIX_RE.test(normalized)) return false;
252 - if (SENSITIVE.has(normalized) && !CONTEXT_ONLY.has(normalized)) return true;
253 - if (
254 - [...CONTEXT_ONLY].some(
255 - (context) => normalized === context || normalized.endsWith(`_${context}`),
256 - )
257 - )
258 - return false;
259 - return isSensitiveKey(normalized);
252 + return hasTokens(normalized.split("_"), LEARNABLE);
255 +function hasTokens(tokens: string[], names: Set<string>) {
256 + return [...names].some((name) => {
257 + const expected = name.split("_");
258 + return tokens.some((_, start) =>
259 + expected.every((token, offset) => tokens[start + offset] === token),
260 + );
261 + });
262 264 function dotenvWarning(message: string): WarningRecord {

extensions/leaks/src/engine.ts

Modified
136 136 if (labels.length > 0) this.recentLabels = labels.slice(-12);
137 137 if (placeholders.length > 0) {
138 - this.latestTags = placeholders.map((placeholder) =>
139 - placeholder.replace(/^__LEAK_V1_/, "").replace(/_[a-f0-9]{32}__$/, ""),
140 - );
138 + this.latestTags = placeholders
139 + .map((placeholder) =>
140 + placeholder
141 + .replace(/^__LEAK_V1_/, "")
142 + .replace(/_[a-f0-9]{32}__$/, ""),
144 + .slice(-12);
142 146 return {
143 147 text: redacted,
331 335 private learn(cwd: string, env: Record<string, string | undefined>) {
332 336 const dotenv = loadDotenvValues(cwd);
333 337 const processEnv = learnEnvironmentValues(env);
334 - this.learned = [...dotenv.values, ...processEnv.values].slice(0, 8192);
338 + const learned = [...dotenv.values, ...processEnv.values];
339 + this.learned = learned.slice(0, 8192);
340 + if (learned.length > this.learned.length)
341 + this.addWarning({
342 + code: "learned.cap",
343 + message: "combined learned value cap reached; further values ignored",
344 + severity: "warning",
345 + });
335 346 for (const warning of [...dotenv.warnings, ...processEnv.warnings])
336 347 this.addWarning(warning);

extensions/leaks/src/pi.ts

Modified
119 119 pi.on("message_end", async (event, ctx) => {
120 + if (containsLeaksSourceMutation(event.message, ctx.cwd)) return;
120 121 const restored = engine.restoreDeep(event.message).value;
121 - if (!containsLeaksSourceMutation(restored, ctx.cwd))
122 - warnUnknownLocal(
123 - engine,
124 - restored,
125 - ctx,
126 - "assistant message",
127 - reportedWarnings,
128 - );
122 + warnUnknownLocal(
123 + engine,
124 + restored,
125 + ctx,
126 + "assistant message",
127 + reportedWarnings,
128 + );
129 129 return { message: restored };
338 338 engine.configurePersistence(entries, (customType, data) => {
340 + const manager = ctx.sessionManager;
341 + const before = new Set(manager?.getBranch().map((entry) => entry.id));
340 342 pi.appendEntry(customType, data);
343 + if (manager?.getSessionFile()) {
344 + const appended = manager
345 + .getBranch()
346 + .findLast(
347 + (entry) =>
348 + entry.type === "custom" &&
349 + entry.customType === customType &&
350 + !before.has(entry.id),
351 + );
352 + if (!appended || !persistedEntryIds(ctx)?.has(appended.id)) {
353 + notify(
354 + ctx,
355 + "session entry is not durable yet; values remain visible (run /reload after the first assistant message)",
356 + "warning",
357 + );
358 + throw new Error("leaks mapping was not durably appended");
341 361 } catch (error) {
342 362 state.tainted = true;
343 363 throw error;

extensions/leaks/src/safety.ts

Modified
1 1 export const V1_PLACEHOLDER_SOURCE =
2 - "__LEAK_V1(?:_[A-Z0-9_]{1,64}){1,6}_[a-f0-9]{32}__";
2 + "__LEAK_V1_[A-Z0-9_]{1,389}_[a-f0-9]{32}__";
3 3 export const ANY_PLACEHOLDER_SOURCE =
4 - "__LEAK_(?:V1(?:_[A-Z0-9_]{1,64}){1,6}_[a-f0-9]{32}|(?:[A-Z0-9]{1,64}_)*[a-f0-9]{12}(?:_\\d+)?)__";
4 + "__LEAK_(?:V1_[A-Z0-9_]{1,389}_[a-f0-9]{32}|[A-Z0-9_]{0,390}[a-f0-9]{12}(?:_\\d+)?)__";
5 5 const PLACEHOLDER_RE = new RegExp(`^(?:${ANY_PLACEHOLDER_SOURCE})$`);
7 7 export function isPlainObject(

scripts/extensions.json

Modified
2 - "secretScanIgnore": ["extensions/leaks/__tests__/ux.test.ts"]
2 + "secretScanIgnore": [
3 + "extensions/leaks/__tests__/detectors.test.ts",
4 + "extensions/leaks/__tests__/dotenv.test.ts",
5 + "extensions/leaks/__tests__/harness.test.ts",
6 + "extensions/leaks/__tests__/settings.test.ts",
7 + "extensions/leaks/__tests__/structures.test.ts",
8 + "extensions/leaks/__tests__/unit.test.ts",
9 + "extensions/leaks/__tests__/ux.test.ts"
10 + ]

scripts/extensions.mjs

Modified
427 427 `cross-extension relative import ${spec} in ${rel(file, ext)}`,
430 - if (
431 - !file.split(/[\\/]/).includes("__tests__") &&
432 - SECRET_RE.test(text) &&
433 - !isSecretScanIgnored(file, ignores, root)
430 + if (SECRET_RE.test(text) && !isSecretScanIgnored(file, ignores, root))
435 431 err(errors, ext, `possible secret in ${rel(file, ext)}`);
437 433 for (const dep of [...packages].filter((p) => PI_PEERS.has(p)).sort())

scripts/test_extensions_static_check.mjs

Modified
251 251 assert.deepEqual(legacyReferenceViolations, []);
253 +withTemp((root) => {
254 + const ext = writeExtension(root, "test-secret", {});
255 + writeFileSync(
256 + join(ext, "__tests__", "harness.test.ts"),
257 + 'const apiKey = "not-a-real-secret-value";\n',
258 + "utf8",
259 + );
260 + assert(
261 + staticCheck(ext, { root }).includes(
262 + "test-secret: possible secret in __tests__/harness.test.ts",
263 + ),
264 + );
265 +});
253 267 console.log("OK static-check tests");