pi-ext
bugabingas pi extensions
owned by bugabinga
fix(leaks): close hydration and persistence gaps
3e8608d890c9b72ca35b603497f8fd682ba28086
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
Browse filesCompare with HEAD
Changed files (10)
extensions/leaks/__tests__/direct-modules.test.ts
Modified154 154 expect(selected.
map((item)
=> item.
value)).
toEqual([
"ab",
"abc"]);
156 156 const store
= new PlaceholderStore({
digest: ()
=> "abc" });
157 + store.
configure([], ()
=> {});
157 158 const placeholder
= store.
create(value, [
tag(
"DIRECT")]);
158 159 expect(store.
restore(placeholder)).
toBe(value);
203 204 onWarning: (warning)
=> capWarnings.
push(warning),
207 + capped.
configure([], ()
=> {});
206 208 for (
let index
= 0; index
< 3; index
++)
207 209 capped.
create(
`value-${index}`, [
tag(
"CAP")]);
extensions/leaks/__tests__/edge-coverage.test.ts
Modified 98 98 it(
"covers placeholder fallback and clear branches", ()
=> {
99 99 const store
= new __test.
PlaceholderStore({
digest: ()
=> "xyz" });
100 + store.
configure([], ()
=> {});
100 101 const placeholder
= store.
create(fixture, []);
101 102 expect(placeholder).
toBe(
102 103 "__LEAK_V1_SECRET_00000000000000000000000000000000__",
108 109 const collision
= new __test.
PlaceholderStore({
109 110 digest: ()
=> "abcdef123456",
112 + collision.
configure([], ()
=> {});
111 113 collision.
create(
"a", [
label()]);
112 114 expect(collision.
create(
"b", [
label()])).
toBeUndefined();
113 115 expect(collision.
create(
"c", [
label()])).
toBeUndefined();
extensions/leaks/__tests__/harness.test.ts
Modified 42 42 expect(messages[
0].
content).
toContain(secret);
45 + it(
"does not nest mappings across context and provider passes",
async ()
=> {
46 + const cwd
= path.
join(os.
tmpdir(),
`leaks-double-pass-${Date.now()}`);
47 + mkdirSync(path.
join(cwd,
".pi"), { recursive:
true });
49 + path.
join(cwd,
".pi",
"settings.json"),
54 + pattern:
"(__LEAK_V1_[A-Z0-9_]+_[a-f0-9]{32}__)",
55 + label:
"PLACEHOLDER",
61 + t
= await createTestSession({ cwd, extensions: [
extPath()] });
62 + const context
= await t.session.extensionRunner.
emitContext([
63 + { role:
"user", content: secret },
65 + const provider
= await t.session.extensionRunner.
emitBeforeProviderRequest({
68 + const placeholder
= JSON.
stringify(provider).
match(
69 + /__LEAK_V1_[A-Z0-9_]+_[a-f0-9]{32}__/,
71 + const mappings
= t.session.sessionManager
75 + entry.
type === "custom" && entry.customType
=== "leaks-mapping-v1",
77 + expect(mappings).
toHaveLength(
1);
81 + toolCallId:
"double-pass",
82 + input: { value: placeholder },
84 + const blocked
= await t.session.extensionRunner.
emitToolCall(
87 + expect(blocked).
toBeUndefined();
88 + expect(
event.
input.
value).
toBe(secret);
45 91 it(
"redacts provider payload authoritatively and preserves auth headers",
async ()
=> {
46 92 t
= await createTestSession({ extensions: [
extPath()] });
extensions/leaks/__tests__/lifecycle.test.ts
Modified 65 + it(
"allows shared acyclic objects", ()
=> {
66 + const engine
= configuredEngine();
67 + const shared
= { value:
"ordinary" };
69 + engine.
hydrateToolInput({ left: shared, right: shared }).issue,
65 73 it(
"leaves provider values visible when mapping append fails", ()
=> {
66 74 const engine
= new __test.
LeaksEngine({ detectors: [detector] });
74 82 expect(result.
text).
toBe(fixture);
75 83 expect(result.count).
toBe(
0);
76 84 expect(engine.
stats().warnings.
map((warning)
=> warning.
code)).
toContain(
77 - "placeholder.persistence",
85 + "placeholder.state",
extensions/leaks/__tests__/placeholders.test.ts
Modified134 134 ).
toBeUndefined();
137 + it(
"retains hydration but disables creation after failed clear", ()
=> {
138 + const entries
: PersistedEntry[]
= [];
140 + const store
= new __test.
PlaceholderStore();
141 + store.
configure(entries, (customType, data)
=> {
142 + if (fail)
throw new Error(
"disk full");
143 + entries.
push({ type:
"custom", customType, data });
145 + const placeholder
= store.
create(secretA, [
146 + label(
"TOKEN",
10,
"dotenv"),
151 + expect(store.
restore(placeholder)).
toBe(secretA);
153 + store.
create(secretB, [
label(
"TOKEN",
10,
"dotenv")]),
157 + it(
"retains authenticated mappings when an unsupported version disables creation", ()
=> {
158 + const { entries, store }
= persistentStore();
159 + const placeholder
= store.
create(secretA, [
160 + label(
"TOKEN",
10,
"dotenv"),
164 + customType:
"leaks-generation-v2",
165 + data: { version:
2 },
167 + const resumed
= new __test.
PlaceholderStore();
168 + resumed.
configure(entries, ()
=> {});
170 + expect(resumed.
restore(placeholder)).
toBe(secretA);
172 + resumed.
create(secretB, [
label(
"TOKEN",
10,
"dotenv")]),
176 + it(
"rejects noncanonical persisted encodings and malformed bounds", ()
=> {
177 + const { entries, store }
= persistentStore();
178 + store.
create(secretA, [
label(
"TOKEN",
10,
"dotenv")]);
179 + const mutations
= [
180 + (data
: Record<
string,
unknown>)
=> {
181 + data.nonce
= `${data.nonce}=`;
183 + (data
: Record<
string,
unknown>)
=> {
184 + data.authTag
= "AA";
186 + (data
: Record<
string,
unknown>)
=> {
187 + data.placeholder
= `${data.placeholder}suffix`;
190 + for (
const mutate
of mutations) {
191 + const copy
= structuredClone(entries);
192 + mutate(copy[
1].
data as Record<
string,
unknown>);
193 + const resumed
= new __test.
PlaceholderStore();
194 + resumed.
configure(copy, ()
=> {});
195 + expect(resumed.
stats().persistenceEnabled).
toBe(
false);
137 199 it(
"restores exact known placeholders and reports unknown ones", ()
=> {
138 200 const { store }
= persistentStore();
139 201 const placeholder
= store.
create(secretA, [
extensions/leaks/src/engine.ts
Modified 37 37 private readonly
detectors: Detector[];
38 38 private readonly
warnings: WarningRecord[]
= [];
39 39 private readonly warningKeys
= new Set<
string>();
40 + private readonly
ephemeralEntries: PersistedEntry[]
= [];
40 41 private readonly placeholders
= new PlaceholderStore({
41 42 onWarning: (warning)
=> this.
addWarning(warning),
50 51 readonly notifiedPlaceholders
= new Set<
string>();
52 53 constructor(options: LeaksEngineOptions
= {}) {
54 + this.placeholders.
configure(this.ephemeralEntries, (customType, data)
=> 55 + this.ephemeralEntries.
push({ type:
"custom", customType, data }),
53 57 this.settings
= { regex: options.settings?.regex
?? [] };
54 58 this.detectors
= options.detectors
?? [];
55 59 this.
compileSettings();
119 123 if (labels.
length > 0) this.recentLabels
= labels.
slice(
-12);
120 124 if (placeholders.
length > 0) {
121 125 this.latestTags
= placeholders.
map((placeholder)
=> 123 - .
replace(
/^__LEAK_?/,
"")
124 - .
replace(
/_[a-f0-9]{12}(?:_\d+)?__$/,
""),
126 + placeholder.
replace(
/^__LEAK_V1_/,
"").
replace(
/_[a-f0-9]{32}__$/,
""),
127 129 return { text: redacted, count, labels, placeholders, redactions };
170 172 if (
Array.
isArray(item)) {
171 173 for (
let index
= 0; index
< item.
length; index
++) {
172 174 const issue
= inspect(item[index], depth
+ 1,
`${path}[${index}]`);
173 - if (issue)
return issue;
175 181 return undefined;
177 - if (
!isPlainObject(item))
183 + if (
!isPlainObject(item)) {
178 185 return { path: path
|| "$", label:
"UNSUPPORTED_OBJECT" };
179 187 for (
const [key, child]
of Object.
entries(item)) {
180 188 const issue
= inspect(child, depth
+ 1,
childPath(path, key));
181 - if (issue)
return issue;
183 195 return undefined;
185 197 return { value: restored, issue:
inspect(restored,
0,
"") };
extensions/leaks/src/pi.ts
Modified 35 35 return result.
value;
38 - pi.
on(
"message_update",
async (event)
=> {
38 + pi.
on(
"message_update",
async (event, ctx)
=> {
39 39 restoreEventProperty(
41 41 event as unknown as Record<
string,
unknown>,
44 + warnUnknownLocal(engine,
event.message, ctx,
"assistant message");
46 47 pi.
on(
"tool_call",
async (event, ctx)
=> {
76 - pi.
on(
"tool_result",
async (event)
=> {
77 - return restoreToolResult(
77 + pi.
on(
"tool_result",
async (event, ctx)
=> {
78 + const patch
= restoreToolResult(
79 80 event as unknown as Record<
string,
unknown>,
82 + warnUnknownLocal(engine,
event.
content, ctx,
"tool result");
83 + warnUnknownLocal(engine,
event.details, ctx,
"tool result");
83 - pi.
on(
"message_end",
async (event)
=> {
87 + pi.
on(
"message_end",
async (event, ctx)
=> {
84 88 const restored
= engine.
restoreDeep(
event.message).
value;
89 + warnUnknownLocal(engine, restored, ctx,
"assistant message");
85 90 return { message: restored };
90 95 async
handler(args, ctx) {
91 96 const command
= args.
trim();
92 97 if (command
=== "status" || command
=== "") {
93 - ctx.ui.
notify(
formatStatus(engine),
"info");
98 + notify(ctx,
formatStatus(engine),
"info");
96 101 if (command
=== "reload") {
97 102 rebuildPersistence(engine, pi, ctx);
98 103 engine.
reload(ctx.cwd,
readSettings(ctx.cwd),
process.
env);
99 104 flushWarnings(engine, ctx, reportedWarnings);
100 - ctx.ui.
notify(
"leaks reloaded",
"info");
105 + notify(ctx,
"leaks reloaded",
"info");
103 108 if (command
=== "clear") {
107 112 "Old placeholders become unrestorable.",
109 114 if (
!confirmed) {
110 - ctx.ui.
notify(
"clear cancelled",
"info");
115 + notify(ctx,
"clear cancelled",
"info");
115 120 flushWarnings(engine, ctx, reportedWarnings);
116 - ctx.ui.
notify(
"leaks cleared",
"info");
121 + notify(ctx,
"leaks cleared",
"info");
119 124 if (command.
startsWith(
"test ")) {
120 125 const result
= engine.
redactText(command.
slice(
5),
"diagnostic");
121 - ctx.ui.
notify(
`matches: ${result.count}\n${result.text}`,
"info");
126 + notify(ctx,
`matches: ${result.count}`,
"info");
124 - ctx.ui.
notify(
"usage: /leaks status|reload|test <text>|clear",
"warning");
129 + notify(ctx,
"usage: /leaks status|reload|test <text>|clear",
"warning");
204 209 Object.
assign(target, source);
212 +function warnUnknownLocal(
213 + engine
: LeaksEngine,
215 + ctx
: ExtensionContext,
218 + if (value
=== undefined)
return;
219 + const issue
= engine.
hydrateToolInput(value).issue;
220 + if (
!issue
|| issue.
label.
startsWith(
"TRAVERSAL_"))
return;
223 + `unresolved leaks placeholder in ${surface} at ${issue.path} (${issue.label})`,
207 228 function rebuildPersistence(
208 229 engine
: LeaksEngine,
209 230 pi
: ExtensionAPI,
252 273 severity:
"info" | "warning" | "error",
254 275 if (ctx.hasUI) ctx.ui.
notify(message, severity);
255 - else console.
error(
`[leaks] ${message}`);
276 + else console.
error(
`[leaks] ${message.replace(/\s*\n\s*/g, " | ")}`);
258 279 function readSettings(cwd
: string) {
extensions/leaks/src/placeholders.ts
Modified 7 7 } from
"node:crypto";
8 -import { sanitizeTag }
from "./safety.ts";
9 + ANY_PLACEHOLDER_SOURCE,
11 + V1_PLACEHOLDER_SOURCE,
12 +}
from "./safety.ts";
9 13 import type { SpanLabel, WarningRecord }
from "./types.ts";
12 16 const GENERATION_TYPE
= "leaks-generation-v1";
13 17 const MAPPING_TYPE
= "leaks-mapping-v1";
14 -const PLACEHOLDER_RE
= /__LEAK_V1(?:_[A-Z0-9_]{1,64}){1,6}_[a-f0-9]{32}__/g;
15 -export const ANY_PLACEHOLDER_RE
= /__LEAK_[A-Za-z0-9_]+__/g;
18 +const PLACEHOLDER_RE
= new RegExp(V1_PLACEHOLDER_SOURCE,
"g");
19 +const EXACT_PLACEHOLDER_RE
= new RegExp(
`^(?:${V1_PLACEHOLDER_SOURCE})$`);
20 +export const ANY_PLACEHOLDER_RE
= new RegExp(ANY_PLACEHOLDER_SOURCE,
"g");
16 21 const SOURCE_RANK
: Record<
SpanLabel[
"source"],
number>
= {
69 74 this.digestOverride
= options.digest;
70 75 this.onWarning
= options.onWarning;
71 76 this.cap
= options.cap
?? CAP;
72 - this.append
= options.append;
73 - this.
startGeneration();
77 + if (options.append) this.
configure([], options.append);
76 80 configure(entries: PersistedEntry[], append: AppendEntry) {
83 87 else if (entry.customType.
startsWith(
"leaks-generation-"))
84 88 this.
disable(
"unsupported leaks generation version");
86 - if (
!this.enabled)
return;
87 90 if (generationIndex
< 0) {
88 - this.
startGeneration();
91 + if (this.enabled) this.
startGeneration();
91 94 const generation
= parseGeneration(entries[generationIndex].
data);
145 148 const record
= this.
encrypt(placeholder, value);
146 - this.
append?.(MAPPING_TYPE, record);
149 + if (
!this.append)
throw new Error(
"missing persistence sink");
150 + this.
append(MAPPING_TYPE, record);
149 - "placeholder.persistence",
150 - "mapping persistence failed; value left visible",
152 + this.
disable(
"mapping persistence failed");
152 153 return undefined;
154 155 this.secretToPlaceholder.
set(value, placeholder);
159 160 restore(text: string): string {
160 - return text.
replace(
162 - (placeholder)
=> this.placeholderToSecret.
get(placeholder)
?? placeholder,
161 + let current
= text;
162 + const seen
= new Set<
string>();
163 + for (
let pass
= 0; pass
<= this.placeholderToSecret.
size; pass
++) {
164 + if (seen.
has(current))
return current;
166 + const next
= current.
replace(
169 + this.placeholderToSecret.
get(placeholder)
?? placeholder,
171 + if (next
=== current)
return current;
166 177 unresolved(text: string) {
167 - return [
...text.
matchAll(ANY_PLACEHOLDER_RE)]
168 - .
map((match)
=> match[
0])
169 - .
filter((placeholder)
=> !this.placeholderToSecret.
has(placeholder));
178 + return [
...text.
matchAll(ANY_PLACEHOLDER_RE)].
map((match)
=> match[
0]);
173 182 const generation
= newGeneration();
175 - this.
append?.(GENERATION_TYPE, generation);
184 + if (
!this.append)
throw new Error(
"missing persistence sink");
185 + this.
append(GENERATION_TYPE, generation);
177 187 this.
activate(generation);
180 - "placeholder.persistence",
181 - "generation persistence failed; mappings kept active",
189 + this.
disable(
"generation persistence failed; mappings kept active");
194 201 private
startGeneration() {
195 202 const generation
= newGeneration();
197 - this.
append?.(GENERATION_TYPE, generation);
204 + if (
!this.append)
throw new Error(
"missing persistence sink");
205 + this.
append(GENERATION_TYPE, generation);
198 206 this.
activate(generation);
200 208 this.
disable(
"generation persistence failed");
249 257 this.encryptionKey
as Buffer,
259 + {
authTagLength:
16 },
252 261 cipher.
setAAD(
aad(generation.generationId, placeholder));
253 262 const ciphertext
= Buffer.
concat([
271 280 this.encryptionKey
as Buffer,
272 281 Buffer.
from(record.nonce,
"base64url"),
282 + {
authTagLength:
16 },
274 284 decipher.
setAAD(
aad(record.generationId, record.placeholder));
275 285 decipher.
setAuthTag(
Buffer.
from(record.authTag,
"base64url"));
326 336 !/^[a-f0-9]{32}$/.
test(data.generationId)
328 338 return undefined;
330 - typeof data.rootKey
!== "string" || 331 - Buffer.
from(data.rootKey,
"base64url").
length !== 32 339 + if (
typeof data.rootKey
!== "string" || !canonicalBase64Url(data.rootKey,
32))
333 340 return undefined;
334 341 return data
as GenerationRecord;
346 353 if (
typeof data[key]
!== "string")
return undefined;
347 - PLACEHOLDER_RE.
lastIndex = 0;
348 - const validPlaceholder
= PLACEHOLDER_RE.
test(data.placeholder
as string);
349 - PLACEHOLDER_RE.
lastIndex = 0;
350 - if (
!validPlaceholder)
return undefined;
354 + if (
!EXACT_PLACEHOLDER_RE.
test(data.placeholder
as string))
return undefined;
355 + if (
!/^[a-f0-9]{32}$/.
test(data.generationId
as string))
return undefined;
356 + if (
!canonicalBase64Url(data.nonce
as string,
12))
return undefined;
357 + if (
!canonicalBase64Url(data.authTag
as string,
16))
return undefined;
358 + if (
!canonicalBase64Url(data.ciphertext
as string))
return undefined;
351 359 return data
as MappingRecord;
362 +function canonicalBase64Url(value
: string, length
?: number) {
363 + if (
!/^[A-Za-z0-9_-]*$/.
test(value))
return false;
364 + const decoded
= Buffer.
from(value,
"base64url");
366 + (length
=== undefined || decoded.
length === length)
&& 367 + decoded.
toString(
"base64url")
=== value
354 371 function isRecord(value
: unknown)
: value
is Record<
string,
unknown> {
355 372 return value
!== null && typeof value
=== "object" && !Array.
isArray(value);
extensions/leaks/src/safety.ts
Modified 1 -const PLACEHOLDER_RE
= /^__LEAK(?:_[A-Z0-9]{1,64})*_[a-f0-9]{12}(?:_\d+)?__$/;
1 +export const V1_PLACEHOLDER_SOURCE
= 2 + "__LEAK_V1(?:_[A-Z0-9_]{1,64}){1,6}_[a-f0-9]{32}__";
3 +export const ANY_PLACEHOLDER_SOURCE
= 4 + "__LEAK_(?:V1(?:_[A-Z0-9_]{1,64}){1,6}_[a-f0-9]{32}|(?:[A-Z0-9]{1,64}_)*[a-f0-9]{12}(?:_\\d+)?)__";
5 +const PLACEHOLDER_RE
= new RegExp(
`^(?:${ANY_PLACEHOLDER_SOURCE})$`);
3 7 export function isPlainObject(
extensions/leaks/src/selector.ts
Modified 1 +import { ANY_PLACEHOLDER_SOURCE }
from "./safety.ts";
1 2 import type { SecretSpan, SpanLabel }
from "./types.ts";
3 -const PLACEHOLDER_RE
= /__LEAK(?:_[A-Z0-9]{1,64})*_[a-f0-9]{12}(?:_\d+)?__/g;
4 +const PLACEHOLDER_RE
= new RegExp(ANY_PLACEHOLDER_SOURCE,
"g");
4 5 const KIND_RANK
: Record<
SecretSpan[
"kind"],
number>
= {
23 24 for (
const span
of spans) {
24 25 if (span.
start < 0 || span.end
<= span.
start || span.end
> text.
length)
27 - placeholderRanges.
some(
28 - (range)
=> span.
start >= range.
start && span.end
<= range.end,
27 + if (placeholderRanges.
some((range)
=> overlaps(range, span)))
continue;
32 28 const key
= `${span.start}:${span.end}`;
33 29 const current
= merged.
get(key);
72 -function overlaps(left
: SecretSpan, right
: SecretSpan) {
69 + left
: Pick<
SecretSpan,
"start" | "end">,
70 + right
: Pick<
SecretSpan,
"start" | "end">,
73 72 return left.
start < right.end
&& right.
start < left.end;