@lelouchhe/agent-evals 0.2.0 → 0.3.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 +25 -20
- package/dist/cli.js +39 -4
- package/dist/engine/check-runner.js +18 -3
- package/dist/engine/check-sandbox.js +9 -6
- package/dist/engine/env-source.js +82 -14
- package/dist/engine/files.js +2 -1
- package/dist/engine/mocks.js +72 -46
- package/dist/engine/registry.d.ts +2 -0
- package/dist/engine/run.js +3 -4
- package/dist/engine/skills.js +12 -5
- package/dist/engine/view.js +6 -3
- package/dist/engine/workspace.js +13 -28
- package/dist/platform/index.d.ts +31 -0
- package/dist/platform/index.js +88 -0
- package/dist/platform/linux/index.d.ts +2 -0
- package/dist/platform/linux/index.js +40 -0
- package/dist/platform/linux/launch.d.ts +8 -0
- package/dist/platform/linux/launch.js +25 -0
- package/dist/platform/linux/policy.d.ts +46 -0
- package/dist/platform/linux/policy.js +388 -0
- package/dist/platform/linux/supervise.d.ts +8 -0
- package/dist/platform/linux/supervise.js +124 -0
- package/dist/platform/macos/index.d.ts +2 -0
- package/dist/platform/macos/index.js +59 -0
- package/dist/platform/macos/launch.d.ts +2 -8
- package/dist/platform/macos/launch.js +5 -3
- package/dist/platform/macos/policy.d.ts +3 -21
- package/dist/platform/macos/policy.js +5 -2
- package/dist/platform/macos/supervise.d.ts +3 -25
- package/dist/platform/posix-forwarder-client.mjs +129 -0
- package/dist/platform/posix-forwarder.sh +9 -0
- package/dist/platform/posix-helpers.d.ts +22 -0
- package/dist/platform/posix-helpers.js +97 -0
- package/dist/platform/types.d.ts +118 -0
- package/dist/platform/types.js +1 -0
- package/dist/platform/windows/forwarder-client.mjs +165 -0
- package/dist/platform/windows/forwarder.cmd +3 -0
- package/dist/platform/windows/forwarder.ps1 +2 -0
- package/dist/platform/windows/forwarder.sh +2 -0
- package/dist/platform/windows/index.d.ts +2 -0
- package/dist/platform/windows/index.js +225 -0
- package/dist/platform/windows/launch.d.ts +3 -0
- package/dist/platform/windows/launch.js +30 -0
- package/dist/platform/windows/policy.d.ts +17 -0
- package/dist/platform/windows/policy.js +155 -0
- package/dist/platform/windows/setup.ps1 +112 -0
- package/dist/platform/windows/supervise.d.ts +2 -0
- package/dist/platform/windows/supervise.js +142 -0
- package/dist/platform/windows/win-sandbox.exe +0 -0
- package/dist/runners/acp/commands.js +2 -3
- package/dist/runners/acp/environment.d.ts +1 -7
- package/dist/runners/acp/environment.js +18 -48
- package/dist/runners/acp/index.js +1 -1
- package/dist/runners/acp/session.d.ts +1 -1
- package/dist/runners/pi/index.js +2 -2
- package/dist/runners/pi/session.d.ts +1 -1
- package/dist/runners/pi/session.js +27 -44
- package/dist/runners/pi/supervisor.js +1 -0
- package/package.json +5 -3
- package/dist/engine/forwarder.sh +0 -61
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import { accessSync, closeSync, constants, lstatSync, openSync, readlinkSync, realpathSync, statSync, unlinkSync, } from "node:fs";
|
|
4
|
+
import { writeFile } from "node:fs/promises";
|
|
5
|
+
import { dirname, isAbsolute, join, resolve } from "node:path";
|
|
6
|
+
export const SYSTEM_PATH = ["/usr/bin", "/bin", "/usr/sbin", "/sbin"];
|
|
7
|
+
const DISTRO_TOP_DIRS = ["/bin", "/sbin", "/lib", "/lib64"];
|
|
8
|
+
const NETWORK_ETC_FILES = ["/etc/resolv.conf", "/etc/hosts"];
|
|
9
|
+
const PROBE_TRUE_PATHS = ["/usr/bin/true", "/bin/true"];
|
|
10
|
+
/** Minimal trusted environment for host-side launcher processes (`bwrap`, `script`). */
|
|
11
|
+
export function trustedHostEnv(includeTerm = false) {
|
|
12
|
+
return {
|
|
13
|
+
PATH: "/usr/bin:/bin",
|
|
14
|
+
SHELL: "/bin/sh",
|
|
15
|
+
...(includeTerm && process.env.TERM !== undefined ? { TERM: process.env.TERM } : {}),
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
let cachedBwrapPath;
|
|
19
|
+
export function createSandboxPolicy(options) {
|
|
20
|
+
return {
|
|
21
|
+
writableRoots: options.writableRoots,
|
|
22
|
+
readableRoots: options.readableRoots,
|
|
23
|
+
...(options.readableFiles === undefined ? {} : { readableFiles: options.readableFiles }),
|
|
24
|
+
...(options.writableFiles === undefined ? {} : { writableFiles: options.writableFiles }),
|
|
25
|
+
...(options.denyExecutables === undefined ? {} : { denyExecutables: options.denyExecutables }),
|
|
26
|
+
allowNetwork: options.allowNetwork,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/** Verify `bwrap` is installed and unprivileged user namespaces work; fail closed otherwise. */
|
|
30
|
+
export function assertBwrapAvailable() {
|
|
31
|
+
if (process.platform !== "linux") {
|
|
32
|
+
throw new Error("the Linux bubblewrap backend requires linux");
|
|
33
|
+
}
|
|
34
|
+
if (cachedBwrapPath !== undefined)
|
|
35
|
+
return;
|
|
36
|
+
const bwrap = findBwrap();
|
|
37
|
+
if (bwrap === undefined) {
|
|
38
|
+
throw new Error("the Linux sandbox requires bubblewrap (bwrap), which was not found on PATH; install bubblewrap (e.g. apt-get install bubblewrap)");
|
|
39
|
+
}
|
|
40
|
+
const probeTrue = PROBE_TRUE_PATHS.find((candidate) => safeStat(candidate)?.isFile() === true);
|
|
41
|
+
if (probeTrue === undefined) {
|
|
42
|
+
throw new Error("the Linux sandbox could not find /usr/bin/true or /bin/true for preflight verification");
|
|
43
|
+
}
|
|
44
|
+
const probe = spawnSync(bwrap, ["--ro-bind", "/", "/", "--unshare-pid", "--die-with-parent", "--new-session", "--", probeTrue], { env: trustedHostEnv(), encoding: "utf8", timeout: 5_000 });
|
|
45
|
+
if (probe.error !== undefined || probe.status !== 0) {
|
|
46
|
+
const detail = (probe.stderr || probe.error?.message || `exit status ${String(probe.status)}`).trim();
|
|
47
|
+
throw new Error(`bubblewrap (bwrap) cannot create a sandbox on this host (${detail}); ensure unprivileged user namespaces are permitted`);
|
|
48
|
+
}
|
|
49
|
+
cachedBwrapPath = bwrap;
|
|
50
|
+
}
|
|
51
|
+
export function bwrapExecutablePath() {
|
|
52
|
+
assertBwrapAvailable();
|
|
53
|
+
return cachedBwrapPath;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Build a minimal cBPF seccomp filter for network-disabled sandboxes. Denies `socket(2)`,
|
|
57
|
+
* `connect(2)`, `io_uring_setup(2)`, and (on x86_64) x32 ABI syscalls with `EPERM`, blocking
|
|
58
|
+
* stream and connectionless `AF_UNIX` sockets across the entire filesystem while keeping
|
|
59
|
+
* `pipe2`/`socketpair` and ordinary read-only files working.
|
|
60
|
+
*/
|
|
61
|
+
export function buildNoNetworkSeccomp(arch = process.arch) {
|
|
62
|
+
let auditArch;
|
|
63
|
+
let deniedSyscalls;
|
|
64
|
+
if (arch === "arm64") {
|
|
65
|
+
auditArch = 0xc00000b7; // AUDIT_ARCH_AARCH64
|
|
66
|
+
deniedSyscalls = [198, 203, 425]; // socket, connect, io_uring_setup
|
|
67
|
+
}
|
|
68
|
+
else if (arch === "x64") {
|
|
69
|
+
auditArch = 0xc000003e; // AUDIT_ARCH_X86_64
|
|
70
|
+
deniedSyscalls = [41, 42, 425]; // socket, connect, io_uring_setup
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
throw new Error(`unsupported Linux architecture for seccomp network isolation: ${arch}`);
|
|
74
|
+
}
|
|
75
|
+
const SECCOMP_RET_ERRNO_EPERM = 0x00050001;
|
|
76
|
+
const SECCOMP_RET_ALLOW = 0x7fff0000;
|
|
77
|
+
const insn = (code, jt, jf, k) => {
|
|
78
|
+
const buf = Buffer.alloc(8);
|
|
79
|
+
buf.writeUInt16LE(code, 0);
|
|
80
|
+
buf.writeUInt8(jt, 2);
|
|
81
|
+
buf.writeUInt8(jf, 3);
|
|
82
|
+
buf.writeUInt32LE(k >>> 0, 4);
|
|
83
|
+
return buf;
|
|
84
|
+
};
|
|
85
|
+
const ops = [
|
|
86
|
+
insn(0x20, 0, 0, 4), // LD seccomp_data.arch
|
|
87
|
+
insn(0x15, 1, 0, auditArch), // JEQ auditArch -> +1
|
|
88
|
+
insn(0x06, 0, 0, SECCOMP_RET_ERRNO_EPERM), // unknown arch: fail closed
|
|
89
|
+
insn(0x20, 0, 0, 0), // LD seccomp_data.nr
|
|
90
|
+
];
|
|
91
|
+
if (arch === "x64") {
|
|
92
|
+
// Reject x32 ABI syscalls (nr >= 0x40000000) sharing AUDIT_ARCH_X86_64.
|
|
93
|
+
ops.push(insn(0x35, 0, 1, 0x40000000)); // JGE 0x40000000 -> next else +1
|
|
94
|
+
ops.push(insn(0x06, 0, 0, SECCOMP_RET_ERRNO_EPERM));
|
|
95
|
+
}
|
|
96
|
+
for (const nr of deniedSyscalls) {
|
|
97
|
+
ops.push(insn(0x15, 0, 1, nr)); // JEQ nr -> next else +1
|
|
98
|
+
ops.push(insn(0x06, 0, 0, SECCOMP_RET_ERRNO_EPERM));
|
|
99
|
+
}
|
|
100
|
+
ops.push(insn(0x06, 0, 0, SECCOMP_RET_ALLOW));
|
|
101
|
+
return Buffer.concat(ops);
|
|
102
|
+
}
|
|
103
|
+
/** Build the `bwrap` option list (everything before `-- <command>`) implementing `policy`. */
|
|
104
|
+
export function buildBwrapOptions(policy, options = {}) {
|
|
105
|
+
const flags = ["--unshare-pid", "--die-with-parent"];
|
|
106
|
+
if (options.newSession !== false) {
|
|
107
|
+
flags.push("--new-session");
|
|
108
|
+
}
|
|
109
|
+
if (!policy.allowNetwork) {
|
|
110
|
+
flags.push("--unshare-net");
|
|
111
|
+
}
|
|
112
|
+
const roMounted = new Set();
|
|
113
|
+
const addRoBind = (source, dest = source) => {
|
|
114
|
+
const key = `${source}\0${dest}`;
|
|
115
|
+
if (roMounted.has(key))
|
|
116
|
+
return;
|
|
117
|
+
roMounted.add(key);
|
|
118
|
+
flags.push("--ro-bind", source, dest);
|
|
119
|
+
};
|
|
120
|
+
const addSymlink = (target, dest) => {
|
|
121
|
+
const key = `sym\0${target}\0${dest}`;
|
|
122
|
+
if (roMounted.has(key))
|
|
123
|
+
return;
|
|
124
|
+
roMounted.add(key);
|
|
125
|
+
flags.push("--symlink", target, dest);
|
|
126
|
+
};
|
|
127
|
+
if (policy.readAll === true) {
|
|
128
|
+
flags.push("--ro-bind", "/", "/", "--dev", "/dev", "--proc", "/proc");
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
addRoBind("/usr");
|
|
132
|
+
for (const dir of DISTRO_TOP_DIRS) {
|
|
133
|
+
const info = safeLstat(dir);
|
|
134
|
+
if (info === undefined)
|
|
135
|
+
continue;
|
|
136
|
+
if (info.isSymbolicLink()) {
|
|
137
|
+
addSymlink(readlinkSync(dir), dir);
|
|
138
|
+
}
|
|
139
|
+
else if (info.isDirectory()) {
|
|
140
|
+
addRoBind(dir);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (safeLstat("/etc") !== undefined) {
|
|
144
|
+
addRoBind("/etc");
|
|
145
|
+
}
|
|
146
|
+
if (policy.allowNetwork) {
|
|
147
|
+
for (const file of NETWORK_ETC_FILES) {
|
|
148
|
+
const mount = resolveExternalSymlinkTarget(file);
|
|
149
|
+
if (mount !== undefined) {
|
|
150
|
+
addRoBind(mount.source, mount.dest);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
flags.push("--dev", "/dev", "--proc", "/proc");
|
|
155
|
+
for (const root of policy.readableRoots) {
|
|
156
|
+
mountReadablePath(root, addRoBind, addSymlink);
|
|
157
|
+
}
|
|
158
|
+
for (const file of policy.readableFiles ?? []) {
|
|
159
|
+
mountReadablePath(file, addRoBind, addSymlink);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
// Writable mounts come after read-only mounts so they overlay cleanly.
|
|
163
|
+
const rwMounted = new Set();
|
|
164
|
+
const addRwBind = (path) => {
|
|
165
|
+
if (rwMounted.has(path))
|
|
166
|
+
return;
|
|
167
|
+
rwMounted.add(path);
|
|
168
|
+
flags.push("--bind", path, path);
|
|
169
|
+
};
|
|
170
|
+
const mountWritablePath = (rawPath) => {
|
|
171
|
+
const resolved = resolve(rawPath);
|
|
172
|
+
const info = safeLstat(resolved);
|
|
173
|
+
if (info === undefined)
|
|
174
|
+
return;
|
|
175
|
+
if (info.isSymbolicLink()) {
|
|
176
|
+
const real = safeRealpath(resolved);
|
|
177
|
+
if (real === undefined)
|
|
178
|
+
return;
|
|
179
|
+
addRwBind(real);
|
|
180
|
+
addSymlink(real, resolved);
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
addRwBind(resolved);
|
|
184
|
+
const real = safeRealpath(resolved);
|
|
185
|
+
if (real !== undefined && real !== resolved)
|
|
186
|
+
addRwBind(real);
|
|
187
|
+
};
|
|
188
|
+
for (const root of policy.writableRoots) {
|
|
189
|
+
mountWritablePath(root);
|
|
190
|
+
}
|
|
191
|
+
for (const file of policy.writableFiles ?? []) {
|
|
192
|
+
mountWritablePath(file);
|
|
193
|
+
}
|
|
194
|
+
// Denied executables come last, masking each real binary with non-executable /dev/null.
|
|
195
|
+
const masked = new Set();
|
|
196
|
+
for (const executable of policy.denyExecutables ?? []) {
|
|
197
|
+
const resolved = resolve(executable);
|
|
198
|
+
const target = safeRealpath(resolved);
|
|
199
|
+
if (target === undefined || masked.has(target))
|
|
200
|
+
continue;
|
|
201
|
+
const info = safeStat(target);
|
|
202
|
+
if (info === undefined || !info.isFile())
|
|
203
|
+
continue;
|
|
204
|
+
masked.add(target);
|
|
205
|
+
flags.push("--ro-bind", "/dev/null", target);
|
|
206
|
+
}
|
|
207
|
+
if (options.env !== undefined) {
|
|
208
|
+
flags.push("--clearenv");
|
|
209
|
+
for (const [key, value] of Object.entries(options.env)) {
|
|
210
|
+
if (value === undefined)
|
|
211
|
+
continue;
|
|
212
|
+
if (key.includes("\0") || value.includes("\0")) {
|
|
213
|
+
throw new Error(`sandbox environment variable "${key.replaceAll("\0", "")}" contains a NUL byte`);
|
|
214
|
+
}
|
|
215
|
+
flags.push("--setenv", key, value);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
if (options.seccompFd !== undefined) {
|
|
219
|
+
flags.push("--seccomp", String(options.seccompFd));
|
|
220
|
+
}
|
|
221
|
+
for (const token of flags) {
|
|
222
|
+
if (token.includes("\0")) {
|
|
223
|
+
throw new Error("sandbox argument list contains an embedded NUL byte");
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
return flags;
|
|
227
|
+
}
|
|
228
|
+
/** Build the full `bwrap` argument list implementing `policy` around `command` and `args`. */
|
|
229
|
+
export function buildBwrapArgs(policy, command, args) {
|
|
230
|
+
return [...buildBwrapOptions(policy), "--", command, ...args];
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Create unlinked read-only FDs for `bwrap --args` (including `--clearenv`/`--setenv` workload
|
|
234
|
+
* env) and optional `--seccomp` BPF program. Each backing file is created exclusively with mode
|
|
235
|
+
* 0600, opened, and unlinked immediately before spawning so no credentials ever remain on disk.
|
|
236
|
+
*/
|
|
237
|
+
export async function prepareBwrapLaunch(policyDirectory, policy, env, options = {}) {
|
|
238
|
+
const needsSeccomp = !policy.allowNetwork;
|
|
239
|
+
const seccompFd = needsSeccomp ? 4 : undefined;
|
|
240
|
+
const bwrapOptions = buildBwrapOptions(policy, {
|
|
241
|
+
env,
|
|
242
|
+
...(options.newSession === undefined ? {} : { newSession: options.newSession }),
|
|
243
|
+
...(seccompFd === undefined ? {} : { seccompFd }),
|
|
244
|
+
});
|
|
245
|
+
const openArgsFd = await openUnlinkedTempFd(join(policyDirectory, `.bwrap-args-${randomUUID()}`), bwrapOptions.join("\0") + "\0");
|
|
246
|
+
let openSeccompFd;
|
|
247
|
+
if (needsSeccomp) {
|
|
248
|
+
try {
|
|
249
|
+
openSeccompFd = await openUnlinkedTempFd(join(policyDirectory, `.bwrap-seccomp-${randomUUID()}`), buildNoNetworkSeccomp());
|
|
250
|
+
}
|
|
251
|
+
catch (error) {
|
|
252
|
+
closeSync(openArgsFd);
|
|
253
|
+
throw error;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
let closed = false;
|
|
257
|
+
const closeFds = () => {
|
|
258
|
+
if (closed)
|
|
259
|
+
return;
|
|
260
|
+
closed = true;
|
|
261
|
+
try {
|
|
262
|
+
closeSync(openArgsFd);
|
|
263
|
+
}
|
|
264
|
+
catch {
|
|
265
|
+
// Already closed.
|
|
266
|
+
}
|
|
267
|
+
if (openSeccompFd !== undefined) {
|
|
268
|
+
try {
|
|
269
|
+
closeSync(openSeccompFd);
|
|
270
|
+
}
|
|
271
|
+
catch {
|
|
272
|
+
// Already closed.
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
return {
|
|
277
|
+
argsFd: openArgsFd,
|
|
278
|
+
...(openSeccompFd === undefined ? {} : { seccompFd: openSeccompFd }),
|
|
279
|
+
closeFds,
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
async function openUnlinkedTempFd(path, content) {
|
|
283
|
+
await writeFile(path, content, { mode: 0o600, flag: "wx" });
|
|
284
|
+
try {
|
|
285
|
+
return openSync(path, constants.O_RDONLY);
|
|
286
|
+
}
|
|
287
|
+
finally {
|
|
288
|
+
try {
|
|
289
|
+
unlinkSync(path);
|
|
290
|
+
}
|
|
291
|
+
catch {
|
|
292
|
+
// Best-effort cleanup if already removed.
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Walk a symlink chain starting inside `/etc` until the first hop outside `/etc` and `/usr`.
|
|
298
|
+
* Mounting the final resolved target at that first unmounted hop keeps multi-hop resolver
|
|
299
|
+
* chains working inside the read-only `/etc` bind.
|
|
300
|
+
*/
|
|
301
|
+
export function resolveExternalSymlinkTarget(file, isMounted = (path) => path.startsWith("/etc/") || path.startsWith("/usr/")) {
|
|
302
|
+
let current = file;
|
|
303
|
+
let firstUnmounted;
|
|
304
|
+
for (let depth = 0; depth < 16; depth++) {
|
|
305
|
+
const info = safeLstat(current);
|
|
306
|
+
if (info === undefined || !info.isSymbolicLink())
|
|
307
|
+
break;
|
|
308
|
+
const next = resolve(dirname(current), readlinkSync(current));
|
|
309
|
+
if (firstUnmounted === undefined && !isMounted(next)) {
|
|
310
|
+
firstUnmounted = next;
|
|
311
|
+
}
|
|
312
|
+
current = next;
|
|
313
|
+
}
|
|
314
|
+
if (firstUnmounted === undefined)
|
|
315
|
+
return undefined;
|
|
316
|
+
const finalReal = safeRealpath(file);
|
|
317
|
+
if (finalReal === undefined)
|
|
318
|
+
return undefined;
|
|
319
|
+
return { source: finalReal, dest: firstUnmounted };
|
|
320
|
+
}
|
|
321
|
+
function mountReadablePath(rawPath, addRoBind, addSymlink) {
|
|
322
|
+
const resolved = resolve(rawPath);
|
|
323
|
+
const info = safeLstat(resolved);
|
|
324
|
+
if (info === undefined)
|
|
325
|
+
return;
|
|
326
|
+
if (info.isSymbolicLink()) {
|
|
327
|
+
const real = safeRealpath(resolved);
|
|
328
|
+
if (real === undefined)
|
|
329
|
+
return;
|
|
330
|
+
addRoBind(real);
|
|
331
|
+
addSymlink(real, resolved);
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
addRoBind(resolved);
|
|
335
|
+
const real = safeRealpath(resolved);
|
|
336
|
+
if (real !== undefined && real !== resolved)
|
|
337
|
+
addRoBind(real);
|
|
338
|
+
}
|
|
339
|
+
function findBwrap() {
|
|
340
|
+
const candidates = [
|
|
341
|
+
"/usr/bin/bwrap",
|
|
342
|
+
"/bin/bwrap",
|
|
343
|
+
...(process.env.PATH ?? "")
|
|
344
|
+
.split(":")
|
|
345
|
+
.filter((entry) => entry.length > 0 && isAbsolute(entry))
|
|
346
|
+
.map((entry) => join(entry, "bwrap")),
|
|
347
|
+
];
|
|
348
|
+
for (const candidate of candidates) {
|
|
349
|
+
const real = safeRealpath(candidate);
|
|
350
|
+
if (real === undefined)
|
|
351
|
+
continue;
|
|
352
|
+
const info = safeStat(real);
|
|
353
|
+
if (info === undefined || !info.isFile())
|
|
354
|
+
continue;
|
|
355
|
+
try {
|
|
356
|
+
accessSync(real, constants.X_OK);
|
|
357
|
+
return real;
|
|
358
|
+
}
|
|
359
|
+
catch {
|
|
360
|
+
// Keep searching.
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
return undefined;
|
|
364
|
+
}
|
|
365
|
+
function safeLstat(path) {
|
|
366
|
+
try {
|
|
367
|
+
return lstatSync(path);
|
|
368
|
+
}
|
|
369
|
+
catch {
|
|
370
|
+
return undefined;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
function safeStat(path) {
|
|
374
|
+
try {
|
|
375
|
+
return statSync(path);
|
|
376
|
+
}
|
|
377
|
+
catch {
|
|
378
|
+
return undefined;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
function safeRealpath(path) {
|
|
382
|
+
try {
|
|
383
|
+
return realpathSync(path);
|
|
384
|
+
}
|
|
385
|
+
catch {
|
|
386
|
+
return undefined;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { SpawnSupervisedOptions, SupervisedProcess } from "../types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Start a command inside a bubblewrap PID namespace (`--unshare-pid --die-with-parent`).
|
|
4
|
+
* Workload environment is installed via `--args` on FD 3 so host launchers (`bwrap`, `script`)
|
|
5
|
+
* run with a minimal trusted environment. When interactive on a real TTY, `util-linux` `script`
|
|
6
|
+
* allocates a sandbox-owned PTY so `/dev/tty` works without exposing the operator's terminal.
|
|
7
|
+
*/
|
|
8
|
+
export declare function spawnSupervised(options: SpawnSupervisedOptions): Promise<SupervisedProcess>;
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { accessSync, constants } from "node:fs";
|
|
3
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
4
|
+
import { bwrapExecutablePath, prepareBwrapLaunch, trustedHostEnv } from "./policy.js";
|
|
5
|
+
const SCRIPT_BINARY = "/usr/bin/script";
|
|
6
|
+
/**
|
|
7
|
+
* Start a command inside a bubblewrap PID namespace (`--unshare-pid --die-with-parent`).
|
|
8
|
+
* Workload environment is installed via `--args` on FD 3 so host launchers (`bwrap`, `script`)
|
|
9
|
+
* run with a minimal trusted environment. When interactive on a real TTY, `util-linux` `script`
|
|
10
|
+
* allocates a sandbox-owned PTY so `/dev/tty` works without exposing the operator's terminal.
|
|
11
|
+
*/
|
|
12
|
+
export async function spawnSupervised(options) {
|
|
13
|
+
const executable = bwrapExecutablePath();
|
|
14
|
+
const [program, ...args] = options.command;
|
|
15
|
+
const usePty = options.interactive && process.stdin.isTTY === true;
|
|
16
|
+
if (usePty) {
|
|
17
|
+
assertScriptAvailable();
|
|
18
|
+
}
|
|
19
|
+
const launch = await prepareBwrapLaunch(options.policyDirectory, options.policy, options.env, {
|
|
20
|
+
// When `script` allocates a private PTY, it already starts a new session with that PTY as
|
|
21
|
+
// controlling terminal; omit `--new-session` so `/dev/tty` stays attached to the private PTY.
|
|
22
|
+
newSession: !usePty,
|
|
23
|
+
});
|
|
24
|
+
const mode = options.interactive ? "inherit" : "pipe";
|
|
25
|
+
// FD 3 = bwrap --args, FD 4 = optional --seccomp, FD 5 = optional check-runner report pipe.
|
|
26
|
+
const stdio = [mode, mode, mode, launch.argsFd];
|
|
27
|
+
if (launch.seccompFd !== undefined) {
|
|
28
|
+
stdio.push(launch.seccompFd);
|
|
29
|
+
}
|
|
30
|
+
else if (options.report === true) {
|
|
31
|
+
stdio.push("ignore");
|
|
32
|
+
}
|
|
33
|
+
if (options.report === true)
|
|
34
|
+
stdio.push("pipe");
|
|
35
|
+
const bwrapCommand = [executable, "--args", "3", "--", program, ...args];
|
|
36
|
+
let launchCommand = executable;
|
|
37
|
+
let launchArgs = bwrapCommand.slice(1);
|
|
38
|
+
if (usePty) {
|
|
39
|
+
const quoted = bwrapCommand.map(shellQuote).join(" ");
|
|
40
|
+
launchCommand = SCRIPT_BINARY;
|
|
41
|
+
launchArgs = ["-q", "-e", "-f", "-c", quoted, "/dev/null"];
|
|
42
|
+
}
|
|
43
|
+
let child;
|
|
44
|
+
try {
|
|
45
|
+
child = spawn(launchCommand, launchArgs, {
|
|
46
|
+
cwd: options.cwd,
|
|
47
|
+
env: trustedHostEnv(usePty),
|
|
48
|
+
stdio,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
finally {
|
|
52
|
+
launch.closeFds();
|
|
53
|
+
}
|
|
54
|
+
const report = options.report === true
|
|
55
|
+
? child.stdio[5]
|
|
56
|
+
: undefined;
|
|
57
|
+
let exited = false;
|
|
58
|
+
const exit = new Promise((resolve, reject) => {
|
|
59
|
+
child.once("exit", (code, signal) => {
|
|
60
|
+
exited = true;
|
|
61
|
+
resolve({ code, signal });
|
|
62
|
+
});
|
|
63
|
+
child.once("error", (error) => {
|
|
64
|
+
exited = true;
|
|
65
|
+
reject(error);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
const completion = exit.then(({ code }) => {
|
|
69
|
+
if (code === null) {
|
|
70
|
+
throw new Error("sandbox supervisor exited without a workload completion record");
|
|
71
|
+
}
|
|
72
|
+
return code;
|
|
73
|
+
});
|
|
74
|
+
// Query operations intentionally stop a still-running ACP server, so they
|
|
75
|
+
// need not await its native exit result. Rejections remain observable to login.
|
|
76
|
+
completion.catch(() => { });
|
|
77
|
+
const stopGraceMs = usePty ? 2_500 : 1_000;
|
|
78
|
+
const waitForExit = async (ms = stopGraceMs) => {
|
|
79
|
+
const deadline = new AbortController();
|
|
80
|
+
try {
|
|
81
|
+
return await Promise.race([exit.then(() => true), delay(ms, false, { signal: deadline.signal })]);
|
|
82
|
+
}
|
|
83
|
+
finally {
|
|
84
|
+
deadline.abort();
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
let stopping;
|
|
88
|
+
const stop = () => (stopping ??= (async () => {
|
|
89
|
+
try {
|
|
90
|
+
if (!exited) {
|
|
91
|
+
child.kill("SIGCONT");
|
|
92
|
+
child.kill("SIGTERM");
|
|
93
|
+
if (!(await waitForExit())) {
|
|
94
|
+
child.kill("SIGKILL");
|
|
95
|
+
const reaped = await waitForExit(1_000);
|
|
96
|
+
if (!reaped) {
|
|
97
|
+
throw new Error("sandbox supervisor did not exit after SIGKILL");
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
await exit;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
finally {
|
|
106
|
+
child.stdin?.destroy();
|
|
107
|
+
child.stdout?.destroy();
|
|
108
|
+
child.stderr?.destroy();
|
|
109
|
+
report?.destroy();
|
|
110
|
+
}
|
|
111
|
+
})());
|
|
112
|
+
return { child, completion, stop, ...(report === undefined ? {} : { report }) };
|
|
113
|
+
}
|
|
114
|
+
function assertScriptAvailable() {
|
|
115
|
+
try {
|
|
116
|
+
accessSync(SCRIPT_BINARY, constants.X_OK);
|
|
117
|
+
}
|
|
118
|
+
catch {
|
|
119
|
+
throw new Error(`interactive Linux sandboxes require ${SCRIPT_BINARY} (util-linux) to isolate the controlling terminal`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
function shellQuote(value) {
|
|
123
|
+
return `'${value.replaceAll("'", "'\\''")}'`;
|
|
124
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { realpath, stat } from "node:fs/promises";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { promisify } from "node:util";
|
|
5
|
+
import { posixPlatformHelpers } from "../posix-helpers.js";
|
|
6
|
+
import { spawnSandboxed } from "./launch.js";
|
|
7
|
+
import { assertSeatbeltAvailable, createSandboxPolicy, SYSTEM_PATH } from "./policy.js";
|
|
8
|
+
import { spawnSupervised } from "./supervise.js";
|
|
9
|
+
/**
|
|
10
|
+
* Resolve the real binaries behind PATH targets whose rules never pass. Besides the PATH
|
|
11
|
+
* directories, the active Xcode developer directory is included because `/usr/bin/make` and
|
|
12
|
+
* `/usr/bin/git` are macOS shims that exec the toolchain's own copy.
|
|
13
|
+
*/
|
|
14
|
+
async function resolveDeniedExecutables(targets, pathEntries) {
|
|
15
|
+
if (targets.length === 0)
|
|
16
|
+
return [];
|
|
17
|
+
const developerDir = await activeDeveloperDir();
|
|
18
|
+
const dirs = developerDir === undefined ? pathEntries : [...pathEntries, join(developerDir, "usr", "bin")];
|
|
19
|
+
const denied = [];
|
|
20
|
+
for (const target of targets) {
|
|
21
|
+
for (const dir of dirs) {
|
|
22
|
+
const candidate = join(dir, target);
|
|
23
|
+
try {
|
|
24
|
+
if ((await stat(candidate)).isFile())
|
|
25
|
+
denied.push(candidate, await realpath(candidate));
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
// Not installed here.
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return denied;
|
|
33
|
+
}
|
|
34
|
+
async function activeDeveloperDir() {
|
|
35
|
+
try {
|
|
36
|
+
const { stdout } = await promisify(execFile)("/usr/bin/xcode-select", ["-p"]);
|
|
37
|
+
const path = stdout.trim();
|
|
38
|
+
return path.length === 0 ? undefined : path;
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export const macosBackend = {
|
|
45
|
+
...posixPlatformHelpers,
|
|
46
|
+
systemPath: SYSTEM_PATH,
|
|
47
|
+
caseInsensitivePaths: true,
|
|
48
|
+
assertSandboxAvailable: assertSeatbeltAvailable,
|
|
49
|
+
async setupSandbox({ teardown }) {
|
|
50
|
+
if (teardown)
|
|
51
|
+
return "macOS Seatbelt requires no host teardown.\n";
|
|
52
|
+
assertSeatbeltAvailable();
|
|
53
|
+
return "macOS Seatbelt sandbox is ready (/usr/bin/sandbox-exec).\n";
|
|
54
|
+
},
|
|
55
|
+
createSandboxPolicy,
|
|
56
|
+
resolveDeniedExecutables,
|
|
57
|
+
spawnSupervised,
|
|
58
|
+
spawnSandboxed,
|
|
59
|
+
};
|
|
@@ -1,13 +1,7 @@
|
|
|
1
1
|
import { type ChildProcessWithoutNullStreams } from "node:child_process";
|
|
2
|
-
|
|
3
|
-
readonly policyPath: string;
|
|
4
|
-
readonly command: string;
|
|
5
|
-
readonly args: readonly string[];
|
|
6
|
-
readonly cwd: string;
|
|
7
|
-
readonly env: NodeJS.ProcessEnv;
|
|
8
|
-
}
|
|
2
|
+
import type { SpawnSandboxedOptions } from "../types.js";
|
|
9
3
|
/**
|
|
10
4
|
* Launch a command inside the Seatbelt sandbox as its own process group.
|
|
11
5
|
* The process group lets the owner terminate the harness and its children.
|
|
12
6
|
*/
|
|
13
|
-
export declare function spawnSandboxed(options:
|
|
7
|
+
export declare function spawnSandboxed(options: SpawnSandboxedOptions): Promise<ChildProcessWithoutNullStreams>;
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
|
-
import { sandboxExecutablePath, sandboxLaunchCommand } from "./policy.js";
|
|
2
|
+
import { sandboxExecutablePath, sandboxLaunchCommand, writeSeatbeltPolicy } from "./policy.js";
|
|
3
3
|
/**
|
|
4
4
|
* Launch a command inside the Seatbelt sandbox as its own process group.
|
|
5
5
|
* The process group lets the owner terminate the harness and its children.
|
|
6
6
|
*/
|
|
7
|
-
export function spawnSandboxed(options) {
|
|
8
|
-
|
|
7
|
+
export async function spawnSandboxed(options) {
|
|
8
|
+
const executable = sandboxExecutablePath();
|
|
9
|
+
const policyPath = await writeSeatbeltPolicy(options.policyDirectory, options.policy);
|
|
10
|
+
return spawn(executable, sandboxLaunchCommand(policyPath, options.command, options.args), {
|
|
9
11
|
cwd: options.cwd,
|
|
10
12
|
env: options.env,
|
|
11
13
|
stdio: ["pipe", "pipe", "pipe"],
|
|
@@ -1,26 +1,8 @@
|
|
|
1
|
+
import type { CreateSandboxPolicyOptions, SandboxPolicy } from "../types.js";
|
|
2
|
+
export type { SandboxPolicy };
|
|
1
3
|
export declare const SYSTEM_PATH: readonly ["/usr/bin", "/bin", "/usr/sbin", "/sbin"];
|
|
2
|
-
export interface SandboxPolicy {
|
|
3
|
-
/** Read and write access. */
|
|
4
|
-
readonly writableRoots: readonly string[];
|
|
5
|
-
/** Read-only access. */
|
|
6
|
-
readonly readableRoots: readonly string[];
|
|
7
|
-
/**
|
|
8
|
-
* Read everything the host user can read, as the check sandbox does after the evaluated agent
|
|
9
|
-
* stopped. It replaces `readableRoots` for the read clauses.
|
|
10
|
-
*/
|
|
11
|
-
readonly readAll?: boolean;
|
|
12
|
-
/** Read-only single files, such as a launcher script. */
|
|
13
|
-
readonly readableFiles?: readonly string[];
|
|
14
|
-
/** Executables the sandbox may not start, such as the real binary behind a mocked command. */
|
|
15
|
-
readonly denyExecutables?: readonly string[];
|
|
16
|
-
/** Interactive terminal device; allow termios mode changes, not arbitrary ioctl operations. */
|
|
17
|
-
readonly terminal?: string;
|
|
18
|
-
readonly allowNetwork: boolean;
|
|
19
|
-
}
|
|
20
4
|
/** Add macOS system access and current-terminal handling to caller-owned roots. */
|
|
21
|
-
export declare function createSandboxPolicy(options:
|
|
22
|
-
readonly interactive: boolean;
|
|
23
|
-
}): SandboxPolicy;
|
|
5
|
+
export declare function createSandboxPolicy(options: CreateSandboxPolicyOptions): SandboxPolicy;
|
|
24
6
|
/**
|
|
25
7
|
* Render a deny-default macOS Seatbelt profile. Reads are limited to explicit
|
|
26
8
|
* system/toolchain roots and run directories; writes are limited to run state.
|
|
@@ -23,7 +23,8 @@ export function createSandboxPolicy(options) {
|
|
|
23
23
|
...(options.allowNetwork ? ["/private/var/db/mds"] : []),
|
|
24
24
|
...options.readableRoots,
|
|
25
25
|
],
|
|
26
|
-
readableFiles: ["/var", "/etc"],
|
|
26
|
+
readableFiles: ["/var", "/etc", ...(options.readableFiles ?? [])],
|
|
27
|
+
...(options.writableFiles === undefined ? {} : { writableFiles: options.writableFiles }),
|
|
27
28
|
...(options.denyExecutables === undefined ? {} : { denyExecutables: options.denyExecutables }),
|
|
28
29
|
allowNetwork: options.allowNetwork,
|
|
29
30
|
...(terminal === undefined ? {} : { terminal }),
|
|
@@ -35,9 +36,10 @@ export function createSandboxPolicy(options) {
|
|
|
35
36
|
*/
|
|
36
37
|
export function renderSeatbeltPolicy(policy) {
|
|
37
38
|
const writable = normalize(policy.writableRoots);
|
|
39
|
+
const writableFiles = normalize(policy.writableFiles ?? []);
|
|
38
40
|
const readable = normalize([...policy.readableRoots, ...policy.writableRoots]);
|
|
39
41
|
const terminals = policy.terminal === undefined ? [] : normalize([policy.terminal, "/dev/tty"]);
|
|
40
|
-
const files = normalize([...(policy.readableFiles ?? []), ...terminals]);
|
|
42
|
+
const files = normalize([...(policy.readableFiles ?? []), ...writableFiles, ...terminals]);
|
|
41
43
|
const readClauses = [
|
|
42
44
|
'(literal "/")',
|
|
43
45
|
...readable.map((path) => `(subpath ${quote(path)})`),
|
|
@@ -74,6 +76,7 @@ export function renderSeatbeltPolicy(policy) {
|
|
|
74
76
|
const terminalClauses = terminals.map((path) => `(literal ${quote(path)})`);
|
|
75
77
|
const writeClauses = [
|
|
76
78
|
...writable.map((path) => `(subpath ${quote(path)})`),
|
|
79
|
+
...writableFiles.map((path) => `(literal ${quote(path)})`),
|
|
77
80
|
...terminalClauses,
|
|
78
81
|
'(literal "/dev/null")',
|
|
79
82
|
];
|