@lelouchhe/agent-evals 0.1.1 → 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 +27 -18
- package/dist/checkers/file/index.js +181 -13
- package/dist/checkers/judge/index.d.ts +1 -1
- package/dist/checkers/judge/index.js +243 -31
- package/dist/checkers/judge/prompt.d.ts +4 -3
- package/dist/checkers/judge/prompt.js +22 -3
- package/dist/cli.js +171 -35
- package/dist/engine/authoring.js +108 -13
- package/dist/engine/check-runner.js +18 -3
- package/dist/engine/check-sandbox.js +9 -6
- package/dist/engine/config.d.ts +31 -2
- package/dist/engine/config.js +16 -10
- package/dist/engine/env-source.js +82 -14
- package/dist/engine/files.d.ts +2 -5
- package/dist/engine/files.js +4 -6
- package/dist/engine/mocks.js +72 -46
- package/dist/engine/params.d.ts +2 -0
- package/dist/engine/params.js +10 -0
- package/dist/engine/registry.d.ts +11 -0
- package/dist/engine/report.js +43 -8
- package/dist/engine/results.d.ts +7 -0
- package/dist/engine/results.js +9 -6
- package/dist/engine/run.d.ts +4 -1
- package/dist/engine/run.js +171 -28
- package/dist/engine/skills.js +12 -5
- package/dist/engine/view.d.ts +1 -1
- package/dist/engine/view.js +125 -10
- package/dist/engine/workspace.d.ts +7 -0
- package/dist/engine/workspace.js +37 -27
- package/dist/index.d.ts +4 -4
- package/dist/index.js +2 -2
- 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 +5 -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,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
|
];
|
|
@@ -1,26 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
import { type SandboxPolicy } from "./policy.js";
|
|
4
|
-
export interface SupervisedProcess {
|
|
5
|
-
readonly child: ChildProcess;
|
|
6
|
-
/** Native workload result, not the supervisor's intentional SIGKILL exit. */
|
|
7
|
-
readonly completion: Promise<number>;
|
|
8
|
-
readonly stop: () => Promise<void>;
|
|
9
|
-
/**
|
|
10
|
-
* The workload's private report pipe at FD 5, present only when requested. FD 5 is not a
|
|
11
|
-
* standard stream, so it stays out of the workload's own output; `spawn` closes it for the
|
|
12
|
-
* workload's children, so untrusted descendants cannot write it.
|
|
13
|
-
*/
|
|
14
|
-
readonly report?: Readable;
|
|
15
|
-
}
|
|
1
|
+
import type { SpawnSupervisedOptions, SupervisedProcess } from "../types.js";
|
|
2
|
+
export type { SupervisedProcess };
|
|
16
3
|
/** Start a sandbox-wide-cleanup supervisor; never execute its program on the host. */
|
|
17
|
-
export declare function spawnSupervised(options:
|
|
18
|
-
readonly policy: SandboxPolicy;
|
|
19
|
-
readonly policyDirectory: string;
|
|
20
|
-
readonly command: readonly [string, ...string[]];
|
|
21
|
-
readonly cwd: string;
|
|
22
|
-
readonly env: NodeJS.ProcessEnv;
|
|
23
|
-
readonly interactive: boolean;
|
|
24
|
-
/** Add a private report pipe at FD 5 for the workload, exposed as `report`. */
|
|
25
|
-
readonly report?: boolean;
|
|
26
|
-
}): Promise<SupervisedProcess>;
|
|
4
|
+
export declare function spawnSupervised(options: SpawnSupervisedOptions): Promise<SupervisedProcess>;
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { accessSync, appendFileSync, constants, realpathSync, statSync, writeSync } from "node:fs";
|
|
2
|
+
import { createConnection } from "node:net";
|
|
3
|
+
import { basename, dirname, join, resolve } from "node:path";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* POSIX mock forwarder client.
|
|
7
|
+
*
|
|
8
|
+
* Invoked by `forwarder.sh` as:
|
|
9
|
+
* node posix-forwarder-client.mjs <bindir> <socket> <fault> <self> [...args] 4>&1 1>&3
|
|
10
|
+
*
|
|
11
|
+
* Reports the canonicalised invocation path and arguments to the engine's host Unix socket.
|
|
12
|
+
* For mocked results, replays stdout/stderr directly to FDs 1/2 and exits with the mocked code.
|
|
13
|
+
* For `pass`, writes the resolved absolute target path to FD 4 so `forwarder.sh` replaces itself
|
|
14
|
+
* with `exec "$target" "$@"`.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
const [bindir, socketPath, faultPath, rawSelf, ...args] = process.argv.slice(2);
|
|
18
|
+
if (bindir === undefined || socketPath === undefined || faultPath === undefined || rawSelf === undefined) {
|
|
19
|
+
process.exit(126);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const self = canonicalSelf(rawSelf);
|
|
23
|
+
const name = basename(self);
|
|
24
|
+
|
|
25
|
+
function canonicalSelf(path) {
|
|
26
|
+
try {
|
|
27
|
+
return join(realpathSync(dirname(path)), basename(path));
|
|
28
|
+
} catch {
|
|
29
|
+
return path;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function encode(value) {
|
|
34
|
+
return Buffer.from(value, "utf8").toString("base64");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function failUnavailable() {
|
|
38
|
+
process.stderr.write(`${name}: Service unavailable\n`);
|
|
39
|
+
try {
|
|
40
|
+
appendFileSync(faultPath, `${name}\n`);
|
|
41
|
+
} catch {
|
|
42
|
+
// Fault log is best-effort when the temporary directory is inaccessible.
|
|
43
|
+
}
|
|
44
|
+
process.exitCode = 126;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function isExecutableFile(path) {
|
|
48
|
+
try {
|
|
49
|
+
if (!statSync(path).isFile()) return false;
|
|
50
|
+
accessSync(path, constants.X_OK);
|
|
51
|
+
return true;
|
|
52
|
+
} catch {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function findPassTarget() {
|
|
58
|
+
if (self.startsWith(`${bindir}/`)) {
|
|
59
|
+
for (const folder of (process.env.PATH ?? "").split(":")) {
|
|
60
|
+
if (folder.length === 0) continue;
|
|
61
|
+
const candidate = resolve(folder, name);
|
|
62
|
+
if (!isExecutableFile(candidate)) continue;
|
|
63
|
+
if (canonicalSelf(candidate) === self) continue;
|
|
64
|
+
return candidate;
|
|
65
|
+
}
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
const real = resolve(dirname(self), `.${name}`);
|
|
69
|
+
return isExecutableFile(real) ? real : undefined;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function runPass() {
|
|
73
|
+
const target = findPassTarget();
|
|
74
|
+
if (target === undefined) {
|
|
75
|
+
process.stderr.write(`${name}: Operation not permitted\n`);
|
|
76
|
+
process.exitCode = 126;
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
writeSync(4, target);
|
|
80
|
+
process.exitCode = 0;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const socket = createConnection(socketPath);
|
|
84
|
+
const chunks = [];
|
|
85
|
+
let settled = false;
|
|
86
|
+
|
|
87
|
+
socket.on("error", () => {
|
|
88
|
+
if (settled) return;
|
|
89
|
+
settled = true;
|
|
90
|
+
socket.destroy();
|
|
91
|
+
failUnavailable();
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
socket.on("data", (chunk) => {
|
|
95
|
+
chunks.push(chunk);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
socket.on("end", () => {
|
|
99
|
+
if (settled) return;
|
|
100
|
+
settled = true;
|
|
101
|
+
socket.destroy();
|
|
102
|
+
const reply = Buffer.concat(chunks)
|
|
103
|
+
.toString("utf8")
|
|
104
|
+
.replace(/\r?\n$/, "");
|
|
105
|
+
if (reply.length === 0) {
|
|
106
|
+
failUnavailable();
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
const [kind, codeText, outText, errText] = reply.split(" ");
|
|
110
|
+
if (kind === "pass") {
|
|
111
|
+
runPass();
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
const code = Number(codeText);
|
|
115
|
+
if (!Number.isInteger(code)) {
|
|
116
|
+
failUnavailable();
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
if (outText !== undefined && outText !== "" && outText !== "-") {
|
|
120
|
+
process.stdout.write(Buffer.from(outText, "base64"));
|
|
121
|
+
}
|
|
122
|
+
if (errText !== undefined && errText !== "" && errText !== "-") {
|
|
123
|
+
process.stderr.write(Buffer.from(errText, "base64"));
|
|
124
|
+
}
|
|
125
|
+
process.exitCode = code;
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
const payload = [self, ...args].map(encode).join("\n") + "\n";
|
|
129
|
+
socket.end(payload);
|
|
@@ -0,0 +1,22 @@
|
|
|
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 { DirectoryIdentity, MockEndpoint, MockForwarderSpec, ShellLookupHandle } from "./types.js";
|
|
5
|
+
export declare const posixPlatformHelpers: {
|
|
6
|
+
executableExtensions: readonly string[];
|
|
7
|
+
usesPiShellWrapper: boolean;
|
|
8
|
+
dirEnv(dirs: {
|
|
9
|
+
readonly home: string;
|
|
10
|
+
readonly tmp: string;
|
|
11
|
+
}): Record<string, string>;
|
|
12
|
+
checkChildEnv(sourceEnv: NodeJS.ProcessEnv): NodeJS.ProcessEnv;
|
|
13
|
+
openDirectoryIdentity(canonicalPath: string): Promise<DirectoryIdentity>;
|
|
14
|
+
openRegularFileNoFollow(absolutePath: string): Promise<FileHandle>;
|
|
15
|
+
isExecutableAccessible(path: string): Promise<boolean>;
|
|
16
|
+
wrapExecutableCommand(executable: string, args: readonly string[]): readonly [string, ...string[]];
|
|
17
|
+
spawnShellLookup(command: string, cwd: string): ShellLookupHandle;
|
|
18
|
+
mockForwarderSpec(): MockForwarderSpec;
|
|
19
|
+
listenMockServer(server: Server, runRootDir: string): Promise<MockEndpoint>;
|
|
20
|
+
};
|
|
21
|
+
export type PosixPlatformHelpers = typeof posixPlatformHelpers;
|
|
22
|
+
export type { ChildProcessWithoutNullStreams };
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { access, constants, open, rm } from "node:fs/promises";
|
|
3
|
+
import { basename, dirname, join } from "node:path";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
export const posixPlatformHelpers = {
|
|
7
|
+
executableExtensions: [""],
|
|
8
|
+
usesPiShellWrapper: true,
|
|
9
|
+
dirEnv(dirs) {
|
|
10
|
+
return { HOME: dirs.home, TMPDIR: dirs.tmp };
|
|
11
|
+
},
|
|
12
|
+
checkChildEnv(sourceEnv) {
|
|
13
|
+
return {
|
|
14
|
+
PATH: sourceEnv.PATH,
|
|
15
|
+
HOME: sourceEnv.HOME,
|
|
16
|
+
TMPDIR: sourceEnv.TMPDIR,
|
|
17
|
+
};
|
|
18
|
+
},
|
|
19
|
+
async openDirectoryIdentity(canonicalPath) {
|
|
20
|
+
const handle = await open(canonicalPath, constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW);
|
|
21
|
+
try {
|
|
22
|
+
const stats = await handle.stat({ bigint: true });
|
|
23
|
+
if (!stats.isDirectory()) {
|
|
24
|
+
throw Object.assign(new Error(`not a directory: ${canonicalPath}`), { code: "ENOTDIR" });
|
|
25
|
+
}
|
|
26
|
+
return { dev: stats.dev, ino: stats.ino };
|
|
27
|
+
}
|
|
28
|
+
finally {
|
|
29
|
+
await handle.close();
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
async openRegularFileNoFollow(absolutePath) {
|
|
33
|
+
return open(absolutePath, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
|
34
|
+
},
|
|
35
|
+
async isExecutableAccessible(path) {
|
|
36
|
+
try {
|
|
37
|
+
await access(path, constants.X_OK);
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
wrapExecutableCommand(executable, args) {
|
|
45
|
+
return [executable, ...args];
|
|
46
|
+
},
|
|
47
|
+
spawnShellLookup(command, cwd) {
|
|
48
|
+
// Spawn in its own process group so abort/timeout kills the shell and any direct child
|
|
49
|
+
// regardless of whether `/bin/sh` exec-optimizes simple commands (dash vs bash).
|
|
50
|
+
const child = spawn("/bin/sh", ["-c", command], {
|
|
51
|
+
cwd,
|
|
52
|
+
env: process.env,
|
|
53
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
54
|
+
detached: true,
|
|
55
|
+
});
|
|
56
|
+
return {
|
|
57
|
+
child,
|
|
58
|
+
terminate() {
|
|
59
|
+
if (child.pid !== undefined) {
|
|
60
|
+
try {
|
|
61
|
+
process.kill(-child.pid, "SIGKILL");
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
// Already exited.
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
},
|
|
70
|
+
mockForwarderSpec() {
|
|
71
|
+
return {
|
|
72
|
+
assetPath: fileURLToPath(new URL("./posix-forwarder.sh", import.meta.url)),
|
|
73
|
+
clientPath: fileURLToPath(new URL("./posix-forwarder-client.mjs", import.meta.url)),
|
|
74
|
+
templateName: "forward",
|
|
75
|
+
quote: (value) => `'${value.replaceAll("'", "'\\''")}'`,
|
|
76
|
+
targetFileName: (target) => target,
|
|
77
|
+
resolvePathTargetName: (selfPath, forwarderRoot) => dirname(selfPath) === forwarderRoot ? basename(selfPath) : undefined,
|
|
78
|
+
};
|
|
79
|
+
},
|
|
80
|
+
async listenMockServer(server, runRootDir) {
|
|
81
|
+
// A Unix socket path is limited to about 100 bytes, so it cannot live under the run root.
|
|
82
|
+
const socketPath = join(tmpdir(), `${basename(runRootDir)}.sock`);
|
|
83
|
+
await new Promise((resolve, reject) => {
|
|
84
|
+
server.once("error", reject);
|
|
85
|
+
server.listen(socketPath, () => {
|
|
86
|
+
server.off("error", reject);
|
|
87
|
+
resolve();
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
return {
|
|
91
|
+
socketPath,
|
|
92
|
+
cleanup: async () => {
|
|
93
|
+
await rm(socketPath, { force: true });
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
},
|
|
97
|
+
};
|