ceulen 0.10.1 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +107 -1
- package/README.md +56 -1
- package/extensions/index.ts +5 -1
- package/extensions/lib/env.test.ts +147 -0
- package/extensions/lib/env.ts +180 -0
- package/extensions/lib/order.test.ts +74 -0
- package/extensions/lib/plan-bridge.ts +18 -0
- package/extensions/lib/registry.test.ts +8 -5
- package/extensions/lib/registry.ts +22 -3
- package/extensions/modules/a2a/configPanel.ts +42 -1
- package/extensions/modules/a2a/index.ts +4 -2
- package/extensions/modules/a2a/lib/config.ts +8 -3
- package/extensions/modules/a2a/test/configPanel.test.ts +7 -4
- package/extensions/modules/advisor/index.ts +6 -3
- package/extensions/modules/advisor/test/module.test.ts +31 -0
- package/extensions/modules/attachments/configPanel.ts +91 -0
- package/extensions/modules/attachments/index.ts +274 -0
- package/extensions/modules/attachments/lib/clipboard-files.ts +80 -0
- package/extensions/modules/attachments/lib/pastes.ts +45 -0
- package/extensions/modules/attachments/lib/paths.ts +74 -0
- package/extensions/modules/attachments/lib/registry.ts +70 -0
- package/extensions/modules/attachments/lib/settings.ts +99 -0
- package/extensions/modules/attachments/lib/tray.ts +74 -0
- package/extensions/modules/attachments/test/configPanel.test.ts +122 -0
- package/extensions/modules/attachments/test/index.test.ts +870 -0
- package/extensions/modules/attachments/test/module.test.ts +53 -0
- package/extensions/modules/classifier/index.ts +55 -1
- package/extensions/modules/classifier/test/classifier.test.ts +62 -0
- package/extensions/modules/composer/test/module.test.ts +2 -2
- package/extensions/modules/config/index.ts +3 -0
- package/extensions/modules/config/test/renderTools.test.ts +1 -1
- package/extensions/modules/cron/configPanel.ts +103 -0
- package/extensions/modules/cron/index.ts +638 -0
- package/extensions/modules/cron/lib/jobs.ts +201 -0
- package/extensions/modules/cron/lib/schedule.ts +161 -0
- package/extensions/modules/cron/test/cwd-repro.test.ts +98 -0
- package/extensions/modules/cron/test/index.test.ts +859 -0
- package/extensions/modules/cron/test/schedule.test.ts +93 -0
- package/extensions/modules/munin/index.ts +3 -1
- package/extensions/modules/permission/index.ts +567 -0
- package/extensions/modules/permission/test/permission.test.ts +789 -0
- package/extensions/modules/plan/index.ts +5 -0
- package/extensions/modules/repair/index.ts +160 -5
- package/extensions/modules/repair/test/freshness.test.ts +209 -0
- package/extensions/modules/repair/test/guard-selector.test.ts +67 -0
- package/extensions/modules/router/configPanel.ts +4 -4
- package/extensions/modules/router/index.ts +1 -1
- package/extensions/modules/router/lib/config.ts +9 -4
- package/extensions/modules/router/lib/systemone.ts +17 -4
- package/extensions/modules/router/test/unit.test.ts +46 -1
- package/extensions/modules/rules/index.ts +13 -3
- package/extensions/modules/rules/lib/rules.ts +24 -12
- package/extensions/modules/rules/test/lib.test.ts +20 -0
- package/extensions/modules/rules/test/module.test.ts +21 -0
- package/extensions/modules/subagent/index.ts +49 -10
- package/extensions/modules/subagent/lib/herdr.ts +5 -0
- package/extensions/modules/subagent/lib/routing.ts +31 -8
- package/extensions/modules/subagent/lib/runner.ts +356 -14
- package/extensions/modules/subagent/test/routing.test.ts +45 -0
- package/extensions/modules/subagent/test/worktree.test.ts +230 -0
- package/extensions/modules/usage/index.ts +193 -71
- package/extensions/modules/usage/test/context.test.ts +160 -120
- package/extensions/modules/web/configPanel.ts +67 -8
- package/extensions/modules/web/index.ts +3 -1
- package/extensions/modules/web/test/a11y.test.ts +1 -1
- package/extensions/modules/web/test/configPanel.test.ts +7 -3
- package/extensions/modules/web/test/settings.test.ts +6 -0
- package/package.json +2 -2
- package/skills/cron/SKILL.md +58 -0
|
@@ -0,0 +1,789 @@
|
|
|
1
|
+
// permission module — ported from pi-permission 0.2.10's node:test suite
|
|
2
|
+
// (extensions/test/permission.test.js), converted to TS. Plus the ceulen
|
|
3
|
+
// delta tests: plan-mode deferral via the shared plan-bridge flag.
|
|
4
|
+
import assert from "node:assert/strict";
|
|
5
|
+
import test from "node:test";
|
|
6
|
+
import { mkdtempSync, mkdirSync, writeFileSync, rmSync, readFileSync, existsSync } from "node:fs";
|
|
7
|
+
import { join, win32 } from "node:path";
|
|
8
|
+
import { tmpdir } from "node:os";
|
|
9
|
+
|
|
10
|
+
import permissionModule, {
|
|
11
|
+
wildcardToRegex,
|
|
12
|
+
resolveRule,
|
|
13
|
+
expandHome,
|
|
14
|
+
readSettingsKey,
|
|
15
|
+
persistAllowlistRule,
|
|
16
|
+
isExternal,
|
|
17
|
+
} from "../index.ts";
|
|
18
|
+
import { isPlanActive, setPlanActive } from "../../../lib/plan-bridge.ts";
|
|
19
|
+
|
|
20
|
+
// ── Extension wiring (tool_call handler) ──────────────────────────────────
|
|
21
|
+
|
|
22
|
+
interface FakePi {
|
|
23
|
+
flags: Map<string, boolean>;
|
|
24
|
+
flagOpts: Record<string, unknown>;
|
|
25
|
+
getFlag(name: string): unknown;
|
|
26
|
+
registerFlag(name: string): void;
|
|
27
|
+
getSetting(name: string): unknown;
|
|
28
|
+
config: Record<string, unknown>;
|
|
29
|
+
handlers: Record<string, (event: unknown, ctx: unknown) => Promise<unknown>>;
|
|
30
|
+
handler: (event: unknown, ctx: unknown) => Promise<unknown>;
|
|
31
|
+
sessionStart: (event: unknown, ctx: unknown) => Promise<unknown>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function harness({ rules = {}, flags = {}, getFlagThrows = false }:
|
|
35
|
+
{ rules?: Record<string, unknown> | undefined; flags?: Record<string, boolean>; getFlagThrows?: boolean } = {}): FakePi {
|
|
36
|
+
const pi = {
|
|
37
|
+
flags: new Map<string, boolean>(),
|
|
38
|
+
flagOpts: flags,
|
|
39
|
+
getFlag(this: FakePi, name: string) {
|
|
40
|
+
// Regression: after session replacement/reload the SDK's runtime is
|
|
41
|
+
// stale and getFlag throws — load-time capture must swallow it.
|
|
42
|
+
if (getFlagThrows) {
|
|
43
|
+
throw new Error("This extension ctx is stale after session replacement or reload.");
|
|
44
|
+
}
|
|
45
|
+
return this.flagOpts[name];
|
|
46
|
+
},
|
|
47
|
+
registerFlag(this: FakePi, name: string) {
|
|
48
|
+
this.flags.set(name, true);
|
|
49
|
+
},
|
|
50
|
+
getSetting(name: string) {
|
|
51
|
+
return name === "permission" ? rules : undefined;
|
|
52
|
+
},
|
|
53
|
+
config: {},
|
|
54
|
+
handlers: {} as Record<string, (event: unknown, ctx: unknown) => Promise<unknown>>,
|
|
55
|
+
handler: null as unknown as (event: unknown, ctx: unknown) => Promise<unknown>,
|
|
56
|
+
sessionStart: null as unknown as (event: unknown, ctx: unknown) => Promise<unknown>,
|
|
57
|
+
on(this: Record<string, unknown>, eventName: string, handler: (event: unknown, ctx: unknown) => Promise<unknown>) {
|
|
58
|
+
(this.handlers as Record<string, (event: unknown, ctx: unknown) => Promise<unknown>>)[eventName] = handler;
|
|
59
|
+
this.handler = handler; // keep legacy alias (last-registered) for old tests
|
|
60
|
+
},
|
|
61
|
+
} as unknown as FakePi;
|
|
62
|
+
permissionModule(pi as never);
|
|
63
|
+
const wires = pi as unknown as Record<string, unknown>;
|
|
64
|
+
wires.sessionStart = pi.handlers["session_start"];
|
|
65
|
+
wires.handler = pi.handlers["tool_call"];
|
|
66
|
+
return pi;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
interface TestCtx {
|
|
70
|
+
hasUI: boolean;
|
|
71
|
+
cwd: string;
|
|
72
|
+
home?: string;
|
|
73
|
+
ppath?: typeof import("node:path").posix | typeof import("node:path").win32;
|
|
74
|
+
isProjectTrusted?: () => boolean;
|
|
75
|
+
notifies: { m: string; t: string }[];
|
|
76
|
+
ui: {
|
|
77
|
+
notify(m: string, t: string): void;
|
|
78
|
+
select(title: string, opts: string[]): Promise<string | undefined>;
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function ctx({ hasUI = true, selectChoice = "Allow once", cwd = "/proj", home = "/home/user" }:
|
|
83
|
+
{ hasUI?: boolean; selectChoice?: string | undefined; cwd?: string; home?: string | undefined } = {}): TestCtx {
|
|
84
|
+
const notifies: { m: string; t: string }[] = [];
|
|
85
|
+
let selected: string | undefined = selectChoice;
|
|
86
|
+
return {
|
|
87
|
+
hasUI,
|
|
88
|
+
cwd,
|
|
89
|
+
home,
|
|
90
|
+
notifies,
|
|
91
|
+
ui: {
|
|
92
|
+
notify(m: string, t: string) {
|
|
93
|
+
notifies.push({ m, t });
|
|
94
|
+
},
|
|
95
|
+
async select(_title: string, _opts: string[]) {
|
|
96
|
+
return selected;
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// ── Settings reading ──────────────────────────────────────────────────────
|
|
103
|
+
|
|
104
|
+
test("readSettingsKey reads .pi/settings.json from cwd (production path)", () => {
|
|
105
|
+
const dir = mkdtempSync(join(tmpdir(), "perm-settings-"));
|
|
106
|
+
try {
|
|
107
|
+
mkdirSync(join(dir, ".pi"), { recursive: true });
|
|
108
|
+
writeFileSync(join(dir, ".pi", "settings.json"), JSON.stringify({ permission: { bash: { "*": "deny" } } }));
|
|
109
|
+
const key = readSettingsKey(dir, "permission");
|
|
110
|
+
assert.deepEqual(key, { bash: { "*": "deny" } });
|
|
111
|
+
} finally {
|
|
112
|
+
rmSync(dir, { recursive: true, force: true });
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
test("readSettingsKey returns undefined when no settings file", () => {
|
|
117
|
+
const dir = mkdtempSync(join(tmpdir(), "perm-settings-"));
|
|
118
|
+
try {
|
|
119
|
+
assert.equal(readSettingsKey(dir, "permission"), undefined);
|
|
120
|
+
} finally {
|
|
121
|
+
rmSync(dir, { recursive: true, force: true });
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
test("readSettingsKey treats non-object values as misconfig (undefined)", () => {
|
|
126
|
+
const dir = mkdtempSync(join(tmpdir(), "perm-settings-"));
|
|
127
|
+
try {
|
|
128
|
+
mkdirSync(join(dir, ".pi"), { recursive: true });
|
|
129
|
+
// String value is invalid config — must not be returned.
|
|
130
|
+
writeFileSync(join(dir, ".pi", "settings.json"), JSON.stringify({ permission: "ask" }));
|
|
131
|
+
assert.equal(readSettingsKey(dir, "permission"), undefined);
|
|
132
|
+
// Array value is also invalid.
|
|
133
|
+
writeFileSync(join(dir, ".pi", "settings.json"), JSON.stringify({ permission: ["bash"] }));
|
|
134
|
+
assert.equal(readSettingsKey(dir, "permission"), undefined);
|
|
135
|
+
} finally {
|
|
136
|
+
rmSync(dir, { recursive: true, force: true });
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
test("readSettingsKey: project=false skips cwd/.pi (trust gate)", () => {
|
|
141
|
+
const dir = mkdtempSync(join(tmpdir(), "perm-settings-"));
|
|
142
|
+
try {
|
|
143
|
+
mkdirSync(join(dir, ".pi"), { recursive: true });
|
|
144
|
+
writeFileSync(join(dir, ".pi", "settings.json"), JSON.stringify({ permission: { "*": "allow" } }));
|
|
145
|
+
// Untrusted project: cwd/.pi must be ignored entirely.
|
|
146
|
+
assert.equal(readSettingsKey(dir, "permission", { project: false }), undefined);
|
|
147
|
+
// Trusted project: cwd/.pi is read (shadowing global, as before).
|
|
148
|
+
assert.deepEqual(readSettingsKey(dir, "permission", { project: true }), { "*": "allow" });
|
|
149
|
+
} finally {
|
|
150
|
+
rmSync(dir, { recursive: true, force: true });
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
test("untrusted cwd ignores project .pi/settings.json (tool_call trust gate)", async () => {
|
|
155
|
+
const dir = mkdtempSync(join(tmpdir(), "perm-trust-"));
|
|
156
|
+
try {
|
|
157
|
+
mkdirSync(join(dir, ".pi"), { recursive: true });
|
|
158
|
+
// Untrusted repo ships a blanket allow — must be ignored.
|
|
159
|
+
writeFileSync(join(dir, ".pi", "settings.json"), JSON.stringify({ permission: { "*": "allow" } }));
|
|
160
|
+
const pi = harness({ rules: { bash: { "*": "ask", "rm *": "deny" } } });
|
|
161
|
+
const untrusted = ctx({ cwd: dir, selectChoice: "Deny" });
|
|
162
|
+
untrusted.isProjectTrusted = () => false;
|
|
163
|
+
const blocked = (await pi.handler({ toolName: "bash", input: { command: "git push" } }, untrusted)) as { block: boolean; reason: string };
|
|
164
|
+
assert.equal(blocked.block, true, "global ask still fires when project settings are untrusted");
|
|
165
|
+
assert.match(blocked.reason, /denied by user/);
|
|
166
|
+
// Same cwd, trusted: the project's blanket allow now shadows global.
|
|
167
|
+
const trusted = ctx({ cwd: dir });
|
|
168
|
+
trusted.isProjectTrusted = () => true;
|
|
169
|
+
assert.equal(await pi.handler({ toolName: "bash", input: { command: "git push" } }, trusted), undefined);
|
|
170
|
+
} finally {
|
|
171
|
+
rmSync(dir, { recursive: true, force: true });
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
test("untrusted cwd: permanent allowlist persists to global scope, never cwd/.pi (0.2.9)", async () => {
|
|
176
|
+
const cwd = mkdtempSync(join(tmpdir(), "perm-untrusted-"));
|
|
177
|
+
const agentDir = mkdtempSync(join(tmpdir(), "perm-global-"));
|
|
178
|
+
const savedAgentDir = process.env.PI_CODING_AGENT_DIR;
|
|
179
|
+
process.env.PI_CODING_AGENT_DIR = agentDir;
|
|
180
|
+
try {
|
|
181
|
+
const pi = harness({ rules: { bash: { "*": "ask" } } });
|
|
182
|
+
const c = ctx({ cwd, selectChoice: "Add to permanent allowlist" });
|
|
183
|
+
c.isProjectTrusted = () => false;
|
|
184
|
+
assert.equal(await pi.handler({ toolName: "bash", input: { command: "git push" } }, c), undefined);
|
|
185
|
+
// cwd/.pi untouched; rule landed in the global agent dir.
|
|
186
|
+
assert.equal(existsSync(join(cwd, ".pi", "settings.json")), false, "untrusted repo file not created");
|
|
187
|
+
const global = JSON.parse(readFileSync(join(agentDir, "settings.json"), "utf8"));
|
|
188
|
+
assert.deepEqual(global.permission.bash, { "git push": "allow" });
|
|
189
|
+
// The persisted rule suppresses the next identical ask (read path is global).
|
|
190
|
+
const c2 = ctx({ cwd, selectChoice: "Deny" });
|
|
191
|
+
c2.isProjectTrusted = () => false;
|
|
192
|
+
assert.equal(await pi.handler({ toolName: "bash", input: { command: "git push" } }, c2), undefined, "no re-prompt");
|
|
193
|
+
} finally {
|
|
194
|
+
if (savedAgentDir === undefined) delete process.env.PI_CODING_AGENT_DIR;
|
|
195
|
+
else process.env.PI_CODING_AGENT_DIR = savedAgentDir;
|
|
196
|
+
rmSync(cwd, { recursive: true, force: true });
|
|
197
|
+
rmSync(agentDir, { recursive: true, force: true });
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
test("one-time warning on invalid rule shapes", async () => {
|
|
202
|
+
const pi = harness({ rules: { bash: { "*": "ask", "git *": "allow" }, external_directory: "/ext" } });
|
|
203
|
+
const c = ctx();
|
|
204
|
+
await pi.handler({ toolName: "bash", input: { command: "ls" } }, c);
|
|
205
|
+
assert.equal(c.notifies.length, 1, "warned once");
|
|
206
|
+
assert.match(c.notifies[0]!.m, /external_directory must be a rule object/);
|
|
207
|
+
// One-time: second call stays silent.
|
|
208
|
+
await pi.handler({ toolName: "bash", input: { command: "ls" } }, c);
|
|
209
|
+
assert.equal(c.notifies.length, 1, "no repeat warning");
|
|
210
|
+
|
|
211
|
+
const pi2 = harness({ rules: { bash: { "*": "ask", "git *": "block" } } });
|
|
212
|
+
const c2 = ctx();
|
|
213
|
+
// The warning latch is module-scoped (one extension instance per process):
|
|
214
|
+
// reset it the way a new session would, then trigger the unknown action.
|
|
215
|
+
await pi2.sessionStart({}, c2);
|
|
216
|
+
await pi2.handler({ toolName: "bash", input: { command: "git status" } }, c2);
|
|
217
|
+
assert.match(c2.notifies[0]!.m, /unknown action "block"/);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
test("valid rules never warn", async () => {
|
|
221
|
+
const pi = harness({ rules: { bash: { "*": "ask", "git *": "allow" } } });
|
|
222
|
+
const c = ctx();
|
|
223
|
+
await pi.handler({ toolName: "bash", input: { command: "ls" } }, c);
|
|
224
|
+
assert.equal(c.notifies.length, 0, "valid rules are silent");
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
// ── Pattern matching (the non-trivial logic) ──────────────────────────────
|
|
228
|
+
|
|
229
|
+
test("wildcardToRegex: * matches zero+ chars, ? matches exactly one", () => {
|
|
230
|
+
const r = wildcardToRegex("git *");
|
|
231
|
+
assert.equal(r.test("git status"), true);
|
|
232
|
+
assert.equal(r.test("git"), false, "trailing space required");
|
|
233
|
+
assert.equal(r.test("gitstatus"), false, "space literal");
|
|
234
|
+
|
|
235
|
+
const q = wildcardToRegex("a?c");
|
|
236
|
+
assert.equal(q.test("abc"), true);
|
|
237
|
+
assert.equal(q.test("ac"), false, "? requires one char");
|
|
238
|
+
assert.equal(q.test("abbc"), false);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
test("wildcardToRegex: regex specials escaped literally", () => {
|
|
242
|
+
// `*` crosses `/` (OpenCode semantics), so `src/*.ts` matches nested too.
|
|
243
|
+
const r = wildcardToRegex("src/*.ts");
|
|
244
|
+
assert.equal(r.test("src/a/b.ts"), true);
|
|
245
|
+
assert.equal(r.test("src/x.ts"), true);
|
|
246
|
+
assert.equal(r.test("test/x.ts"), false);
|
|
247
|
+
|
|
248
|
+
const dot = wildcardToRegex("package.json");
|
|
249
|
+
assert.equal(dot.test("package.json"), true);
|
|
250
|
+
assert.equal(dot.test("packageXjson"), false, "dot is literal");
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
test("resolveRule: last matching rule wins", () => {
|
|
254
|
+
const rules = { "*": "ask", "git *": "allow", "git push *": "deny" };
|
|
255
|
+
assert.equal(resolveRule(rules, "git status"), "allow");
|
|
256
|
+
assert.equal(resolveRule(rules, "git push origin main"), "deny");
|
|
257
|
+
assert.equal(resolveRule(rules, "npm test"), "ask");
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
test("resolveRule: deny + more specific allow", () => {
|
|
261
|
+
const rules = { "*": "deny", "src/*.ts": "allow" };
|
|
262
|
+
assert.equal(resolveRule(rules, "src/a.ts"), "allow");
|
|
263
|
+
assert.equal(resolveRule(rules, "src/x/a.ts"), "allow");
|
|
264
|
+
assert.equal(resolveRule(rules, "test/a.ts"), "deny");
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
test("resolveRule: returns null when no rule matches", () => {
|
|
268
|
+
assert.equal(resolveRule({ "git *": "allow" }, "npm test"), null);
|
|
269
|
+
assert.equal(resolveRule(null as never, "x"), null);
|
|
270
|
+
assert.equal(resolveRule(undefined as never, "x"), null);
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
test("resolveRule: .env deny pattern (OpenCode default security)", () => {
|
|
274
|
+
const rules = { "*": "allow", "*.env": "deny", "*.env.*": "deny", "*.env.example": "allow" };
|
|
275
|
+
assert.equal(resolveRule(rules, "/proj/.env"), "deny");
|
|
276
|
+
assert.equal(resolveRule(rules, "/proj/.env.local"), "deny");
|
|
277
|
+
assert.equal(resolveRule(rules, "/proj/.env.example"), "allow");
|
|
278
|
+
assert.equal(resolveRule(rules, "/proj/src/index.ts"), "allow");
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
test("expandHome: empty home leaves the pattern unchanged", () => {
|
|
282
|
+
// Windows / scrubbed env: ctx.home undefined + HOME unset → "". Expanding
|
|
283
|
+
// used to turn ~/x into /x via join, silently mismatching the rule.
|
|
284
|
+
assert.equal(expandHome("~/x", ""), "~/x");
|
|
285
|
+
assert.equal(expandHome("~", ""), "~");
|
|
286
|
+
assert.equal(expandHome("$HOME/x", ""), "$HOME/x");
|
|
287
|
+
assert.equal(expandHome("/abs/path", ""), "/abs/path");
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
// ── win32 containment (upstream regression 0.2.5) ─────────────────────────
|
|
291
|
+
|
|
292
|
+
test("isExternal: win32-style backslash paths classified correctly", () => {
|
|
293
|
+
assert.equal(isExternal("C:\\proj\\src\\a.ts", "C:\\proj", win32), false, "inside root, backslash sep");
|
|
294
|
+
assert.equal(isExternal("src\\a.ts", "C:\\proj", win32), false, "relative backslash path resolves inside");
|
|
295
|
+
assert.equal(isExternal("C:\\proj", "C:\\proj", win32), false, "root itself is not external");
|
|
296
|
+
assert.equal(isExternal("..\\other\\s.txt", "C:\\proj", win32), true, "traversal resolves outside");
|
|
297
|
+
assert.equal(isExternal("C:\\Windows\\system32", "C:\\proj", win32), true, "sibling dir is external");
|
|
298
|
+
assert.equal(isExternal("D:\\proj\\x", "C:\\proj", win32), true, "different drive is external");
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
test("isExternal: posix-style forward-slash paths still classified correctly", () => {
|
|
302
|
+
assert.equal(isExternal("/proj/src/a.ts", "/proj"), false);
|
|
303
|
+
assert.equal(isExternal("/etc/passwd", "/proj"), true);
|
|
304
|
+
assert.equal(isExternal("/proj2/file", "/proj"), true, "sibling dir (prefix-string of root) is external");
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
test("isExternal: dot-dot-prefixed filenames inside the workspace are NOT external", () => {
|
|
308
|
+
// `path.relative('/proj', '/proj/..env')` returns '..env' — a naive
|
|
309
|
+
// startsWith('..') check classifies a legal workspace file as external.
|
|
310
|
+
assert.equal(isExternal("/proj/..env", "/proj"), false, "..env file inside root is internal");
|
|
311
|
+
assert.equal(isExternal("..env", "/proj"), false, "relative ..env resolves inside");
|
|
312
|
+
assert.equal(isExternal("../..env", "/proj"), true, "parent-dir ..env is external");
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
// ── Handler behavior ──────────────────────────────────────────────────────
|
|
316
|
+
|
|
317
|
+
test("no permission config → allows everything (no opinion)", async () => {
|
|
318
|
+
const pi = harness({ rules: undefined });
|
|
319
|
+
const result = await pi.handler({ toolName: "bash", input: { command: "rm -rf /" } }, ctx());
|
|
320
|
+
assert.equal(result, undefined);
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
test("explicit deny blocks regardless of yolo", async () => {
|
|
324
|
+
const pi = harness({
|
|
325
|
+
rules: { bash: { "*": "ask", "rm *": "deny" } },
|
|
326
|
+
flags: { yolo: true },
|
|
327
|
+
});
|
|
328
|
+
const result = (await pi.handler({ toolName: "bash", input: { command: "rm -rf /tmp" } }, ctx())) as { block: boolean; reason: string };
|
|
329
|
+
assert.equal(result.block, true);
|
|
330
|
+
assert.match(result.reason, /denied by permission rule/);
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
test("ask prompts UI when hasUI; Allow once passes through", async () => {
|
|
334
|
+
const pi = harness({ rules: { bash: { "*": "ask" } } });
|
|
335
|
+
const result = await pi.handler({ toolName: "bash", input: { command: "git status" } }, ctx({ selectChoice: "Allow once" }));
|
|
336
|
+
assert.equal(result, undefined);
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
test("ask + Deny → block", async () => {
|
|
340
|
+
const pi = harness({ rules: { bash: { "*": "ask" } } });
|
|
341
|
+
const result = (await pi.handler({ toolName: "bash", input: { command: "git status" } }, ctx({ selectChoice: "Deny" }))) as { block: boolean; reason: string };
|
|
342
|
+
assert.equal(result.block, true);
|
|
343
|
+
assert.match(result.reason, /denied by user/);
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
test("ask without UI fails closed (block)", async () => {
|
|
347
|
+
const pi = harness({ rules: { bash: { "*": "ask" } } });
|
|
348
|
+
const result = (await pi.handler({ toolName: "bash", input: { command: "git status" } }, ctx({ hasUI: false }))) as { block: boolean; reason: string };
|
|
349
|
+
assert.equal(result.block, true);
|
|
350
|
+
assert.match(result.reason, /requires approval \(no UI\)/);
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
test("--yolo auto-approves ask but keeps deny", async () => {
|
|
354
|
+
const pi = harness({ rules: { bash: { "*": "ask", "rm *": "deny" } }, flags: { yolo: true } });
|
|
355
|
+
assert.equal(await pi.handler({ toolName: "bash", input: { command: "git status" } }, ctx()), undefined);
|
|
356
|
+
const blocked = (await pi.handler({ toolName: "bash", input: { command: "rm x" } }, ctx())) as { block: boolean; reason: string };
|
|
357
|
+
assert.equal(blocked.block, true);
|
|
358
|
+
assert.match(blocked.reason, /denied by permission rule/);
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
test("global * default applies when no tool rule matches", async () => {
|
|
362
|
+
const pi = harness({ rules: { "*": "ask", bash: { "git *": "allow" } } });
|
|
363
|
+
// read has no tool rule → falls to * → ask
|
|
364
|
+
const result = (await pi.handler({ toolName: "read", input: { path: "src/a.ts" } }, ctx({ selectChoice: "Deny" }))) as { block: boolean; reason: string };
|
|
365
|
+
assert.equal(result.block, true);
|
|
366
|
+
assert.match(result.reason, /denied by user/);
|
|
367
|
+
// bash git → allow
|
|
368
|
+
const allowed = await pi.handler({ toolName: "bash", input: { command: "git status" } }, ctx());
|
|
369
|
+
assert.equal(allowed, undefined);
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
test("allow rule passes through silently", async () => {
|
|
373
|
+
const pi = harness({ rules: { bash: { "*": "deny", "git status": "allow" } } });
|
|
374
|
+
const result = await pi.handler({ toolName: "bash", input: { command: "git status" } }, ctx());
|
|
375
|
+
assert.equal(result, undefined);
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
// ── Doom-loop guard ───────────────────────────────────────────────────────
|
|
379
|
+
|
|
380
|
+
test("doom-loop blocks the 3rd identical call", async () => {
|
|
381
|
+
const pi = harness({ rules: { "*": "allow" } });
|
|
382
|
+
const call = { toolName: "bash", input: { command: "ls" } };
|
|
383
|
+
const c = ctx();
|
|
384
|
+
assert.equal(await pi.handler(call, c), undefined, "1st: allow");
|
|
385
|
+
assert.equal(await pi.handler(call, c), undefined, "2nd: allow");
|
|
386
|
+
const blocked = (await pi.handler(call, c)) as { block: boolean; reason: string };
|
|
387
|
+
assert.equal(blocked.block, true);
|
|
388
|
+
assert.match(blocked.reason, /doom-loop/);
|
|
389
|
+
assert.equal(c.notifies.length, 1);
|
|
390
|
+
assert.match(c.notifies[0]!.m, /Doom-loop blocked/);
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
test("doom-loop does not trigger on different commands", async () => {
|
|
394
|
+
const pi = harness({ rules: { "*": "allow" } });
|
|
395
|
+
const c = ctx();
|
|
396
|
+
await pi.handler({ toolName: "bash", input: { command: "ls" } }, c);
|
|
397
|
+
await pi.handler({ toolName: "bash", input: { command: "ls -la" } }, c);
|
|
398
|
+
const result = await pi.handler({ toolName: "bash", input: { command: "pwd" } }, c);
|
|
399
|
+
assert.equal(result, undefined, "different command resets loop tracking");
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
test("denied calls do not count toward doom-loop: deny reason reported every time", async () => {
|
|
403
|
+
const pi = harness({ rules: { bash: { "rm *": "deny" } } });
|
|
404
|
+
const call = { toolName: "bash", input: { command: "rm x" } };
|
|
405
|
+
const c = ctx();
|
|
406
|
+
for (let i = 1; i <= 3; i++) {
|
|
407
|
+
const result = (await pi.handler(call, c)) as { block: boolean; reason: string };
|
|
408
|
+
assert.equal(result.block, true, `${i}th call: blocked`);
|
|
409
|
+
assert.match(result.reason, /denied by permission rule/, `${i}th call: real deny reason, not doom-loop`);
|
|
410
|
+
}
|
|
411
|
+
assert.equal(c.notifies.length, 0, "no doom-loop notification");
|
|
412
|
+
// a subsequent allowed call still trips the guard (ring works for allowed loops)
|
|
413
|
+
const allowCall = { toolName: "bash", input: { command: "ls" } };
|
|
414
|
+
await pi.handler(allowCall, c);
|
|
415
|
+
await pi.handler(allowCall, c);
|
|
416
|
+
const doom = (await pi.handler(allowCall, c)) as { reason: string };
|
|
417
|
+
assert.match(doom.reason, /doom-loop/);
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
// ── external_directory boundary ───────────────────────────────────────────
|
|
421
|
+
|
|
422
|
+
test("external_directory: inside-cwd path bypasses the boundary, no rule → allow", async () => {
|
|
423
|
+
const pi = harness({ rules: { external_directory: { "~/secret/**": "allow" } } });
|
|
424
|
+
const result = await pi.handler({ toolName: "write", input: { path: "/proj/src/a.ts" } }, ctx());
|
|
425
|
+
assert.equal(result, undefined, "inside cwd: no external check, no rule → allow");
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
test("external_directory deny blocks path outside cwd", async () => {
|
|
429
|
+
const pi = harness({
|
|
430
|
+
rules: { external_directory: { "*": "deny", "~/projects/**": "allow" } },
|
|
431
|
+
});
|
|
432
|
+
const result = (await pi.handler({ toolName: "write", input: { path: "/etc/passwd" } }, ctx())) as { block: boolean; reason: string };
|
|
433
|
+
assert.equal(result.block, true);
|
|
434
|
+
assert.match(result.reason, /external_directory/);
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
test("external_directory deny does NOT block a path equal to the workspace root", async () => {
|
|
438
|
+
// Regression: isExternal treated path === cwd as external, so a root-path
|
|
439
|
+
// read (e.g. ls /proj) was blocked by external_directory {"*":"deny"}.
|
|
440
|
+
const pi = harness({ rules: { external_directory: { "*": "deny" } } });
|
|
441
|
+
const result = await pi.handler({ toolName: "read", input: { path: "/proj" } }, ctx());
|
|
442
|
+
assert.equal(result, undefined, "cwd itself is not external → no rule matched → allow");
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
test("external_directory deny catches `..` traversal (upstream regression 0.2.4)", async () => {
|
|
446
|
+
// resolve() used a hand-rolled join that left `..` unnormalized: "../x"
|
|
447
|
+
// became "/proj/../x" and the isExternal prefix check classified it
|
|
448
|
+
// internal — bypassing the deny gate entirely.
|
|
449
|
+
const pi = harness({ rules: { external_directory: { "*": "deny" } } });
|
|
450
|
+
const r1 = (await pi.handler({ toolName: "read", input: { path: "../outside/s.txt" } }, ctx())) as { block: boolean; reason: string };
|
|
451
|
+
assert.equal(r1.block, true, "../outside/s.txt resolves outside /proj");
|
|
452
|
+
assert.match(r1.reason, /external_directory/);
|
|
453
|
+
const r2 = (await pi.handler({ toolName: "read", input: { path: "../../etc/passwd" } }, ctx())) as { block: boolean; reason: string };
|
|
454
|
+
assert.equal(r2.block, true, "../../etc/passwd resolves outside /proj");
|
|
455
|
+
assert.match(r2.reason, /external_directory/);
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
test("external_directory deny catches `~/…` paths (upstream regression 0.2.6)", async () => {
|
|
459
|
+
// The boundary check ran on the RAW path: `~/ext/file` posix-resolved to
|
|
460
|
+
// /proj/~/ext/file → classified internal → deny gate never fired, while the
|
|
461
|
+
// path tool expands ~ to an external file. ~ is now expanded before the
|
|
462
|
+
// boundary check (same as the rule-matching subject already did).
|
|
463
|
+
const pi = harness({
|
|
464
|
+
rules: { external_directory: { "*": "deny", "~/projects/**": "allow" } },
|
|
465
|
+
});
|
|
466
|
+
const denied = (await pi.handler({ toolName: "read", input: { path: "~/ext/file" } }, ctx())) as { block?: boolean; reason?: string };
|
|
467
|
+
assert.equal(denied?.block, true, "~/ext/file expands outside cwd → deny gate fires");
|
|
468
|
+
assert.match(denied.reason ?? "", /external_directory/);
|
|
469
|
+
const allowed = await pi.handler({ toolName: "read", input: { path: "~/projects/x" } }, ctx());
|
|
470
|
+
assert.equal(allowed, undefined, "~/projects/** allow rule still applies");
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
test("external_directory allow lets tool rules still apply", async () => {
|
|
474
|
+
// Per OpenCode: external_directory is a gate. Once allowed through, tool rules
|
|
475
|
+
// still apply. Here path is external but allowed; tool rule denies → deny.
|
|
476
|
+
const pi = harness({
|
|
477
|
+
rules: {
|
|
478
|
+
external_directory: { "~/projects/**": "allow" },
|
|
479
|
+
read: { "~/projects/secret/**": "deny" },
|
|
480
|
+
},
|
|
481
|
+
});
|
|
482
|
+
const c = ctx();
|
|
483
|
+
c.home = "/home/user";
|
|
484
|
+
const result = (await pi.handler({ toolName: "read", input: { path: "/home/user/projects/secret/x" } }, c)) as { block: boolean; reason: string };
|
|
485
|
+
assert.equal(result.block, true, "external allowed but tool rule denies");
|
|
486
|
+
assert.match(result.reason, /read/);
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
// ── Session promotions + permanent allowlist ──────────────────────────────
|
|
490
|
+
|
|
491
|
+
test("'Allow for this session' actually suppresses subsequent prompts (upstream review: HIGH)", async () => {
|
|
492
|
+
const pi = harness({ rules: { bash: { "*": "ask" } } });
|
|
493
|
+
const c = ctx({ selectChoice: "Allow for this session" });
|
|
494
|
+
assert.equal(await pi.handler({ toolName: "bash", input: { command: "git status" } }, c), undefined);
|
|
495
|
+
const c2 = ctx({ selectChoice: "Deny" });
|
|
496
|
+
assert.equal(await pi.handler({ toolName: "bash", input: { command: "git status" } }, c2), undefined,
|
|
497
|
+
"session allow recorded, no second prompt");
|
|
498
|
+
// A DIFFERENT subject must still prompt (deny).
|
|
499
|
+
const c3 = ctx({ selectChoice: "Deny" });
|
|
500
|
+
const r = (await pi.handler({ toolName: "bash", input: { command: "rm x" } }, c3)) as { block: boolean };
|
|
501
|
+
assert.equal(r.block, true, "different subject still prompts");
|
|
502
|
+
// Reset for the next test — sessionAllows is module state now.
|
|
503
|
+
await pi.sessionStart({}, c3);
|
|
504
|
+
});
|
|
505
|
+
|
|
506
|
+
test("'Allow always this session' promotions survive rules re-read from disk (upstream regression)", async () => {
|
|
507
|
+
const pi = harness({ rules: { bash: { "*": "ask" } } });
|
|
508
|
+
assert.equal(
|
|
509
|
+
await pi.handler({ toolName: "bash", input: { command: "git status" } }, ctx({ selectChoice: "Allow for this session" })),
|
|
510
|
+
undefined,
|
|
511
|
+
);
|
|
512
|
+
// Different rules object instance (as if re-read from disk), same subject.
|
|
513
|
+
const rules2 = { bash: { "*": "ask" } };
|
|
514
|
+
(pi as unknown as { getSetting: (n: string) => unknown }).getSetting = (name) => (name === "permission" ? rules2 : undefined);
|
|
515
|
+
assert.equal(
|
|
516
|
+
await pi.handler({ toolName: "bash", input: { command: "git status" } }, ctx({ selectChoice: "Deny" })),
|
|
517
|
+
undefined,
|
|
518
|
+
"promotion survives settings re-read",
|
|
519
|
+
);
|
|
520
|
+
await pi.sessionStart({}, ctx());
|
|
521
|
+
});
|
|
522
|
+
|
|
523
|
+
test("'Add to permanent allowlist' persists the rule and allows (upstream regression)", async () => {
|
|
524
|
+
const pi = harness({ rules: { bash: { "*": "ask" } } });
|
|
525
|
+
const c = ctx({ selectChoice: "Add to permanent allowlist" });
|
|
526
|
+
const cwd = mkdtempSync(join(tmpdir(), "perm-allowlist-"));
|
|
527
|
+
const agentDir = mkdtempSync(join(tmpdir(), "perm-allowlist-global-"));
|
|
528
|
+
const savedAgentDir = process.env.PI_CODING_AGENT_DIR;
|
|
529
|
+
process.env.PI_CODING_AGENT_DIR = agentDir; // isolate: never touch the real global settings.json
|
|
530
|
+
try {
|
|
531
|
+
c.cwd = cwd; // persistAllowlistRule writes <cwd>/.pi/settings.json
|
|
532
|
+
c.isProjectTrusted = () => true; // trusted project → project scope is read/written
|
|
533
|
+
assert.equal(await pi.handler({ toolName: "bash", input: { command: "npm test" } }, c), undefined);
|
|
534
|
+
const written = JSON.parse(readFileSync(join(cwd, ".pi", "settings.json"), "utf8"));
|
|
535
|
+
assert.deepEqual(written.permission.bash, { "npm test": "allow" });
|
|
536
|
+
assert.equal(c.notifies.length, 1, "notify fired once");
|
|
537
|
+
assert.match(c.notifies[0]!.m, /Permission rule added/);
|
|
538
|
+
// Second call: session-promoted too — no prompt, no deny.
|
|
539
|
+
const c2 = ctx({ selectChoice: "Deny" });
|
|
540
|
+
c2.cwd = cwd;
|
|
541
|
+
assert.equal(await pi.handler({ toolName: "bash", input: { command: "npm test" } }, c2), undefined);
|
|
542
|
+
} finally {
|
|
543
|
+
if (savedAgentDir === undefined) delete process.env.PI_CODING_AGENT_DIR;
|
|
544
|
+
else process.env.PI_CODING_AGENT_DIR = savedAgentDir;
|
|
545
|
+
rmSync(agentDir, { recursive: true, force: true });
|
|
546
|
+
rmSync(cwd, { recursive: true, force: true });
|
|
547
|
+
}
|
|
548
|
+
await pi.sessionStart({}, c);
|
|
549
|
+
});
|
|
550
|
+
|
|
551
|
+
test("persistAllowlistRule: whole-tool string rule preserved as '*'", () => {
|
|
552
|
+
const cwd = mkdtempSync(join(tmpdir(), "perm-allowlist2-"));
|
|
553
|
+
try {
|
|
554
|
+
mkdirSync(join(cwd, ".pi"), { recursive: true });
|
|
555
|
+
writeFileSync(join(cwd, ".pi", "settings.json"), JSON.stringify({ permission: { read: "ask" }, theme: "dark" }));
|
|
556
|
+
const r = persistAllowlistRule("read", "src/a.ts", { cwd });
|
|
557
|
+
assert.equal(r.error, undefined);
|
|
558
|
+
const written = JSON.parse(readFileSync(join(cwd, ".pi", "settings.json"), "utf8"));
|
|
559
|
+
assert.deepEqual(written.permission.read, { "*": "ask", "src/a.ts": "allow" });
|
|
560
|
+
assert.equal(written.theme, "dark", "other keys preserved");
|
|
561
|
+
} finally {
|
|
562
|
+
rmSync(cwd, { recursive: true, force: true });
|
|
563
|
+
}
|
|
564
|
+
});
|
|
565
|
+
|
|
566
|
+
test("persistAllowlistRule: prefers the settings.json that already has permission config", () => {
|
|
567
|
+
const home = mkdtempSync(join(tmpdir(), "perm-home-"));
|
|
568
|
+
const cwd = mkdtempSync(join(tmpdir(), "perm-cwd-"));
|
|
569
|
+
try {
|
|
570
|
+
mkdirSync(join(home, ".pi"), { recursive: true });
|
|
571
|
+
writeFileSync(join(home, ".pi", "settings.json"), JSON.stringify({ permission: { bash: { "*": "ask" } } }));
|
|
572
|
+
const r = persistAllowlistRule("bash", "npm test", { cwd }, [join(cwd, ".pi"), join(home, ".pi")]);
|
|
573
|
+
assert.equal(r.error, undefined);
|
|
574
|
+
assert.equal(r.file, join(home, ".pi", "settings.json"), "wrote where permission config lives");
|
|
575
|
+
assert.ok(!existsSync(join(cwd, ".pi", "settings.json")), "no stray file in cwd");
|
|
576
|
+
} finally {
|
|
577
|
+
rmSync(home, { recursive: true, force: true });
|
|
578
|
+
rmSync(cwd, { recursive: true, force: true });
|
|
579
|
+
}
|
|
580
|
+
});
|
|
581
|
+
|
|
582
|
+
test("dismissed ask dialog (Esc → undefined) fails closed", async () => {
|
|
583
|
+
const pi = harness({ rules: { bash: { "*": "ask" } } });
|
|
584
|
+
const c = ctx({});
|
|
585
|
+
c.ui.select = async () => undefined; // dismissed
|
|
586
|
+
const r = (await pi.handler({ toolName: "bash", input: { command: "git status" } }, c)) as { block: boolean; reason: string };
|
|
587
|
+
assert.equal(r.block, true, "dismissed dialog blocks");
|
|
588
|
+
assert.match(r.reason, /denied by user/);
|
|
589
|
+
});
|
|
590
|
+
|
|
591
|
+
test("session promotion never overrides an explicit deny (upstream review: HIGH)", async () => {
|
|
592
|
+
const pi = harness({ rules: { bash: { "git status": "ask" } } });
|
|
593
|
+
assert.equal(
|
|
594
|
+
await pi.handler({ toolName: "bash", input: { command: "git status" } }, ctx({ selectChoice: "Allow for this session" })),
|
|
595
|
+
undefined,
|
|
596
|
+
);
|
|
597
|
+
// ...then settings are re-read from disk with the same subject denied.
|
|
598
|
+
(pi as unknown as { getSetting: (n: string) => unknown }).getSetting = (name) => (name === "permission" ? { bash: { "git status": "deny" } } : undefined);
|
|
599
|
+
const r1 = (await pi.handler({ toolName: "bash", input: { command: "git status" } }, ctx({ selectChoice: "Allow once" }))) as { block?: boolean; reason?: string };
|
|
600
|
+
assert.ok(r1?.block, "tool-level deny wins over session promotion");
|
|
601
|
+
assert.match(r1.reason ?? "", /denied by permission rule/);
|
|
602
|
+
// Global deny also wins.
|
|
603
|
+
(pi as unknown as { getSetting: (n: string) => unknown }).getSetting = (name) => (name === "permission" ? { "*": "deny" } : undefined);
|
|
604
|
+
const r2 = (await pi.handler({ toolName: "bash", input: { command: "git status" } }, ctx({ selectChoice: "Allow once" }))) as { block?: boolean };
|
|
605
|
+
assert.ok(r2?.block, "global deny wins over session promotion");
|
|
606
|
+
await pi.sessionStart({}, ctx());
|
|
607
|
+
});
|
|
608
|
+
|
|
609
|
+
test("wildcard-bearing subjects are refused by the permanent allowlist (upstream review: MED)", () => {
|
|
610
|
+
const r = persistAllowlistRule("bash", "git add *", { cwd: "/tmp" }, ["/nonexistent-dir-xyz"]);
|
|
611
|
+
assert.ok(r.error, "glob subject refused");
|
|
612
|
+
assert.match(r.error ?? "", /wildcard/);
|
|
613
|
+
});
|
|
614
|
+
|
|
615
|
+
test("persistAllowlistRule refuses to overwrite unparseable settings.json (upstream review: MED)", () => {
|
|
616
|
+
const cwd = mkdtempSync(join(tmpdir(), "perm-corrupt-"));
|
|
617
|
+
try {
|
|
618
|
+
mkdirSync(join(cwd, ".pi"), { recursive: true });
|
|
619
|
+
writeFileSync(join(cwd, ".pi", "settings.json"), "{bad json");
|
|
620
|
+
const before = readFileSync(join(cwd, ".pi", "settings.json"), "utf8");
|
|
621
|
+
const r = persistAllowlistRule("bash", "npm test", { cwd });
|
|
622
|
+
assert.ok(r.error, "corrupt file → error");
|
|
623
|
+
assert.match(r.error ?? "", /not valid JSON/);
|
|
624
|
+
assert.equal(readFileSync(join(cwd, ".pi", "settings.json"), "utf8"), before, "file untouched");
|
|
625
|
+
} finally {
|
|
626
|
+
rmSync(cwd, { recursive: true, force: true });
|
|
627
|
+
}
|
|
628
|
+
});
|
|
629
|
+
|
|
630
|
+
test("persistAllowlistRule re-appends subject so the allow wins last-match-wins (upstream review: LOW)", () => {
|
|
631
|
+
const cwd = mkdtempSync(join(tmpdir(), "perm-order-"));
|
|
632
|
+
try {
|
|
633
|
+
mkdirSync(join(cwd, ".pi"), { recursive: true });
|
|
634
|
+
writeFileSync(
|
|
635
|
+
join(cwd, ".pi", "settings.json"),
|
|
636
|
+
JSON.stringify({ permission: { bash: { "git push origin main": "ask", "git push *": "deny" } } }),
|
|
637
|
+
);
|
|
638
|
+
const r = persistAllowlistRule("bash", "git push origin main", { cwd });
|
|
639
|
+
assert.equal(r.error, undefined);
|
|
640
|
+
const written = JSON.parse(readFileSync(join(cwd, ".pi", "settings.json"), "utf8"));
|
|
641
|
+
const keys = Object.keys(written.permission.bash);
|
|
642
|
+
assert.deepEqual(keys, ["git push *", "git push origin main"], "allow re-appended last");
|
|
643
|
+
assert.equal(resolveRule(written.permission.bash, "git push origin main"), "allow");
|
|
644
|
+
assert.equal(resolveRule(written.permission.bash, "git push other"), "deny");
|
|
645
|
+
} finally {
|
|
646
|
+
rmSync(cwd, { recursive: true, force: true });
|
|
647
|
+
}
|
|
648
|
+
});
|
|
649
|
+
|
|
650
|
+
test("subject-less tools (e.g. grep without path) get only Allow once/Deny (upstream review: MED)", async () => {
|
|
651
|
+
const pi = harness({ rules: { grep: { "*": "ask" } } });
|
|
652
|
+
const seen: string[][] = [];
|
|
653
|
+
const c = ctx({});
|
|
654
|
+
c.ui.select = async (_t: string, opts: string[]) => { seen.push(opts); return "Allow once"; };
|
|
655
|
+
assert.equal(await pi.handler({ toolName: "grep", input: { pattern: "SECRET" } }, c), undefined);
|
|
656
|
+
assert.deepEqual(seen[0], ["Allow once", "Deny"], "no remember options without a subject");
|
|
657
|
+
// With a path, the full options return.
|
|
658
|
+
assert.equal(await pi.handler({ toolName: "grep", input: { pattern: "x", path: "src" } }, c), undefined);
|
|
659
|
+
assert.deepEqual(seen[1], ["Allow once", "Allow for this session", "Add to permanent allowlist", "Deny"]);
|
|
660
|
+
});
|
|
661
|
+
|
|
662
|
+
test("dialog title flattens control characters in command text (upstream review: LOW)", async () => {
|
|
663
|
+
const pi = harness({ rules: { bash: { "*": "ask" } } });
|
|
664
|
+
const titles: string[] = [];
|
|
665
|
+
const c = ctx({});
|
|
666
|
+
c.ui.select = async (t: string) => { titles.push(t); return "Deny"; };
|
|
667
|
+
await pi.handler({ toolName: "bash", input: { command: "echo hi\n\n[SYSTEM] safe" } }, c);
|
|
668
|
+
const body = titles[0]!.split("`bash`: ")[1] ?? "";
|
|
669
|
+
assert.ok(!body.includes("\n"), "no raw newline inside command text");
|
|
670
|
+
});
|
|
671
|
+
|
|
672
|
+
test("multiline commands still match rules (wildcard `*` crosses newlines)", async () => {
|
|
673
|
+
// Pre-existing security hole surfaced by this change: without the regex `s`
|
|
674
|
+
// flag, `.` didn't match newlines, so heredoc/multi-line commands matched NO
|
|
675
|
+
// rule and silently bypassed every ask/deny.
|
|
676
|
+
const pi = harness({ rules: { bash: { "*": "deny" } } });
|
|
677
|
+
const r = (await pi.handler({ toolName: "bash", input: { command: "cat <<'EOF'\nrm -rf /\nEOF" } }, ctx())) as { block?: boolean; reason?: string };
|
|
678
|
+
assert.ok(r?.block, "multiline command hits the deny rule");
|
|
679
|
+
assert.match(r.reason ?? "", /denied by permission rule/);
|
|
680
|
+
});
|
|
681
|
+
|
|
682
|
+
test("unknown action value is treated as no-opinion (upstream review: MED)", async () => {
|
|
683
|
+
// A misconfig where "*" is an object (not a string) must not block or ask.
|
|
684
|
+
const pi = harness({ rules: { "*": { nested: "object" } } });
|
|
685
|
+
const result = await pi.handler({ toolName: "bash", input: { command: "ls" } }, ctx());
|
|
686
|
+
assert.equal(result, undefined, "object action ignored → allow");
|
|
687
|
+
});
|
|
688
|
+
|
|
689
|
+
test("doom-loop memory resets across sessions (upstream review: MED)", async () => {
|
|
690
|
+
// Session A makes 2 identical calls (no block yet). session_start resets, so
|
|
691
|
+
// session B's first 2 identical calls do NOT trip the guard.
|
|
692
|
+
const pi = harness({ rules: { "*": "allow" } });
|
|
693
|
+
const call = { toolName: "bash", input: { command: "ls" } };
|
|
694
|
+
const c = ctx();
|
|
695
|
+
await pi.handler(call, c);
|
|
696
|
+
await pi.handler(call, c);
|
|
697
|
+
await pi.sessionStart({}, c);
|
|
698
|
+
assert.equal(await pi.handler(call, c), undefined, "post-reset 1st: allow");
|
|
699
|
+
assert.equal(await pi.handler(call, c), undefined, "post-reset 2nd: allow");
|
|
700
|
+
});
|
|
701
|
+
|
|
702
|
+
test("stale runner (getFlag throws at load) never crashes handlers (upstream regression)", async () => {
|
|
703
|
+
const pi = harness({ rules: { bash: "*" }, getFlagThrows: true }); // string rule → "ask"
|
|
704
|
+
const c = ctx({ selectChoice: "Allow once" });
|
|
705
|
+
const call = { toolName: "bash", input: { command: "ls" } };
|
|
706
|
+
const result = await pi.handler(call, c);
|
|
707
|
+
assert.equal(result, undefined, "ask → Allow once → allow");
|
|
708
|
+
const second = await pi.handler(call, c);
|
|
709
|
+
assert.equal(second, undefined, "second call handled identically");
|
|
710
|
+
});
|
|
711
|
+
|
|
712
|
+
// ── Scoped path-rule normalization (upstream 2026-09-26 nightly) ──────────
|
|
713
|
+
|
|
714
|
+
test("scoped write allow cannot be bypassed with ../ traversal", async () => {
|
|
715
|
+
const pi = harness({ rules: { write: { "*": "deny", "src/*": "allow" } } });
|
|
716
|
+
const result = (await pi.handler({ toolName: "write", input: { path: "src/../../outside.txt" } }, ctx())) as { block?: boolean; reason?: string };
|
|
717
|
+
assert.ok(result?.block, "src/../../outside.txt must NOT match src/*");
|
|
718
|
+
assert.match(result?.reason ?? "", /denied by permission rule/);
|
|
719
|
+
});
|
|
720
|
+
|
|
721
|
+
test("absolute path still matches a relative scoped deny", async () => {
|
|
722
|
+
const pi = harness({ rules: { read: { "*": "allow", "private/*": "deny" } } });
|
|
723
|
+
// ctx().cwd = /proj → /proj/private/key.pem normalizes to private/key.pem.
|
|
724
|
+
const result = (await pi.handler({ toolName: "read", input: { path: "/proj/private/key.pem" } }, ctx())) as { block?: boolean; reason?: string };
|
|
725
|
+
assert.ok(result?.block, "/proj/private/key.pem must match private/* deny");
|
|
726
|
+
assert.match(result?.reason ?? "", /denied by permission rule/);
|
|
727
|
+
});
|
|
728
|
+
|
|
729
|
+
test("legitimate in-scope path still allowed under scoped rules", async () => {
|
|
730
|
+
const pi = harness({ rules: { write: { "*": "deny", "src/*": "allow" } } });
|
|
731
|
+
const result = await pi.handler({ toolName: "write", input: { path: "src/a.ts" } }, ctx());
|
|
732
|
+
assert.equal(result, undefined, "src/a.ts stays allowed");
|
|
733
|
+
});
|
|
734
|
+
|
|
735
|
+
test("win32 drive-letter absolute pattern matches the absolute subject (deny fires)", async () => {
|
|
736
|
+
// Regression (0.2.7 review): `pat.startsWith("/")` only recognized posix
|
|
737
|
+
// absolutes — a `C:\private\*` deny was matched against the RELATIVE subject
|
|
738
|
+
// and never fired on Windows.
|
|
739
|
+
const pi = harness({ rules: { read: { "*": "allow", "C:\\private\\*": "deny" } } });
|
|
740
|
+
const c = ctx({ cwd: "C:\\proj" });
|
|
741
|
+
(c as TestCtx).ppath = win32;
|
|
742
|
+
const result = (await pi.handler({ toolName: "read", input: { path: "C:\\private\\key.pem" } }, c)) as { block?: boolean };
|
|
743
|
+
assert.ok(result?.block, "win32 drive-letter deny must fire on the absolute subject");
|
|
744
|
+
});
|
|
745
|
+
|
|
746
|
+
// ── ceulen delta: plan-mode deferral via the shared plan-bridge flag ──────
|
|
747
|
+
|
|
748
|
+
test("plan mode active → permission has NO opinion (no prompt, no block)", async () => {
|
|
749
|
+
const pi = harness({ rules: { bash: { "*": "ask", "rm *": "deny" } } });
|
|
750
|
+
const prev = isPlanActive();
|
|
751
|
+
try {
|
|
752
|
+
setPlanActive(true);
|
|
753
|
+
// Even an explicit DENY is deferred — plan mode owns gating while on.
|
|
754
|
+
assert.equal(await pi.handler({ toolName: "bash", input: { command: "rm x" } }, ctx()), undefined);
|
|
755
|
+
assert.equal(await pi.handler({ toolName: "bash", input: { command: "git status" } }, ctx({ selectChoice: "Deny" })), undefined);
|
|
756
|
+
// The doom-loop ring must not consume deferred calls either.
|
|
757
|
+
await pi.sessionStart({}, ctx());
|
|
758
|
+
} finally {
|
|
759
|
+
setPlanActive(prev);
|
|
760
|
+
}
|
|
761
|
+
});
|
|
762
|
+
|
|
763
|
+
test("plan mode off again → permission resumes gating", async () => {
|
|
764
|
+
const pi = harness({ rules: { bash: { "*": "ask" } } });
|
|
765
|
+
const prev = isPlanActive();
|
|
766
|
+
try {
|
|
767
|
+
setPlanActive(true);
|
|
768
|
+
assert.equal(await pi.handler({ toolName: "bash", input: { command: "git status" } }, ctx()), undefined);
|
|
769
|
+
setPlanActive(false);
|
|
770
|
+
const r = (await pi.handler({ toolName: "bash", input: { command: "git status" } }, ctx({ selectChoice: "Deny" }))) as { block: boolean; reason: string };
|
|
771
|
+
assert.equal(r.block, true, "gating resumes the moment plan mode exits");
|
|
772
|
+
await pi.sessionStart({}, ctx());
|
|
773
|
+
} finally {
|
|
774
|
+
setPlanActive(prev);
|
|
775
|
+
}
|
|
776
|
+
});
|
|
777
|
+
|
|
778
|
+
test("plan-bridge flag round-trips", () => {
|
|
779
|
+
const prev = isPlanActive();
|
|
780
|
+
try {
|
|
781
|
+
assert.equal(typeof isPlanActive(), "boolean");
|
|
782
|
+
setPlanActive(true);
|
|
783
|
+
assert.equal(isPlanActive(), true);
|
|
784
|
+
setPlanActive(false);
|
|
785
|
+
assert.equal(isPlanActive(), false);
|
|
786
|
+
} finally {
|
|
787
|
+
setPlanActive(prev);
|
|
788
|
+
}
|
|
789
|
+
});
|