alineo 0.7.0 → 0.8.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/README.md +1 -1
- package/dist/claude-bridge.cjs +633 -0
- package/dist/codex-bridge.cjs +855 -0
- package/dist/index.d.mts +941 -85
- package/dist/index.mjs +1488 -318
- package/dist/pi-bridge.cjs +905 -0
- package/dist/pi-permission-gate.mjs +288 -0
- package/package.json +7 -6
- package/dist/pi-bridge.js +0 -999
- package/dist/pi-permission-gate.js +0 -337
|
@@ -1,337 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* alineo permission gate — a Pi extension loaded via `-e /alineo-permission-gate.js` when
|
|
3
|
-
* `AgentSpec.permissions` is set to anything other than "auto".
|
|
4
|
-
*
|
|
5
|
-
* It reads the normalized policy from /etc/alineo-pi.json (written by
|
|
6
|
-
* `packages/agent/src/adapters/pi.ts`'s `configure()`), and:
|
|
7
|
-
* - on `session_start`, applies `restrictToTools` / `disabledTools` via Pi's
|
|
8
|
-
* `setActiveTools` so the model never sees a tool it may not use;
|
|
9
|
-
* - on every `tool_call`, decides allow / deny / rate-limit / classify / ask. An "ask"
|
|
10
|
-
* call blocks on `ctx.ui.select()` with a marker-prefixed title that `pi-bridge.js`
|
|
11
|
-
* recognizes, holds open, and forwards to the host as a `permission_request` — resolved
|
|
12
|
-
* by a matching POST to `/permission-response`.
|
|
13
|
-
*
|
|
14
|
-
* The matcher here mirrors `packages/agent/src/permissions.ts` (kept in sync by hand — a
|
|
15
|
-
* jiti-loaded extension can't import from the published `alineo` package it sits beside;
|
|
16
|
-
* same tradeoff pi-bridge.js already makes). `test/permissions.test.ts` covers the TS copy;
|
|
17
|
-
* `test/pi-permission-gate.test.ts` covers this one.
|
|
18
|
-
*/
|
|
19
|
-
import { readFileSync } from "node:fs";
|
|
20
|
-
|
|
21
|
-
const CONFIG_FILE = process.env.ALINEO_PI_CONFIG || "/etc/alineo-pi.json";
|
|
22
|
-
/** Title prefix pi-bridge.js keys on to route a select() dialog to the host instead of auto-cancelling. */
|
|
23
|
-
const MARKER = "ALINEO_PERM ";
|
|
24
|
-
|
|
25
|
-
/** Mirror of `permissions.ts` SAFE_BASH_COMMANDS. */
|
|
26
|
-
const SAFE_BASH_COMMANDS = new Set([
|
|
27
|
-
"ls",
|
|
28
|
-
"cat",
|
|
29
|
-
"head",
|
|
30
|
-
"tail",
|
|
31
|
-
"grep",
|
|
32
|
-
"rg",
|
|
33
|
-
"egrep",
|
|
34
|
-
"fgrep",
|
|
35
|
-
"zgrep",
|
|
36
|
-
"find",
|
|
37
|
-
"pwd",
|
|
38
|
-
"whoami",
|
|
39
|
-
"id",
|
|
40
|
-
"env",
|
|
41
|
-
"printenv",
|
|
42
|
-
"echo",
|
|
43
|
-
"printf",
|
|
44
|
-
"which",
|
|
45
|
-
"type",
|
|
46
|
-
"file",
|
|
47
|
-
"stat",
|
|
48
|
-
"wc",
|
|
49
|
-
"date",
|
|
50
|
-
"uname",
|
|
51
|
-
"hostname",
|
|
52
|
-
"df",
|
|
53
|
-
"du",
|
|
54
|
-
"tree",
|
|
55
|
-
"basename",
|
|
56
|
-
"dirname",
|
|
57
|
-
"realpath",
|
|
58
|
-
"readlink",
|
|
59
|
-
"true",
|
|
60
|
-
"false",
|
|
61
|
-
"cmp",
|
|
62
|
-
"diff",
|
|
63
|
-
"column",
|
|
64
|
-
"nl",
|
|
65
|
-
"less",
|
|
66
|
-
"more",
|
|
67
|
-
"tac",
|
|
68
|
-
"hexdump",
|
|
69
|
-
"xxd",
|
|
70
|
-
"od",
|
|
71
|
-
"strings",
|
|
72
|
-
"sha1sum",
|
|
73
|
-
"sha256sum",
|
|
74
|
-
"md5sum",
|
|
75
|
-
"cksum",
|
|
76
|
-
]);
|
|
77
|
-
const SAFE_GIT_SUBCOMMANDS = new Set([
|
|
78
|
-
"status",
|
|
79
|
-
"log",
|
|
80
|
-
"diff",
|
|
81
|
-
"show",
|
|
82
|
-
"branch",
|
|
83
|
-
"remote",
|
|
84
|
-
"rev-parse",
|
|
85
|
-
"describe",
|
|
86
|
-
"blame",
|
|
87
|
-
"tag",
|
|
88
|
-
"config",
|
|
89
|
-
"ls-files",
|
|
90
|
-
"ls-tree",
|
|
91
|
-
"shortlog",
|
|
92
|
-
"reflog",
|
|
93
|
-
"cat-file",
|
|
94
|
-
"for-each-ref",
|
|
95
|
-
"whatchanged",
|
|
96
|
-
"rev-list",
|
|
97
|
-
"name-rev",
|
|
98
|
-
"symbolic-ref",
|
|
99
|
-
"count-objects",
|
|
100
|
-
]);
|
|
101
|
-
|
|
102
|
-
/** @returns {{default: string, rules: Array, disabledTools: string[], restrictToTools: string[]} | null} */
|
|
103
|
-
function loadPolicy() {
|
|
104
|
-
try {
|
|
105
|
-
const cfg = JSON.parse(readFileSync(CONFIG_FILE, "utf8"));
|
|
106
|
-
const p = cfg && cfg.permissions;
|
|
107
|
-
if (!p || typeof p !== "object") return null;
|
|
108
|
-
return {
|
|
109
|
-
default: p.default || "ask",
|
|
110
|
-
rules: Array.isArray(p.rules) ? p.rules : [],
|
|
111
|
-
disabledTools: Array.isArray(p.disabledTools) ? p.disabledTools : [],
|
|
112
|
-
restrictToTools: Array.isArray(p.restrictToTools) ? p.restrictToTools : [],
|
|
113
|
-
};
|
|
114
|
-
} catch {
|
|
115
|
-
return null;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
const WRAPPER_COMMANDS = new Set(["env", "command", "nice", "stdbuf", "time"]);
|
|
120
|
-
|
|
121
|
-
// Linear scan for a file output redirection — no regex (ReDoS-safe). A `>` next to `&`
|
|
122
|
-
// (`2>&1`, `>&2`), preceded by a digit (`2>`), or preceded by `&` is an fd op, not a write.
|
|
123
|
-
function hasOutputRedirect(seg) {
|
|
124
|
-
for (let i = 0; i < seg.length; i++) {
|
|
125
|
-
if (seg[i] !== ">") continue;
|
|
126
|
-
const next = seg[i + 1];
|
|
127
|
-
const prev = seg[i - 1];
|
|
128
|
-
if (next === "&" || prev === "&" || (prev >= "0" && prev <= "9")) continue;
|
|
129
|
-
return true;
|
|
130
|
-
}
|
|
131
|
-
return false;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/** Mirror of `permissions.ts` isReadOnlyBashCommand. */
|
|
135
|
-
function isReadOnlyBashCommand(command) {
|
|
136
|
-
const segments = String(command)
|
|
137
|
-
.split(/&&|\|\||[;\n|]/)
|
|
138
|
-
.map((s) => s.trim())
|
|
139
|
-
.filter(Boolean);
|
|
140
|
-
if (segments.length === 0) return false;
|
|
141
|
-
return segments.every((seg) => {
|
|
142
|
-
if (hasOutputRedirect(seg)) return false;
|
|
143
|
-
let tokens = seg.split(/\s+/).filter(Boolean);
|
|
144
|
-
while (tokens.length > 0 && /^[A-Za-z_][A-Za-z0-9_]*=/.test(tokens[0])) {
|
|
145
|
-
tokens = tokens.slice(1);
|
|
146
|
-
}
|
|
147
|
-
while (tokens.length > 1 && WRAPPER_COMMANDS.has(tokens[0])) tokens = tokens.slice(1);
|
|
148
|
-
if (tokens[0] === "timeout" && tokens.length > 2) tokens = tokens.slice(2);
|
|
149
|
-
const first = tokens[0] || "";
|
|
150
|
-
const slash = first.lastIndexOf("/");
|
|
151
|
-
const cmd = slash === -1 ? first : first.slice(slash + 1);
|
|
152
|
-
if (!cmd) return false;
|
|
153
|
-
if (SAFE_BASH_COMMANDS.has(cmd)) return true;
|
|
154
|
-
if (cmd === "git" && SAFE_GIT_SUBCOMMANDS.has(tokens[1] || "")) return true;
|
|
155
|
-
return false;
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/** Tool-specific target string, pulled from Pi's typed `event.input`. */
|
|
160
|
-
function targetOf(toolName, input) {
|
|
161
|
-
if (!input || typeof input !== "object") return "";
|
|
162
|
-
switch (toolName) {
|
|
163
|
-
case "bash":
|
|
164
|
-
case "powershell":
|
|
165
|
-
return String(input.command ?? "");
|
|
166
|
-
case "read":
|
|
167
|
-
case "write":
|
|
168
|
-
case "edit":
|
|
169
|
-
case "ls":
|
|
170
|
-
return String(input.path ?? "");
|
|
171
|
-
case "grep":
|
|
172
|
-
return String(input.pattern ?? input.query ?? "");
|
|
173
|
-
case "find":
|
|
174
|
-
return String(input.pattern ?? input.name ?? "");
|
|
175
|
-
default:
|
|
176
|
-
try {
|
|
177
|
-
return JSON.stringify(input);
|
|
178
|
-
} catch {
|
|
179
|
-
return "";
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
function globToRegExp(glob) {
|
|
185
|
-
const body = String(glob)
|
|
186
|
-
.replace(/[.+^${}()|[\]\\]/g, "\\$&")
|
|
187
|
-
.replace(/\*/g, ".*")
|
|
188
|
-
.replace(/\?/g, ".");
|
|
189
|
-
return new RegExp(`^${body}$`, "s");
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
function ruleMatches(rule, toolName, target) {
|
|
193
|
-
if (!globToRegExp(rule.tool).test(toolName)) return false;
|
|
194
|
-
if (
|
|
195
|
-
rule.pattern !== undefined &&
|
|
196
|
-
rule.pattern !== null &&
|
|
197
|
-
!globToRegExp(rule.pattern).test(target)
|
|
198
|
-
) {
|
|
199
|
-
return false;
|
|
200
|
-
}
|
|
201
|
-
return true;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
function evaluate(policy, toolName, target) {
|
|
205
|
-
if (policy.disabledTools.indexOf(toolName) !== -1) return { action: "deny" };
|
|
206
|
-
let match;
|
|
207
|
-
for (const rule of policy.rules) {
|
|
208
|
-
if (ruleMatches(rule, toolName, target)) match = rule;
|
|
209
|
-
}
|
|
210
|
-
let action = match ? match.action : policy.default;
|
|
211
|
-
if (action === "classify") {
|
|
212
|
-
action = toolName === "bash" && isReadOnlyBashCommand(target) ? "allow" : "ask";
|
|
213
|
-
}
|
|
214
|
-
return { action, rule: match };
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
/** Rolling-window rate limiter, keyed per rule. */
|
|
218
|
-
const rateHits = new Map();
|
|
219
|
-
function underLimit(rule, key) {
|
|
220
|
-
if (!rule || !rule.limit) return true;
|
|
221
|
-
const now = Date.now();
|
|
222
|
-
const windowStart = now - rule.limit.windowMs;
|
|
223
|
-
const hits = (rateHits.get(key) || []).filter((t) => t >= windowStart);
|
|
224
|
-
if (hits.length >= rule.limit.count) {
|
|
225
|
-
rateHits.set(key, hits);
|
|
226
|
-
return false;
|
|
227
|
-
}
|
|
228
|
-
hits.push(now);
|
|
229
|
-
rateHits.set(key, hits);
|
|
230
|
-
return true;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* Apply `restrictToTools` (allowlist) then `disabledTools` (denylist) to the model's
|
|
235
|
-
* visible toolset. Best-effort: `setActiveTools` may not exist on older Pi — the
|
|
236
|
-
* `tool_call` deny backstop still covers `disabledTools` in that case.
|
|
237
|
-
*/
|
|
238
|
-
function applyToolset(pi, policy) {
|
|
239
|
-
if (typeof pi.setActiveTools !== "function" || typeof pi.getActiveTools !== "function") {
|
|
240
|
-
return;
|
|
241
|
-
}
|
|
242
|
-
let active = pi.getActiveTools();
|
|
243
|
-
if (!Array.isArray(active)) return;
|
|
244
|
-
if (policy.restrictToTools.length > 0) {
|
|
245
|
-
active = active.filter((t) => policy.restrictToTools.indexOf(t) !== -1);
|
|
246
|
-
}
|
|
247
|
-
if (policy.disabledTools.length > 0) {
|
|
248
|
-
active = active.filter((t) => policy.disabledTools.indexOf(t) === -1);
|
|
249
|
-
}
|
|
250
|
-
try {
|
|
251
|
-
pi.setActiveTools(active);
|
|
252
|
-
} catch {}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
export default function (pi) {
|
|
256
|
-
const policy = loadPolicy();
|
|
257
|
-
if (!policy) return; // shouldn't happen — the extension is only loaded when a policy exists
|
|
258
|
-
|
|
259
|
-
// Action methods (setActiveTools) can't be called during extension load — only from a
|
|
260
|
-
// lifecycle hook once the runtime is up. Re-applying is idempotent (getActiveTools()
|
|
261
|
-
// returns the already-filtered set), so run it on session start and before every turn
|
|
262
|
-
// in case Pi re-registers tools.
|
|
263
|
-
if (policy.restrictToTools.length > 0 || policy.disabledTools.length > 0) {
|
|
264
|
-
const apply = () => {
|
|
265
|
-
applyToolset(pi, policy);
|
|
266
|
-
};
|
|
267
|
-
pi.on("session_start", apply);
|
|
268
|
-
pi.on("before_agent_start", apply);
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
/** Session-scoped "always allow" memory, keyed tool + target. */
|
|
272
|
-
const alwaysAllow = new Set();
|
|
273
|
-
|
|
274
|
-
pi.on("tool_call", async (event, ctx) => {
|
|
275
|
-
const toolName = event.toolName;
|
|
276
|
-
const target = targetOf(toolName, event.input);
|
|
277
|
-
const key = `${toolName} ${target}`;
|
|
278
|
-
|
|
279
|
-
if (alwaysAllow.has(key)) return undefined;
|
|
280
|
-
|
|
281
|
-
const { action, rule } = evaluate(policy, toolName, target);
|
|
282
|
-
|
|
283
|
-
if (action === "allow") return undefined;
|
|
284
|
-
if (action === "deny") {
|
|
285
|
-
return {
|
|
286
|
-
block: true,
|
|
287
|
-
reason: `The operator's policy denies this tool call: ${toolName} ${target}`.trim(),
|
|
288
|
-
};
|
|
289
|
-
}
|
|
290
|
-
if (action === "rate_limit") {
|
|
291
|
-
return underLimit(rule, key)
|
|
292
|
-
? undefined
|
|
293
|
-
: {
|
|
294
|
-
block: true,
|
|
295
|
-
reason: `Rate limit reached for ${toolName}. Wait before trying this again, or take a different approach.`,
|
|
296
|
-
};
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
// action === "ask"
|
|
300
|
-
if (!ctx.hasUI) {
|
|
301
|
-
return {
|
|
302
|
-
block: true,
|
|
303
|
-
reason: "This tool call needs operator approval, but no UI is attached.",
|
|
304
|
-
};
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
const title =
|
|
308
|
-
MARKER + JSON.stringify({ tool: toolName, target, title: `Run ${toolName}: ${target}` });
|
|
309
|
-
let raw;
|
|
310
|
-
try {
|
|
311
|
-
raw = await ctx.ui.select(title, ["decide"]);
|
|
312
|
-
} catch {
|
|
313
|
-
raw = undefined;
|
|
314
|
-
}
|
|
315
|
-
if (raw === undefined || raw === null) {
|
|
316
|
-
return { block: true, reason: "Approval request was cancelled or timed out." };
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
let decision;
|
|
320
|
-
try {
|
|
321
|
-
decision = JSON.parse(raw);
|
|
322
|
-
} catch {
|
|
323
|
-
decision = { verdict: "reject" };
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
if (decision.verdict === "allow") {
|
|
327
|
-
if (decision.scope === "always") alwaysAllow.add(key);
|
|
328
|
-
return undefined;
|
|
329
|
-
}
|
|
330
|
-
return {
|
|
331
|
-
block: true,
|
|
332
|
-
reason: decision.feedback
|
|
333
|
-
? `The operator declined this and said: ${decision.feedback}`
|
|
334
|
-
: "The operator declined this tool call.",
|
|
335
|
-
};
|
|
336
|
-
});
|
|
337
|
-
}
|