repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

test leaks UI restoration symmetry

da7ff42f0cb895fb076b15a8f9ef04f02a3433d2

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (4)

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

Modified
30 +const LEAK_PLACEHOLDER_RE = /__LEAK_[A-Z0-9_]+_[a-f0-9]{12}(?:_\d+)?__/;
32 +function serialized(value: unknown) {
33 + return JSON.stringify(value);
36 +function expectLocalRestored(
37 + value: unknown,
38 + secret: string,
39 + placeholder: string,
40 +) {
41 + const text = serialized(value);
42 + expect(text).toContain(secret);
43 + expect(text).not.toContain(placeholder);
44 + expect(text).not.toMatch(LEAK_PLACEHOLDER_RE);
30 47 function ui(confirm = true) {
31 48 const notices: string[] = [];
32 49 const statuses: string[] = [];
166 183 ).toBe(true);
186 + it("restores every local Pi surface symmetrically", async () => {
187 + const runtime = fakePi();
188 + leaks(runtime.pi as never);
189 + const view = ui();
190 + await runtime.handlers.get("session_start")?.({}, view.ctx);
191 + const secret = "ghp_" + "S".repeat(36);
192 + const providerPayload = { nested: { text: secret } };
193 + const provider = await runtime.handlers.get("before_provider_request")?.(
194 + { payload: providerPayload },
195 + view.ctx,
196 + );
197 + const placeholder =
198 + serialized(provider).match(LEAK_PLACEHOLDER_RE)?.[0] ?? "";
199 + expect(placeholder).toBeTruthy();
200 + expectLocalRestored(providerPayload, secret, placeholder);
202 + const contextSource = [{ role: "tool", content: `ctx ${secret}` }];
203 + const context = await runtime.handlers.get("context")?.(
204 + { messages: contextSource },
205 + view.ctx,
206 + );
207 + expect(serialized(context)).toMatch(LEAK_PLACEHOLDER_RE);
208 + expectLocalRestored(contextSource, secret, placeholder);
210 + const messageUpdate = {
211 + message: {
212 + role: "assistant",
213 + content: [{ type: "text", text: `stream ${placeholder}` }],
214 + },
215 + };
216 + await runtime.handlers.get("message_update")?.(messageUpdate, view.ctx);
217 + expectLocalRestored(messageUpdate, secret, placeholder);
219 + const toolCall = {
220 + toolName: "read",
221 + input: { path: "file.txt", note: `call ${placeholder}` },
222 + };
223 + await runtime.handlers.get("tool_call")?.(toolCall, view.ctx);
224 + expectLocalRestored(toolCall, secret, placeholder);
226 + for (const hook of ["tool_execution_start", "tool_execution_update"]) {
227 + const event = { args: { command: `exec ${placeholder}` } };
228 + await runtime.handlers.get(hook)?.(event, view.ctx);
229 + expectLocalRestored(event, secret, placeholder);
232 + const toolResultEvent = {
233 + content: [{ type: "text", text: `result ${placeholder}` }],
234 + details: { preview: [`detail ${placeholder}`] },
235 + };
236 + const toolResult = await runtime.handlers.get("tool_result")?.(
237 + toolResultEvent,
238 + view.ctx,
239 + );
240 + expectLocalRestored(toolResultEvent, secret, placeholder);
241 + expectLocalRestored(toolResult, secret, placeholder);
243 + const messageEnd = await runtime.handlers.get("message_end")?.(
245 + message: {
246 + role: "assistant",
247 + content: [{ type: "text", text: `final ${placeholder}` }],
248 + },
249 + },
250 + view.ctx,
251 + );
252 + expectLocalRestored(messageEnd, secret, placeholder);
253 + });
169 255 it("blocks unresolved leak placeholders in mutating tools outside leaks", async () => {
170 256 const runtime = fakePi();
171 257 leaks(runtime.pi as never);

extensions/leaks/__tests__/structures.test.ts

Modified
143 + it("does not redact sensitive-name code references", () => {
144 + const engine = new __test.LeaksEngine();
145 + const samples = [
146 + "const token = loadToken(email);",
147 + "const key = match[1] as string;",
148 + "console.log(` access_token: ${token.access_token ? 'present' : 'missing'}`);",
149 + "const key = `${redaction.path}: ${redaction.labels.join(', ')}`;",
150 + "let session: BaseTestSession;",
151 + "const value: ${account.hasAccessToken = token.access_token;",
152 + ];
154 + for (const sample of samples) {
155 + expect(engine.redactText(sample, "provider").text, sample).toBe(sample);
156 + expect(__test.findAssignmentSpans(sample), sample).toEqual([]);
158 + });
143 160 it("stoplist does not block keys that overlap with SENSITIVE", () => {
144 161 const longValue = "abcdef1234567890";
145 162 // 'key' is in dotenv.SENSITIVE; assignment detection must still match.

extensions/leaks/__tests__/unit.test.ts

Modified
86 86 expect(JSON.stringify(result.redactions)).not.toContain(sensitiveValue);
89 + it("restores every provider redaction back to original text", () => {
90 + const engine = new __test.LeaksEngine();
91 + const secret = "ghp_" + "R".repeat(36);
92 + const original = {
93 + message: `send ${secret}`,
94 + nested: [{ text: `again ${secret}` }],
95 + };
97 + const redacted = engine.redactDeep(original, "provider");
98 + expect(JSON.stringify(redacted.value)).not.toContain(secret);
99 + expect(redacted.count).toBe(2);
101 + const restored = engine.restoreDeep(redacted.value);
102 + expect(restored.value).toEqual(original);
103 + expect(restored.count).toBe(2);
104 + });
89 106 it("fails open when a detector throws", () => {
90 107 const engine = new __test.LeaksEngine({
91 108 detectors: [

extensions/leaks/src/structures.ts

Modified
154 154 const value = match[2] as string;
155 155 if (ASSIGNMENT_STOP_KEYS.has(key.toLowerCase())) continue;
156 156 if (!isSensitiveKey(key)) continue;
157 + if (isCodeReference(value)) continue;
157 158 if (isWeakValue(value) && !highConfidenceSecretShape(value)) continue;
158 159 spans.push(
159 160 makeSpan(match, 2, value, [label(key, "structure")], "structure"),
198 +function isCodeReference(value: string) {
199 + const text = value.trim();
200 + return (
201 + text.startsWith("$") ||
202 + /[()[\]{}]/.test(text) ||
203 + /^[A-Z][A-Za-z0-9_$]*$/.test(text) ||
204 + /^(?:[A-Za-z_$][A-Za-z0-9_$]*\.)+[A-Za-z_$][A-Za-z0-9_$]*$/.test(text)
205 + );
197 208 function matches(
198 209 regex: RegExp,
199 210 text: string,