repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

fix(leaks): block unresolved placeholders in writes

861008b2d5e29b4367fbb0f1194343bbc9790455

Review <review@example.invalid> ·

unsigned

Browse filesCompare with HEAD

Changed files (3)

CHANGELOG.md

Modified
52 52 final answer, with regression and manual coverage for create-check-answer flow
53 53 - Fix goldfish task output semantics so active tasks render as accent `◌`, done
54 54 tasks as success `✓`, idle widgets as warning, and errors as error
55 +- Block unresolved `__LEAK_*__` placeholders in mutating tool calls outside the
56 + leaks extension
56 58 ## 0.0.3

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

Modified
154 154 view.notices.some((notice) => notice.includes("usage: /leaks")),
155 155 ).toBe(true);
158 + it("blocks unresolved leak placeholders in mutating tools outside leaks", async () => {
159 + const runtime = fakePi();
160 + leaks(runtime.pi as never);
161 + const view = ui();
162 + const placeholder = "__LEAK_TOKEN_abcdef123456__";
163 + const hook = runtime.handlers.get("tool_call");
164 + if (!hook) throw new Error("missing tool_call hook");
166 + const writeResult = await hook(
168 + type: "tool_call",
169 + toolName: "write",
170 + toolCallId: "write-2",
171 + input: { path: "README.md", content: placeholder },
172 + },
173 + view.ctx,
174 + );
175 + expect(writeResult).toEqual({
176 + block: true,
177 + reason:
178 + "Blocked: unresolved `__LEAK_*__` placeholder in write target outside `pi-ext/extensions/leaks/`. Ask user for real value, restore from source, or remove placeholder.",
179 + });
181 + const editResult = await hook(
183 + type: "tool_call",
184 + toolName: "edit",
185 + toolCallId: "edit-1",
186 + input: {
187 + path: "README.md",
188 + edits: [{ oldText: "old", newText: placeholder }],
189 + },
190 + },
191 + view.ctx,
192 + );
193 + expect(editResult).toMatchObject({ block: true });
195 + const oldTextOnly = await hook(
197 + type: "tool_call",
198 + toolName: "edit",
199 + toolCallId: "edit-2",
200 + input: {
201 + path: "README.md",
202 + edits: [{ oldText: placeholder, newText: "clean" }],
203 + },
204 + },
205 + view.ctx,
206 + );
207 + expect(oldTextOnly).toBeUndefined();
209 + const bashResult = await hook(
211 + type: "tool_call",
212 + toolName: "bash",
213 + toolCallId: "bash-1",
214 + input: { command: `printf %s ${placeholder}` },
215 + },
216 + view.ctx,
217 + );
218 + expect(bashResult).toMatchObject({ block: true });
219 + });
221 + it("allows unresolved leak placeholders inside the leaks extension", async () => {
222 + const runtime = fakePi();
223 + leaks(runtime.pi as never);
224 + const placeholder = "__LEAK_TOKEN_abcdef123456__";
225 + const root = path.resolve(import.meta.dirname, "..");
226 + const hook = runtime.handlers.get("tool_call");
227 + if (!hook) throw new Error("missing tool_call hook");
229 + await expect(
230 + hook(
232 + type: "tool_call",
233 + toolName: "write",
234 + toolCallId: "write-1",
235 + input: { path: path.join(root, "README.md"), content: placeholder },
236 + },
237 + ui().ctx,
238 + ),
239 + ).resolves.toBeUndefined();
240 + await expect(
241 + hook(
243 + type: "tool_call",
244 + toolName: "bash",
245 + toolCallId: "bash-1",
246 + input: { command: `printf %s ${placeholder}` },
247 + },
248 + { ...ui().ctx, cwd: root },
249 + ),
250 + ).resolves.toBeUndefined();
251 + });

extensions/leaks/src/pi.ts

Modified
47 47 restoreEventProperty(engine, event as Record<string, unknown>, "message");
50 - pi.on("tool_call", async (event) => {
50 + pi.on("tool_call", async (event, ctx) => {
51 51 restoreEventProperty(engine, event as Record<string, unknown>, "input");
52 + return blockUnresolvedPlaceholderToolCall(event, ctx.cwd);
54 55 pi.on("tool_execution_start", async (event) => {
126 127 Object.assign(target, source);
130 +const LEAK_PLACEHOLDER_RE = /__LEAK_[A-Za-z0-9_]*__/;
131 +const LEAKS_EXTENSION_ROOT = path.resolve(import.meta.dirname, "..");
132 +const BLOCK_REASON_SUFFIX =
133 + "outside `pi-ext/extensions/leaks/`. Ask user for real value, restore from source, or remove placeholder.";
135 +function blockUnresolvedPlaceholderToolCall(
136 + event: unknown,
137 + cwd: string,
138 +): { block: true; reason: string } | undefined {
139 + if (!isPlainObject(event)) return undefined;
140 + const toolName = event.toolName;
141 + const input = event.input;
142 + if (!isPlainObject(input)) return undefined;
144 + if (toolName === "bash") {
145 + if (containsLeakPlaceholder(input.command) && !isInLeaksExtension(cwd))
146 + return blockPlaceholder("bash command");
147 + return undefined;
150 + if (toolName === "write") {
151 + if (
152 + containsLeakPlaceholder(input.content) &&
153 + !isPathInLeaksExtension(cwd, input.path)
155 + return blockPlaceholder("write target");
156 + return undefined;
159 + if (toolName === "edit") {
160 + if (isPathInLeaksExtension(cwd, input.path)) return undefined;
161 + if (!Array.isArray(input.edits)) return undefined;
162 + if (input.edits.some((edit) => containsEditWritePlaceholder(edit)))
163 + return blockPlaceholder("edit target");
166 + return undefined;
169 +function containsEditWritePlaceholder(edit: unknown) {
170 + return isPlainObject(edit) && containsLeakPlaceholder(edit.newText);
173 +function containsLeakPlaceholder(value: unknown) {
174 + return typeof value === "string" && LEAK_PLACEHOLDER_RE.test(value);
177 +function blockPlaceholder(target: string) {
178 + return {
179 + block: true,
180 + reason: `Blocked: unresolved \`__LEAK_*__\` placeholder in ${target} ${BLOCK_REASON_SUFFIX}`,
181 + } as const;
184 +function isPathInLeaksExtension(cwd: string, file: unknown) {
185 + if (typeof file !== "string") return false;
186 + return isInLeaksExtension(path.resolve(cwd, file));
189 +function isInLeaksExtension(candidate: string) {
190 + const relative = path.relative(LEAKS_EXTENSION_ROOT, path.resolve(candidate));
191 + return (
192 + relative === "" ||
193 + (!relative.startsWith("..") && !path.isAbsolute(relative))
194 + );
129 197 function readSettings(cwd: string) {
130 198 const file = path.join(cwd, ".pi", "settings.json");
131 199 if (!existsSync(file)) return { regex: [] };