@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
package/dist/engine/run.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { randomBytes } from "node:crypto";
|
|
2
2
|
import { copyFile, cp, lstat, mkdir, readdir, realpath, rm, stat } from "node:fs/promises";
|
|
3
3
|
import { join } from "node:path";
|
|
4
|
-
import {
|
|
5
|
-
import { assertSeatbeltAvailable } from "../platform/macos/policy.js";
|
|
4
|
+
import { assertSandboxAvailable, defaultTempParent } from "../platform/index.js";
|
|
6
5
|
import { ConfigError, errorMessage, resolveAuthoringAsset, } from "./config.js";
|
|
7
6
|
import { requireChecker, requireRunner, } from "./registry.js";
|
|
8
7
|
import { classifyOutcome, createResultDirectory, newRunId, writeResult, } from "./results.js";
|
|
@@ -27,7 +26,7 @@ export async function prepareEvaluation(request) {
|
|
|
27
26
|
// Placeholders need the run directories' paths; they are created only after validation.
|
|
28
27
|
// The parent is canonicalized first: the sandbox authorizes the `deps` link only when the
|
|
29
28
|
// run-root path and its target carry no symlink spelling such as `/tmp` for `/private/tmp`.
|
|
30
|
-
const parent = await realpath(request.tempParent ??
|
|
29
|
+
const parent = await realpath(request.tempParent ?? defaultTempParent());
|
|
31
30
|
const root = join(parent, `run-${randomBytes(6).toString("hex")}`);
|
|
32
31
|
const workspaceDir = join(root, "workspace");
|
|
33
32
|
const homeDir = join(root, "home");
|
|
@@ -509,7 +508,7 @@ async function prepareImplementations(request, values, signal) {
|
|
|
509
508
|
validateFiles: (files) => validateCheckFiles(files, request.case.dir, "check files", signal),
|
|
510
509
|
...(mockTargets === undefined ? {} : { mockTargets }),
|
|
511
510
|
runnerSupportsHome,
|
|
512
|
-
validateCheckPlatform: async () =>
|
|
511
|
+
validateCheckPlatform: async () => assertSandboxAvailable(),
|
|
513
512
|
createRunner,
|
|
514
513
|
};
|
|
515
514
|
const { runner } = await createRunner();
|
package/dist/engine/skills.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { realpath } from "node:fs/promises";
|
|
2
|
-
import { basename, dirname, isAbsolute, join, resolve } from "node:path";
|
|
2
|
+
import { basename, dirname, isAbsolute, join, parse as parsePath, resolve } from "node:path";
|
|
3
|
+
import { caseInsensitivePaths } from "../platform/index.js";
|
|
3
4
|
import { parse } from "yaml";
|
|
4
5
|
import { readRunFile } from "./workspace.js";
|
|
5
6
|
/** The file every harness's skill loader reads when it consults a skill's body. */
|
|
@@ -185,16 +186,22 @@ async function canonicalExternalPath(target) {
|
|
|
185
186
|
* Walking from `/` downward finds the host's root before entering any of its children.
|
|
186
187
|
*/
|
|
187
188
|
async function relativeWithin(rootPath, target) {
|
|
188
|
-
const
|
|
189
|
-
|
|
189
|
+
const resolved = resolve(target);
|
|
190
|
+
const root = parsePath(resolved).root;
|
|
191
|
+
const segments = resolved
|
|
192
|
+
.slice(root.length)
|
|
193
|
+
.split(/[\\/]+/)
|
|
194
|
+
.filter(Boolean);
|
|
195
|
+
const matchesRoot = (candidate) => caseInsensitivePaths ? candidate.toLowerCase() === rootPath.toLowerCase() : candidate === rootPath;
|
|
196
|
+
let probe = root;
|
|
190
197
|
for (const [index, segment] of segments.entries()) {
|
|
191
198
|
probe = join(probe, segment);
|
|
192
199
|
const rest = segments.slice(index + 1);
|
|
193
|
-
if (probe
|
|
200
|
+
if (matchesRoot(probe)) {
|
|
194
201
|
return rest.length === 0 ? undefined : join(...rest);
|
|
195
202
|
}
|
|
196
203
|
try {
|
|
197
|
-
if ((await realpath(probe))
|
|
204
|
+
if (matchesRoot(await realpath(probe))) {
|
|
198
205
|
return rest.length === 0 ? undefined : join(...rest);
|
|
199
206
|
}
|
|
200
207
|
}
|
package/dist/engine/view.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { randomBytes } from "node:crypto";
|
|
2
|
-
import { basename } from "node:path";
|
|
2
|
+
import { basename, sep } from "node:path";
|
|
3
3
|
import { Document, isMap, isNode, isScalar, isSeq } from "yaml";
|
|
4
4
|
/** Render the resolved configuration with inline `# <file>:<line>` origins and placement. */
|
|
5
5
|
export function formatResolvedCase(resolved, options) {
|
|
@@ -21,11 +21,11 @@ export function formatResolvedCase(resolved, options) {
|
|
|
21
21
|
continue;
|
|
22
22
|
}
|
|
23
23
|
for (const placed of summary.placed) {
|
|
24
|
-
lines.push(` place ${placed.path} <- ${placed.source}`);
|
|
24
|
+
lines.push(` place ${toPosix(placed.path)} <- ${toPosix(placed.source)}`);
|
|
25
25
|
}
|
|
26
26
|
for (const excluded of summary.excluded) {
|
|
27
27
|
const kind = excluded.directory ? " (directory)" : "";
|
|
28
|
-
lines.push(` exclude ${excluded.path}${kind} <- .agentevalsignore at ${excluded.ignoreDir}`);
|
|
28
|
+
lines.push(` exclude ${toPosix(excluded.path)}${kind} <- .agentevalsignore at ${toPosix(excluded.ignoreDir)}`);
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
31
|
return lines.join("\n") + "\n";
|
|
@@ -148,3 +148,6 @@ function formatSource(origin, marker) {
|
|
|
148
148
|
function count(value, label) {
|
|
149
149
|
return `${value} ${label}`;
|
|
150
150
|
}
|
|
151
|
+
function toPosix(path) {
|
|
152
|
+
return sep === "/" ? path : path.split(sep).join("/");
|
|
153
|
+
}
|
package/dist/engine/workspace.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { lstat, open, readdir, realpath } from "node:fs/promises";
|
|
1
|
+
import { lstat, readdir, realpath } from "node:fs/promises";
|
|
3
2
|
import { basename, dirname, isAbsolute, join, posix, relative, resolve, sep } from "node:path";
|
|
4
3
|
import { ConfigError, errorMessage } from "./config.js";
|
|
4
|
+
import { caseInsensitivePaths, openDirectoryIdentity, openRegularFileNoFollow } from "../platform/index.js";
|
|
5
5
|
/** Largest file a check will read. Checks stay bounded and predictable. */
|
|
6
6
|
export const MAX_CHECK_BYTES = 1024 * 1024;
|
|
7
7
|
export class WorkspacePathError extends Error {
|
|
@@ -12,23 +12,13 @@ export class WorkspacePathError extends Error {
|
|
|
12
12
|
}
|
|
13
13
|
export async function openRunRoot(path) {
|
|
14
14
|
const canonical = await realpath(path);
|
|
15
|
-
let handle;
|
|
16
15
|
try {
|
|
17
|
-
|
|
16
|
+
const identity = await openDirectoryIdentity(canonical);
|
|
17
|
+
return { path: canonical, dev: identity.dev, ino: identity.ino };
|
|
18
18
|
}
|
|
19
19
|
catch (error) {
|
|
20
20
|
throw new WorkspacePathError(`could not open run directory ${path}: ${errorMessage(error)}`);
|
|
21
21
|
}
|
|
22
|
-
try {
|
|
23
|
-
const stats = await handle.stat({ bigint: true });
|
|
24
|
-
if (!stats.isDirectory()) {
|
|
25
|
-
throw new WorkspacePathError(`run path is not a directory: ${path}`);
|
|
26
|
-
}
|
|
27
|
-
return { path: canonical, dev: stats.dev, ino: stats.ino };
|
|
28
|
-
}
|
|
29
|
-
finally {
|
|
30
|
-
await handle.close();
|
|
31
|
-
}
|
|
32
22
|
}
|
|
33
23
|
/**
|
|
34
24
|
* Verify the directory path still refers to the prepared directory. A path
|
|
@@ -45,21 +35,15 @@ export async function assertRunRoot(root) {
|
|
|
45
35
|
if (entry.isSymbolicLink() || !entry.isDirectory() || entry.dev !== root.dev || entry.ino !== root.ino) {
|
|
46
36
|
throw new WorkspacePathError(`run directory was replaced after preparation: ${root.path}`);
|
|
47
37
|
}
|
|
48
|
-
let
|
|
38
|
+
let identity;
|
|
49
39
|
try {
|
|
50
|
-
|
|
40
|
+
identity = await openDirectoryIdentity(root.path);
|
|
51
41
|
}
|
|
52
42
|
catch (error) {
|
|
53
43
|
throw new WorkspacePathError(`run directory can no longer be opened safely: ${root.path}: ${errorMessage(error)}`);
|
|
54
44
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
if (!stats.isDirectory() || stats.dev !== root.dev || stats.ino !== root.ino) {
|
|
58
|
-
throw new WorkspacePathError(`run directory was replaced while opening it: ${root.path}`);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
finally {
|
|
62
|
-
await handle.close();
|
|
45
|
+
if (identity.dev !== root.dev || identity.ino !== root.ino) {
|
|
46
|
+
throw new WorkspacePathError(`run directory was replaced while opening it: ${root.path}`);
|
|
63
47
|
}
|
|
64
48
|
}
|
|
65
49
|
/**
|
|
@@ -210,7 +194,7 @@ export async function readRunTree(root, requestedPath) {
|
|
|
210
194
|
async function readBoundedFile(absolute, requestedPath) {
|
|
211
195
|
let handle;
|
|
212
196
|
try {
|
|
213
|
-
handle = await
|
|
197
|
+
handle = await openRegularFileNoFollow(absolute);
|
|
214
198
|
}
|
|
215
199
|
catch (error) {
|
|
216
200
|
if (isMissing(error)) {
|
|
@@ -263,7 +247,7 @@ export async function runPathExists(root, requestedPath) {
|
|
|
263
247
|
}
|
|
264
248
|
}
|
|
265
249
|
export function requireConfigurationPath(declaringDir, relativePath, label) {
|
|
266
|
-
if (relativePath.startsWith("/") || relativePath.includes("\0")) {
|
|
250
|
+
if (isAbsolute(relativePath) || relativePath.startsWith("/") || relativePath.includes("\0")) {
|
|
267
251
|
throw new ConfigError(`${label} must be a relative path`);
|
|
268
252
|
}
|
|
269
253
|
return resolve(declaringDir, relativePath);
|
|
@@ -295,11 +279,12 @@ export function isProtectedHomePath(relativePath, protectedPaths) {
|
|
|
295
279
|
});
|
|
296
280
|
}
|
|
297
281
|
function normalizeProtectedPath(path) {
|
|
298
|
-
const
|
|
282
|
+
const withSlashes = sep === "/" ? path : path.split(sep).join("/");
|
|
283
|
+
const cleaned = withSlashes.replace(/^\/+/u, "").replace(/\/+$/u, "");
|
|
299
284
|
if (cleaned.length === 0)
|
|
300
285
|
return "";
|
|
301
286
|
const normalized = posix.normalize(cleaned).replace(/\/+$/u, "");
|
|
302
287
|
if (normalized === ".")
|
|
303
288
|
return "";
|
|
304
|
-
return
|
|
289
|
+
return caseInsensitivePaths ? normalized.toLowerCase() : normalized;
|
|
305
290
|
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { ChildProcessWithoutNullStreams } from "node:child_process";
|
|
2
|
+
import type { FileHandle } from "node:fs/promises";
|
|
3
|
+
import type { Server } from "node:net";
|
|
4
|
+
import type { CreateSandboxPolicyOptions, DirectoryIdentity, MockEndpoint, MockForwarderSpec, MockForwarderTemplate, PlatformBackend, SandboxPolicy, ShellLookupHandle, SpawnSandboxedOptions, SpawnSupervisedOptions, SupervisedProcess } from "./types.js";
|
|
5
|
+
export type { CreateSandboxPolicyOptions, DirectoryIdentity, MockEndpoint, MockForwarderSpec, MockForwarderTemplate, PlatformBackend, SandboxPolicy, ShellLookupHandle, SpawnSandboxedOptions, SpawnSupervisedOptions, SupervisedProcess, };
|
|
6
|
+
export declare const isSandboxPlatformSupported: boolean;
|
|
7
|
+
export declare const SYSTEM_PATH: readonly string[];
|
|
8
|
+
export declare const caseInsensitivePaths: boolean;
|
|
9
|
+
export declare const executableExtensions: readonly string[];
|
|
10
|
+
export declare const usesPiShellWrapper: boolean;
|
|
11
|
+
export declare function defaultTempParent(): string;
|
|
12
|
+
export declare function dirEnv(dirs: {
|
|
13
|
+
readonly home: string;
|
|
14
|
+
readonly tmp: string;
|
|
15
|
+
}): Record<string, string>;
|
|
16
|
+
export declare function checkChildEnv(sourceEnv: NodeJS.ProcessEnv): NodeJS.ProcessEnv;
|
|
17
|
+
export declare function openDirectoryIdentity(canonicalPath: string): Promise<DirectoryIdentity>;
|
|
18
|
+
export declare function openRegularFileNoFollow(absolutePath: string): Promise<FileHandle>;
|
|
19
|
+
export declare function isExecutableAccessible(path: string): Promise<boolean>;
|
|
20
|
+
export declare function wrapExecutableCommand(executable: string, args: readonly string[]): readonly [string, ...string[]];
|
|
21
|
+
export declare function spawnShellLookup(command: string, cwd: string): ShellLookupHandle;
|
|
22
|
+
export declare function mockForwarderSpec(): MockForwarderSpec;
|
|
23
|
+
export declare function listenMockServer(server: Server, runRootDir: string): Promise<MockEndpoint>;
|
|
24
|
+
export declare function assertSandboxAvailable(): void;
|
|
25
|
+
export declare function setupSandbox(options: {
|
|
26
|
+
readonly teardown: boolean;
|
|
27
|
+
}): Promise<string>;
|
|
28
|
+
export declare function createSandboxPolicy(options: CreateSandboxPolicyOptions): SandboxPolicy;
|
|
29
|
+
export declare function resolveDeniedExecutables(targets: readonly string[], pathEntries: readonly string[]): Promise<readonly string[]>;
|
|
30
|
+
export declare function spawnSupervised(options: SpawnSupervisedOptions): Promise<SupervisedProcess>;
|
|
31
|
+
export declare function spawnSandboxed(options: SpawnSandboxedOptions): Promise<ChildProcessWithoutNullStreams>;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { tmpdir } from "node:os";
|
|
2
|
+
import { linuxBackend } from "./linux/index.js";
|
|
3
|
+
import { macosBackend } from "./macos/index.js";
|
|
4
|
+
import { windowsBackend } from "./windows/index.js";
|
|
5
|
+
function unsupportedPlatform() {
|
|
6
|
+
throw new Error(`unsupported host platform: ${process.platform}; supported platforms are darwin, linux, and win32`);
|
|
7
|
+
}
|
|
8
|
+
const unsupportedBackend = {
|
|
9
|
+
systemPath: [],
|
|
10
|
+
caseInsensitivePaths: false,
|
|
11
|
+
executableExtensions: [""],
|
|
12
|
+
usesPiShellWrapper: false,
|
|
13
|
+
dirEnv: (dirs) => ({ HOME: dirs.home, TMPDIR: dirs.tmp }),
|
|
14
|
+
checkChildEnv: (env) => ({ PATH: env.PATH, HOME: env.HOME, TMPDIR: env.TMPDIR }),
|
|
15
|
+
openDirectoryIdentity: async () => unsupportedPlatform(),
|
|
16
|
+
openRegularFileNoFollow: async () => unsupportedPlatform(),
|
|
17
|
+
isExecutableAccessible: async () => false,
|
|
18
|
+
wrapExecutableCommand: (executable, args) => [executable, ...args],
|
|
19
|
+
spawnShellLookup: () => unsupportedPlatform(),
|
|
20
|
+
mockForwarderSpec: () => unsupportedPlatform(),
|
|
21
|
+
listenMockServer: async () => unsupportedPlatform(),
|
|
22
|
+
assertSandboxAvailable: unsupportedPlatform,
|
|
23
|
+
setupSandbox: async () => unsupportedPlatform(),
|
|
24
|
+
createSandboxPolicy: unsupportedPlatform,
|
|
25
|
+
resolveDeniedExecutables: async () => unsupportedPlatform(),
|
|
26
|
+
spawnSupervised: async () => unsupportedPlatform(),
|
|
27
|
+
spawnSandboxed: async () => unsupportedPlatform(),
|
|
28
|
+
};
|
|
29
|
+
const backend = process.platform === "darwin"
|
|
30
|
+
? macosBackend
|
|
31
|
+
: process.platform === "linux"
|
|
32
|
+
? linuxBackend
|
|
33
|
+
: process.platform === "win32"
|
|
34
|
+
? windowsBackend
|
|
35
|
+
: unsupportedBackend;
|
|
36
|
+
export const isSandboxPlatformSupported = process.platform === "darwin" || process.platform === "linux" || process.platform === "win32";
|
|
37
|
+
export const SYSTEM_PATH = backend.systemPath;
|
|
38
|
+
export const caseInsensitivePaths = backend.caseInsensitivePaths;
|
|
39
|
+
export const executableExtensions = backend.executableExtensions;
|
|
40
|
+
export const usesPiShellWrapper = backend.usesPiShellWrapper;
|
|
41
|
+
export function defaultTempParent() {
|
|
42
|
+
return backend.defaultTempParent?.() ?? tmpdir();
|
|
43
|
+
}
|
|
44
|
+
export function dirEnv(dirs) {
|
|
45
|
+
return backend.dirEnv(dirs);
|
|
46
|
+
}
|
|
47
|
+
export function checkChildEnv(sourceEnv) {
|
|
48
|
+
return backend.checkChildEnv(sourceEnv);
|
|
49
|
+
}
|
|
50
|
+
export function openDirectoryIdentity(canonicalPath) {
|
|
51
|
+
return backend.openDirectoryIdentity(canonicalPath);
|
|
52
|
+
}
|
|
53
|
+
export function openRegularFileNoFollow(absolutePath) {
|
|
54
|
+
return backend.openRegularFileNoFollow(absolutePath);
|
|
55
|
+
}
|
|
56
|
+
export function isExecutableAccessible(path) {
|
|
57
|
+
return backend.isExecutableAccessible(path);
|
|
58
|
+
}
|
|
59
|
+
export function wrapExecutableCommand(executable, args) {
|
|
60
|
+
return backend.wrapExecutableCommand(executable, args);
|
|
61
|
+
}
|
|
62
|
+
export function spawnShellLookup(command, cwd) {
|
|
63
|
+
return backend.spawnShellLookup(command, cwd);
|
|
64
|
+
}
|
|
65
|
+
export function mockForwarderSpec() {
|
|
66
|
+
return backend.mockForwarderSpec();
|
|
67
|
+
}
|
|
68
|
+
export function listenMockServer(server, runRootDir) {
|
|
69
|
+
return backend.listenMockServer(server, runRootDir);
|
|
70
|
+
}
|
|
71
|
+
export function assertSandboxAvailable() {
|
|
72
|
+
backend.assertSandboxAvailable();
|
|
73
|
+
}
|
|
74
|
+
export function setupSandbox(options) {
|
|
75
|
+
return backend.setupSandbox(options);
|
|
76
|
+
}
|
|
77
|
+
export function createSandboxPolicy(options) {
|
|
78
|
+
return backend.createSandboxPolicy(options);
|
|
79
|
+
}
|
|
80
|
+
export function resolveDeniedExecutables(targets, pathEntries) {
|
|
81
|
+
return backend.resolveDeniedExecutables(targets, pathEntries);
|
|
82
|
+
}
|
|
83
|
+
export function spawnSupervised(options) {
|
|
84
|
+
return backend.spawnSupervised(options);
|
|
85
|
+
}
|
|
86
|
+
export function spawnSandboxed(options) {
|
|
87
|
+
return backend.spawnSandboxed(options);
|
|
88
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { realpath, stat } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { posixPlatformHelpers } from "../posix-helpers.js";
|
|
4
|
+
import { spawnSandboxed } from "./launch.js";
|
|
5
|
+
import { assertBwrapAvailable, createSandboxPolicy, SYSTEM_PATH } from "./policy.js";
|
|
6
|
+
import { spawnSupervised } from "./supervise.js";
|
|
7
|
+
async function resolveDeniedExecutables(targets, pathEntries) {
|
|
8
|
+
if (targets.length === 0)
|
|
9
|
+
return [];
|
|
10
|
+
const denied = [];
|
|
11
|
+
for (const target of targets) {
|
|
12
|
+
for (const dir of pathEntries) {
|
|
13
|
+
const candidate = join(dir, target);
|
|
14
|
+
try {
|
|
15
|
+
if ((await stat(candidate)).isFile())
|
|
16
|
+
denied.push(candidate, await realpath(candidate));
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
// Not installed here.
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return denied;
|
|
24
|
+
}
|
|
25
|
+
export const linuxBackend = {
|
|
26
|
+
...posixPlatformHelpers,
|
|
27
|
+
systemPath: SYSTEM_PATH,
|
|
28
|
+
caseInsensitivePaths: false,
|
|
29
|
+
assertSandboxAvailable: assertBwrapAvailable,
|
|
30
|
+
async setupSandbox({ teardown }) {
|
|
31
|
+
if (teardown)
|
|
32
|
+
return "Linux bubblewrap requires no host teardown.\n";
|
|
33
|
+
assertBwrapAvailable();
|
|
34
|
+
return "Linux bubblewrap sandbox is ready.\n";
|
|
35
|
+
},
|
|
36
|
+
createSandboxPolicy,
|
|
37
|
+
resolveDeniedExecutables,
|
|
38
|
+
spawnSupervised,
|
|
39
|
+
spawnSandboxed,
|
|
40
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type ChildProcessWithoutNullStreams } from "node:child_process";
|
|
2
|
+
import type { SpawnSandboxedOptions } from "../types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Launch a command inside a bubblewrap sandbox as its own process group.
|
|
5
|
+
* `--unshare-pid --die-with-parent` ensures the kernel reaps every descendant on exit,
|
|
6
|
+
* while `--args` passes the workload environment via FD 3 instead of host env/argv.
|
|
7
|
+
*/
|
|
8
|
+
export declare function spawnSandboxed(options: SpawnSandboxedOptions): Promise<ChildProcessWithoutNullStreams>;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { bwrapExecutablePath, prepareBwrapLaunch, trustedHostEnv } from "./policy.js";
|
|
3
|
+
/**
|
|
4
|
+
* Launch a command inside a bubblewrap sandbox as its own process group.
|
|
5
|
+
* `--unshare-pid --die-with-parent` ensures the kernel reaps every descendant on exit,
|
|
6
|
+
* while `--args` passes the workload environment via FD 3 instead of host env/argv.
|
|
7
|
+
*/
|
|
8
|
+
export async function spawnSandboxed(options) {
|
|
9
|
+
const executable = bwrapExecutablePath();
|
|
10
|
+
const launch = await prepareBwrapLaunch(options.policyDirectory, options.policy, options.env);
|
|
11
|
+
try {
|
|
12
|
+
const stdio = ["pipe", "pipe", "pipe", launch.argsFd];
|
|
13
|
+
if (launch.seccompFd !== undefined)
|
|
14
|
+
stdio.push(launch.seccompFd);
|
|
15
|
+
return spawn(executable, ["--args", "3", "--", options.command, ...options.args], {
|
|
16
|
+
cwd: options.cwd,
|
|
17
|
+
env: trustedHostEnv(),
|
|
18
|
+
stdio,
|
|
19
|
+
detached: true,
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
finally {
|
|
23
|
+
launch.closeFds();
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { CreateSandboxPolicyOptions, SandboxPolicy } from "../types.js";
|
|
2
|
+
export type { SandboxPolicy };
|
|
3
|
+
export declare const SYSTEM_PATH: readonly ["/usr/bin", "/bin", "/usr/sbin", "/sbin"];
|
|
4
|
+
/** Minimal trusted environment for host-side launcher processes (`bwrap`, `script`). */
|
|
5
|
+
export declare function trustedHostEnv(includeTerm?: boolean): NodeJS.ProcessEnv;
|
|
6
|
+
export declare function createSandboxPolicy(options: CreateSandboxPolicyOptions): SandboxPolicy;
|
|
7
|
+
/** Verify `bwrap` is installed and unprivileged user namespaces work; fail closed otherwise. */
|
|
8
|
+
export declare function assertBwrapAvailable(): void;
|
|
9
|
+
export declare function bwrapExecutablePath(): string;
|
|
10
|
+
/**
|
|
11
|
+
* Build a minimal cBPF seccomp filter for network-disabled sandboxes. Denies `socket(2)`,
|
|
12
|
+
* `connect(2)`, `io_uring_setup(2)`, and (on x86_64) x32 ABI syscalls with `EPERM`, blocking
|
|
13
|
+
* stream and connectionless `AF_UNIX` sockets across the entire filesystem while keeping
|
|
14
|
+
* `pipe2`/`socketpair` and ordinary read-only files working.
|
|
15
|
+
*/
|
|
16
|
+
export declare function buildNoNetworkSeccomp(arch?: string): Buffer;
|
|
17
|
+
/** Build the `bwrap` option list (everything before `-- <command>`) implementing `policy`. */
|
|
18
|
+
export declare function buildBwrapOptions(policy: SandboxPolicy, options?: {
|
|
19
|
+
readonly env?: NodeJS.ProcessEnv;
|
|
20
|
+
readonly newSession?: boolean;
|
|
21
|
+
readonly seccompFd?: number;
|
|
22
|
+
}): string[];
|
|
23
|
+
/** Build the full `bwrap` argument list implementing `policy` around `command` and `args`. */
|
|
24
|
+
export declare function buildBwrapArgs(policy: SandboxPolicy, command: string, args: readonly string[]): string[];
|
|
25
|
+
export interface PreparedBwrapLaunch {
|
|
26
|
+
readonly argsFd: number;
|
|
27
|
+
readonly seccompFd?: number;
|
|
28
|
+
readonly closeFds: () => void;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Create unlinked read-only FDs for `bwrap --args` (including `--clearenv`/`--setenv` workload
|
|
32
|
+
* env) and optional `--seccomp` BPF program. Each backing file is created exclusively with mode
|
|
33
|
+
* 0600, opened, and unlinked immediately before spawning so no credentials ever remain on disk.
|
|
34
|
+
*/
|
|
35
|
+
export declare function prepareBwrapLaunch(policyDirectory: string, policy: SandboxPolicy, env: NodeJS.ProcessEnv, options?: {
|
|
36
|
+
readonly newSession?: boolean;
|
|
37
|
+
}): Promise<PreparedBwrapLaunch>;
|
|
38
|
+
/**
|
|
39
|
+
* Walk a symlink chain starting inside `/etc` until the first hop outside `/etc` and `/usr`.
|
|
40
|
+
* Mounting the final resolved target at that first unmounted hop keeps multi-hop resolver
|
|
41
|
+
* chains working inside the read-only `/etc` bind.
|
|
42
|
+
*/
|
|
43
|
+
export declare function resolveExternalSymlinkTarget(file: string, isMounted?: (path: string) => boolean): {
|
|
44
|
+
readonly source: string;
|
|
45
|
+
readonly dest: string;
|
|
46
|
+
} | undefined;
|