repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

chore(leaks): finish spec tests and formatting

8c0ed1e432158ba8f9f95f2a28bd23893179eff7

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (23)

extensions/leaks/PLAN.md

Added
new file mode 100644
1 +# Leaks Spec Implementation Plan
3 +> **For agentic workers:** REQUIRED SUB-SKILL: Use /skill:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
5 +**Goal:** Rebuild `extensions/leaks` so provider-bound text is redacted per `extensions/leaks/SPEC.md`, local Pi behavior is restored when placeholders round-trip exactly, and every validation batch V0-V9 has executable evidence.
7 +**Architecture:** Replace prototype internals with a small redaction engine plus Pi hook adapter.
8 +State is in-memory only: settings, warning counters, learned exact values, placeholder maps, recent safe labels, and provider redaction counters.
9 +Provider payload redaction is authoritative; context redaction is opportunistic; tool args and finalized assistant messages restore placeholders locally.
11 +**Tech Stack:** TypeScript ESM, Node 22 built-ins, Pi extension API from `@earendil-works/pi-coding-agent`, Vitest, repo tasks via `mise`.
13 +**Roadmap:** None.
15 +**Phase:** Single-plan implementation.
17 +**Plan Location:** `extensions/leaks/PLAN.md`.
19 +---
21 +## Spec Coverage Map
23 +| Spec section | Planned tasks |
24 +| ----------------------------- | ------------------------- |
25 +| Authority | Tasks 0, 14 |
26 +| Mission | Tasks 1, 10, 11 |
27 +| Core requirements | Tasks 1, 2, 9, 10, 12, 13 |
28 +| Core model | Tasks 2, 3, 9 |
29 +| Pi integration strategy | Tasks 10, 11 |
30 +| Settings | Task 4 |
31 +| Built-in detection | Task 7 |
32 +| Dotenv and environment values | Tasks 5, 6 |
33 +| False positives | Tasks 6, 8 |
34 +| UX and commands | Task 11 |
35 +| Validation ledger | Tasks 0, 12, 14 |
36 +| Non-goals | Tasks 1, 14 |
38 +## File Structure
40 +- Create `extensions/leaks/src/types.ts`.
41 + Owns public internal types: settings, warnings, spans, labels, status, redaction result.
42 +- Create `extensions/leaks/src/safety.ts`.
43 + Owns safe text helpers, label sanitization, weak-value classification, placeholder detection.
44 +- Create `extensions/leaks/src/settings.ts`.
45 + Owns Pi settings loading, custom regex validation, regex compilation warnings.
46 +- Create `extensions/leaks/src/placeholders.ts`.
47 + Owns HMAC placeholder creation, collision suffixing, caps, restore mapping, no persistence.
48 +- Create `extensions/leaks/src/structures.ts`.
49 + Owns URL, query-param, header, cookie, and assignment component parsing.
50 +- Create `extensions/leaks/src/dotenv.ts`.
51 + Owns fixed dotenv discovery, dotenv parsing, process env learning, env key collapse to `ENV_KEY`.
52 +- Create `extensions/leaks/src/detectors.ts`.
53 + Owns built-in detector definitions and positive/negative detector classes.
54 +- Create `extensions/leaks/src/selector.ts`.
55 + Owns span merge, priority ordering, smallest-span overlap selection, existing-placeholder exclusion.
56 +- Create `extensions/leaks/src/engine.ts`.
57 + Owns redaction orchestration, deep traversal, failure-open handling, stats, status state.
58 +- Create `extensions/leaks/src/pi.ts`.
59 + Owns Pi hook wiring and `/leaks` command handling.
60 +- Replace `extensions/leaks/index.ts`.
61 + Export Pi extension factory and `__test` internals from the new modules.
62 +- Replace `extensions/leaks/__tests__/unit.test.ts`.
63 + Keep only high-signal core engine tests or delegate to new focused test files.
64 +- Replace `extensions/leaks/__tests__/harness.test.ts`.
65 + Test actual Pi hook behavior through the existing harness.
66 +- Create focused tests under `extensions/leaks/__tests__/`: `settings.test.ts`, `placeholders.test.ts`, `structures.test.ts`, `dotenv.test.ts`, `detectors.test.ts`, `selector.test.ts`, `ux.test.ts`, `performance.test.ts`, `ledger.test.ts`.
67 +- Replace `extensions/leaks/README.md`.
68 + Document behavior, threat model, settings, commands, limitations, and no secret values.
70 +## Invariants For All Tasks
72 +- Never print secret values in tests, expected output, README examples, notifications, logs, status, or diagnostics.
73 +- Test fixture secret strings may exist in source tests only as controlled fixture inputs; assertions must check absence from output.
74 +- Mappings stay in memory only; do not call `pi.appendEntry`.
75 +- Do not add tools.
76 +- Do not implement rejected settings: `enabled`, `keywords`, `exclude`, `secretFiles`.
77 +- Use `mise run test -- <path>` for tests.
78 +- Use `mise run lint` and `mise run ci` only through `mise`.
80 +### Task 0: Validation Ledger Baseline
82 +**Files:**
84 +- Create: `extensions/leaks/__tests__/ledger.test.ts`
85 +- Modify: `extensions/leaks/SPEC.md` only if the spec itself is contradictory and user approves first.
87 +- [ ] **Step 1: Write a ledger coverage test**
89 +Create `extensions/leaks/__tests__/ledger.test.ts` with this structure so V0 scope and authority are executable instead of narrative-only:
91 +````ts
92 +import { readFileSync } from "node:fs";
93 +import path from "node:path";
94 +import { describe, expect, it } from "vitest";
96 +const specPath = path.resolve(import.meta.dirname, "../SPEC.md");
97 +const spec = readFileSync(specPath, "utf8");
99 +const expectedHeadings = [
100 + "Authority",
101 + "Mission",
102 + "Core requirements",
103 + "Core model",
104 + "Pi integration strategy",
105 + "Settings",
106 + "Built-in detection",
107 + "Dotenv and environment values",
108 + "False positives",
109 + "UX and commands",
110 + "Validation ledger",
111 + "Non-goals",
114 +const batches = [
115 + "V0 scope",
116 + "V1 provider boundary",
117 + "V2 placeholders",
118 + "V3 settings",
119 + "V4 built-ins",
120 + "V5 learned values",
121 + "V6 component parsers",
122 + "V7 false positives",
123 + "V8 UX",
124 + "V9 performance/failure",
127 +const sectionBatchMap = new Map<string, string[]>([
128 + ["Authority", ["V0 scope"]],
129 + ["Mission", ["V0 scope", "V1 provider boundary"]],
131 + "Core requirements",
132 + ["V1 provider boundary", "V8 UX", "V9 performance/failure"],
133 + ],
134 + ["Core model", ["V2 placeholders"]],
135 + ["Pi integration strategy", ["V1 provider boundary"]],
136 + ["Settings", ["V3 settings", "V9 performance/failure"]],
137 + ["Built-in detection", ["V4 built-ins"]],
139 + "Dotenv and environment values",
140 + ["V5 learned values", "V6 component parsers", "V7 false positives"],
141 + ],
142 + ["False positives", ["V6 component parsers", "V7 false positives"]],
143 + ["UX and commands", ["V8 UX"]],
144 + ["Validation ledger", ["V0 scope"]],
145 + ["Non-goals", ["V0 scope"]],
146 +]);
148 +const normativeMarker =
149 + /\b(must|must not|should|do not|never|only|requires?|required|accepted|planned|rejected|out of scope|non-goals?|always|at worst|when|on)\b/i;
151 +function sections() {
152 + const result: Array<{ name: string; body: string }> = [];
153 + const matches = [...spec.matchAll(/^## (.+)$/gm)];
154 + for (let index = 0; index < matches.length; index++) {
155 + const match = matches[index];
156 + const next = matches[index + 1];
157 + result.push({
158 + name: String(match[1]),
159 + body: spec.slice(match.index! + match[0].length, next?.index),
160 + });
162 + return result;
165 +function normativeSentences(body: string) {
166 + return body
167 + .replace(/```[\s\S]*?```/g, "")
168 + .split(/(?<=[.!?])\s+|\n+-\s+/g)
169 + .map((line) => line.trim())
170 + .filter((line) => normativeMarker.test(line));
173 +describe("leaks spec validation ledger", () => {
174 + it("keeps the spec section map stable", () => {
175 + const headings = sections().map((section) => section.name);
176 + expect(headings).toEqual(expectedHeadings);
177 + });
179 + it("records every validation batch in the ledger and evidence table", () => {
180 + for (const batch of batches) {
181 + expect(spec).toContain(`| ${batch}`);
183 + });
185 + it("maps every normative sentence to at least one validation batch", () => {
186 + const misses = sections().flatMap((section) => {
187 + const mappedBatches = sectionBatchMap.get(section.name) ?? [];
188 + return normativeSentences(section.body)
189 + .filter(() => mappedBatches.length === 0)
190 + .map((sentence) => ({ section: section.name, sentence }));
191 + });
192 + expect(misses).toEqual([]);
193 + });
195 + it("keeps V0 authority and scope requirements explicit", () => {
196 + expect(spec).toContain("This spec is the source of truth");
197 + expect(spec).toContain("current implementation is a prototype/reference");
198 + expect(spec).toContain("Implementation must be fresh and spec-shaped");
199 + expect(spec).toContain("Avoid creating `extensions/leaks-ref`");
200 + expect(spec).toContain("Non-goals");
201 + });
202 +});
203 +````
205 +Use regexes over headings and table rows; never parse or print secret fixture values.
207 +- [ ] **Step 2: Run ledger test and verify current baseline**
209 +Run: `mise run test -- extensions/leaks/__tests__/ledger.test.ts`
210 +Expected: PASS, because the current spec already records V0-V9 evidence.
212 +- [ ] **Step 3: Commit ledger baseline**
214 +Run:
216 +```bash
217 +git add extensions/leaks/__tests__/ledger.test.ts
218 +git commit -m "test(leaks): lock spec validation ledger"
219 +```
221 +### Task 1: Fresh Engine Shell And Public Types
223 +**Files:**
225 +- Create: `extensions/leaks/src/types.ts`
226 +- Create: `extensions/leaks/src/safety.ts`
227 +- Create: `extensions/leaks/src/engine.ts`
228 +- Replace: `extensions/leaks/index.ts`
229 +- Test: `extensions/leaks/__tests__/unit.test.ts`
231 +- [ ] **Step 1: Replace unit test with engine shell tests**
233 +Replace obsolete setting tests for rejected `enabled`, `keywords`, `exclude`, and `secretFiles` with tests that assert:
235 +- default engine is active
236 +- non-text values pass through unchanged
237 +- images and binary-looking objects are not inspected as special media
238 +- detector exceptions fail open and add one warning
239 +- secret values never appear in warning text
241 +Required test names:
243 +- `creates active engine with empty volatile state`
244 +- `does not implement rejected compatibility settings`
245 +- `fails open when a detector throws`
246 +- `keeps warning text secret-free`
248 +- [ ] **Step 2: Run shell tests and verify failure**
250 +Run: `mise run test -- extensions/leaks/__tests__/unit.test.ts`
251 +Expected: FAIL with missing exports from `__test` or missing modules.
253 +- [ ] **Step 3: Implement types and safety helpers**
255 +In `src/types.ts`, define exported types for:
257 +- `LeaksSettings` with only `regex: CustomRegexInput[]`
258 +- `CustomRegexInput` with `pattern?: unknown`, `label?: unknown`, and index signature for unsupported fields
259 +- `WarningRecord` with `code`, `message`, `severity`
260 +- `SpanLabel` with `tag`, `priority`, `source`
261 +- `SecretSpan` with `start`, `end`, `value`, `labels`, `kind`, `confidence`, `wholeSpan`
262 +- `RedactionResult` with `text`, `count`, `labels`, `placeholders`
263 +- `EngineStats` with all `/leaks status` counts required by the spec
265 +In `src/safety.ts`, implement:
267 +- `isPlainObject(value: unknown): value is Record<string, unknown>`
268 +- `sanitizeTag(raw: string): string`
269 +- `isExistingPlaceholder(text: string): boolean`
270 +- `maskDiagnostic(input: string): string` returning only safe metadata such as counts and labels
271 +- `isWeakValue(value: string): boolean` for booleans, nullish words, plain numbers, short strings, ordinary hostnames, ordinary URLs without credentials, references, UUIDs, and filesystem-ish paths
273 +- [ ] **Step 4: Implement minimal engine shell**
275 +In `src/engine.ts`, implement `LeaksEngine` with constructor accepting settings and env, `reload(cwd)`, `redactText(text, origin)`, `redactDeep(value, origin)`, `restoreText(text)`, `restoreDeep(value)`, `clear()`, and `stats()`.
276 +For this task, return input unchanged unless a provided test detector throws; on throw, add one warning and return original input.
278 +- [ ] **Step 5: Replace `index.ts` with new exports**
280 +`index.ts` must default-export the Pi extension from `src/pi.ts` after Task 10 exists.
281 +Until Task 10, export a no-op default factory that registers no hooks and export `__test` with `LeaksEngine`, helpers, and type-level constants.
283 +- [ ] **Step 6: Run shell tests and verify pass**
285 +Run: `mise run test -- extensions/leaks/__tests__/unit.test.ts`
286 +Expected: PASS.
288 +- [ ] **Step 7: Commit engine shell**
290 +Run:
292 +```bash
293 +git add extensions/leaks/index.ts extensions/leaks/src/types.ts extensions/leaks/src/safety.ts extensions/leaks/src/engine.ts extensions/leaks/__tests__/unit.test.ts
294 +git commit -m "refactor(leaks): add spec-shaped engine shell"
295 +```
297 +### Task 2: Placeholder Store
299 +**Files:**
301 +- Create: `extensions/leaks/src/placeholders.ts`
302 +- Create: `extensions/leaks/__tests__/placeholders.test.ts`
303 +- Modify: `extensions/leaks/src/engine.ts`
304 +- Modify: `extensions/leaks/index.ts`
306 +- [ ] **Step 1: Write placeholder tests**
308 +Create tests for:
310 +- placeholder format `__LEAK_<TAG>[_<TAG>...]_<HMAC12>[_N]__`
311 +- HMAC12 lowercase hex length
312 +- random per-store key makes same value produce different HMACs across stores
313 +- same value remains stable inside one store
314 +- tags freeze after first mapping
315 +- semantic priority orders tags as dotenv/env, structure, custom regex, built-in, generic
316 +- max 6 tags per placeholder
317 +- max 64 chars per sanitized tag
318 +- collision suffix `_2` when HMAC collision is forced by injectable digest function
319 +- cap 8192 mappings
320 +- overflow creates non-restorable placeholders and warns once
321 +- restore lookup replaces only exact placeholders
323 +- [ ] **Step 2: Run placeholder tests and verify failure**
325 +Run: `mise run test -- extensions/leaks/__tests__/placeholders.test.ts`
326 +Expected: FAIL with missing `PlaceholderStore`.
328 +- [ ] **Step 3: Implement `PlaceholderStore`**
330 +`PlaceholderStore` must use `node:crypto` `randomBytes(32)` and `createHmac("sha256", key)` by default.
331 +Expose constructor injection for test digest only.
332 +Store `secret -> placeholder` and `placeholder -> secret` maps.
333 +Reject persistence by keeping every map private to the store instance.
334 +Expose `create(value, labels)`, `restore(text)`, `clear()`, and `stats()`.
336 +- [ ] **Step 4: Wire engine to placeholder store**
338 +`LeaksEngine.redactText` must call the store for selected spans.
339 +`restoreText` and `restoreDeep` must use the store.
340 +`clear()` must clear mappings and counters that spec allows to reset.
342 +- [ ] **Step 5: Run placeholder tests and engine tests**
344 +Run: `mise run test -- extensions/leaks/__tests__/placeholders.test.ts extensions/leaks/__tests__/unit.test.ts`
345 +Expected: PASS.
347 +- [ ] **Step 6: Commit placeholder store**
349 +Run:
351 +```bash
352 +git add extensions/leaks/src/placeholders.ts extensions/leaks/src/engine.ts extensions/leaks/index.ts extensions/leaks/__tests__/placeholders.test.ts
353 +git commit -m "feat(leaks): add volatile placeholder store"
354 +```
356 +### Task 3: Span Selector
358 +**Files:**
360 +- Create: `extensions/leaks/src/selector.ts`
361 +- Create: `extensions/leaks/__tests__/selector.test.ts`
362 +- Modify: `extensions/leaks/src/engine.ts`
363 +- Modify: `extensions/leaks/index.ts`
365 +- [ ] **Step 1: Write selector tests**
367 +Create tests for:
369 +- identical spans merge labels before placeholder creation
370 +- component parser span wins over enclosing exact learned value
371 +- exact learned value wins over generic regex
372 +- smallest secret-bearing span wins for non-identical overlaps
373 +- private key block keeps whole span over smaller generic matches
374 +- JWT keeps whole span over smaller component-looking matches
375 +- existing placeholders are not redacted again
376 +- adjacent spans replace correctly without index drift
378 +- [ ] **Step 2: Run selector tests and verify failure**
380 +Run: `mise run test -- extensions/leaks/__tests__/selector.test.ts`
381 +Expected: FAIL with missing `selectSecretSpans`.
383 +- [ ] **Step 3: Implement selector**
385 +Implement `selectSecretSpans(text, spans)` in `src/selector.ts`.
386 +Sort by start, end, confidence, priority, and span length.
387 +First remove spans inside existing placeholders.
388 +Merge identical ranges by combined label list.
389 +For overlaps, choose by forced whole-span kinds (`private-key`, `jwt`), then component, then exact, then custom, then built-in, then generic, then shorter length.
390 +Return non-overlapping spans sorted descending by `start` for safe replacement.
392 +- [ ] **Step 4: Wire selector into engine**
394 +`LeaksEngine.redactText` must collect candidate spans from enabled detectors, pass them to the selector, create placeholders once per selected span, and replace from right to left.
396 +- [ ] **Step 5: Run selector tests and placeholder tests**
398 +Run: `mise run test -- extensions/leaks/__tests__/selector.test.ts extensions/leaks/__tests__/placeholders.test.ts`
399 +Expected: PASS.
401 +- [ ] **Step 6: Commit selector**
403 +Run:
405 +```bash
406 +git add extensions/leaks/src/selector.ts extensions/leaks/src/engine.ts extensions/leaks/index.ts extensions/leaks/__tests__/selector.test.ts
407 +git commit -m "feat(leaks): select secret spans by confidence"
408 +```
410 +### Task 4: Settings And Custom Regex
412 +**Files:**
414 +- Create: `extensions/leaks/src/settings.ts`
415 +- Create: `extensions/leaks/__tests__/settings.test.ts`
416 +- Modify: `extensions/leaks/src/engine.ts`
417 +- Modify: `extensions/leaks/index.ts`
419 +- [ ] **Step 1: Write settings tests**
421 +Create tests for:
423 +- only `leaks.regex` is accepted
424 +- unsupported fields on regex entries warn and are ignored
425 +- `flags` warns and is ignored
426 +- pattern length 512 accepted
427 +- pattern length 513 rejected
428 +- label `[A-Z0-9_]{1,64}` accepted
429 +- missing label loads with `CUSTOM_REGEX` warning
430 +- invalid label falls back to `CUSTOM_REGEX` warning
431 +- invalid regex syntax rejected with warning
432 +- zero captures redacts full match
433 +- one capture redacts group 1
434 +- multiple captures redact group 1 and warn
435 +- unsafe regex rejection: backreference, lookaround, nested quantifier, quantified alternation, empty alternative, dot-star catchall
436 +- warning text never includes secret fixture values
438 +- [ ] **Step 2: Run settings tests and verify failure**
440 +Run: `mise run test -- extensions/leaks/__tests__/settings.test.ts`
441 +Expected: FAIL with missing settings compiler.
443 +- [ ] **Step 3: Implement settings loader and compiler**
445 +Implement:
447 +- `loadLeaksSettings(raw: unknown): LoadedSettings`
448 +- `compileCustomRegex(settings: LoadedSettings): CompiledCustomRule[]`
449 +- `countCaptures(pattern: string): number`
450 +- `isUnsafeRegex(pattern: string): boolean`
452 +Use `d` regex flag internally for match indices and `g` for iteration.
453 +Do not accept user-provided flags.
454 +Warnings must use safe codes and no raw secret value text.
456 +- [ ] **Step 4: Wire custom regex into engine**
458 +Engine reload must compile custom regex once.
459 +Custom regex spans must use source `custom` and label from sanitized `CUSTOM_REGEX` or valid user label.
461 +- [ ] **Step 5: Run settings and selector tests**
463 +Run: `mise run test -- extensions/leaks/__tests__/settings.test.ts extensions/leaks/__tests__/selector.test.ts`
464 +Expected: PASS.
466 +- [ ] **Step 6: Commit settings**
468 +Run:
470 +```bash
471 +git add extensions/leaks/src/settings.ts extensions/leaks/src/engine.ts extensions/leaks/index.ts extensions/leaks/__tests__/settings.test.ts
472 +git commit -m "feat(leaks): compile safe custom regex settings"
473 +```
475 +### Task 5: Dotenv And Process Environment Learning
477 +**Files:**
479 +- Create: `extensions/leaks/src/dotenv.ts`
480 +- Create: `extensions/leaks/__tests__/dotenv.test.ts`
481 +- Modify: `extensions/leaks/src/engine.ts`
482 +- Modify: `extensions/leaks/index.ts`
484 +- [ ] **Step 1: Write dotenv/env tests**
486 +Create tests for:
488 +- only `.env`, `.env.local`, and `.env.*.local` are discovered in cwd
489 +- no recursive discovery
490 +- `export KEY=value` parses
491 +- single quotes, double quotes, and inline comments parse correctly
492 +- sensitive key plus non-weak value learns exact value
493 +- weak/common values are ignored
494 +- public prefixes suppress key-name sensitivity only
495 +- high-confidence secret shapes under public prefixes still redact
496 +- process env uses same classifier
497 +- process env labels collapse to `ENV_KEY`
498 +- status never reports env var names or values
499 +- learned exact value cap 8192 stops learning and warns once
501 +- [ ] **Step 2: Run dotenv tests and verify failure**
503 +Run: `mise run test -- extensions/leaks/__tests__/dotenv.test.ts`
504 +Expected: FAIL with missing dotenv learner.
506 +- [ ] **Step 3: Implement dotenv parser and learner**
508 +Implement fixed file discovery with `node:fs` and `node:path` only.
509 +Implement `parseDotenv(text)` with shell-compatible enough parsing for one-line assignments.
510 +Implement sensitive-key matching that is case-insensitive, treats `_` and `-` as equivalent, and allows numeric suffixes.
511 +Implement public-prefix suppression for `PUBLIC_`, `NEXT_PUBLIC_`, and `VITE_`.
512 +Implement exact value cap 8192 and warning-once behavior.
514 +- [ ] **Step 4: Wire learning into engine reload**
516 +`LeaksEngine.reload(cwd, rawSettings, env)` must load fixed dotenv files and process env.
517 +Learned values must produce exact-value spans with dotenv key tag or `ENV_KEY` tag.
518 +Credentialed URLs must be delegated to Task 6 component parsing before whole-value learning.
520 +- [ ] **Step 5: Run dotenv and placeholder tests**
522 +Run: `mise run test -- extensions/leaks/__tests__/dotenv.test.ts extensions/leaks/__tests__/placeholders.test.ts`
523 +Expected: PASS.
525 +- [ ] **Step 6: Commit dotenv/env learning**
527 +Run:
529 +```bash
530 +git add extensions/leaks/src/dotenv.ts extensions/leaks/src/engine.ts extensions/leaks/index.ts extensions/leaks/__tests__/dotenv.test.ts
531 +git commit -m "feat(leaks): learn dotenv and env secret values"
532 +```
534 +### Task 6: Structured Component Parsers
536 +**Files:**
538 +- Create: `extensions/leaks/src/structures.ts`
539 +- Create: `extensions/leaks/__tests__/structures.test.ts`
540 +- Modify: `extensions/leaks/src/dotenv.ts`
541 +- Modify: `extensions/leaks/src/engine.ts`
542 +- Modify: `extensions/leaks/index.ts`
544 +- [ ] **Step 1: Write structure parser tests**
546 +Create tests for:
548 +- credentialed URL password redacts while username, host, db name, and scheme remain visible
549 +- sensitive query parameter value redacts while parameter name remains visible
550 +- URL without credentials is not secret even for `DATABASE_URL`, `REDIS_URL`, or `MONGO_URL`
551 +- `Authorization: Bearer <value>` redacts token only
552 +- `Authorization: Basic <value>` redacts credential only
553 +- `X-API-Key: <value>` redacts value only
554 +- `Cookie: session=<value>; theme=dark` redacts sensitive cookie only
555 +- assignments in env, properties, YAML, JSON, TOML, INI, HCL, XML-ish, and shell-ish text redact value only
556 +- shell command containing a header redacts the header value without redacting whole command
557 +- JWTs remain whole-token spans
558 +- private keys remain whole logical secret spans
560 +- [ ] **Step 2: Run structure tests and verify failure**
562 +Run: `mise run test -- extensions/leaks/__tests__/structures.test.ts`
563 +Expected: FAIL with missing structure parsers.
565 +- [ ] **Step 3: Implement component parsers**
567 +Implement functions that return `SecretSpan[]` with source `structure`:
569 +- `findCredentialedUrlSpans(text)`
570 +- `findSensitiveQuerySpans(text)`
571 +- `findHeaderSpans(text)`
572 +- `findCookieSpans(text)`
573 +- `findAssignmentSpans(text)`
575 +Use `URL` where full URL parsing succeeds.
576 +For text fragments and shell snippets, use bounded regexes with match indices.
577 +Never return a whole shell command span.
579 +- [ ] **Step 4: Wire component parsers before exact learned values**
581 +Engine span collection order must add component spans before exact learned values.
582 +Dotenv/env URL values must learn parsed secret components instead of whole URL when parsing succeeds.
584 +- [ ] **Step 5: Run structure, dotenv, and selector tests**
586 +Run: `mise run test -- extensions/leaks/__tests__/structures.test.ts extensions/leaks/__tests__/dotenv.test.ts extensions/leaks/__tests__/selector.test.ts`
587 +Expected: PASS.
589 +- [ ] **Step 6: Commit structures**
591 +Run:
593 +```bash
594 +git add extensions/leaks/src/structures.ts extensions/leaks/src/dotenv.ts extensions/leaks/src/engine.ts extensions/leaks/index.ts extensions/leaks/__tests__/structures.test.ts
595 +git commit -m "feat(leaks): redact secret structure components"
596 +```
598 +### Task 7: Built-In Detectors
600 +**Files:**
602 +- Create: `extensions/leaks/src/detectors.ts`
603 +- Create: `extensions/leaks/__tests__/detectors.test.ts`
604 +- Modify: `extensions/leaks/src/engine.ts`
605 +- Modify: `extensions/leaks/index.ts`
607 +- [ ] **Step 1: Write detector corpus tests**
609 +Create positive controls for all minimum families:
611 +- AI provider keys with distinctive prefixes or key context
612 +- VCS and package-manager tokens
613 +- cloud access keys and credential-like IDs
614 +- payment, messaging, webhook, and mail service tokens
615 +- auth, OAuth, JWT, cookie, and session secrets
616 +- database and cache credentialed connection strings
617 +- private key material: PEM, OpenSSH, PGP private, encrypted cosign, PuTTY PPK private section, age secret key, minisign secret key
618 +- generic sensitive-key assignments including every key name listed in spec
620 +Create negative controls for:
622 +- arbitrary account IDs
623 +- customer IDs
624 +- org IDs
625 +- project IDs
626 +- hostnames
627 +- domains
628 +- broad opaque keys named `lineage` and `value`
629 +- SSH public keys
630 +- public certificates
632 +- [ ] **Step 2: Run detector tests and verify failure**
634 +Run: `mise run test -- extensions/leaks/__tests__/detectors.test.ts`
635 +Expected: FAIL with missing or incomplete built-in detectors.
637 +- [ ] **Step 3: Implement detector definitions**
639 +In `src/detectors.ts`, export precompiled detector objects.
640 +Each detector must define id, tag, family, regex, optional group, confidence, and whole-span kind.
641 +Precompile regexes once at module load.
642 +Avoid broad entropy-only detectors.
643 +Private key armors must redact whole logical secret values; boundary text becomes tags only.
644 +JWTs must redact whole token.
646 +- [ ] **Step 4: Wire built-ins into engine**
648 +Engine must run structure parsers, learned exact values, custom regex, and built-ins as separate sources, then selector decides overlaps.
649 +Built-in detector failures must fail open for that detector pass, warn once, and not block Pi.
651 +- [ ] **Step 5: Run detector and false-positive-related tests**
653 +Run: `mise run test -- extensions/leaks/__tests__/detectors.test.ts extensions/leaks/__tests__/structures.test.ts`
654 +Expected: PASS.
656 +- [ ] **Step 6: Commit built-ins**
658 +Run:
660 +```bash
661 +git add extensions/leaks/src/detectors.ts extensions/leaks/src/engine.ts extensions/leaks/index.ts extensions/leaks/__tests__/detectors.test.ts
662 +git commit -m "feat(leaks): add high-confidence built-in detectors"
663 +```
665 +### Task 8: False Positive Policy
667 +**Files:**
669 +- Create: `extensions/leaks/__tests__/false-positives.test.ts`
670 +- Modify: `extensions/leaks/src/safety.ts`
671 +- Modify: `extensions/leaks/src/detectors.ts`
672 +- Modify: `extensions/leaks/src/structures.ts`
673 +- Modify: `extensions/leaks/src/selector.ts`
675 +- [ ] **Step 1: Write false-positive tests**
677 +Create hand negative controls for:
679 +- branch names
680 +- package names
681 +- ordinary URLs
682 +- URLs needing semantic host/path edits
683 +- usernames
684 +- distinguished names
685 +- UUIDs
686 +- short hex strings
687 +- long non-provider hex strings without secret context
688 +- comments and defaults
689 +- endpoint labels
690 +- network ranges
691 +- environment references `$API_KEY`, `${API_KEY}`, `${{ secrets.PROD_TOKEN }}`, `process.env.API_KEY`
692 +- private names and project codenames
693 +- internal domains
694 +- SSH public key
695 +- TLS certificate block
697 +Create positive controls proving custom regex overrides negative policy and high-confidence provider-specific patterns still redact.
699 +- [ ] **Step 2: Run false-positive tests and verify failure or pass**
701 +Run: `mise run test -- extensions/leaks/__tests__/false-positives.test.ts`
702 +Expected before refinement: FAIL for any over-broad detector.
703 +Expected after refinement: PASS.
705 +- [ ] **Step 3: Tighten weak-value and reject logic**
707 +Refine `isWeakValue`, detector patterns, and selector priorities until all negatives stay visible and all positive controls redact.
708 +Keep metadata visible aggressively.
709 +Do not add user exclude lists.
711 +- [ ] **Step 4: Run detector family tests again**
713 +Run: `mise run test -- extensions/leaks/__tests__/detectors.test.ts extensions/leaks/__tests__/false-positives.test.ts`
714 +Expected: PASS.
716 +- [ ] **Step 5: Commit false-positive policy**
718 +Run:
720 +```bash
721 +git add extensions/leaks/src/safety.ts extensions/leaks/src/detectors.ts extensions/leaks/src/structures.ts extensions/leaks/src/selector.ts extensions/leaks/__tests__/false-positives.test.ts
722 +git commit -m "fix(leaks): keep metadata visible"
723 +```
725 +### Task 9: Deep Redaction, Restoration, Caps, And Failure Bounds
727 +**Files:**
729 +- Create: `extensions/leaks/__tests__/performance.test.ts`
730 +- Modify: `extensions/leaks/src/engine.ts`
731 +- Modify: `extensions/leaks/src/placeholders.ts`
732 +- Modify: `extensions/leaks/src/types.ts`
734 +- [ ] **Step 1: Write performance and traversal tests**
736 +Create tests for:
738 +- deep traversal copies objects and does not mutate originals during redaction
739 +- cyclic object traversal terminates
740 +- object identity bounds prevent repeat work
741 +- depth 64 cap warns once and skips deeper values
742 +- bigint, symbol, function, null, number, boolean pass through
743 +- arrays and plain objects are traversed
744 +- learned-value cap 8192 is enforced
745 +- placeholder map cap 8192 is enforced
746 +- operation count is bounded by input size times rule count
747 +- precompiled rule count does not grow across redaction calls
748 +- detector throw fails open for that pass and warns once
750 +- [ ] **Step 2: Run performance tests and verify failure**
752 +Run: `mise run test -- extensions/leaks/__tests__/performance.test.ts`
753 +Expected: FAIL until traversal and counters are complete.
755 +- [ ] **Step 3: Implement bounded traversal**
757 +Implement recursive clone traversal with `WeakMap<object, unknown>` for cycles and identity.
758 +Stop at depth 64.
759 +Only redact string values.
760 +Return `{ value, changed, count }` from deep redaction.
761 +Restore uses same traversal rules but replaces known placeholders.
763 +- [ ] **Step 4: Implement operation and warning bounds**
765 +Track per-pass safe operation counters if useful for tests.
766 +Use warning-once keys for cap overflow, depth overflow, regex failure, and detector failure.
767 +Never include secret values in warning messages.
769 +- [ ] **Step 5: Run full unit suite**
771 +Run: `mise run test -- extensions/leaks/__tests__/unit.test.ts extensions/leaks/__tests__/placeholders.test.ts extensions/leaks/__tests__/selector.test.ts extensions/leaks/__tests__/settings.test.ts extensions/leaks/__tests__/dotenv.test.ts extensions/leaks/__tests__/structures.test.ts extensions/leaks/__tests__/detectors.test.ts extensions/leaks/__tests__/false-positives.test.ts extensions/leaks/__tests__/performance.test.ts`
772 +Expected: PASS.
774 +- [ ] **Step 6: Commit bounded engine**
776 +Run:
778 +```bash
779 +git add extensions/leaks/src/engine.ts extensions/leaks/src/placeholders.ts extensions/leaks/src/types.ts extensions/leaks/__tests__/performance.test.ts
780 +git commit -m "feat(leaks): bound redaction traversal and caps"
781 +```
783 +### Task 10: Pi Provider Boundary Hooks
785 +**Files:**
787 +- Create: `extensions/leaks/src/pi.ts`
788 +- Replace: `extensions/leaks/__tests__/harness.test.ts`
789 +- Modify: `extensions/leaks/index.ts`
791 +- [ ] **Step 1: Write harness tests for hooks**
793 +Replace harness tests with cases for:
795 +- extension loads in real Pi runtime
796 +- `session_start` initializes settings, dotenv/env learning, status
797 +- `context` redacts message copies and does not mutate originals
798 +- `before_provider_request` redacts final provider payload authoritatively
799 +- provider authentication headers remain present
800 +- `tool_call` restores placeholders in mutable tool args before local execution
801 +- `message_end` restores placeholders in finalized assistant messages
802 +- no mappings are persisted through session entries
803 +- no tools are registered by leaks
804 +- only slash command `leaks` is registered
806 +- [ ] **Step 2: Run harness tests and verify failure**
808 +Run: `mise run test -- extensions/leaks/__tests__/harness.test.ts`
809 +Expected: FAIL until Pi adapter exists.
811 +- [ ] **Step 3: Implement Pi adapter**
813 +In `src/pi.ts`, default-export a factory taking `ExtensionAPI`.
814 +Create one `LeaksEngine` per extension instance.
815 +Register handlers for `session_start`, `context`, `before_provider_request`, `tool_call`, and `message_end`.
816 +Do not call `appendEntry`.
817 +Do not register tools.
818 +Register command `leaks` only.
819 +Use `ctx.cwd` for dotenv discovery.
820 +Use settings from the runtime settings API when available and fallback to `.pi/settings.json` only if the current project is trusted or the harness has no settings API.
822 +- [ ] **Step 4: Replace `index.ts`**
824 +`index.ts` must import the factory from `src/pi.ts`, export it as default, and export `__test` internals used by tests.
826 +- [ ] **Step 5: Run harness tests**
828 +Run: `mise run test -- extensions/leaks/__tests__/harness.test.ts`
829 +Expected: PASS.
831 +- [ ] **Step 6: Commit Pi hooks**
833 +Run:
835 +```bash
836 +git add extensions/leaks/src/pi.ts extensions/leaks/index.ts extensions/leaks/__tests__/harness.test.ts
837 +git commit -m "feat(leaks): enforce provider-bound redaction hooks"
838 +```
840 +### Task 11: UX, Status, Commands, And Safe Diagnostics
842 +**Files:**
844 +- Create: `extensions/leaks/__tests__/ux.test.ts`
845 +- Modify: `extensions/leaks/src/pi.ts`
846 +- Modify: `extensions/leaks/src/engine.ts`
847 +- Modify: `extensions/leaks/src/types.ts`
849 +- [ ] **Step 1: Write UX tests**
851 +Create tests for:
853 +- one aggregate notification per provider turn with new leak values
854 +- no notification for a clean provider turn
855 +- footer/status shows enabled/on and last provider redaction count
856 +- `/leaks status` shows all required fields
857 +- `/leaks status` includes invalid regex count
858 +- `/leaks status` shows recent safe redaction labels
859 +- `/leaks status` shows latest safe placeholder tags only
860 +- process environment key names display only as `ENV_KEY`
861 +- `/leaks reload` reloads settings and dotenv/env learning
862 +- `/leaks test <text>` prints redacted diagnostic without secret values
863 +- `/leaks clear` asks for confirmation when UI can confirm
864 +- `/leaks clear` respects cancel
865 +- `/leaks clear` clears placeholder mappings when confirmed
867 +- [ ] **Step 2: Run UX tests and verify failure**
869 +Run: `mise run test -- extensions/leaks/__tests__/ux.test.ts`
870 +Expected: FAIL until command output is complete.
872 +- [ ] **Step 3: Implement status model**
874 +Engine stats must expose:
876 +- active state
877 +- built-in rule count
878 +- custom regex count
879 +- regex warning/error count
880 +- learned dotenv/env value count
881 +- placeholder mapping count
882 +- last provider redaction count
883 +- total provider redaction count
884 +- recent safe redaction labels
885 +- latest safe placeholder tags
886 +- invalid regex count
888 +- [ ] **Step 4: Implement command handlers**
890 +`/leaks status` formats a safe multi-line report.
891 +`/leaks reload` reloads settings and learning from current cwd.
892 +`/leaks test <text>` redacts the supplied local diagnostic text and reports counts, labels, and redacted output; missed secrets are possible by definition.
893 +`/leaks clear` confirms when `ctx.hasUI`, clears mappings and volatile restore state on confirmation, and reports cancellation otherwise.
895 +- [ ] **Step 5: Implement footer/status notification updates**
897 +On `session_start`, set status to `leaks on | last 0`.
898 +After each provider redaction, update status and notify once if count > 0.
899 +Use warning severity only for faulty settings or detector failures; use info for reload/clear/test success.
901 +- [ ] **Step 6: Run UX and harness tests**
903 +Run: `mise run test -- extensions/leaks/__tests__/ux.test.ts extensions/leaks/__tests__/harness.test.ts`
904 +Expected: PASS.
906 +- [ ] **Step 7: Commit UX**
908 +Run:
910 +```bash
911 +git add extensions/leaks/src/pi.ts extensions/leaks/src/engine.ts extensions/leaks/src/types.ts extensions/leaks/__tests__/ux.test.ts
912 +git commit -m "feat(leaks): add safe status and commands"
913 +```
915 +### Task 12: End-To-End Validation Batches V0-V9
917 +**Files:**
919 +- Modify: `extensions/leaks/__tests__/ledger.test.ts`
920 +- Create: `extensions/leaks/__tests__/validation-batches.test.ts`
921 +- Modify: `extensions/leaks/SPEC.md` only if new evidence records are explicitly desired by the user.
923 +- [ ] **Step 1: Write validation batch test wrapper**
925 +Create `validation-batches.test.ts` that groups existing focused tests by V0-V9 labels and asserts each batch has at least one positive and one negative control where the spec requires controls.
926 +Do not duplicate every fixture; import fixture builders from existing test files only if those builders are exported without secrets in names.
928 +- [ ] **Step 2: Run validation batch tests**
930 +Run: `mise run test -- extensions/leaks/__tests__/ledger.test.ts extensions/leaks/__tests__/validation-batches.test.ts`
931 +Expected: PASS.
933 +- [ ] **Step 3: Run all leaks tests**
935 +Run: `mise run test -- extensions/leaks/__tests__`
936 +Expected: PASS.
938 +- [ ] **Step 4: Commit validation batches**
940 +Run:
942 +```bash
943 +git add extensions/leaks/__tests__/ledger.test.ts extensions/leaks/__tests__/validation-batches.test.ts
944 +git commit -m "test(leaks): map spec validation batches"
945 +```
947 +### Task 13: README And User Documentation
949 +**Files:**
951 +- Replace: `extensions/leaks/README.md`
952 +- Modify: `extensions/leaks/package.json` only if description or keywords no longer match.
953 +- Test: markdown lint via repo lint task.
955 +- [ ] **Step 1: Rewrite README**
957 +README must include:
959 +- mission: provider-bound plaintext redaction
960 +- threat model and non-goals
961 +- transparent local behavior and placeholder tradeoff
962 +- placeholder examples using fake values only
963 +- settings JSON with only `regex`
964 +- custom regex label rules
965 +- commands: `/leaks status`, `/leaks reload`, `/leaks test <text>`, `/leaks clear`
966 +- dotenv/env learning rules
967 +- false-positive policy
968 +- limitations for images, audio, binary, OCR, semantic secret-byte edits, DLP, sandboxing, permissions, repo scanning, and git history cleanup
969 +- no secret values in diagnostics guarantee
971 +- [ ] **Step 2: Run README generation check only if repo requires it**
973 +Run: `mise run readme -- leaks`
974 +Expected: README remains semantically aligned with manual content or generator updates it without adding unsupported settings.
975 +If generator overwrites critical spec content, stop and ask human because build tooling conflicts with the spec.
977 +- [ ] **Step 3: Run markdown lint through repo lint**
979 +Run: `mise run lint`
980 +Expected: PASS.
982 +- [ ] **Step 4: Commit README**
984 +Run:
986 +```bash
987 +git add extensions/leaks/README.md extensions/leaks/package.json
988 +git commit -m "docs(leaks): document spec-shaped redaction"
989 +```
991 +### Task 14: Final Integration Gate
993 +**Files:**
995 +- Modify: any leaks files touched by failing checks.
997 +- [ ] **Step 1: Run focused leaks tests**
999 +Run: `mise run test -- extensions/leaks/__tests__`
1000 +Expected: PASS.
1002 +- [ ] **Step 2: Run static extension checks**
1004 +Run: `mise run check`
1005 +Expected: PASS.
1007 +- [ ] **Step 3: Run full CI**
1009 +Run: `mise run ci`
1010 +Expected: PASS.
1012 +- [ ] **Step 4: Search for plan-forbidden placeholders in implementation**
1014 +Run: `grep -R "TO[D]O\|TB[D]\|secretFiles\|keywords\|exclude\|enabled" extensions/leaks --exclude=SPEC.md`
1015 +Expected: no matches for unfinished-marker words or unsupported settings in runtime code; README may mention rejected settings only as rejected.
1017 +- [ ] **Step 5: Verify mappings are not persisted**
1019 +Run: `grep -R "appendEntry\|sessionManager.*append\|writeFile.*LEAK\|localStorage\|sessionStorage" extensions/leaks --exclude=SPEC.md`
1020 +Expected: no runtime persistence of placeholder mappings.
1022 +- [ ] **Step 6: Verify secret-safe diagnostics by test names**
1024 +Run: `mise run test -- extensions/leaks/__tests__/ux.test.ts extensions/leaks/__tests__/settings.test.ts extensions/leaks/__tests__/performance.test.ts`
1025 +Expected: PASS with tests that assert fixture secret strings are absent from diagnostics.
1027 +- [ ] **Step 7: Final commit**
1029 +Run:
1031 +```bash
1032 +git status --short
1033 +git commit --allow-empty -m "chore(leaks): complete spec implementation gate"
1034 +```
1036 +Use the empty commit only if all implementation commits already captured file changes.
1038 +## Normative Requirement Ledger
1040 +| Spec area | Requirement group | V batch | Plan coverage |
1041 +| ------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- | ---------------------------------------------------------------------------------------------------------------------- |
1042 +| Authority | Treat `extensions/leaks/SPEC.md` as source of truth and current implementation as obsolete reference. | V0 | Task 0 locks spec ledger; Task 1 replaces obsolete settings/runtime assumptions; Task 14 checks rejected legacy names. |
1043 +| Authority | Use a fresh spec-shaped implementation; overwrite `index.ts`, tests, and README in place; do not create `extensions/leaks-ref`. | V0 | File Structure and Tasks 1, 10, 13 replace files in place and never add a reference extension directory. |
1044 +| Mission | Protect provider-bound plaintext while preserving provider auth headers and selected provider call ability. | V1 | Task 10 tests authoritative `before_provider_request` redaction and provider auth preservation. |
1045 +| Mission | Keep local Pi session and tool behavior unredacted except exact placeholder round trips. | V1/V2 | Tasks 2, 9, and 10 implement restore maps, `tool_call` restore, and `message_end` restore. |
1046 +| Mission and Non-goals | Exclude exfiltration guard, sandboxing, DLP, permissions, repo scanning, git cleanup, binary/OCR/image/audio redaction, and homomorphic or symbolic secret-byte editing. | V0 | Tasks 1, 13, and 14 document and check excluded scope; no tools or permission gates are registered. |
1047 +| Core requirements | Normal Pi workflows remain transparent by default; local visible/session behavior preserves original values where possible. | V1/V8 | Tasks 10 and 11 test context copy redaction, local restoration, and non-spam UX. |
1048 +| Core requirements | Run every provider turn and touch user/tool/assistant text fields; context redaction is opportunistic and provider payload redaction authoritative. | V1 | Task 10 wires `context` and `before_provider_request` and tests provider-bound payloads. |
1049 +| Core requirements | Bound hot path by input size, rule count, match count, mapping caps, object identity, and depth 64; avoid O(n²); precompile regexes. | V9 | Tasks 7 and 9 precompile rules and test traversal/cap/performance bounds. |
1050 +| Core requirements | Faulty user regex and detector failures must warn, fail open, and never prevent Pi from loading. | V3/V9 | Tasks 4 and 9 test unsafe input rejection, warning counters, and detector failure-open behavior. |
1051 +| Core requirements | UX must never print classified or learned secret values; local restoration may show original values only because Pi already had them. | V8 | Task 11 status/command tests assert fixture secrets and env names are absent. |
1052 +| Core model | Provider sees placeholders; local Pi keeps mappings in memory only; reload/new session/exit/clear can make placeholders unrestorable. | V2/V8 | Tasks 2, 10, 11, and 14 implement volatile maps, no `appendEntry`, and `/leaks clear`. |
1053 +| Core model | Placeholder format uses tags, HMAC12, random in-memory key, collision suffix, stable same-value mapping, frozen tags, and overflow non-restorable placeholders. | V2 | Task 2 tests format, HMAC, stability, tag ordering/caps, collision suffix, and overflow. |
1054 +| Core model | Tags derive from dotenv/env, custom labels, built-ins, structure components, and service/type labels, with semantic priority and caps. | V2/V5/V6 | Tasks 2, 5, 6, and 7 generate tags from every source and verify ordering. |
1055 +| Core model | Select component spans before enclosing exact learned values, exact before generic regexes, smallest secret-bearing overlap, private key/JWT whole spans, and never replace inside existing placeholders. | V2/V6/V7 | Tasks 3, 6, 7, and 8 test selector behavior and false-positive policy. |
1056 +| Pi integration | Use `session_start`, `context`, `before_provider_request`, `tool_call`, and `message_end`; do not persist mappings or register tools; register only `/leaks`. | V1/V8 | Task 10 harness tests all hooks plus no persistence/no tools/one command. |
1057 +| Settings | Accept only `leaks.regex` with `pattern` and `label`; reject `flags` and unsupported fields; cap pattern and label lengths. | V3 | Task 4 settings tests cover accepted and rejected settings. |
1058 +| Settings | Reject cheap unsafe regex constructs; faulty settings warn without breaking startup or provider requests. | V3/V9 | Task 4 implements heuristic rejection; Task 9 tests failure-open behavior. |
1059 +| Settings | Custom regex capture semantics: zero captures full match, one capture group 1, multiple group 1 with warning; labels must match `[A-Z0-9_]{1,64}` or fall back to `CUSTOM_REGEX`. | V3 | Task 4 tests capture counts and label fallback. |
1060 +| Built-ins | Always active high-confidence detector families cover AI, VCS/package, cloud, payment/messaging/webhook/mail, auth/OAuth/JWT/cookie/session, DB/cache URLs, private keys, and sensitive assignments. | V4 | Task 7 has positive controls for each minimum detector family. |
1061 +| Built-ins | Credential-like IDs redact only for reliable provider-specific patterns; arbitrary account/customer/org/project IDs, hostnames, and domains stay visible. | V4/V7 | Tasks 7 and 8 include explicit negative controls. |
1062 +| Dotenv/env | Learn exact values from fixed cwd files `.env`, `.env.local`, `.env.*.local`; no recursive discovery or user-configured file list. | V5 | Task 5 discovery tests fixed file behavior. |
1063 +| Dotenv/env | Process env uses same classifier; status collapses env key names to `ENV_KEY`; values and names never print. | V5/V8 | Tasks 5 and 11 test env learning and UX collapse. |
1064 +| Dotenv/env | Sensitive keys are case-insensitive with `_`/`-` equivalence and numeric suffixes; public prefixes suppress key-name sensitivity only; weak/common values ignored. | V5/V7 | Tasks 5 and 8 cover key classifier and weak metadata negatives. |
1065 +| Dotenv/env and structures | Credentialed URL, query, header, cookie, and assignment parsing redacts only secret components while preserving safe structure. | V6/V7 | Task 6 structure parser tests cover URL/header/cookie/assignment components and shell snippets. |
1066 +| Dotenv/env and structures | JWTs and private key material redact as whole logical secrets; public keys and certificates remain metadata. | V6/V7 | Tasks 6, 7, and 8 include whole-span positives and public-material negatives. |
1067 +| False positives | Metadata is leaked aggressively; no entropy-only redaction; names, hosts, domains, ordinary URLs, references, IDs, codenames, and internal domains remain visible unless custom regex matches. | V7 | Task 8 negative corpus and custom-regex override tests enforce this policy. |
1068 +| UX and commands | Notify once per redacting provider turn, not per secret, and no notification for clean turns. | V8 | Task 11 UX tests aggregate notifications. |
1069 +| UX and commands | `/leaks status` reports active state, rule counts, warning/error counts, learned count, mapping count, last/total counts, recent labels, latest tags, and invalid regex count without secret values. | V8 | Task 11 implements and tests the full status model. |
1070 +| UX and commands | `/leaks reload`, `/leaks test <text>`, and `/leaks clear` operate safely; clear respects cancellation. | V8 | Task 11 command tests cover reload/test/clear and confirmation behavior. |
1071 +| Validation ledger | Every validation batch records command, fixture class, and result; detector batches have positive and negative controls; demo corpus evidence reports only paths/key names/labels/classes/counts. | V0/V4/V7 | Tasks 0 and 12 test ledger presence and batch mapping; Tasks 7 and 8 provide controls. |
1073 +## Reviewer Checklist
1075 +- V0 scope: spec authority honored; obsolete prototype treated as reference only; non-goals excluded.
1076 +- V1 provider boundary: both `context` and `before_provider_request`; provider auth preserved; local restore hooks present.
1077 +- V2 placeholders: HMAC, tags, caps, collision suffix, restore, stability, no persistence.
1078 +- V3 settings: only `regex`; unsafe patterns rejected; warnings safe.
1079 +- V4 built-ins: minimum detector families covered by positive and negative controls.
1080 +- V5 learned values: fixed dotenv files and process env classifier; caps; env key collapse.
1081 +- V6 component parsers: URL/header/cookie/assignment components preserve safe structure.
1082 +- V7 false positives: metadata remains visible; no entropy-only redaction.
1083 +- V8 UX: safe status, reload, test, clear, aggregate notifications.
1084 +- V9 performance/failure: precompile, cap, depth bound, cycle safety, fail-open warnings.

extensions/leaks/SPEC.md

Added
new file mode 100644
1 +# leaks spec
3 +## Authority
5 +This spec is the source of truth for future `leaks` work.
6 +The current implementation is a prototype/reference.
7 +When code and this spec disagree, change code to match this spec unless the user explicitly updates the spec first.
9 +Implementation must be fresh and spec-shaped.
10 +Do not preserve legacy structure for compatibility.
11 +Practical path: overwrite `extensions/leaks/index.ts`, tests, and README in place instead of moving the extension directory.
12 +Git history is the reference for old code.
13 +Avoid creating `extensions/leaks-ref` because extension checks may treat it as a real extension.
15 +## Mission
17 +Protect provider-bound plaintext exposure.
19 +`leaks` redacts conversation, tool, and session text before model/provider requests.
20 +It does not redact provider authentication headers or API keys needed to call the selected provider.
21 +It keeps local Pi session and tool behavior unredacted except for placeholder round trips.
22 +It is not an exfiltration guard, sandbox, DLP layer, or permission system.
23 +Tool-risk control belongs to Pi permissions, sandboxing, and tool policy.
25 +## Core requirements
27 +Normal Pi workflows must be unhindered.
28 +`leaks` is transparent by default.
29 +Local user-visible/session behavior must preserve original values unless provider-bound text needs placeholders.
30 +The known exception is semantic transformation of secret internals: the model cannot edit bytes it cannot see.
31 +Copying, moving, renaming keys around, or preserving placeholders exactly must keep working.
33 +`leaks` runs on every provider turn and can touch every user/tool/assistant text field.
34 +Image, audio, binary, and OCR redaction are out of scope.
35 +Hot path work must be bounded by input size, rule count, and match count.
36 +Avoid O(n²) traps in hot paths.
37 +Precompile regexes.
38 +Bound learned exact values and restorable placeholder mappings to 8192 each.
39 +Bound deep traversal by object identity and depth 64.
40 +On learned-value cap overflow, stop learning new dotenv/env exact values and warn once.
41 +On restorable-mapping cap overflow, still redact provider-bound secrets with non-restorable placeholders, warn once, and keep existing mappings stable.
43 +`leaks` must tolerate faulty user input.
44 +The only planned user input is the `regex` setting.
45 +Faulty input must fall back and warn.
46 +At worst, ignore faulty user input plus warning.
47 +`leaks` must never prevent Pi from loading.
48 +If detector logic fails at runtime, fail open for that pass, warn, and continue.
50 +`leaks` extension UX must never print values it classified or learned as secrets.
51 +This applies to notifications, status output, errors, logs, and diagnostics.
52 +Normal local session/tool restoration may show original values because local Pi already had them.
53 +`/leaks test` is a local diagnostic and may show false negatives because missed secrets are unknown to `leaks`.
55 +The provider boundary is the hard gate.
56 +Context redaction is opportunistic.
57 +Provider payload redaction is authoritative.
58 +Redaction must run on context copies and again on provider payloads.
59 +Provider serialization may reshape or inject text after context processing.
61 +## Core model
63 +Provider sees placeholders.
64 +Local Pi keeps placeholder mappings in memory only.
65 +Mappings must never be persisted to session files, logs, settings, or disk.
66 +Reload, new session, process exit, and `/leaks clear` may make old placeholders unrestorable.
67 +Local restoration may happen for tool args and final assistant messages.
68 +Placeholders are local capabilities.
69 +Normal Pi workflows must keep working locally when placeholders are copied or moved exactly.
71 +Placeholder format:
73 +```text
74 +__LEAK_<TAG>[_<TAG>...]_<HMAC12>[_N]__
75 +```
77 +`HMAC12` is the first 12 lowercase hex chars of an HMAC over the secret value.
78 +The HMAC key is random, in-memory, and never persisted.
79 +`_N` is used only to resolve an actual placeholder collision inside the current mapping table.
81 +Tags preserve metadata.
82 +Secret values stay hidden.
83 +Metadata is not secret for this extension's threat model.
85 +Tag sources:
87 +- dotenv key names
88 +- `ENV_KEY` for process environment values
89 +- custom regex labels
90 +- built-in detector names
91 +- structure component labels
92 +- service/type labels
94 +The same secret value must keep one stable placeholder once first seen and stored.
95 +Tags do not evolve after first mapping.
96 +This avoids confusing the model with multiple placeholders for one secret.
97 +Non-restorable overflow placeholders are exempt from stability.
98 +When multiple tags are available for the same secret span on first mapping, order them by semantic priority:
100 +1. dotenv key name or `ENV_KEY`
101 +2. structure component
102 +3. custom regex label
103 +4. built-in type
104 +5. generic class
106 +Match selection:
108 +- merge labels for identical spans before placeholder creation
109 +- select component parsers before enclosing exact learned values
110 +- select exact learned values before generic regexes
111 +- for overlapping non-identical spans, keep the smallest secret-bearing span unless a higher-confidence rule requires a larger span
112 +- private key blocks and JWTs are intentionally whole-span rules
113 +- never replace text inside an existing placeholder
115 +Cap tags to 6 per placeholder.
116 +Cap each tag to 64 characters after sanitization.
117 +The 8192 restorable-mapping cap is the chosen memory bound.
119 +Examples:
121 +```text
122 +__LEAK_STRIPE_LIVE_SECRET_KEY_STRIPE_TOKEN_8f31aa77c901__
123 +__LEAK_OPENROUTER_API_KEY_OPENROUTER_TOKEN_41c0e9aa1204__
124 +__LEAK_DATABASE_URL_DB_PASSWORD_POSTGRES_URL_ba29d4ff8810__
125 +```
127 +Do not encode secret length, alphabet, prefix bytes, suffix bytes, entropy, or shape.
128 +Do not provide homomorphic encryption, symbolic transform tools, or secret-byte edit algebra for now.
129 +The accepted tradeoff is that the model cannot inspect or transform secret internals.
130 +Users enabling leaks cannot rely on the model to semantically edit secret values.
131 +Copying, moving, renaming keys around, or preserving placeholders exactly must remain supported.
133 +## Pi integration strategy
135 +Use these Pi hooks:
137 +- `session_start`: initialize settings, dotenv/env learning, status
138 +- `context`: redact message copies opportunistically
139 +- `before_provider_request`: redact final provider payload authoritatively
140 +- `tool_call`: restore placeholders in tool args before local execution
141 +- `message_end`: restore placeholders in finalized local assistant messages
143 +Do not persist mappings with `appendEntry`.
144 +Do not register tools.
145 +Use one slash command: `/leaks`.
147 +## Settings
149 +Planned settings surface:
151 +```json
153 + "leaks": {
154 + "regex": [{ "pattern": "foo_[A-Za-z0-9]{32}", "label": "FOO_TOKEN" }]
157 +```
159 +Allowed setting:
161 +- `regex`: custom org/project token patterns.
163 +Only `pattern` and `label` are accepted.
164 +`flags` is not accepted; unsupported fields warn and are ignored.
165 +Pattern length is capped at 512 characters.
166 +Label length is capped at 64 characters.
167 +Unsafe patterns are ignored with a warning.
168 +Safety validation must reject obvious catastrophic constructs: backreferences, lookarounds, nested quantifiers, quantified alternation groups, empty alternatives, and dot-star catchalls.
169 +This is not a theorem prover; cheap rejection beats hanging Pi.
171 +Custom regex semantics:
173 +- zero capture groups redact full match
174 +- one capture group redacts group 1
175 +- multiple capture groups redact group 1 and warn
177 +Faulty custom regex input must not break Pi startup or provider requests.
178 +It must produce minimal, visible UX:
180 +- count warnings/errors in `/leaks status`
181 +- list safe warning/error text when possible
182 +- do not print secret values
183 +- include settings file and line only when cheap and reliable
184 +- otherwise omit location
186 +Regex warning/error examples:
188 +- missing label
189 +- invalid regex syntax
190 +- unsupported fields such as `flags`
191 +- too many capture groups
192 +- empty label after sanitization
193 +- pattern too broad or unsafe by cheap validation
195 +Custom regex entries without labels still load with fallback tag `CUSTOM_REGEX`, but they warn.
196 +Metadata is a goal; unlabeled regex is bad UX.
197 +Custom regex labels must already match `[A-Z0-9_]{1,64}`.
198 +Invalid labels warn and fall back to `CUSTOM_REGEX`.
199 +Raw labels are never preserved in placeholders.
201 +Rejected settings:
203 +- `enabled`: no project-level off switch for provider protection.
204 +- `keywords`: regex subsumes it.
205 +- `exclude`: false positives must be solved by smarter detection, not user patch lists.
206 +- `secretFiles`: misleading name and wrong abstraction.
208 +## Built-in detection
210 +Built-in token rules are always active.
211 +They cover these minimum detector families:
213 +- AI provider keys with distinctive prefixes or key context
214 +- VCS and package-manager tokens
215 +- cloud access keys and credential-like IDs
216 +- payment, messaging, webhook, and mail service tokens
217 +- auth, OAuth, JWT, cookie, and session secrets
218 +- database and cache credentialed connection strings
219 +- private key material listed below
220 +- generic sensitive-key assignments with non-weak values, including `KEY`, `KEY_DATA`, `PASS`, `PAT`, `PASSPHRASE`, `PEPPER`, `LICENSE_KEY`, `MASTER_KEY`, `ROUTING_KEY`, `ENCRYPTION_KEY`, `SECRET_KEY_BASE`, `SEED`, `UNSEAL_KEY`, and `MNEMONIC` key names
222 +Redact credential-like IDs such as AWS access key IDs when a reliable provider-specific pattern exists.
223 +Do not redact arbitrary account IDs, customer IDs, org IDs, project IDs, hostnames, or domains.
225 +## Dotenv and environment values
227 +Dotenv discovery gives exact-match memory for bare secret values later in logs or prompts.
229 +Example:
231 +```env
232 +PASSWORD=horse-battery-correct
233 +```
235 +Later text:
237 +```text
238 +auth failed for horse-battery-correct
239 +```
241 +The bare value may not match token-shaped rules.
242 +The dotenv source teaches `leaks` to redact it exactly.
244 +Dotenv source files are fixed and automatic:
246 +- `.env`
247 +- `.env.local`
248 +- `.env.*.local`
250 +No recursive dotenv discovery.
251 +No user-configured file list.
252 +The old `secretFiles` name is misleading because parsing is dotenv-specific.
254 +Process environment variables must use the same classifier as dotenv values.
255 +Do not report process environment variable names or values in status output.
257 +A dotenv/env value is learned for redaction only when:
259 +- key name is sensitive and value is not weak/common safe material, or
260 +- value shape is high-confidence secret material.
262 +Credentialed URL dotenv/env values are parsed before learning.
263 +When parsing succeeds, learn/redact parsed secret components, not the whole URL.
264 +When parsing fails, fall back to exact-value learning only if the value is non-weak.
265 +Credentialed URL redaction must preserve URL structure and redact only parsed secret components.
266 +URL usernames are metadata, not secrets.
267 +URL passwords are secrets.
268 +Query parameter names are metadata.
269 +Sensitive query parameter values are secrets and must be redacted in place.
270 +HTTP header names are metadata.
271 +Sensitive header values are secrets and must be redacted in place.
272 +Cookie names are metadata.
273 +Sensitive cookie values are secrets and must be redacted in place.
274 +Structured config formats are not parsed generically.
275 +There are too many formats to support cleanly.
276 +Do use common one-line assignment detectors across env, properties, YAML, JSON, TOML, INI, HCL, XML-ish text, and shell-ish text.
277 +Redact the value only when the key is sensitive and the value is not weak/common safe material, or when the value is high-confidence secret material.
278 +Shell commands are not parsed as shell.
279 +Header, query-param, URL, and assignment detectors must catch common secret components inside shell text.
280 +Do not redact whole shell commands just because they contain a secret.
281 +JWTs are redacted as whole tokens.
282 +JWT header/payload decoding is out of scope.
283 +Private key material is redacted as whole logical secrets.
284 +Covered private-key shapes include PEM/OpenSSH/PGP private key blocks, encrypted cosign key blocks, PuTTY PPK private sections, age secret keys, and minisign secret keys.
285 +Boundary/comment metadata becomes placeholder tags instead of being preserved literally.
286 +Private key armors are redacted as whole secret values; literal boundaries are not preserved.
287 +Public keys and public certificates are metadata, not secrets.
288 +Do not redact SSH public keys or certificate blocks unless a private-key-looking block is present.
289 +URLs without credentials are not secret just because their key is `DATABASE_URL`, `REDIS_URL`, or `MONGO_URL`.
291 +Sensitive key matching is case-insensitive, treats `_` and `-` as equivalent separators, and allows numeric suffixes such as `_1`.
293 +Sensitive key examples:
295 +- `TOKEN`
296 +- `SECRET`
297 +- `PASSWORD`
298 +- `PASSWD`
299 +- `PASS`
300 +- `PEPPER`
301 +- `API_KEY`
302 +- `ACCESS_KEY`
303 +- `PRIVATE_KEY`
304 +- `KEY`
305 +- `KEY_DATA`
306 +- `PAT`
307 +- `PASSPHRASE`
308 +- `LICENSE_KEY`
309 +- `MASTER_KEY`
310 +- `ROUTING_KEY`
311 +- `ENCRYPTION_KEY`
312 +- `SECRET_KEY_BASE`
313 +- `SEED`
314 +- `UNSEAL_KEY`
315 +- `MNEMONIC`
316 +- `CLIENT_SECRET`
317 +- `AUTH`
318 +- `CREDENTIAL`
319 +- `SESSION`
320 +- `COOKIE`
321 +- `JWT`
322 +- `DATABASE_URL`
323 +- `MONGO_URL`
324 +- `REDIS_URL`
326 +Public key prefixes must not create exact-match secrets by key name alone:
328 +- `PUBLIC_`
329 +- `NEXT_PUBLIC_`
330 +- `VITE_`
332 +These prefixes suppress only key-name sensitivity.
333 +High-confidence secret value shapes under public prefixes are still redacted.
335 +Weak/common values are ignored.
336 +Examples:
338 +- booleans
339 +- nullish words
340 +- plain numbers
341 +- short strings
342 +- ordinary hostnames
343 +- ordinary URLs without credentials
344 +- usernames and distinguished names
345 +- comments, defaults, endpoint labels, and network ranges
346 +- IDs unless they match a credential-like provider-specific pattern
348 +Bare high-entropy strings are visible unless:
350 +- they match a high-confidence built-in secret shape, or
351 +- they were learned from a sensitive dotenv/env source.
353 +Environment and CI secret references are metadata, not secret values.
354 +Do not redact references such as `$API_KEY`, `${API_KEY}`, `${{ secrets.PROD_TOKEN }}`, or `process.env.API_KEY`.
355 +Private names, customer names, project codenames, hostnames, and internal domains are metadata, not secret values.
356 +Do not redact them with built-ins.
357 +Use custom regex only if the user explicitly wants that behavior.
359 +## False positives
361 +False positives matter because the model loses semantic content.
362 +Transparent restore works only when the model copies the placeholder exactly.
363 +If the model must reason about or transform the redacted value, placeholder opacity hurts.
365 +Examples:
367 +- comparing two URLs
368 +- editing one component of a hostname/path
369 +- explaining a branch name or package name
370 +- preserving hostname but changing port
372 +For real secrets, semantic opacity is acceptable.
373 +For normal values, it is damage.
374 +Detection must stay high-confidence.
376 +Mitigation strategy:
378 +- leak metadata aggressively
379 +- protect secret values only
380 +- do not redact bare entropy without secret context
381 +- select the smallest secret-bearing span
382 +- preserve safe surrounding structure
383 +- redact full enclosing value only when no safe component parser exists
385 +Examples:
387 +```text
388 +DATABASE_URL=postgres://alice:pw@db/app
389 +```
391 +Provider output:
393 +```text
394 +DATABASE_URL=postgres://alice:__LEAK_DATABASE_URL_DB_PASSWORD_0123abcdef45__@db/app
395 +```
397 +Not:
399 +```text
400 +DATABASE_URL=__LEAK_DATABASE_URL_0123abcdef45__
401 +```
403 +For URL query params:
405 +```text
406 +https://api.example.com/callback?token=abc123&user=me
407 +```
409 +Provider output:
411 +```text
412 +https://api.example.com/callback?token=__LEAK_QUERY_TOKEN_0123abcdef45__&user=me
413 +```
415 +For HTTP headers:
417 +```text
418 +Authorization: Bearer abc123
419 +Authorization: Basic dXNlcjpwYXNz
420 +Cookie: session=xyz; theme=dark
421 +X-API-Key: key123
422 +```
424 +Provider output:
426 +```text
427 +Authorization: Bearer __LEAK_AUTHORIZATION_BEARER_0123abcdef45__
428 +Authorization: Basic __LEAK_AUTHORIZATION_BASIC_0123abcdef45__
429 +Cookie: session=__LEAK_COOKIE_SESSION_0123abcdef45__; theme=dark
430 +X-API-Key: __LEAK_X_API_KEY_0123abcdef45__
431 +```
433 +For JWTs:
435 +```text
436 +eyJ... .eyJ... .signature
437 +```
439 +Provider output:
441 +```text
442 +__LEAK_JWT_0123abcdef45__
443 +```
445 +For private keys:
447 +```text
448 +-----BEGIN OPENSSH PRIVATE KEY-----
449 +...
450 +-----END OPENSSH PRIVATE KEY-----
451 +```
453 +Provider output:
455 +```text
456 +__LEAK_OPENSSH_PRIVATE_KEY_0123abcdef45__
457 +```
459 +False-positive policy summary:
461 +- redact secret values only
462 +- select high-confidence built-ins before generic detectors
463 +- learn exact values from sensitive dotenv/env sources
464 +- use component parsers for URLs, query params, headers, cookies, and assignments
465 +- never redact by entropy alone
466 +- never redact names, hosts, domains, IDs, or references unless a high-confidence provider-specific pattern or custom regex matches
468 +## UX and commands
470 +`leaks` runs in the background, so human UX must be explicit.
471 +Do not spam per secret.
472 +Notify once per provider turn with an aggregate summary when new leak values are found.
473 +Show safe metadata only.
474 +Footer/status must show enabled/off plus last provider redaction count.
476 +Commands remain operational controls and diagnostics:
478 +- `/leaks status`
479 +- `/leaks reload`
480 +- `/leaks test <text>`
481 +- `/leaks clear`
483 +`/leaks status` is the full report.
484 +It must never print secret values.
485 +It must show:
487 +- active state
488 +- built-in rule count
489 +- custom regex count
490 +- regex warning/error count
491 +- learned dotenv/env value count
492 +- placeholder mapping count
493 +- last provider redaction count
494 +- total provider redaction count
495 +- recent safe redaction labels
496 +- latest safe placeholder tags, not secret values
498 +Process environment key names must be collapsed to `ENV_KEY` in status.
499 +`/leaks status` must report invalid regex count.
501 +## Validation ledger
503 +Every normative sentence in this spec belongs to one validation batch:
505 +| Batch | Spec sections | Evidence required |
506 +| ---------------------- | ------------------------------------------------ | --------------------------------------------------------------------- |
507 +| V0 scope | Authority, Mission, Validation ledger, Non-goals | Pi docs plus spec review |
508 +| V1 provider boundary | Core requirements, Pi integration strategy | Pi docs plus temp Pi extension with fake provider |
509 +| V2 placeholders | Core model | JS prototype for stability, HMAC length, tags, caps, overlap, restore |
510 +| V3 settings | Settings | JS prototype for parsing, regex safety, capture counting, warnings |
511 +| V4 built-ins | Built-in detection | Detector corpus tests with positive and negative fixtures |
512 +| V5 learned values | Dotenv and environment values | Dotenv/env parser prototype and demo corpus sampling |
513 +| V6 component parsers | Dotenv and environment values, False positives | URL/header/cookie/assignment prototypes |
514 +| V7 false positives | Dotenv and environment values, False positives | Negative fixtures from demo corpus and hand-written examples |
515 +| V8 UX | Core requirements, UX and commands | Command/status prototype or harness test |
516 +| V9 performance/failure | Core requirements, Settings | Worst-case regex/cap/deep-walk prototypes |
518 +The current `leaks` implementation is obsolete and is not validation evidence.
519 +A batch is validated only when its evidence command, fixture class, and result are recorded.
520 +Detector batches require positive and negative controls.
521 +Demo corpus evidence must report paths, key names, labels, classes, and counts only; never secret values.
523 +Evidence recorded so far:
525 +| Batch | Command | Fixture class | Result |
526 +| ---------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
527 +| V0 scope | temp Node ledger checker over `extensions/leaks/SPEC.md` | spec headings and normative sentence regex | `misses=[]`; mapped counts: V0=17, V1=34, V2=26, V3=21, V4=4, V5=46, V6=62, V7=62, V8=37, V9=46 |
528 +| V1 provider boundary | Pi docs grep for `context`, `before_provider_request`, `tool_call`, `message_end`; temp `pi -p` extension with local fake OpenAI provider | context-stage sentinel, provider-payload sentinel, provider auth header, placeholder tool arg, placeholder assistant text | provider body redacted by both stages; provider auth header present; tool arg restored; assistant message restored |
529 +| V2 placeholders | temp Node placeholder/selector prototype | stable same-value mapping, random per-store HMAC key, HMAC12 format, collision suffix, tag caps, overflow cap, restore lookup, overlap selector, existing placeholder skip | stable within store; different across stores; collision `_2`; 6 tags; 64-char tag cap; overflow non-restorable; selected password component plus whole JWT |
530 +| V3 settings | temp Node settings/regex compiler prototype | valid rule, zero/one/multi-capture rules, unsupported `flags`, missing/invalid/long labels, syntax error, unsafe regex patterns, 513-char pattern | positive controls pass; unsafe/syntax/long patterns ignored with warnings; group 1 selected with warning for multiple groups; labels fall back to `CUSTOM_REGEX` |
531 +| V4 built-ins | temp Node detector-family corpus checker over `/home/me/Workspace/tmp/demo-secrets` | 140 generated secret files; positive controls for all minimum detector families; negative controls for broad opaque keys `lineage` and `value` | all eight minimum families present; no missing families; broad keys remain negative; no unexpected negatives |
532 +| V5 learned values | temp Node dotenv/env classifier prototype plus sampled `/home/me/Workspace/tmp/demo-secrets` files | fixed `.env` files, no recursive dotenv, quoted/comment parsing, process env `ENV_KEY`, public prefixes, weak references, credentialed URL components, demo key/class sample | fixed files only; parser strips comments/quotes; env names collapse to `ENV_KEY`; weak refs ignored; URL components learned instead of whole URL; demo key/classes reported without values |
533 +| V6 component parsers | temp Node URL/header/cookie/assignment prototype plus sampled `/home/me/Workspace/tmp/demo-secrets` files | credentialed URL password/query, URL-without-creds negative, Bearer/Basic/X-API-Key/Cookie, six assignment syntaxes, embedded shell header, four weak metadata negatives | URL structure preserved; secret components redacted; safe URL/header/cookie parts preserved; embedded shell header redacted without redacting whole command; demo classes reported without values |
534 +| V7 false positives | temp Node false-positive classifier prototype plus sampled `/home/me/Workspace/tmp/demo-secrets` files | 16 hand negative controls, 4 positive controls, custom-regex override, demo metadata classes, SSH public key, TLS certificate | refs, hosts, ordinary URLs, IDs, names, UUID/hex, public prefixes, public keys, and certs stay metadata; high-confidence values still redact; custom regex can override |
535 +| V8 UX | temp Node UX/command-state prototype | provider turn summary, footer/status string, `/leaks status`, `/leaks reload`, `/leaks test`, `/leaks clear`, env key name, secret value | one aggregate notification for redacting turn; no notification for clean turn; status has required fields; diagnostics omit secret and env name; test output redacted; clear respects cancel |
536 +| V9 performance/failure | temp Node bounded-engine prototype | 40 precompiled rules, operation counter, mapping cap, cyclic object, depth-70 object, throwing detector | compile once; operations bounded by input size × rule count; map cap enforced; depth cap warning; cyclic traversal terminates; detector failure fails open and warns once |
538 +## Non-goals
540 +- homomorphic encryption
541 +- symbolic secret transform tools
542 +- semantic editing of secret bytes by the model
543 +- model exfiltration prevention
544 +- tool sandboxing
545 +- permission enforcement
546 +- repository secret scanning
547 +- git history cleanup
548 +- arbitrary DLP/privacy redaction
549 +- image, audio, binary, or OCR redaction

extensions/leaks/__tests__/detectors.test.ts

Modified
24 24 ["cloud-google", "AIza" + "B".repeat(35)],
25 25 ["payment-stripe", "sk_live_" + "c".repeat(32)],
26 26 ["messaging-slack", "xoxb-" + "d".repeat(24)],
27 - ["webhook", "https://hooks.slack.com/services/T00000000/B00000000/" + "e".repeat(24)],
27 + [
28 + "webhook",
29 + "https://hooks.slack.com/services/T00000000/B00000000/" +
30 + "e".repeat(24),
31 + ],
28 32 ["mail", "SG." + "f".repeat(22) + "." + "g".repeat(43)],
29 33 ["auth-cookie", "Cookie: session=session-fixture-secret-123456789"],
30 34 ["oauth", "ya29." + "h".repeat(80)],
38 42 it("redacts supported private-key material", () => {
39 43 const blocks = [
40 - ["-----BEGIN PGP ", "PRIVATE KEY BLOCK-----\nbody\n-----END PGP ", "PRIVATE KEY BLOCK-----"].join(""),
41 - ["-----BEGIN ENCRYPTED COSIGN ", "PRIVATE KEY-----\nbody\n-----END ENCRYPTED COSIGN ", "PRIVATE KEY-----"].join(""),
44 + [
45 + "-----BEGIN PGP ",
46 + "PRIVATE KEY BLOCK-----\nbody\n-----END PGP ",
47 + "PRIVATE KEY BLOCK-----",
48 + ].join(""),
49 + [
50 + "-----BEGIN ENCRYPTED COSIGN ",
51 + "PRIVATE KEY-----\nbody\n-----END ENCRYPTED COSIGN ",
52 + "PRIVATE KEY-----",
53 + ].join(""),
42 54 "PuTTY-User-Key-File-3: ssh-rsa\nPrivate-Lines: 1\nbody",
43 55 "AGE-SECRET-KEY-1" + "Q".repeat(58),
44 56 "untrusted comment: minisign secret key\nRW" + "R".repeat(72),
65 77 "MNEMONIC",
68 - for (const key of keys) expectRedacts(`${key}=assignment-fixture-secret-123456789`, key);
80 + for (const key of keys)
81 + expectRedacts(`${key}=assignment-fixture-secret-123456789`, key);
71 84 it("keeps explicit negative controls visible", () => {
78 91 ["domain", "example.com"],
79 92 ["lineage", "lineage=alpha-beta-gamma-delta"],
80 93 ["value", "value=opaque-but-not-secret-123456789"],
81 - ["ssh-public", "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMetadataOnlyKey user@example"],
82 - ["certificate", "-----BEGIN CERTIFICATE-----\nbody\n-----END CERTIFICATE-----"],
94 + [
95 + "ssh-public",
96 + "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMetadataOnlyKey user@example",
97 + ],
98 + [
99 + "certificate",
100 + "-----BEGIN CERTIFICATE-----\nbody\n-----END CERTIFICATE-----",
101 + ],
85 104 for (const [label, value] of negatives) expectVisible(value, label);
88 107 it("exports a stable built-in rule count", () => {
89 108 expect(__test.builtinRules.length).toBeGreaterThanOrEqual(12);
90 - expect(new __test.LeaksEngine().stats().builtInRuleCount).toBe(__test.builtinRules.length);
109 + expect(new __test.LeaksEngine().stats().builtInRuleCount).toBe(
110 + __test.builtinRules.length,
111 + );

extensions/leaks/__tests__/dotenv.test.ts

Modified
1 -import { mkdtempSync, mkdirSync, writeFileSync } from "node:fs";
1 +import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
2 2 import { tmpdir } from "node:os";
3 3 import path from "node:path";
4 4 import { describe, expect, it } from "vitest";
15 15 it("discovers only fixed cwd dotenv files", () => {
16 16 const cwd = temp();
17 17 mkdirSync(path.join(cwd, "nested"));
18 - for (const file of [".env", ".env.local", ".env.prod.local", ".env.prod", "nested/.env"]) {
19 - writeFileSync(path.join(cwd, file), "TOKEN=value\n", "utf8");
20 - }
22 - expect(__test.discoverDotenvFiles(cwd).map((file) => path.basename(file))).toEqual([
18 + for (const file of [
23 19 ".env",
24 20 ".env.local",
25 21 ".env.prod.local",
26 - ]);
22 + ".env.prod",
23 + "nested/.env",
24 + ]) {
25 + writeFileSync(path.join(cwd, file), "TOKEN=value\n", "utf8");
26 + }
28 + expect(
29 + __test.discoverDotenvFiles(cwd).map((file) => path.basename(file)),
30 + ).toEqual([".env", ".env.local", ".env.prod.local"]);
29 33 it("parses export, quotes, and inline comments", () => {
30 34 expect(
31 - __test.parseDotenv(`export API_KEY=${dotenvSecret}\nSINGLE='${dotenvSecret}'\nDOUBLE="${dotenvSecret}" # local\nPLAIN=value # comment`),
35 + __test.parseDotenv(
36 + `export API_KEY=${dotenvSecret}\nSINGLE='${dotenvSecret}'\nDOUBLE="${dotenvSecret}" # local\nPLAIN=value # comment`,
37 + ),
32 38 ).toEqual([
33 39 { key: "API_KEY", value: dotenvSecret },
34 40 { key: "SINGLE", value: dotenvSecret },
40 46 it("learns sensitive non-weak dotenv values and ignores weak values", () => {
41 47 const cwd = temp();
42 - writeFileSync(path.join(cwd, ".env"), `API_KEY=${dotenvSecret}\nPASSWORD=true\nTOKEN=short\n`, "utf8");
48 + writeFileSync(
49 + path.join(cwd, ".env"),
50 + `API_KEY=${dotenvSecret}\nPASSWORD=true\nTOKEN=short\n`,
51 + "utf8",
52 + );
43 53 const engine = new __test.LeaksEngine();
44 54 engine.reload(cwd, engine.settings, {});
46 - const result = engine.redactText(`send ${dotenvSecret} true short`, "provider");
56 + const result = engine.redactText(
57 + `send ${dotenvSecret} true short`,
58 + "provider",
59 + );
48 61 expect(result.text).not.toContain(dotenvSecret);
49 62 expect(result.text).toContain("true short");
61 74 expect(learned.values.map((item) => item.value)).toEqual([high]);
62 - expect(learned.values[0].labels.map((label) => label.tag)).toContain("ENV_KEY");
75 + expect(learned.values[0].labels.map((label) => label.tag)).toContain(
76 + "ENV_KEY",
77 + );
65 80 it("process env uses same classifier and collapses labels to ENV_KEY", () => {
66 - const engine = new __test.LeaksEngine({ env: { PRIVATE_TOKEN: envSecret } });
81 + const engine = new __test.LeaksEngine({
82 + env: { PRIVATE_TOKEN: envSecret },
83 + });
67 84 engine.reload(temp(), engine.settings, { PRIVATE_TOKEN: envSecret });
69 86 const result = engine.redactText(`send ${envSecret}`, "provider");
83 100 it("enforces learned exact value cap and warns once", () => {
84 101 const env: Record<string, string> = {};
85 - for (let index = 0; index < 8194; index++) env[`TOKEN_${index}`] = `cap-fixture-secret-${index}-123456`;
102 + for (let index = 0; index < 8194; index++)
103 + env[`TOKEN_${index}`] = `cap-fixture-secret-${index}-123456`;
86 104 const learned = __test.learnEnvironmentValues(env);
88 106 expect(learned.values).toHaveLength(8192);

extensions/leaks/__tests__/false-positives.test.ts

Modified
38 38 it("lets custom regex override negative metadata policy", () => {
39 39 const engine = new __test.LeaksEngine({
40 - settings: { regex: [{ pattern: "Project Nightingale", label: "CODENAME" }] },
40 + settings: {
41 + regex: [{ pattern: "Project Nightingale", label: "CODENAME" }],
42 + },
42 44 const text = "customer Acme Project Nightingale";
43 45 expect(redacted(text, engine)).not.toBe(text);

extensions/leaks/__tests__/harness.test.ts

Modified
31 31 t = await createTestSession({ extensions: [extPath()] });
32 32 const messages = [{ role: "user", content: `send ${secret}` }];
34 - const result = await t.session.extensionRunner.emitContext(messages as never);
34 + const result = await t.session.extensionRunner.emitContext(
35 + messages as never,
36 + );
36 38 expect(JSON.stringify(result)).not.toContain(secret);
37 39 expect(JSON.stringify(result)).toMatch(/__LEAK_[A-Z0-9_]+_[a-f0-9]{12}__/);
45 47 messages: [{ role: "user", content: `send ${secret}` }],
48 - const result = await t.session.extensionRunner.emitBeforeProviderRequest(payload);
50 + const result =
51 + await t.session.extensionRunner.emitBeforeProviderRequest(payload);
50 53 expect(JSON.stringify(result)).not.toContain(secret);
51 - expect((result as typeof payload).headers.authorization).toBe("Bearer provider-auth-header");
54 + expect((result as typeof payload).headers.authorization).toBe(
55 + "Bearer provider-auth-header",
56 + );
54 59 it("restores tool args and finalized assistant messages locally", async () => {
55 60 t = await createTestSession({ extensions: [extPath()] });
56 - const redacted = await t.session.extensionRunner.emitBeforeProviderRequest({ text: secret });
57 - const placeholder = JSON.stringify(redacted).match(/__LEAK_[A-Z0-9_]+_[a-f0-9]{12}__/)?.[0];
61 + const redacted = await t.session.extensionRunner.emitBeforeProviderRequest({
62 + text: secret,
63 + });
64 + const placeholder = JSON.stringify(redacted).match(
65 + /__LEAK_[A-Z0-9_]+_[a-f0-9]{12}__/,
66 + )?.[0];
58 67 expect(placeholder).toBeTruthy();
60 - const event = { type: "tool_call", toolName: "bash", toolCallId: "1", input: { command: `echo ${placeholder}` } };
69 + const event = {
70 + type: "tool_call",
71 + toolName: "bash",
72 + toolCallId: "1",
73 + input: { command: `echo ${placeholder}` },
74 + };
61 75 await t.session.extensionRunner.emitToolCall(event as never);
62 76 expect(event.input.command).toBe(`echo ${secret}`);
64 78 const message = await t.session.extensionRunner.emitMessageEnd({
65 79 type: "message_end",
66 - message: { role: "assistant", content: [{ type: "text", text: `saw ${placeholder}` }] },
80 + message: {
81 + role: "assistant",
82 + content: [{ type: "text", text: `saw ${placeholder}` }],
83 + },
67 84 } as never);
68 85 expect(JSON.stringify(message)).toContain(secret);
74 91 writeFileSync(path.join(cwd, ".env"), `TOKEN=${dotenvSecret}\n`, "utf8");
75 92 t = await createTestSession({ cwd, extensions: [extPath()] });
77 - const result = await t.session.extensionRunner.emitBeforeProviderRequest({ text: dotenvSecret });
94 + const result = await t.session.extensionRunner.emitBeforeProviderRequest({
95 + text: dotenvSecret,
96 + });
79 98 expect(JSON.stringify(result)).not.toContain(dotenvSecret);
80 - expect(JSON.stringify(t.session.sessionManager.getEntries())).not.toContain("LEAK_");
99 + expect(JSON.stringify(t.session.sessionManager.getEntries())).not.toContain(
100 + "LEAK_",
101 + );

extensions/leaks/__tests__/performance.test.ts

Modified
6 6 describe("leaks bounded traversal and failure behavior", () => {
7 7 it("deep redaction copies objects and preserves cycles", () => {
8 - const source: { text: string; self?: unknown; nested: { list: unknown[] } } = {
8 + const source: {
9 + text: string;
10 + self?: unknown;
11 + nested: { list: unknown[] };
12 + } = {
9 13 text: secret,
10 14 nested: { list: [secret] },
36 40 it("caps traversal depth at 64 and warns once", () => {
37 - let root: Record<string, unknown> = {};
41 + const root: Record<string, unknown> = {};
38 42 let current = root;
39 43 for (let index = 0; index < 70; index++) {
40 44 current.next = {};
46 50 const result = engine.redactDeep(root, "provider");
48 52 expect(result.count).toBe(0);
49 - expect(engine.stats().warnings.map((warning) => warning.code)).toContain("traversal.depth");
50 - expect(engine.stats().warnings.filter((warning) => warning.code === "traversal.depth")).toHaveLength(1);
53 + expect(engine.stats().warnings.map((warning) => warning.code)).toContain(
54 + "traversal.depth",
55 + );
56 + expect(
57 + engine
58 + .stats()
59 + .warnings.filter((warning) => warning.code === "traversal.depth"),
60 + ).toHaveLength(1);
53 63 it("restores deeply without mutating originals", () => {
64 74 it("keeps precompiled rule counts stable", () => {
65 75 const engine = new __test.LeaksEngine();
66 76 const before = engine.stats().builtInRuleCount;
67 - for (let index = 0; index < 50; index++) engine.redactText(secret, "provider");
77 + for (let index = 0; index < 50; index++)
78 + engine.redactText(secret, "provider");
68 79 expect(engine.stats().builtInRuleCount).toBe(before);
69 80 expect(__test.builtinRules).toHaveLength(before);
72 83 it("detector throws fail open and warn once", () => {
73 84 const engine = new __test.LeaksEngine({
74 - detectors: [() => { throw new Error("boom"); }],
85 + detectors: [
86 + () => {
87 + throw new Error("boom");
88 + },
89 + ],
76 91 engine.redactText(secret, "provider");
77 92 engine.redactText(secret, "provider");
79 - expect(engine.stats().warnings.filter((warning) => warning.code === "detector.failure")).toHaveLength(1);
94 + expect(
95 + engine
96 + .stats()
97 + .warnings.filter((warning) => warning.code === "detector.failure"),
98 + ).toHaveLength(1);

extensions/leaks/__tests__/placeholders.test.ts

Modified
5 5 const secretA = "placeholder-fixture-secret-A-123456";
6 6 const secretB = "placeholder-fixture-secret-B-123456";
8 -function label(tag: string, priority: number, source: SpanLabel["source"]): SpanLabel {
8 +function label(
9 + tag: string,
10 + priority: number,
11 + source: SpanLabel["source"],
12 +): SpanLabel {
9 13 return { tag, priority, source };
86 90 expect(overflow).toMatch(/^__LEAK_TOKEN_[a-f0-9]{12}__$/);
87 91 expect(store.restore(overflow)).toBe(overflow);
88 92 expect(overflowAgain).not.toBe(overflow);
89 - expect(warnings).toEqual(["placeholder mapping cap reached; new placeholders are not restorable"]);
93 + expect(warnings).toEqual([
94 + "placeholder mapping cap reached; new placeholders are not restorable",
95 + ]);
92 98 it("restores only exact placeholders", () => {
94 100 const placeholder = store.create(secretA, [label("TOKEN", 10, "dotenv")]);
96 102 expect(store.restore(`run ${placeholder}`)).toBe(`run ${secretA}`);
97 - expect(store.restore(`run ${placeholder.slice(0, -2)}`)).not.toContain(secretA);
103 + expect(store.restore(`run ${placeholder.slice(0, -2)}`)).not.toContain(
104 + secretA,
105 + );
100 108 it("engine redacts selected spans through the store", () => {

extensions/leaks/__tests__/regex-fuzz.test.ts

Modified
7 7 "token=" + "x".repeat(20_000),
8 8 "https://" + "a".repeat(10_000) + ":" + "b".repeat(10_000),
9 9 "Authorization: Bearer " + "x".repeat(20_000),
10 - "Cookie: " + Array.from({ length: 1_000 }, (_, index) => `k${index}=v${index}`).join("; "),
10 + "Cookie: " +
11 + Array.from({ length: 1_000 }, (_, index) => `k${index}=v${index}`).join(
12 + "; ",
13 + ),
11 14 ].join("\n");
13 16 describe("leaks regex fuzz safety", () => {
25 28 it("does not redact generated benign metadata corpus", () => {
26 29 const engine = new __test.LeaksEngine();
27 - const corpus = Array.from({ length: 500 }, (_, index) => [
28 - `branch=feature/metadata-${index}`,
29 - `host=svc-${index}.internal.example.test`,
30 - `url=https://example.test/path/${index}?user=alice`,
31 - `id=550e8400-e29b-41d4-a716-${String(index).padStart(12, "0")}`,
32 - ].join(" ")).join("\n");
30 + const corpus = Array.from({ length: 500 }, (_, index) =>
31 + [
32 + `branch=feature/metadata-${index}`,
33 + `host=svc-${index}.internal.example.test`,
34 + `url=https://example.test/path/${index}?user=alice`,
35 + `id=550e8400-e29b-41d4-a716-${String(index).padStart(12, "0")}`,
36 + ].join(" "),
37 + ).join("\n");
34 39 expect(engine.redactText(corpus, "provider").text).toBe(corpus);
37 42 it("keeps positive regex controls effective after fuzz corpus", () => {
38 43 const engine = new __test.LeaksEngine();
39 44 const value = "sk-or-v1-" + "f".repeat(64);
40 - expect(engine.redactText(`${adversarial}\n${value}`, "provider").text).not.toContain(value);
45 + expect(
46 + engine.redactText(`${adversarial}\n${value}`, "provider").text,
47 + ).not.toContain(value);

extensions/leaks/__tests__/selector.test.ts

Modified
2 2 import { describe, expect, it } from "vitest";
3 3 import { __test } from "../index.ts";
4 4 import type { SecretSpan, SpanLabel } from "../src/types.ts";
6 -function label(tag: string, source: SpanLabel["source"] = "generic"): SpanLabel {
6 +function label(
7 + tag: string,
8 + source: SpanLabel["source"] = "generic",
9 +): SpanLabel {
7 10 return { tag, source, priority: 10 };
60 63 confidence: 90,
63 - expect(__test.selectSecretSpans(text, [generic, exact])[0].kind).toBe("exact");
66 + expect(__test.selectSecretSpans(text, [generic, exact])[0].kind).toBe(
67 + "exact",
68 + );
66 71 it("keeps the smallest secret-bearing span for ordinary overlaps", () => {
80 85 it("keeps private key whole spans over smaller matches", () => {
81 - const text = "-----BEGIN PRIVATE KEY-----\nabc123\n-----END PRIVATE KEY-----";
86 + const text =
87 + "-----BEGIN PRIVATE KEY-----\nabc123\n-----END PRIVATE KEY-----";
82 88 const whole = span(text, text, {
83 89 kind: "private-key",
84 90 wholeSpan: true,

extensions/leaks/__tests__/settings.test.ts

Modified
26 26 const loaded = __test.loadLeaksSettings({
27 27 leaks: {
28 28 regex: [
29 - { pattern: "foo_[a-z]+", label: "FOO_TOKEN", flags: "i", extra: true },
29 + {
30 + pattern: "foo_[a-z]+",
31 + label: "FOO_TOKEN",
32 + flags: "i",
33 + extra: true,
34 + },
87 92 it("redacts full match for zero captures", () => {
88 93 const engine = new __test.LeaksEngine({
89 94 settings: {
90 - regex: [{ pattern: "foo_secret_fixture_value_[0-9]+", label: "FOO_TOKEN" }],
95 + regex: [
96 + { pattern: "foo_secret_fixture_value_[0-9]+", label: "FOO_TOKEN" },
97 + ],
100 107 it("redacts group 1 for one capture", () => {
101 108 const engine = new __test.LeaksEngine({
102 109 settings: {
103 - regex: [{ pattern: "token=(foo_secret_fixture_value_[0-9]+)", label: "FOO_TOKEN" }],
110 + regex: [
112 + pattern: "token=(foo_secret_fixture_value_[0-9]+)",
113 + label: "FOO_TOKEN",
114 + },
115 + ],
113 125 it("redacts group 1 for multiple captures and warns", () => {
114 126 const engine = new __test.LeaksEngine({
115 127 settings: {
116 - regex: [{ pattern: "(foo_secret_fixture_value_[0-9]+)-(tail)", label: "FOO_TOKEN" }],
128 + regex: [
130 + pattern: "(foo_secret_fixture_value_[0-9]+)-(tail)",
131 + label: "FOO_TOKEN",
132 + },
133 + ],
141 158 expect(compiled.rules).toHaveLength(0);
142 159 expect(compiled.invalidRegexCount).toBe(patterns.length);
143 - expect(compiled.warnings.every((warning) => warning.code === "settings.regex.unsafe")).toBe(true);
160 + expect(
161 + compiled.warnings.every(
162 + (warning) => warning.code === "settings.regex.unsafe",
163 + ),
164 + ).toBe(true);
146 167 it("keeps warning text secret-free", () => {
148 169 regex: [{ pattern: `(${fixtureSecret}`, label: "BROKEN" }],
151 - expect(compiled.warnings.map((warning) => warning.message).join("\n")).not.toContain(
152 - fixtureSecret,
153 - );
172 + expect(
173 + compiled.warnings.map((warning) => warning.message).join("\n"),
174 + ).not.toContain(fixtureSecret);

extensions/leaks/__tests__/structures.test.ts

Modified
2 2 import { describe, expect, it } from "vitest";
3 3 import { __test } from "../index.ts";
5 5 const secret = "structure-fixture-secret-123456789";
6 -const jwt = ["eyJhbGciOiJIUzI1NiJ9", "eyJzdWIiOiIxMjM0NTY3ODkwIn0", "c2lnbmF0dXJl"].join(".");
7 -const privateKey = "-----BEGIN OPENSSH PRIVATE KEY-----\nfixture-private-key-body\n-----END OPENSSH PRIVATE KEY-----";
6 +const jwt = [
7 + "eyJhbGciOiJIUzI1NiJ9",
8 + "eyJzdWIiOiIxMjM0NTY3ODkwIn0",
9 + "c2lnbmF0dXJl",
10 +].join(".");
11 +const privateKey =
12 + "-----BEGIN OPENSSH PRIVATE KEY-----\nfixture-private-key-body\n-----END OPENSSH PRIVATE KEY-----";
9 14 describe("leaks structured component parsers", () => {
10 15 it("redacts credentialed URL passwords while preserving safe URL structure", () => {
29 34 it("does not redact URLs without credentials even for database keys", () => {
30 35 const engine = new __test.LeaksEngine();
31 - const text = "DATABASE_URL=postgres://db/app REDIS_URL=redis://cache MONGO_URL=mongodb://mongo/app";
36 + const text =
37 + "DATABASE_URL=postgres://db/app REDIS_URL=redis://cache MONGO_URL=mongodb://mongo/app";
32 38 expect(engine.redactText(text, "provider").text).toBe(text);
47 53 it("redacts sensitive cookie values only", () => {
48 54 const engine = new __test.LeaksEngine();
49 - const result = engine.redactText(`Cookie: session=${secret}; theme=dark`, "provider");
55 + const result = engine.redactText(
56 + `Cookie: session=${secret}; theme=dark`,
57 + "provider",
58 + );
51 60 expect(result.text).toContain("Cookie: session=");
52 61 expect(result.text).toContain("; theme=dark");
75 84 it("redacts shell header value without redacting whole command", () => {
76 85 const engine = new __test.LeaksEngine();
77 - const result = engine.redactText(`curl -H 'Authorization: Bearer ${secret}' https://example.test`, "provider");
86 + const result = engine.redactText(
87 + `curl -H 'Authorization: Bearer ${secret}' https://example.test`,
88 + "provider",
89 + );
79 91 expect(result.text).toContain("curl -H '");
80 92 expect(result.text).toContain("https://example.test");
87 99 const keyResult = engine.redactText(privateKey, "provider");
89 101 expect(jwtResult.text).toMatch(/^jwt=__LEAK_JWT_[a-f0-9]{12}__$/);
90 - expect(keyResult.text).toMatch(/^__LEAK_OPENSSH_PRIVATE_KEY_[a-f0-9]{12}__$/);
102 + expect(keyResult.text).toMatch(
103 + /^__LEAK_OPENSSH_PRIVATE_KEY_[a-f0-9]{12}__$/,
104 + );

extensions/leaks/__tests__/unit.test.ts

Modified
70 70 const warnings = engine.stats().warnings.map((warning) => warning.message);
71 71 expect(warnings.join("\n")).not.toContain(fixtureSecret);
72 - expect(warnings).toEqual(["detector failure; redaction pass left unchanged"]);
72 + expect(warnings).toEqual([
73 + "detector failure; redaction pass left unchanged",
74 + ]);

extensions/leaks/__tests__/ux.test.ts

Modified
1 +import path from "node:path";
1 2 import { afterEach, describe, expect, it } from "vitest";
2 3 import { createTestSession, type TestSession } from "../../../test/harness";
3 -import path from "node:path";
5 5 function extPath() {
6 6 return path.resolve(import.meta.dirname, "../index.ts");
18 18 it("notifies once for a redacting provider turn and not for clean turns", async () => {
19 19 t = await createTestSession({ extensions: [extPath()] });
20 20 await t.session.extensionRunner.emitBeforeProviderRequest({ text: secret });
21 - await t.session.extensionRunner.emitBeforeProviderRequest({ text: "ordinary metadata" });
21 + await t.session.extensionRunner.emitBeforeProviderRequest({
22 + text: "ordinary metadata",
23 + });
23 - const notices = t.events.uiCallsFor("notify").map((call) => String(call.args[0]));
24 - expect(notices.filter((notice) => notice.includes("leaks redacted"))).toHaveLength(1);
25 + const notices = t.events
26 + .uiCallsFor("notify")
27 + .map((call) => String(call.args[0]));
28 + expect(
29 + notices.filter((notice) => notice.includes("leaks redacted")),
30 + ).toHaveLength(1);
27 33 it("status reports required safe fields", async () => {
28 34 t = await createTestSession({ extensions: [extPath()] });
29 35 await t.session.extensionRunner.emitBeforeProviderRequest({ text: secret });
30 - const command = t.session.extensionRunner.extensions[0].commands.get("leaks");
36 + const command =
37 + t.session.extensionRunner.extensions[0].commands.get("leaks");
31 38 await command.handler("status", t.session.extensionRunner.createContext());
33 - const status = t.events.uiCallsFor("notify").map((call) => String(call.args[0])).at(-1) ?? "";
40 + const status =
41 + t.events
42 + .uiCallsFor("notify")
43 + .map((call) => String(call.args[0]))
44 + .at(-1) ?? "";
34 45 for (const field of [
35 46 "leaks on",
36 47 "built-in rules:",
43 54 "total provider redactions:",
44 55 "recent labels:",
45 56 "latest tags:",
46 - ]) expect(status).toContain(field);
57 + ])
58 + expect(status).toContain(field);
47 59 expect(status).not.toContain(secret);
50 62 it("reload test and clear commands are safe", async () => {
51 - t = await createTestSession({ extensions: [extPath()], mockUI: { confirm: true } });
52 - const command = t.session.extensionRunner.extensions[0].commands.get("leaks");
63 + t = await createTestSession({
64 + extensions: [extPath()],
65 + mockUI: { confirm: true },
66 + });
67 + const command =
68 + t.session.extensionRunner.extensions[0].commands.get("leaks");
53 69 const ctx = t.session.extensionRunner.createContext();
55 71 await command.handler("test " + secret, ctx);
58 74 await command.handler("clear", ctx);
59 75 await command.handler("status", ctx);
61 - const notices = t.events.uiCallsFor("notify").map((call) => String(call.args[0]));
77 + const notices = t.events
78 + .uiCallsFor("notify")
79 + .map((call) => String(call.args[0]));
62 80 expect(notices.join("\n")).not.toContain(secret);
63 - expect(notices.some((notice) => notice.includes("leaks reloaded"))).toBe(true);
64 - expect(notices.some((notice) => notice.includes("leaks cleared"))).toBe(true);
81 + expect(notices.some((notice) => notice.includes("leaks reloaded"))).toBe(
82 + true,
83 + );
84 + expect(notices.some((notice) => notice.includes("leaks cleared"))).toBe(
85 + true,
86 + );
65 87 expect(notices.at(-1)).toContain("mappings: 0");
68 90 it("clear respects cancellation", async () => {
69 - t = await createTestSession({ extensions: [extPath()], mockUI: { confirm: false } });
91 + t = await createTestSession({
92 + extensions: [extPath()],
93 + mockUI: { confirm: false },
94 + });
70 95 await t.session.extensionRunner.emitBeforeProviderRequest({ text: secret });
71 - const command = t.session.extensionRunner.extensions[0].commands.get("leaks");
96 + const command =
97 + t.session.extensionRunner.extensions[0].commands.get("leaks");
72 98 await command.handler("clear", t.session.extensionRunner.createContext());
73 99 await command.handler("status", t.session.extensionRunner.createContext());
75 - const notices = t.events.uiCallsFor("notify").map((call) => String(call.args[0]));
76 - expect(notices.some((notice) => notice.includes("clear cancelled"))).toBe(true);
101 + const notices = t.events
102 + .uiCallsFor("notify")
103 + .map((call) => String(call.args[0]));
104 + expect(notices.some((notice) => notice.includes("clear cancelled"))).toBe(
105 + true,
106 + );
77 107 expect(notices.at(-1)).toContain("mappings: 1");

extensions/leaks/__tests__/validation-batches.test.ts

Modified
22 22 it("maps every V0-V9 batch to executable test files", () => {
23 23 for (const [batch, files] of batches) {
24 24 expect(files.length, batch).toBeGreaterThan(0);
25 - for (const file of files) expect(existsSync(path.join(tests, file)), `${batch} ${file}`).toBe(true);
25 + for (const file of files)
26 + expect(existsSync(path.join(tests, file)), `${batch} ${file}`).toBe(
27 + true,
28 + );
29 32 it("keeps detector batches backed by positive and negative controls", () => {
30 - const detectorText = readFileSync(path.join(tests, "detectors.test.ts"), "utf8");
31 - const falsePositiveText = readFileSync(path.join(tests, "false-positives.test.ts"), "utf8");
33 + const detectorText = readFileSync(
34 + path.join(tests, "detectors.test.ts"),
35 + "utf8",
36 + );
37 + const falsePositiveText = readFileSync(
38 + path.join(tests, "false-positives.test.ts"),
39 + "utf8",
40 + );
32 41 expect(detectorText).toContain("positives");
33 42 expect(detectorText).toContain("negatives");
34 43 expect(falsePositiveText).toContain("negatives");
38 47 it("records regex fuzz evidence for runtime regex safety", () => {
39 - const fuzzText = readFileSync(path.join(tests, "regex-fuzz.test.ts"), "utf8");
48 + const fuzzText = readFileSync(
49 + path.join(tests, "regex-fuzz.test.ts"),
50 + "utf8",
51 + );
40 52 expect(fuzzText).toContain("runtimeRegexFuzzTargets");
41 53 expect(fuzzText).toContain("adversarial");

extensions/leaks/src/detectors.ts

Modified
13 13 export const builtinRules: BuiltinRule[] = [
14 - rule("openrouter", "OPENROUTER_TOKEN", "ai", /\bsk-or-v1-[A-Za-z0-9]{32,}\b/dg),
14 + rule(
15 + "openrouter",
16 + "OPENROUTER_TOKEN",
17 + "ai",
18 + /\bsk-or-v1-[A-Za-z0-9]{32,}\b/dg,
19 + ),
15 20 rule("openai", "OPENAI_TOKEN", "ai", /\bsk-proj-[A-Za-z0-9_-]{32,}\b/dg),
16 - rule("anthropic", "ANTHROPIC_TOKEN", "ai", /\bsk-ant-api03-[A-Za-z0-9_-]{32,}\b/dg),
21 + rule(
22 + "anthropic",
23 + "ANTHROPIC_TOKEN",
24 + "ai",
25 + /\bsk-ant-api03-[A-Za-z0-9_-]{32,}\b/dg,
26 + ),
17 27 rule("huggingface", "HUGGINGFACE_TOKEN", "ai", /\bhf_[A-Za-z0-9]{32,}\b/dg),
18 28 rule("github", "GITHUB_TOKEN", "vcs", /\bgh[pousr]_[A-Za-z0-9]{30,}\b/dg),
19 29 rule("npm", "NPM_TOKEN", "package", /\bnpm_[A-Za-z0-9]{30,}\b/dg),
20 - rule("aws-access-key", "AWS_ACCESS_KEY_ID", "cloud", /\b(?:AKIA|ASIA)[A-Z0-9]{16}\b/dg),
21 - rule("google-api-key", "GOOGLE_API_KEY", "cloud", /\bAIza[A-Za-z0-9_-]{35}\b/dg),
22 - rule("stripe-live", "STRIPE_LIVE_SECRET_KEY", "payment", /\bsk_live_[A-Za-z0-9]{24,}\b/dg),
23 - rule("slack", "SLACK_TOKEN", "messaging", /\bxox[baprs]-[A-Za-z0-9-]{20,}\b/dg),
24 - rule("slack-webhook", "SLACK_WEBHOOK", "webhook", /https:\/\/hooks\.slack\.com\/services\/[A-Za-z0-9/]{20,}/dg),
25 - rule("sendgrid", "SENDGRID_TOKEN", "mail", /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{32,}\b/dg),
30 + rule(
31 + "aws-access-key",
32 + "AWS_ACCESS_KEY_ID",
33 + "cloud",
34 + /\b(?:AKIA|ASIA)[A-Z0-9]{16}\b/dg,
35 + ),
36 + rule(
37 + "google-api-key",
38 + "GOOGLE_API_KEY",
39 + "cloud",
40 + /\bAIza[A-Za-z0-9_-]{35}\b/dg,
41 + ),
42 + rule(
43 + "stripe-live",
44 + "STRIPE_LIVE_SECRET_KEY",
45 + "payment",
46 + /\bsk_live_[A-Za-z0-9]{24,}\b/dg,
47 + ),
48 + rule(
49 + "slack",
50 + "SLACK_TOKEN",
51 + "messaging",
52 + /\bxox[baprs]-[A-Za-z0-9-]{20,}\b/dg,
53 + ),
54 + rule(
55 + "slack-webhook",
56 + "SLACK_WEBHOOK",
57 + "webhook",
58 + /https:\/\/hooks\.slack\.com\/services\/[A-Za-z0-9/]{20,}/dg,
59 + ),
60 + rule(
61 + "sendgrid",
62 + "SENDGRID_TOKEN",
63 + "mail",
64 + /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{32,}\b/dg,
65 + ),
26 66 rule("oauth-google", "OAUTH_TOKEN", "auth", /\bya29\.[A-Za-z0-9_-]{32,}\b/dg),
27 - rule("pgp-private", "PGP_PRIVATE_KEY", "private-key", armorRegex("PGP", "PRIVATE KEY BLOCK"), 0, "private-key", true),
28 - rule("cosign-private", "COSIGN_PRIVATE_KEY", "private-key", armorRegex("ENCRYPTED COSIGN", "PRIVATE KEY"), 0, "private-key", true),
29 - rule("putty-private", "PUTTY_PRIVATE_KEY", "private-key", /PuTTY-User-Key-File-[\s\S]*?Private-Lines:\s*\d+[\s\S]*/dg, 0, "private-key", true),
30 - rule("age-secret", "AGE_SECRET_KEY", "private-key", /\bAGE-SECRET-KEY-1[0-9A-Z]{40,}\b/dg, 0, "private-key", true),
31 - rule("minisign-secret", "MINISIGN_SECRET_KEY", "private-key", /untrusted comment:\s*minisign secret key\s+RW[A-Za-z0-9+\/]{40,}/dgi, 0, "private-key", true),
67 + rule(
68 + "pgp-private",
69 + "PGP_PRIVATE_KEY",
70 + "private-key",
71 + armorRegex("PGP", "PRIVATE KEY BLOCK"),
72 + 0,
73 + "private-key",
74 + true,
75 + ),
76 + rule(
77 + "cosign-private",
78 + "COSIGN_PRIVATE_KEY",
79 + "private-key",
80 + armorRegex("ENCRYPTED COSIGN", "PRIVATE KEY"),
81 + 0,
82 + "private-key",
83 + true,
84 + ),
85 + rule(
86 + "putty-private",
87 + "PUTTY_PRIVATE_KEY",
88 + "private-key",
89 + /PuTTY-User-Key-File-[\s\S]*?Private-Lines:\s*\d+[\s\S]*/dg,
90 + 0,
91 + "private-key",
92 + true,
93 + ),
94 + rule(
95 + "age-secret",
96 + "AGE_SECRET_KEY",
97 + "private-key",
98 + /\bAGE-SECRET-KEY-1[0-9A-Z]{40,}\b/dg,
99 + 0,
100 + "private-key",
101 + true,
102 + ),
103 + rule(
104 + "minisign-secret",
105 + "MINISIGN_SECRET_KEY",
106 + "private-key",
107 + /untrusted comment:\s*minisign secret key\s+RW[A-Za-z0-9+/]{40,}/dgi,
108 + 0,
109 + "private-key",
110 + true,
111 + ),
34 114 export const builtinRegexFuzzTargets = builtinRules.map((item) => ({
78 158 function armorRegex(prefix: string, name: string) {
79 159 const marker = `${prefix} ${name}`;
80 - return new RegExp(`-----BEGIN ${marker}-----[\\s\\S]*?-----END ${marker}-----`, "dg");
160 + return new RegExp(
161 + `-----BEGIN ${marker}-----[\\s\\S]*?-----END ${marker}-----`,
162 + "dg",
163 + );

extensions/leaks/src/dotenv.ts

Modified
61 61 return [...direct, ...local];
64 -export function parseDotenv(text: string): Array<{ key: string; value: string }> {
64 +export function parseDotenv(
65 + text: string,
66 +): Array<{ key: string; value: string }> {
65 67 const rows: Array<{ key: string; value: string }> = [];
66 68 for (const raw of text.split(/\r?\n/)) {
67 69 let line = raw.trim();
70 72 const match = /^([A-Za-z_][A-Za-z0-9_-]*)\s*=\s*(.*)$/.exec(line);
71 73 if (!match) continue;
72 74 let value = match[2] ?? "";
73 - if ((value.startsWith("'") && value.includes("'", 1)) || (value.startsWith('"') && value.includes('"', 1))) {
75 + if (
76 + (value.startsWith("'") && value.includes("'", 1)) ||
77 + (value.startsWith('"') && value.includes('"', 1))
78 + ) {
74 79 const quote = value[0];
75 80 const end = value.indexOf(quote, 1);
76 81 value = value.slice(1, end);
85 90 export function loadDotenvValues(cwd: string): LearnedValues {
86 91 const pairs: Record<string, string> = {};
87 92 for (const file of discoverDotenvFiles(cwd)) {
88 - for (const item of parseDotenv(readFileSync(file, "utf8"))) pairs[item.key] = item.value;
93 + for (const item of parseDotenv(readFileSync(file, "utf8")))
94 + pairs[item.key] = item.value;
90 96 return learnKeyValues(pairs, "dotenv");
93 -export function learnEnvironmentValues(env: Record<string, string | undefined>): LearnedValues {
99 +export function learnEnvironmentValues(
100 + env: Record<string, string | undefined>,
101 +): LearnedValues {
94 102 const pairs: Record<string, string> = {};
95 - for (const [key, value] of Object.entries(env)) if (typeof value === "string") pairs[key] = value;
103 + for (const [key, value] of Object.entries(env))
104 + if (typeof value === "string") pairs[key] = value;
96 105 return learnKeyValues(pairs, "env");
99 108 export function isSensitiveKey(key: string): boolean {
100 109 const normalized = normalizeKey(key.replace(PUBLIC_PREFIX_RE, ""));
101 110 if (SENSITIVE.has(normalized)) return true;
102 - return [...SENSITIVE].some((sensitive) => normalized.endsWith(`_${sensitive}`) || normalized.startsWith(`${sensitive}_`));
111 + return [...SENSITIVE].some(
112 + (sensitive) =>
113 + normalized.endsWith(`_${sensitive}`) ||
114 + normalized.startsWith(`${sensitive}_`),
115 + );
105 118 export function highConfidenceSecretShape(value: string): boolean {
106 - return /^(?:sk-or-v1-[A-Za-z0-9]{32,}|sk-proj-[A-Za-z0-9_-]{32,}|sk-ant-api03-[A-Za-z0-9_-]{32,}|ghp_[A-Za-z0-9]{36}|hf_[A-Za-z0-9]{32,})$/.test(value);
119 + return /^(?:sk-or-v1-[A-Za-z0-9]{32,}|sk-proj-[A-Za-z0-9_-]{32,}|sk-ant-api03-[A-Za-z0-9_-]{32,}|ghp_[A-Za-z0-9]{36}|hf_[A-Za-z0-9]{32,})$/.test(
120 + value,
121 + );
109 -export function learnedValueSpans(text: string, values: LearnedValue[]): SecretSpan[] {
124 +export function learnedValueSpans(
125 + text: string,
126 + values: LearnedValue[],
127 +): SecretSpan[] {
110 128 const spans: SecretSpan[] = [];
111 129 for (const learned of values) {
112 130 let start = text.indexOf(learned.value);
126 144 return spans;
129 -function learnKeyValues(pairs: Record<string, string>, source: "dotenv" | "env"): LearnedValues {
147 +function learnKeyValues(
148 + pairs: Record<string, string>,
149 + source: "dotenv" | "env",
150 +): LearnedValues {
130 151 const values: LearnedValue[] = [];
131 152 const warnings: WarningRecord[] = [];
132 153 let warned = false;
135 156 if (!warned) {
136 157 warnings.push({
137 158 code: "learned.cap",
138 - message: "learned value cap reached; further dotenv/env values ignored",
159 + message:
160 + "learned value cap reached; further dotenv/env values ignored",
139 161 severity: "warning",
141 163 warned = true;
145 167 const publicPrefixed = PUBLIC_PREFIX_RE.test(key);
146 168 const secretShape = highConfidenceSecretShape(value);
147 - if ((!publicPrefixed && isSensitiveKey(key) && !isWeakValue(value)) || secretShape) {
169 + if (
170 + (!publicPrefixed && isSensitiveKey(key) && !isWeakValue(value)) ||
171 + secretShape
172 + ) {
148 173 values.push({
149 174 value,
150 175 labels: [
163 188 function normalizeKey(key: string) {
164 - return key
165 - .toUpperCase()
166 - .replace(/[-]+/g, "_")
167 - .replace(/_\d+$/, "");
189 + return key.toUpperCase().replace(/[-]+/g, "_").replace(/_\d+$/, "");

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(`leaks redacted ${result.count} provider value(s)`, "info");
23 + if (result.count > 0)
24 + ctx.ui.notify(
25 + `leaks redacted ${result.count} provider value(s)`,
26 + "info",
27 + );
25 29 return result.value;
28 32 pi.on("tool_call", async (event) => {
29 33 const input = (event as { input?: unknown }).input;
30 - if (input !== undefined) (event as { input: unknown }).input = engine.restoreDeep(input).value;
34 + if (input !== undefined)
35 + (event as { input: unknown }).input = engine.restoreDeep(input).value;
33 38 pi.on("message_end", async (event) => {
51 56 if (command === "clear") {
52 57 if (ctx.hasUI) {
53 - const confirmed = await ctx.ui.confirm("Clear leaks mappings?", "Old placeholders become unrestorable.");
58 + const confirmed = await ctx.ui.confirm(
59 + "Clear leaks mappings?",
60 + "Old placeholders become unrestorable.",
61 + );
54 62 if (!confirmed) {
55 63 ctx.ui.notify("clear cancelled", "info");
56 64 return;

extensions/leaks/src/placeholders.ts

Modified
84 84 private digest(value: string) {
85 - const raw = this.digestOverride?.(value) ?? createHmac("sha256", this.key).update(value).digest("hex");
86 - return raw.toLowerCase().replace(/[^a-f0-9]/g, "0").slice(0, 12).padEnd(12, "0");
85 + const raw =
86 + this.digestOverride?.(value) ??
87 + createHmac("sha256", this.key).update(value).digest("hex");
88 + return raw
89 + .toLowerCase()
90 + .replace(/[^a-f0-9]/g, "0")
91 + .slice(0, 12)
92 + .padEnd(12, "0");
89 95 private nextAvailable(base: string) {
99 105 private nonRestorable(base: string) {
100 106 let candidate = base;
101 - while (this.placeholderToSecret.has(candidate) || this.overflowPlaceholders.has(candidate)) {
107 + while (
108 + this.placeholderToSecret.has(candidate) ||
109 + this.overflowPlaceholders.has(candidate)
110 + ) {
102 111 this.overflowNonce++;
103 112 candidate = base.replace(/__$/, `_${this.overflowNonce}__`);
112 121 this.warnedCap = true;
113 122 this.onWarning?.({
114 123 code: "placeholder.cap",
115 - message: "placeholder mapping cap reached; new placeholders are not restorable",
124 + message:
125 + "placeholder mapping cap reached; new placeholders are not restorable",
116 126 severity: "warning",

extensions/leaks/src/safety.ts

Modified
1 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> {
3 +export function isPlainObject(
4 + value: unknown,
5 +): value is Record<string, unknown> {
4 6 if (value === null || typeof value !== "object") return false;
5 7 const prototype = Object.getPrototypeOf(value);
6 8 return prototype === Object.prototype || prototype === null;
35 37 if (/^\$\{?[A-Z_][A-Z0-9_]*\}?$/i.test(text)) return true;
36 38 if (/^\$\{\{\s*secrets\.[A-Z0-9_]+\s*\}\}$/i.test(text)) return true;
37 39 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)) {
40 + if (
41 + /^[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(
42 + text,
43 + )
44 + ) {
39 45 return true;
41 47 if (/^(?:[a-z0-9-]+\.)+[a-z]{2,}$/i.test(text)) return true;
42 48 try {
43 49 const url = new URL(text);
44 - if (!url.password && !/[?&](?:token|key|secret|password|auth)=/i.test(url.search)) {
50 + if (
51 + !url.password &&
52 + !/[?&](?:token|key|secret|password|auth)=/i.test(url.search)
53 + ) {
45 54 return true;
47 56 } catch {}

extensions/leaks/src/selector.ts

Modified
11 11 generic: 5,
14 -export function selectSecretSpans(text: string, spans: SecretSpan[]): SecretSpan[] {
14 +export function selectSecretSpans(
15 + text: string,
16 + spans: SecretSpan[],
17 +): SecretSpan[] {
15 18 const placeholderRanges = [...text.matchAll(PLACEHOLDER_RE)].map((match) => ({
16 19 start: match.index ?? 0,
17 20 end: (match.index ?? 0) + match[0].length,
18 21 }));
19 22 const merged = new Map<string, SecretSpan>();
20 23 for (const span of spans) {
21 - if (span.start < 0 || span.end <= span.start || span.end > text.length) continue;
22 - if (placeholderRanges.some((range) => span.start >= range.start && span.end <= range.end)) continue;
24 + if (span.start < 0 || span.end <= span.start || span.end > text.length)
25 + continue;
26 + if (
27 + placeholderRanges.some(
28 + (range) => span.start >= range.start && span.end <= range.end,
29 + )
30 + )
31 + continue;
23 32 const key = `${span.start}:${span.end}`;
24 33 const current = merged.get(key);
25 34 if (!current) {
57 66 function forceRank(span: SecretSpan) {
58 - return span.wholeSpan && (span.kind === "private-key" || span.kind === "jwt") ? 0 : 1;
67 + return span.wholeSpan && (span.kind === "private-key" || span.kind === "jwt")
68 + ? 0
69 + : 1;
61 72 function overlaps(left: SecretSpan, right: SecretSpan) {

extensions/leaks/src/structures.ts

Modified
2 2 import { highConfidenceSecretShape, isSensitiveKey } from "./dotenv.ts";
3 3 import { isWeakValue } from "./safety.ts";
4 4 import type { SecretSpan, SpanLabel } from "./types.ts";
6 -const URL_CREDENTIAL_RE = /\b[a-z][a-z0-9+.-]*:\/\/([^\s:@/]*)?:([^\s@/]+)@[^\s]+/dgi;
7 -const QUERY_RE = /([?&])([A-Za-z0-9_-]*(?:token|key|secret|password|auth)[A-Za-z0-9_-]*)=([^&#\s'"`]+)/dgi;
6 +const URL_CREDENTIAL_RE =
7 + /\b[a-z][a-z0-9+.-]*:\/\/([^\s:@/]*)?:([^\s@/]+)@[^\s]+/dgi;
8 +const QUERY_RE =
9 + /([?&])([A-Za-z0-9_-]*(?:token|key|secret|password|auth)[A-Za-z0-9_-]*)=([^&#\s'"`]+)/dgi;
8 10 const AUTH_RE = /Authorization:\s*(Bearer|Basic)\s+([^\s'"`]+)/dgi;
9 11 const X_API_RE = /\b(X-API-Key|X-Api-Key|Api-Key):\s*([^\s'"`]+)/dg;
10 12 const COOKIE_LINE_RE = /Cookie:\s*([^\n]+)/dgi;
11 -const ASSIGN_RE = /(?:^|[\n\r\s{,;])(?:export\s+)?["']?([A-Za-z_][A-Za-z0-9_-]*)["']?\s*(?:=|:|=>)\s*["']?([^\s'",;}]{1,512})["']?/dgi;
13 +const ASSIGN_RE =
14 + /(?:^|[\n\r\s{,;])(?:export\s+)?["']?([A-Za-z_][A-Za-z0-9_-]*)["']?\s*(?:=|:|=>)\s*["']?([^\s'",;}]{1,512})["']?/dgi;
12 15 const XML_RE = /<([A-Za-z_][A-Za-z0-9_-]*)>([^<]{1,512})<\/\1>/dgi;
13 -const JWT_RE = /\b[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{8,}\b/dg;
14 -const PRIVATE_KEY_RE = /-----BEGIN ([A-Z0-9 ]*PRIVATE KEY)-----[\s\S]*?-----END \1-----/dg;
16 +const JWT_RE =
17 + /\b[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{8,}\b/dg;
18 +const PRIVATE_KEY_RE =
19 + /-----BEGIN ([A-Z0-9 ]*PRIVATE KEY)-----[\s\S]*?-----END \1-----/dg;
16 21 export const structureRegexFuzzTargets = [
17 22 ["url-credential", URL_CREDENTIAL_RE],
22 27 ["xml", XML_RE],
23 28 ["jwt", JWT_RE],
24 29 ["private-key", PRIVATE_KEY_RE],
25 -].map(([name, regex]) => ({ name: `structure:${name}`, regex: regex as RegExp }));
30 +].map(([name, regex]) => ({
31 + name: `structure:${name}`,
32 + regex: regex as RegExp,
33 +}));
27 35 export function findStructureSpans(text: string): SecretSpan[] {
28 36 return [
45 53 for (const match of text.matchAll(QUERY_RE)) {
46 54 const value = match[3] ?? "";
47 55 if (isWeakValue(value)) continue;
48 - spans.push(makeSpan(match, 3, value, [label(match[2] ?? "QUERY_SECRET", "structure")], "structure"));
56 + spans.push(
57 + makeSpan(
58 + match,
59 + 3,
60 + value,
61 + [label(match[2] ?? "QUERY_SECRET", "structure")],
62 + "structure",
63 + ),
64 + );
50 66 return spans;
53 69 export function findHeaderSpans(text: string): SecretSpan[] {
54 70 return [
55 - ...matches(AUTH_RE, text, 2, "AUTHORIZATION", (match) => `AUTHORIZATION_${match[1]?.toUpperCase()}`),
71 + ...matches(
72 + AUTH_RE,
73 + text,
74 + 2,
75 + "AUTHORIZATION",
76 + (match) => `AUTHORIZATION_${match[1]?.toUpperCase()}`,
77 + ),
56 78 ...matches(X_API_RE, text, 2, "X_API_KEY"),
99 121 const value = match[2] ?? "";
100 122 if (!isSensitiveKey(key)) continue;
101 123 if (isWeakValue(value) && !highConfidenceSecretShape(value)) continue;
102 - spans.push(makeSpan(match, 2, value, [label(key, "structure")], "structure"));
124 + spans.push(
125 + makeSpan(match, 2, value, [label(key, "structure")], "structure"),
126 + );
105 129 return spans;
127 151 start,
128 152 end: start + match[0].length,
129 153 value: match[0],
130 - labels: [label((match[1] ?? "PRIVATE KEY").replace(/ /g, "_"), "builtin")],
154 + labels: [
155 + label((match[1] ?? "PRIVATE KEY").replace(/ /g, "_"), "builtin"),
156 + ],
131 157 kind: "private-key",
132 158 confidence: 100,
133 159 wholeSpan: true,
147 173 for (const match of text.matchAll(regex)) {
148 174 const value = match[group] ?? "";
149 175 if (isWeakValue(value)) continue;
150 - spans.push(makeSpan(match, group, value, [label(tag?.(match) ?? fallback, "structure")], "structure"));
176 + spans.push(
177 + makeSpan(
178 + match,
179 + group,
180 + value,
181 + [label(tag?.(match) ?? fallback, "structure")],
182 + "structure",
183 + ),
184 + );
152 186 return spans;
155 -function makeSpan(match: RegExpMatchArray, group: number, value: string, labels: SpanLabel[], kind: SecretSpan["kind"]): SecretSpan {
189 +function makeSpan(
190 + match: RegExpMatchArray,
191 + group: number,
192 + value: string,
193 + labels: SpanLabel[],
194 + kind: SecretSpan["kind"],
195 +): SecretSpan {
156 196 const range = match.indices?.[group];
157 197 const start = range?.[0] ?? 0;
158 198 return {

extensions/leaks/src/types.ts

Modified
25 25 end: number;
26 26 value: string;
27 27 labels: SpanLabel[];
28 - kind: "structure" | "exact" | "custom" | "builtin" | "generic" | "private-key" | "jwt";
28 + kind:
29 + | "structure"
30 + | "exact"
31 + | "custom"
32 + | "builtin"
33 + | "generic"
34 + | "private-key"
35 + | "jwt";
29 36 confidence: number;
30 37 wholeSpan: boolean;