@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
|
@@ -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
|
+
};
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import type { ChildProcess, ChildProcessByStdio, ChildProcessWithoutNullStreams } from "node:child_process";
|
|
2
|
+
import type { FileHandle } from "node:fs/promises";
|
|
3
|
+
import type { Server } from "node:net";
|
|
4
|
+
import type { Readable } from "node:stream";
|
|
5
|
+
export interface DirectoryIdentity {
|
|
6
|
+
readonly dev: bigint;
|
|
7
|
+
readonly ino: bigint;
|
|
8
|
+
}
|
|
9
|
+
export interface ShellLookupHandle {
|
|
10
|
+
readonly child: ChildProcessByStdio<null, Readable, Readable>;
|
|
11
|
+
readonly terminate: () => void;
|
|
12
|
+
}
|
|
13
|
+
export interface MockForwarderTemplate {
|
|
14
|
+
readonly assetPath: string;
|
|
15
|
+
readonly quote: (value: string) => string;
|
|
16
|
+
}
|
|
17
|
+
export interface MockPathForwarderTemplate extends MockForwarderTemplate {
|
|
18
|
+
readonly templateName: string;
|
|
19
|
+
readonly targetFileName: (target: string) => string;
|
|
20
|
+
}
|
|
21
|
+
export interface MockForwarderSpec extends MockPathForwarderTemplate {
|
|
22
|
+
readonly clientPath: string;
|
|
23
|
+
/** Additional PATH shims placed beside the primary one (e.g. extensionless `sh` shims for Git Bash on Windows). */
|
|
24
|
+
readonly extraPathForwarders?: readonly MockPathForwarderTemplate[];
|
|
25
|
+
readonly workspaceForwarder?: (target: string) => MockForwarderTemplate;
|
|
26
|
+
readonly resolvePathTargetName: (selfPath: string, forwarderRoot: string) => string | undefined;
|
|
27
|
+
}
|
|
28
|
+
export interface MockEndpoint {
|
|
29
|
+
readonly socketPath: string;
|
|
30
|
+
readonly expectedNonce?: string;
|
|
31
|
+
readonly cleanup: () => Promise<void>;
|
|
32
|
+
}
|
|
33
|
+
export interface SandboxPolicy {
|
|
34
|
+
/** Read and write access. */
|
|
35
|
+
readonly writableRoots: readonly string[];
|
|
36
|
+
/** Read-only access. */
|
|
37
|
+
readonly readableRoots: readonly string[];
|
|
38
|
+
/**
|
|
39
|
+
* Read everything the host user can read, as the check sandbox does after the evaluated agent
|
|
40
|
+
* stopped. It replaces `readableRoots` for the read clauses.
|
|
41
|
+
*/
|
|
42
|
+
readonly readAll?: boolean;
|
|
43
|
+
/** Read-only single files, such as a launcher script. */
|
|
44
|
+
readonly readableFiles?: readonly string[];
|
|
45
|
+
/** Writable single files or Unix sockets outside `writableRoots`, such as a mock host socket. */
|
|
46
|
+
readonly writableFiles?: readonly string[];
|
|
47
|
+
/** Executables the sandbox may not start, such as the real binary behind a mocked command. */
|
|
48
|
+
readonly denyExecutables?: readonly string[];
|
|
49
|
+
/** Interactive terminal device; allow termios mode changes, not arbitrary ioctl operations. */
|
|
50
|
+
readonly terminal?: string;
|
|
51
|
+
readonly allowNetwork: boolean;
|
|
52
|
+
}
|
|
53
|
+
export interface CreateSandboxPolicyOptions {
|
|
54
|
+
readonly writableRoots: readonly string[];
|
|
55
|
+
readonly readableRoots: readonly string[];
|
|
56
|
+
readonly readableFiles?: readonly string[];
|
|
57
|
+
readonly writableFiles?: readonly string[];
|
|
58
|
+
readonly denyExecutables?: readonly string[];
|
|
59
|
+
readonly allowNetwork: boolean;
|
|
60
|
+
readonly interactive: boolean;
|
|
61
|
+
}
|
|
62
|
+
export interface SupervisedProcess {
|
|
63
|
+
readonly child: ChildProcess;
|
|
64
|
+
/** Native workload exit code, not the supervisor's teardown exit. */
|
|
65
|
+
readonly completion: Promise<number>;
|
|
66
|
+
readonly stop: () => Promise<void>;
|
|
67
|
+
/**
|
|
68
|
+
* The workload's private report pipe at FD 5, present only when requested. FD 5 is not a
|
|
69
|
+
* standard stream, so it stays out of the workload's own output; `spawn` closes it for the
|
|
70
|
+
* workload's children, so untrusted descendants cannot write it.
|
|
71
|
+
*/
|
|
72
|
+
readonly report?: Readable;
|
|
73
|
+
}
|
|
74
|
+
export interface SpawnSupervisedOptions {
|
|
75
|
+
readonly policy: SandboxPolicy;
|
|
76
|
+
readonly policyDirectory: string;
|
|
77
|
+
readonly command: readonly [string, ...string[]];
|
|
78
|
+
readonly cwd: string;
|
|
79
|
+
readonly env: NodeJS.ProcessEnv;
|
|
80
|
+
readonly interactive: boolean;
|
|
81
|
+
/** Add a private report pipe at FD 5 for the workload, exposed as `report`. */
|
|
82
|
+
readonly report?: boolean;
|
|
83
|
+
}
|
|
84
|
+
export interface SpawnSandboxedOptions {
|
|
85
|
+
readonly policy: SandboxPolicy;
|
|
86
|
+
readonly policyDirectory: string;
|
|
87
|
+
readonly command: string;
|
|
88
|
+
readonly args: readonly string[];
|
|
89
|
+
readonly cwd: string;
|
|
90
|
+
readonly env: NodeJS.ProcessEnv;
|
|
91
|
+
}
|
|
92
|
+
export interface PlatformBackend {
|
|
93
|
+
readonly systemPath: readonly string[];
|
|
94
|
+
readonly caseInsensitivePaths: boolean;
|
|
95
|
+
readonly executableExtensions: readonly string[];
|
|
96
|
+
readonly usesPiShellWrapper: boolean;
|
|
97
|
+
defaultTempParent?(): string;
|
|
98
|
+
dirEnv(dirs: {
|
|
99
|
+
readonly home: string;
|
|
100
|
+
readonly tmp: string;
|
|
101
|
+
}): Record<string, string>;
|
|
102
|
+
checkChildEnv(sourceEnv: NodeJS.ProcessEnv): NodeJS.ProcessEnv;
|
|
103
|
+
openDirectoryIdentity(canonicalPath: string): Promise<DirectoryIdentity>;
|
|
104
|
+
openRegularFileNoFollow(absolutePath: string): Promise<FileHandle>;
|
|
105
|
+
isExecutableAccessible(path: string): Promise<boolean>;
|
|
106
|
+
wrapExecutableCommand(executable: string, args: readonly string[]): readonly [string, ...string[]];
|
|
107
|
+
spawnShellLookup(command: string, cwd: string): ShellLookupHandle;
|
|
108
|
+
mockForwarderSpec(): MockForwarderSpec;
|
|
109
|
+
listenMockServer(server: Server, runRootDir: string): Promise<MockEndpoint>;
|
|
110
|
+
assertSandboxAvailable(): void;
|
|
111
|
+
setupSandbox(options: {
|
|
112
|
+
readonly teardown: boolean;
|
|
113
|
+
}): Promise<string>;
|
|
114
|
+
createSandboxPolicy(options: CreateSandboxPolicyOptions): SandboxPolicy;
|
|
115
|
+
resolveDeniedExecutables(targets: readonly string[], pathEntries: readonly string[]): Promise<readonly string[]>;
|
|
116
|
+
spawnSupervised(options: SpawnSupervisedOptions): Promise<SupervisedProcess>;
|
|
117
|
+
spawnSandboxed(options: SpawnSandboxedOptions): Promise<ChildProcessWithoutNullStreams>;
|
|
118
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { appendFileSync, realpathSync, statSync } from "node:fs";
|
|
3
|
+
import { createConnection } from "node:net";
|
|
4
|
+
import { basename, delimiter, dirname, join, resolve, sep } from "node:path";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Windows mock forwarder client.
|
|
8
|
+
*
|
|
9
|
+
* Invoked by `forwarder.cmd` as:
|
|
10
|
+
* node forwarder-client.mjs <bindir> <endpoint> <fault> <self> [...args]
|
|
11
|
+
*
|
|
12
|
+
* Reports the canonicalised invocation path and arguments over the host's authenticated
|
|
13
|
+
* loopback TCP endpoint. For mocked results, replays stdout/stderr directly and exits with
|
|
14
|
+
* the mocked code. For `pass`, runs the resolved real executable with inherited stdio.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
const [bindir, endpoint, faultPath, rawSelf, ...args] = process.argv.slice(2);
|
|
18
|
+
if (bindir === undefined || endpoint === undefined || faultPath === undefined || rawSelf === undefined) {
|
|
19
|
+
process.exit(126);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const self = canonicalSelf(rawSelf);
|
|
23
|
+
const inBinDir = self.toLowerCase().startsWith(`${bindir.toLowerCase()}${sep}`);
|
|
24
|
+
const name = inBinDir && self.toLowerCase().endsWith(".cmd") ? basename(self).slice(0, -4) : basename(self);
|
|
25
|
+
|
|
26
|
+
function canonicalSelf(path) {
|
|
27
|
+
try {
|
|
28
|
+
return join(realpathSync(dirname(path)), basename(path));
|
|
29
|
+
} catch {
|
|
30
|
+
return path;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function encode(value) {
|
|
35
|
+
return Buffer.from(value, "utf8").toString("base64");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function failUnavailable() {
|
|
39
|
+
process.stderr.write(`${name}: Service unavailable\n`);
|
|
40
|
+
try {
|
|
41
|
+
appendFileSync(faultPath, `${name}\n`);
|
|
42
|
+
} catch {
|
|
43
|
+
// Fault log is best-effort when the temporary directory is inaccessible.
|
|
44
|
+
}
|
|
45
|
+
process.exitCode = 126;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function isRegularFile(path) {
|
|
49
|
+
try {
|
|
50
|
+
return statSync(path).isFile();
|
|
51
|
+
} catch {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function findPassTarget() {
|
|
57
|
+
if (inBinDir) {
|
|
58
|
+
const extensions = ["", ".exe", ".cmd", ".bat", ".com"];
|
|
59
|
+
for (const folder of (process.env.PATH ?? "").split(delimiter)) {
|
|
60
|
+
if (folder.length === 0) continue;
|
|
61
|
+
for (const ext of extensions) {
|
|
62
|
+
const candidate = resolve(folder, `${name}${ext}`);
|
|
63
|
+
if (!isRegularFile(candidate)) continue;
|
|
64
|
+
const canonical = canonicalSelf(candidate).toLowerCase();
|
|
65
|
+
if (canonical === self.toLowerCase() || canonical.startsWith(`${bindir.toLowerCase()}${sep}`)) continue;
|
|
66
|
+
return candidate;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
const real = resolve(dirname(self), `.${basename(self)}`);
|
|
72
|
+
return isRegularFile(real) ? real : undefined;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function runPass() {
|
|
76
|
+
const target = findPassTarget();
|
|
77
|
+
if (target === undefined) {
|
|
78
|
+
process.stderr.write(`${name}: Operation not permitted\n`);
|
|
79
|
+
process.exitCode = 126;
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
const isPs1 = target.toLowerCase().endsWith(".ps1");
|
|
83
|
+
const res = isPs1
|
|
84
|
+
? spawnSync(
|
|
85
|
+
"powershell.exe",
|
|
86
|
+
["-NoLogo", "-NoProfile", "-NonInteractive", "-InputFormat", "None", "-File", target, ...args],
|
|
87
|
+
{ stdio: "inherit", windowsHide: true },
|
|
88
|
+
)
|
|
89
|
+
: spawnSync(target, args, {
|
|
90
|
+
stdio: "inherit",
|
|
91
|
+
shell: /\.(?:cmd|bat)$/iu.test(target),
|
|
92
|
+
windowsHide: true,
|
|
93
|
+
});
|
|
94
|
+
if (res.error !== undefined) {
|
|
95
|
+
process.stderr.write(`${name}: ${res.error.message}\n`);
|
|
96
|
+
process.exitCode = 126;
|
|
97
|
+
} else {
|
|
98
|
+
process.exitCode = res.status ?? 1;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function parseEndpoint(value) {
|
|
103
|
+
if (!value.startsWith("tcp://127.0.0.1:")) return undefined;
|
|
104
|
+
const rest = value.slice("tcp://127.0.0.1:".length);
|
|
105
|
+
const slash = rest.indexOf("/");
|
|
106
|
+
if (slash === -1) return undefined;
|
|
107
|
+
const port = Number(rest.slice(0, slash));
|
|
108
|
+
const nonce = rest.slice(slash + 1);
|
|
109
|
+
if (!Number.isInteger(port) || port <= 0 || nonce.length === 0) return undefined;
|
|
110
|
+
return { port, nonce };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const parsed = parseEndpoint(endpoint);
|
|
114
|
+
if (parsed === undefined) {
|
|
115
|
+
failUnavailable();
|
|
116
|
+
process.exit(126);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const socket = createConnection({ host: "127.0.0.1", port: parsed.port });
|
|
120
|
+
const chunks = [];
|
|
121
|
+
let settled = false;
|
|
122
|
+
|
|
123
|
+
socket.on("error", () => {
|
|
124
|
+
if (settled) return;
|
|
125
|
+
settled = true;
|
|
126
|
+
socket.destroy();
|
|
127
|
+
failUnavailable();
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
socket.on("data", (chunk) => {
|
|
131
|
+
chunks.push(chunk);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
socket.on("end", () => {
|
|
135
|
+
if (settled) return;
|
|
136
|
+
settled = true;
|
|
137
|
+
socket.destroy();
|
|
138
|
+
const reply = Buffer.concat(chunks)
|
|
139
|
+
.toString("utf8")
|
|
140
|
+
.replace(/\r?\n$/, "");
|
|
141
|
+
if (reply.length === 0) {
|
|
142
|
+
failUnavailable();
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
const [kind, codeText, outText, errText] = reply.split(" ");
|
|
146
|
+
if (kind === "pass") {
|
|
147
|
+
runPass();
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
const code = Number(codeText);
|
|
151
|
+
if (!Number.isInteger(code)) {
|
|
152
|
+
failUnavailable();
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
if (outText !== undefined && outText !== "" && outText !== "-") {
|
|
156
|
+
process.stdout.write(Buffer.from(outText, "base64"));
|
|
157
|
+
}
|
|
158
|
+
if (errText !== undefined && errText !== "" && errText !== "-") {
|
|
159
|
+
process.stderr.write(Buffer.from(errText, "base64"));
|
|
160
|
+
}
|
|
161
|
+
process.exitCode = code;
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
const payload = [`nonce:${parsed.nonce}`, self, ...args].map(encode).join("\n") + "\n";
|
|
165
|
+
socket.end(payload);
|