✨ Sandbox the local POC's protected checks and installations on macOS, by operator opt-in

In the development host mode, protected checks and dependency
installations ran directly as the operator. They run the agent's code:
a test it wrote could read the home directory's secrets, reach the
network, or plant Git configuration that the trusted Git commands after
it would execute.

With WF_HOST_CHECK_SANDBOX=true they run under Seatbelt instead. File
contents in the home directory are unreadable except the command's own
directory and the paths the operator lists; it may write only there,
in the caches the operator lists and in a temporary directory of its
own, never in a .git directory. Only installations reach the network;
checks may use loopback ports by opt-in, for test suites that start
local servers. The platform's own ports (control plane, Temporal and
its web UI, database, object storage, provisioner, Git service, IDE,
console) stay blocked in any case: the local start lists them from
their configured addresses. Checks get no answer from the keychain,
and a command whose home is the executor's (holding its credentials)
gets a private one. A sandbox that cannot start leaves the run waiting
on infrastructure; verification evidence records checkSandbox.

scripts/check-host-check-sandbox.ts checks all of this on the target
Mac, from outside the sandbox, then runs the POC's installation and
checks sandboxed on a recent droboticflow source. Enabled on the local
POC, a real wish passed through both, including a repair round for the
agent's own failing tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
eust-w 2026-10-26 10:10:47 +08:00
parent 8fc4ab8e47
commit 3623d318af
19 changed files with 827 additions and 22 deletions

View File

@ -23,6 +23,11 @@ const PASSTHROUGH = [
'WF_CLAUDE_COMMAND_SANDBOX',
'WF_CLAUDE_COMMAND_READ_PATHS',
'WF_CLAUDE_COMMAND_ENV',
'WF_HOST_CHECK_SANDBOX',
'WF_HOST_CHECK_READ_PATHS',
'WF_HOST_CHECK_WRITE_PATHS',
'WF_HOST_CHECK_LOOPBACK',
'WF_HOST_CHECK_BLOCKED_PORTS',
'WF_DOCKER_CONFIG',
'TEMPORAL_NAMESPACE_PREFIX',
'TEMPORAL_TLS_CA',

View File

@ -8,6 +8,7 @@ import {
RuntimeError,
safeIdentifier,
type CommandExecutor,
type HostCheckSandbox,
type PortableCheckpoint,
type ProviderAdapter,
} from '@wishforge/runtime';
@ -89,6 +90,8 @@ export interface WorkerConfig {
claudeAuthHome?: string;
/** Operator opt-in to Bash in Claude Code's own sandbox (see claudeCommandSandbox in providers.ts). */
claudeCommandSandbox?: { readable: string[]; env: Record<string, string> };
/** Operator opt-in to a macOS sandbox for host checks and installations (see host-checks.ts). */
hostCheckSandbox?: HostCheckSandbox;
dockerConfig?: string;
/** Docker CLI context of the image builder; operator configuration, never project settings. */
dockerContext?: string;

View File

@ -33,6 +33,7 @@ import {
import { ControlPlaneError, type ControlPlaneClient } from './client.js';
import { LeaseKeeper, activityContext } from './lease.js';
import { installPlan } from './project-contract.js';
import { hostCheckExecutor } from './host-checks.js';
import { providerRegistry } from './providers.js';
/** Where a run continues when it already has a checkpoint or a release; no executor lease is needed for that. */
@ -96,6 +97,7 @@ async function install(
execute: CommandExecutor,
commands: string[][],
cwd: string,
sandboxRoot: string,
home: string,
proxy?: string,
): Promise<{ argv: string[]; result: CommandResult } | undefined> {
@ -106,8 +108,10 @@ async function install(
executable: argv[0]!,
args: argv.slice(1),
cwd,
sandboxRoot,
env: { PATH: process.env.PATH, HOME: home, LANG: 'C.UTF-8', CI: 'true', ...proxyEnvironment(proxy) },
timeoutMs: 15 * 60_000,
network: true,
});
} catch (error) {
if (!(error instanceof RuntimeError) || !['TIMEOUT', 'OUTPUT_LIMIT'].includes(error.code)) throw error;
@ -272,9 +276,10 @@ export async function executeRun(config: WorkerConfig, input: RunInput): Promise
// Conflicted manifests cannot be installed; the agent resolves them first and the verifier installs.
if (plan.commands.length && !conflicts.length) {
const failed = await install(
execute,
hostCheckExecutor(config, execute),
plan.commands,
path.join(workspace, plan.directory),
workspace,
isolatedHome,
config.installProxy,
);

View File

@ -0,0 +1,53 @@
import path from 'node:path';
import { seatbeltCheckExecutor, type CommandExecutor, type HostCheckSandbox } from '@wishforge/runtime';
import type { WorkerConfig } from './common.js';
const flag = (env: NodeJS.ProcessEnv, name: string) => {
const value = env[name];
if (value !== undefined && value !== 'true' && value !== 'false') throw new Error(`${name} must be true or false`);
return value === 'true';
};
const paths = (env: NodeJS.ProcessEnv, name: string) => {
const list = (env[name] ?? '').split(path.delimiter).filter(Boolean);
if (list.some(location => !path.isAbsolute(location) || /["\\\u0000-\u001f\u007f]/.test(location)))
throw new Error(`${name} must list absolute paths`);
return list;
};
/**
* Operator opt-in to sandboxing protected checks and dependency installation in the development
* `host` mode on macOS: WF_HOST_CHECK_SANDBOX=true, plus paths in the home directory the commands
* may read (WF_HOST_CHECK_READ_PATHS) or also write (WF_HOST_CHECK_WRITE_PATHS), separated like
* PATH, and WF_HOST_CHECK_LOOPBACK=true for test suites that need local ports. The ports of the
* platform's services stay blocked (WF_HOST_CHECK_BLOCKED_PORTS, which the local start fills in),
* those of the control plane and Temporal in any case.
*/
export function hostCheckSandbox(env: NodeJS.ProcessEnv): HostCheckSandbox | undefined {
if (!flag(env, 'WF_HOST_CHECK_SANDBOX')) return undefined;
const loopback = flag(env, 'WF_HOST_CHECK_LOOPBACK');
const readable = paths(env, 'WF_HOST_CHECK_READ_PATHS');
const writable = paths(env, 'WF_HOST_CHECK_WRITE_PATHS');
const blockedPorts = new Set<number>();
for (const port of (env.WF_HOST_CHECK_BLOCKED_PORTS ?? '').split(',').filter(value => value.trim())) {
const value = Number(port);
if (!Number.isInteger(value) || value < 1 || value > 65535)
throw new Error('WF_HOST_CHECK_BLOCKED_PORTS must list TCP ports separated by commas');
blockedPorts.add(value);
}
for (const address of [
env.WF_CONTROL_PLANE_URL ?? 'http://127.0.0.1:4000',
`grpc://${env.TEMPORAL_ADDRESS ?? '127.0.0.1:7233'}`,
]) {
const url = new URL(address);
const port = Number(url.port || (url.protocol === 'https:' ? 443 : 80));
blockedPorts.add(port);
}
return { readable, writable, loopback, blockedPorts: [...blockedPorts].sort((a, b) => a - b) };
}
/** The executor for a host's checks and installations: sandboxed when the operator opted in. */
export function hostCheckExecutor(config: WorkerConfig, execute: CommandExecutor): CommandExecutor {
return config.sandboxMode === 'host' && config.hostCheckSandbox
? seatbeltCheckExecutor(config.hostCheckSandbox, execute)
: execute;
}

View File

@ -5,6 +5,7 @@ import { isProduction, requireDeploymentMode } from '@wishforge/contracts';
import { createActivities, type SandboxMode } from './activities.js';
import { temporalTls, workerNamespaces } from './temporal.js';
import { claudeCommandSandbox, claudeLocalAuthHome, providerBinaries } from './providers.js';
import { hostCheckSandbox } from './host-checks.js';
async function main() {
requireDeploymentMode(process.env);
@ -70,6 +71,7 @@ async function main() {
providerBinaries: providerBinaries(process.env),
claudeAuthHome,
claudeCommandSandbox: claudeCommands,
hostCheckSandbox: hostCheckSandbox(process.env),
dockerConfig: process.env.WF_DOCKER_CONFIG,
dockerContext: process.env.WF_DOCKER_CONTEXT,
dockerHost: process.env.WF_DOCKER_HOST,

View File

@ -39,6 +39,7 @@ import { leaseRun } from './executor.js';
import type { LeaseKeeper } from './lease.js';
import { advanceGitHubDelivery, loadGitHubDeliveryState, restoreMergedGitHubSource } from './github-delivery.js';
import { projectContract } from './project-contract.js';
import { hostCheckExecutor } from './host-checks.js';
/** A commit the verifier checked in a workspace of its own, with the evidence it recorded. */
interface Verified {
@ -67,6 +68,8 @@ class Session {
readonly leaseToken: number;
readonly targetRef: string;
readonly isolation: Isolation;
/** Set when development checks run under the operator's macOS sandbox. */
readonly checkSandbox?: 'seatbelt';
readonly execute: CommandExecutor;
readonly checks: CommandExecutor;
readonly git: GitWorkspace;
@ -86,9 +89,13 @@ class Session {
this.targetRef = `refs/heads/${context.project.targetBranch}`;
this.isolation = config.sandboxMode === 'kata' ? 'sandbox' : 'host';
this.execute = command => (config.execute ?? executeCommand)({ ...command, signal: keeper.signal });
// Production checks run under bubblewrap inside the Kata sandbox; development runs them directly.
// Production checks run under bubblewrap inside the Kata sandbox; development runs them directly,
// or under Seatbelt on macOS when the operator opted in.
this.checks =
this.isolation === 'sandbox' ? isolatedVerifierExecutor(config.verifierBinary, this.execute) : this.execute;
this.isolation === 'sandbox'
? isolatedVerifierExecutor(config.verifierBinary, this.execute)
: hostCheckExecutor(config, this.execute);
if (config.sandboxMode === 'host' && config.hostCheckSandbox) this.checkSandbox = 'seatbelt';
this.git = gitFor(config, context, this.execute);
}
get run(): Run {
@ -176,6 +183,7 @@ class Session {
const evidence = await this.evidence({
type: 'verification',
...report,
...(this.checkSandbox ? { checkSandbox: this.checkSandbox } : {}),
...(changedManifests.length ? { changedManifests } : {}),
});
return {

View File

@ -336,7 +336,7 @@ MCP 工具:`list_projects`、`submit_wish`、`get_run`、`get_run_events`、`a
| 模式 | 使用场景 | 行为 |
|---|---|---|
| `host` | 本地开发(`WF_DEPLOYMENT_MODE=development`) | `local` provisioner 在宿主机为每个代次启动 executor 与 verifier 进程,各自使用独立目录与独立令牌;命令直接以当前用户执行。本地 k3s([local-k3s.md](local-k3s.md))也使用这个模式,executor 与 verifier 运行在普通 runc Pod 中。证据标记 `isolation: host`。 |
| `host` | 本地开发(`WF_DEPLOYMENT_MODE=development`) | `local` provisioner 在宿主机为每个代次启动 executor 与 verifier 进程,各自使用独立目录与独立令牌;命令直接以当前用户执行,macOS 上运维可以让受保护检查与依赖安装在 Seatbelt 中运行(见第 11 节)。本地 k3s([local-k3s.md](local-k3s.md))也使用这个模式,executor 与 verifier 运行在普通 runc Pod 中。证据标记 `isolation: host`。 |
| `kata` | 生产 | `kubernetes` provisioner 为每个代次创建两个 Kata Job 与独立 PVC,无 Kubernetes API 令牌、无宿主机挂载;网络策略只放行 DNS、API(模型代理与检查点存储都经 API)、Temporal 与平台 Git;验证命令再经 bubblewrap 隔离。依赖安装经运维提供的出站代理访问包仓库(验证命令没有网络),镜像在运维提供的远程构建器上构建,只有 verifier 能连到它。 |
依赖安装(项目的安装命令或 `wishforge.yaml` 的 `install`)在执行器开始前与验证命令执行前各运行一次,允许访问网络(生产经出口代理);Agent 本身默认不开放外网。
@ -369,7 +369,7 @@ Claude Code 以受限模式运行(`--restricted`,权限模式 `dontAsk`)
- Temporal 连接使用客户端证书(生产必需),每个租户可以使用自己的命名空间(生产中多租户必需)。命名空间之间的授权依赖 Temporal 服务端按证书配置(见 operations.md 第 13 节)。同一租户的沙箱之间没有按任务队列的授权。
- OCT 协作房间保存在内存中,服务重启后需要重新创建房间;已保存的文件不受影响。
- Theia 后端与终端共用同一个 UID,同一工作区内的协作者之间没有强隔离;工作区只应授予可信成员编辑权限。
- `host` 沙箱模式没有任何隔离,只能用于本机开发,并且只能运行你信任的代码与模型。
- `host` 沙箱模式没有任何隔离,只能用于本机开发,并且只能运行你信任的代码与模型。macOS 上运维可以用 `WF_HOST_CHECK_SANDBOX=true` 让受保护检查与依赖安装在 Seatbelt 中运行(主目录内容不可读、只能写工作区与放行的缓存、检查没有网络、平台服务端口始终封锁),证据标记 `checkSandbox: seatbelt`;它不隔离执行器与验证器进程本身,证据仍是 `isolation: host`,生产不接受。上线前在目标机器上运行 `scripts/check-host-check-sandbox.ts` 验收。
- macOS 上,Claude Code 命令沙箱的 Seatbelt 配置固定允许访问系统安全服务(`com.apple.SecurityServer`),所以沙箱中的命令可以查询登录钥匙串:信任 `security` 工具的条目可以被读取,其他条目会弹出授权提示。需要更强的隔离时,用单独的 macOS 用户运行本机服务,或者不打开 `WF_CLAUDE_COMMAND_SANDBOX`。
## 12. 需求验证矩阵

View File

@ -87,6 +87,35 @@ LaunchAgent 随当前用户登录运行,退出登录后停止;电脑关机
3. 先 `stop` 再 `start` 后台服务。之后运行的时间线在「Executor started」处注明命令是否在沙箱中运行;沙箱不能启动时自动退回只有文件工具。删掉 `WF_CLAUDE_COMMAND_SANDBOX` 并重启即可关闭。
## 让受保护检查在沙箱中运行(可选)
本机服务以 `host` 模式运行:受保护的安装与检查默认直接以当前用户执行,而它们运行的是 Agent 写或改过的代码,比如一个测试就能读取主目录里的密钥或访问网络。打开下面的设置后,检查与依赖安装改在 macOS 自带的沙箱(Seatbelt)中运行:
- 读不到主目录里的文件内容,只能读工作区和下面放行的路径(文件是否存在等元数据仍然可见,路径才能正常解析);
- 只能写工作区、下面放行的缓存,以及每条命令独享并在结束后删除的临时目录,任何时候都不能写工作区的 `.git`;
- 只有依赖安装能联网;检查没有网络,打开 `WF_HOST_CHECK_LOOPBACK` 后可以使用本机回环端口;
- 平台自己的服务端口(API、Temporal 及其网页、数据库、对象存储、provisioner、Git 服务、IDE、网页控制台)在任何情况下都连不上,启动时按 `.env.local` 中的地址自动列出,也可以用 `WF_HOST_CHECK_BLOCKED_PORTS` 补充;
- 检查时钥匙串服务不应答。
1. 确认没有执行中的愿望,运行检查。它不调用模型,也不部署:先逐项确认上面这些限制在这台机器上生效,再在沙箱里对最近一次构建的 droboticflow 源码运行 POC 的安装和全部检查:
```sh
node --env-file=.env.local --import tsx scripts/check-host-check-sandbox.ts
```
2. 全部通过后,在 `.env.local` 中加入下面的设置(`<仓库>` 换成本仓库的绝对路径)。droboticflow 的 Go 测试会在本机起临时服务,并写入 `/tmp/workspace`:
```sh
WF_HOST_CHECK_SANDBOX=true
WF_HOST_CHECK_READ_PATHS=<仓库>/scripts/poc-project-check.mjs
WF_HOST_CHECK_WRITE_PATHS=<仓库>/.wishforge/poc-check-cache:/tmp/workspace
WF_HOST_CHECK_LOOPBACK=true
```
3. 先 `stop` 再 `start` 后台服务。之后的验证证据带有 `checkSandbox: seatbelt`;沙箱不能启动时运行停在「基础设施」等待,不会当作检查失败。删掉 `WF_HOST_CHECK_SANDBOX` 并重启即可关闭。
它只隔离检查与安装:Agent 由各自的执行器沙箱约束,执行器和验证器进程本身仍以当前用户运行。打开回环后,检查也能连到本机上其他未封锁的端口。
本机实验套餐由 operator CLI 管理:`node --import tsx scripts/configure-local-poc-plan.ts 20000000` 设置每月 2000 万计量 Token 和单并发;这不购买模型额度或扣款。单次愿望默认 100 万 Token,包含 CLI 缓存读取。CLI 最终计量在回合结束时上报;被暂停、取消或超时打断的会话没有总量,改按它已收到的每次模型响应估算计入,时间线标为估算。超预算会阻止后续执行并保留检查点,不能视为供应商逐请求实时硬上限。
本机到 POC 集群 API 的连接不稳定时(例如经 Wi‑Fi 或企业网络客户端),构建与发布会自动重试没有得到应答的请求:读取最多试 6 次,间隔 2 至 30 秒;上传源码失败会重传,解压可安全重复;`rollout status` 断开后在发布时限内接着观察;发布后的公网检查(健康、就绪、首页、API 的构建提交和受保护业务检查)也各试 6 次,间隔 2 至 10 秒,因为滚动更新刚结束时公网入口可能还连到正在退出的旧 Pod。每次重试都有记录:构建时写进运行时间线,发布时写进 `runner.log`。连接中断超过这些重试时,运行仍停在「基础设施」等待,网络恢复后在网页上继续即可。可以用 `kubectl --kubeconfig <POC kubeconfig> --request-timeout=5s get --raw /readyz` 反复探测这条链路。

View File

@ -236,6 +236,8 @@ export interface Evidence extends Entity {
changedManifests?: string[];
/** How the checks were isolated; production only accepts sandboxed verification. */
isolation?: 'host' | 'sandbox';
/** Development checks the operator sandboxed on macOS; still `host` isolation, never production evidence. */
checkSandbox?: 'seatbelt';
/** Integration evidence: the branch that received the commit, or the conflict that prevented it. */
targetRef?: string;
targetCommit?: string;
@ -500,6 +502,7 @@ export const evidenceInput = z
installFailure: commandEvidence.optional(),
changedManifests: z.array(text.max(1024)).max(200).optional(),
isolation: z.enum(['host', 'sandbox']).optional(),
checkSandbox: z.literal('seatbelt').optional(),
targetRef: z
.string()
.regex(/^refs\/heads\/[A-Za-z0-9._/-]{1,200}$/)

View File

@ -12,6 +12,11 @@ export interface Command {
signal?: AbortSignal;
/** Only honoured by sandboxed executors: dependency installation may reach the network, checks may not. */
network?: boolean;
/**
* Only honoured by the macOS check sandbox: the directory the command may read and write, when
* it runs in a subdirectory of it (an installation in a package of the repository).
*/
sandboxRoot?: string;
}
export interface CommandResult {
exitCode: number;

View File

@ -1,4 +1,6 @@
import { access } from 'node:fs/promises';
import { access, mkdtemp, realpath, rm } from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { executeCommand, type CommandExecutor } from './process.js';
import { RuntimeError } from './types.js';
@ -118,3 +120,154 @@ export function isolatedVerifierExecutor(
});
};
}
/**
* What protected checks in a development `host` sandbox may touch besides system files, when the
* operator sandboxes them on macOS (see {@link seatbeltCheckExecutor}).
*/
export interface HostCheckSandbox {
/** Paths inside the home directory the commands may read besides their own directory: a protected check script, toolchain caches. */
readable: string[];
/** Paths the commands may also write: caches shared between runs. */
writable: string[];
/** Lets checks use this machine's loopback ports, for test suites that start local servers. */
loopback?: boolean;
/** Loopback ports checks may never reach, even with `loopback`: the platform's own services (API, Temporal, database). */
blockedPorts?: number[];
}
/** A path as Seatbelt profiles quote it; anything that could break out of the string is refused. */
const sandboxPath = (location: string) => {
if (!path.isAbsolute(location) || /["\\\u0000-\u001f\u007f]/.test(location))
throw new RuntimeError('INVALID_SANDBOX_PATH', `Sandbox paths must be absolute plain paths: ${location}`);
return `"${location}"`;
};
/**
* The Seatbelt profile of one host check. Everything the user may do stays allowed except: reading
* file contents in the home directory outside the command's root and the operator's paths;
* writing anywhere but there and a private temporary directory, never into a `.git` directory;
* any network unless the command installs dependencies (or loopback, when allowed), and the
* blocked ports even then; and, for checks, the services through which the keychain answers.
* Later rules take precedence.
*/
export function seatbeltProfile(input: {
home: string;
root: string;
temporary: string;
readable: string[];
writable: string[];
network: boolean;
loopback: boolean;
blockedPorts?: number[];
}): string {
const within = (locations: string[]) => locations.map(location => `(subpath ${sandboxPath(location)})`).join(' ');
return [
'(version 1)',
'(allow default)',
// Metadata stays readable so that paths through the home directory still resolve.
`(deny file-read-data (subpath ${sandboxPath(input.home)}))`,
`(allow file-read-data ${within([input.root, input.temporary, ...input.readable, ...input.writable])})`,
'(deny file-write*)',
`(allow file-write* ${within([input.root, input.temporary, ...input.writable])} (literal "/dev/null") (literal "/dev/zero") (literal "/dev/dtracehelper") (regex #"^/dev/tty") (regex #"^/dev/fd/"))`,
// Git configuration and hooks that a command wrote would run in the trusted Git commands after it.
`(deny file-write* (subpath ${sandboxPath(path.join(input.root, '.git'))}))`,
'(deny network*)',
...(input.network
? ['(allow network*)']
: input.loopback
? ['(allow network* (local ip "localhost:*"))', '(allow network* (remote ip "localhost:*"))']
: []),
// On any address: a service may listen on the machine's LAN address too.
...(input.network || input.loopback
? (input.blockedPorts ?? []).map(port => {
if (!Number.isInteger(port) || port < 1 || port > 65535)
throw new RuntimeError('INVALID_SANDBOX_PORT', `Blocked ports must be TCP ports: ${port}`);
return `(deny network-outbound (remote ip "*:${port}"))`;
})
: []),
...(input.network
? []
: [
'(deny mach-lookup (global-name "com.apple.SecurityServer") (global-name "com.apple.securityd.xpc") (global-name "com.apple.security.agent"))',
]),
].join('\n');
}
/**
* Development `host` checks on macOS, run under Seatbelt with {@link seatbeltProfile}: each command
* gets a private temporary directory, removed afterwards, which also becomes its HOME and TMPDIR
* where those lie outside what it may write (an executor's home holds its credentials). A machine where
* sandbox-exec cannot start (another sandbox around the worker, say) is an infrastructure fault,
* never a failing check.
*/
export function seatbeltCheckExecutor(
sandbox: HostCheckSandbox,
execute: CommandExecutor = executeCommand,
binary = '/usr/bin/sandbox-exec',
): CommandExecutor {
let ready: Promise<{ home: string; readable: string[]; writable: string[] }> | undefined;
// Seatbelt matches resolved paths (/tmp is /private/tmp): resolve the nearest existing ancestor.
const real = async (location: string): Promise<string> => {
const resolved = path.resolve(location);
try {
return await realpath(resolved);
} catch {
const parent = path.dirname(resolved);
return parent === resolved ? resolved : path.join(await real(parent), path.basename(resolved));
}
};
const prepare = async () => {
if (process.platform !== 'darwin')
throw new RuntimeError('VERIFIER_ISOLATION_UNAVAILABLE', 'Sandboxed host checks need macOS sandbox-exec');
const probe = await execute({
executable: binary,
args: ['-p', '(version 1) (allow default)', '/usr/bin/true'],
env: { PATH: '/usr/bin:/bin' },
timeoutMs: 30_000,
});
if (probe.exitCode !== 0)
throw new RuntimeError(
'VERIFIER_ISOLATION_UNAVAILABLE',
`sandbox-exec cannot start here: ${probe.stderr.trim().slice(0, 300)}`,
);
return {
home: await real(os.homedir()),
readable: await Promise.all(sandbox.readable.map(real)),
writable: await Promise.all(sandbox.writable.map(real)),
};
};
return async command => {
if (!command.cwd) throw new RuntimeError('INVALID_VERIFIER_COMMAND', 'Sandboxed commands need a working directory');
const paths = await (ready ??= prepare());
const root = await real(command.sandboxRoot ?? command.cwd);
const temporary = await real(await mkdtemp(path.join(os.tmpdir(), 'wishforge-check-')));
try {
const profile = seatbeltProfile({
...paths,
root,
temporary,
network: command.network === true,
loopback: sandbox.loopback === true,
blockedPorts: sandbox.blockedPorts,
});
const writable = async (location?: string) => {
if (!location) return false;
const resolved = await real(location);
return [root, ...paths.writable].some(base => resolved === base || resolved.startsWith(base + path.sep));
};
return await execute({
...command,
executable: binary,
args: ['-p', profile, command.executable, ...command.args],
env: {
...command.env,
HOME: (await writable(command.env?.HOME)) ? command.env!.HOME : temporary,
TMPDIR: (await writable(command.env?.TMPDIR)) ? command.env!.TMPDIR : temporary,
},
});
} finally {
await rm(temporary, { recursive: true, force: true });
}
};
}

View File

@ -138,6 +138,7 @@ export async function verifyWorkspace(
executable: argv[0]!,
args: argv.slice(1),
cwd: path.join(tree, installDirectory),
sandboxRoot: tree,
env: { ...env, ...proxyEnvironment(input.installProxy) },
timeoutMs: input.installTimeoutMs ?? 15 * 60_000,
network: true,
@ -157,6 +158,7 @@ export async function verifyWorkspace(
executable: argv[0]!,
args: argv.slice(1),
cwd: tree,
sandboxRoot: tree,
env,
timeoutMs: input.timeoutMs ?? 10 * 60_000,
network: false,

View File

@ -0,0 +1,170 @@
import { spawnSync } from 'node:child_process';
import { existsSync } from 'node:fs';
import { mkdir, mkdtemp, readFile, realpath, rm, writeFile } from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { afterEach, describe, expect, it } from 'vitest';
import { executeCommand, seatbeltCheckExecutor, seatbeltProfile, type Command } from '../src/index.js';
const dirs: string[] = [];
afterEach(async () => {
await Promise.all(dirs.splice(0).map(dir => rm(dir, { recursive: true, force: true })));
});
const temporary = async (prefix: string) => {
const dir = await realpath(await mkdtemp(path.join(os.tmpdir(), prefix)));
dirs.push(dir);
return dir;
};
const paths = {
home: '/Users/me',
root: '/Users/me/runs/tree',
temporary: '/private/var/folders/x/T/wishforge-check-1',
readable: ['/Users/me/check.mjs'],
writable: ['/Users/me/cache'],
};
describe('the macOS sandbox for host checks', () => {
it('denies the home directory, other writes, the network and the keychain to checks', () => {
const check = seatbeltProfile({ ...paths, network: false, loopback: false });
expect(check).toContain('(deny file-read-data (subpath "/Users/me"))');
expect(check).toContain(
'(allow file-read-data (subpath "/Users/me/runs/tree") (subpath "/private/var/folders/x/T/wishforge-check-1") (subpath "/Users/me/check.mjs") (subpath "/Users/me/cache"))',
);
expect(check).toMatch(
/\(allow file-write\* \(subpath "\/Users\/me\/runs\/tree"\) .*\(subpath "\/Users\/me\/cache"\)/,
);
expect(check).not.toMatch(/\(allow file-write\* [^\n]*check\.mjs/);
expect(check).toContain('(deny file-write* (subpath "/Users/me/runs/tree/.git"))');
expect(check).toContain('(deny network*)');
expect(check).not.toContain('(allow network');
expect(check).toContain('com.apple.SecurityServer');
// Later rules win: the allowances follow what they relax.
expect(check.indexOf('(deny file-write*)')).toBeLessThan(check.indexOf('(allow file-write*'));
});
it('lets installations and, by opt-in, loopback through, but never to the blocked ports', () => {
const install = seatbeltProfile({ ...paths, network: true, loopback: false, blockedPorts: [4000, 7233] });
expect(install).toContain('(allow network*)');
expect(install).toContain('(deny network-outbound (remote ip "*:4000"))');
expect(install).not.toContain('SecurityServer');
const loopback = seatbeltProfile({ ...paths, network: false, loopback: true, blockedPorts: [7233] });
expect(loopback).toContain('(allow network* (remote ip "localhost:*"))');
expect(loopback).toContain('(deny network-outbound (remote ip "*:7233"))');
expect(loopback.indexOf('localhost:*')).toBeLessThan(loopback.indexOf('*:7233'));
expect(seatbeltProfile({ ...paths, network: false, loopback: false, blockedPorts: [7233] })).not.toContain('7233');
});
it('refuses paths and ports that could break out of the profile', () => {
expect(() =>
seatbeltProfile({ ...paths, readable: ['/Users/me/"(allow default)'], network: false, loopback: false }),
).toThrow(/plain paths/);
expect(() => seatbeltProfile({ ...paths, readable: ['relative'], network: false, loopback: false })).toThrow(
/absolute/,
);
expect(() => seatbeltProfile({ ...paths, network: true, loopback: false, blockedPorts: [70000] })).toThrow(
/TCP ports/,
);
});
it('wraps each command with a temporary directory of its own, and a private home unless it may write its own', async () => {
const platform = Object.getOwnPropertyDescriptor(process, 'platform')!;
Object.defineProperty(process, 'platform', { value: 'darwin' });
try {
const root = await temporary('wishforge-seatbelt-root-');
const seen: Command[] = [];
const sandboxed = seatbeltCheckExecutor({ readable: [], writable: [] }, async command => {
seen.push(command);
return { exitCode: 0, stdout: '', stderr: '' };
});
const env = { PATH: '/usr/bin', HOME: path.join(os.tmpdir(), 'provider-home-with-credentials') };
await sandboxed({
executable: 'npm',
args: ['ci'],
cwd: path.join(root, 'web'),
sandboxRoot: root,
env,
network: true,
});
await sandboxed({ executable: 'node', args: ['--test'], cwd: root, env: { PATH: '/usr/bin', HOME: root } });
const [probe, install, check] = seen;
expect(probe).toMatchObject({
executable: '/usr/bin/sandbox-exec',
args: ['-p', '(version 1) (allow default)', '/usr/bin/true'],
});
expect(seen).toHaveLength(3);
expect(install!.executable).toBe('/usr/bin/sandbox-exec');
expect(install!.args.slice(2)).toEqual(['npm', 'ci']);
expect(install!.args[1]).toContain(`(subpath "${root}")`);
expect(install!.args[1]).toContain('(allow network*)');
expect(install!.env!.HOME).toBe(install!.env!.TMPDIR);
expect(install!.env!.TMPDIR).toContain('wishforge-check-');
expect(check!.env!.HOME).toBe(root);
expect(check!.args[1]).not.toContain('(allow network*)');
expect(existsSync(install!.env!.TMPDIR!) || existsSync(check!.env!.TMPDIR!)).toBe(false);
} finally {
Object.defineProperty(process, 'platform', platform);
}
});
it('reports a sandbox that cannot start as an infrastructure fault, not a failing check', async () => {
const platform = Object.getOwnPropertyDescriptor(process, 'platform')!;
Object.defineProperty(process, 'platform', { value: 'darwin' });
try {
const sandboxed = seatbeltCheckExecutor({ readable: [], writable: [] }, async () => ({
exitCode: 71,
stdout: '',
stderr: 'sandbox-exec: sandbox_apply: Operation not permitted',
}));
await expect(sandboxed({ executable: 'true', args: [], cwd: os.tmpdir() })).rejects.toMatchObject({
code: 'VERIFIER_ISOLATION_UNAVAILABLE',
});
} finally {
Object.defineProperty(process, 'platform', platform);
}
});
// Only where sandbox-exec can start: not inside another sandbox, which may itself run these tests.
const seatbelt =
process.platform === 'darwin' &&
spawnSync('/usr/bin/sandbox-exec', ['-p', '(version 1) (allow default)', '/usr/bin/true']).status === 0;
it.skipIf(!seatbelt)('is enforced as written on this Mac', async () => {
const home = await temporary('wishforge-seatbelt-home-');
const root = path.join(home, 'root');
await mkdir(path.join(root, '.git'), { recursive: true });
await writeFile(path.join(home, 'secret.txt'), 'secret');
const scratch = path.join(home, 'scratch');
await mkdir(scratch);
const profile = seatbeltProfile({
home,
root,
temporary: scratch,
readable: [],
writable: [],
network: false,
loopback: false,
});
const run = (script: string) =>
executeCommand({
executable: '/usr/bin/sandbox-exec',
args: ['-p', profile, process.execPath, '-e', script],
cwd: root,
env: { PATH: '/usr/bin:/bin' },
timeoutMs: 30_000,
});
const fs = "const fs = require('fs');";
expect((await run(`${fs} fs.readFileSync(${JSON.stringify(path.join(home, 'secret.txt'))})`)).exitCode).not.toBe(0);
expect(
(
await run(
`${fs} fs.writeFileSync('own.txt', 'x'); fs.writeFileSync(${JSON.stringify(path.join(scratch, 'tmp'))}, 'x')`,
)
).exitCode,
).toBe(0);
expect((await run(`${fs} fs.writeFileSync(${JSON.stringify(path.join(home, 'out.txt'))}, 'x')`)).exitCode).not.toBe(
0,
);
expect((await run(`${fs} fs.writeFileSync('.git/config', '[core]\\n\\tfsmonitor = evil')`)).exitCode).not.toBe(0);
const network = await run(
"require('net').connect(80, '1.1.1.1').on('connect', () => process.exit(0)).on('error', () => process.exit(3))",
);
expect(network.exitCode).toBe(3);
expect(await readFile(path.join(root, 'own.txt'), 'utf8')).toBe('x');
expect(existsSync(path.join(home, 'out.txt'))).toBe(false);
});
});

View File

@ -0,0 +1,236 @@
/**
* Checks the macOS sandbox for protected host checks (WF_HOST_CHECK_SANDBOX) on this machine,
* before it is switched on: fixed probes run sandboxed and their effects are checked here, not
* taken from their own account; then the POC's protected installation and checks run sandboxed on
* a droboticflow source archive. No model is called and nothing is deployed:
*
* node --env-file=.env.local --import tsx scripts/check-host-check-sandbox.ts [source.tar.gz]
*
* Without an archive, the source of the newest POC build is used.
*/
import { execFileSync } from 'node:child_process';
import { randomBytes } from 'node:crypto';
import { access, mkdir, readdir, readFile, rm, stat, writeFile } from 'node:fs/promises';
import http from 'node:http';
import type { AddressInfo } from 'node:net';
import os from 'node:os';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { hostCheckSandbox } from '../apps/worker/src/host-checks.js';
import { platformPorts } from './local-services.mjs';
import {
executeCommand,
outputTail,
seatbeltCheckExecutor,
type CommandResult,
} from '../packages/runtime/src/index.js';
const repository = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const pocCheck = path.join(repository, 'scripts/poc-project-check.mjs');
const pocCache = path.join(repository, '.wishforge/poc-check-cache');
// As the local start configures the workers: the platform's ports from its addresses.
const configured = hostCheckSandbox({
...process.env,
WF_HOST_CHECK_SANDBOX: 'true',
WF_HOST_CHECK_BLOCKED_PORTS: platformPorts(process.env).join(','),
})!;
// Without the operator's settings, check what the POC needs, and say so: its Go tests start local servers.
const sandbox = {
...configured,
readable: configured.readable.length ? configured.readable : [pocCheck],
// droboticflow's Go tests write under /tmp/workspace.
writable: configured.writable.length ? configured.writable : [pocCache, '/tmp/workspace'],
loopback: process.env.WF_HOST_CHECK_LOOPBACK === undefined ? true : configured.loopback,
};
console.log(
`Checking WF_HOST_CHECK_READ_PATHS=${sandbox.readable.join(path.delimiter)} WF_HOST_CHECK_WRITE_PATHS=${sandbox.writable.join(path.delimiter)} WF_HOST_CHECK_LOOPBACK=${sandbox.loopback}, blocked ports ${sandbox.blockedPorts?.join(',')}\n`,
);
const secret = () => randomBytes(12).toString('hex');
const canary = { file: `file-${secret()}`, service: `service-${secret()}` };
// Like a run directory: in the home directory, outside the WishForge source.
const root = path.join(os.homedir(), '.local/share/wishforge/sandbox-check', `host-${secret()}`);
const tree = path.join(root, 'tree');
const outside = path.join(root, 'outside');
await mkdir(path.join(tree, '.git'), { recursive: true, mode: 0o700 });
await mkdir(outside, { recursive: true, mode: 0o700 });
await writeFile(path.join(outside, 'canary.txt'), canary.file);
await writeFile(path.join(tree, '.git/config'), '[core]\n\tbare = false\n');
await writeFile(path.join(tree, 'inside.txt'), 'inside');
await writeFile(
path.join(tree, 'sum.test.mjs'),
"import test from 'node:test';\nimport assert from 'node:assert/strict';\ntest('adds', () => assert.equal(2 + 3, 5));\n",
);
const server = http.createServer((_request, response) => response.end(canary.service));
await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve));
const port = (server.address() as AddressInfo).port;
const execute = seatbeltCheckExecutor(sandbox, executeCommand);
const env = { PATH: '/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin', LANG: 'C.UTF-8', CI: 'true', HOME: tree };
const node = (script: string, network = false) =>
execute({ executable: process.execPath, args: ['-e', script], cwd: tree, env, timeoutMs: 60_000, network });
const text = (result: CommandResult) => `${result.stdout}\n${result.stderr}`;
const exists = (location: string) =>
access(location).then(
() => true,
() => false,
);
type Verdict = 'pass' | 'FAIL' | 'warn';
const results: Array<[string, Verdict, string]> = [];
const record = (label: string, verdict: Verdict, detail: string) => results.push([label, verdict, detail]);
const first = (result: CommandResult) => text(result).trim().split('\n')[0]!.slice(0, 120);
try {
const tests = await execute({
executable: process.execPath,
args: ['--test', 'sum.test.mjs'],
cwd: tree,
env,
timeoutMs: 60_000,
});
record('Tests run inside the sandbox', tests.exitCode === 0 ? 'pass' : 'FAIL', first(tests));
const read = await node(
`process.stdout.write(require('fs').readFileSync(${JSON.stringify(path.join(outside, 'canary.txt'))}, 'utf8'))`,
);
record(
'Files elsewhere in the home directory stay unreadable',
text(read).includes(canary.file) ? 'FAIL' : 'pass',
first(read),
);
const own = await node(`process.stdout.write(require('fs').readFileSync('inside.txt', 'utf8'))`);
record('Its own files are readable', own.stdout === 'inside' ? 'pass' : 'FAIL', first(own));
const written = path.join(outside, 'written.txt');
const write = await node(`require('fs').writeFileSync(${JSON.stringify(written)}, 'x')`);
record('Writes outside its directory are refused', (await exists(written)) ? 'FAIL' : 'pass', first(write));
const git = await node(`require('fs').writeFileSync('.git/config', '[core]\\n\\tfsmonitor = evil\\n')`);
const gitConfig = await readFile(path.join(tree, '.git/config'), 'utf8');
record('Its .git directory stays unwritable', gitConfig.includes('evil') ? 'FAIL' : 'pass', first(git));
const temporary = await node(
`const fs = require('fs'); const file = require('path').join(require('os').tmpdir(), 'probe.txt'); fs.writeFileSync(file, 'x'); process.stdout.write(file)`,
);
record(
'A private temporary directory is writable, and removed afterwards',
temporary.exitCode === 0 && !(await exists(temporary.stdout)) ? 'pass' : 'FAIL',
temporary.stdout || first(temporary),
);
const network = await node(
`fetch('https://registry.npmjs.org/', { method: 'HEAD' }).then(r => console.log('status', r.status), e => console.log(String(e.cause ?? e)))`,
);
record('Checks have no network', /status \d+/.test(network.stdout) ? 'FAIL' : 'pass', first(network));
const installing = await node(
`fetch('https://registry.npmjs.org/', { method: 'HEAD' }).then(r => console.log('status', r.status), e => console.log(String(e.cause ?? e)))`,
true,
);
record(
'Installations reach the network',
/status \d+/.test(installing.stdout) ? 'pass' : 'warn',
`${first(installing)} (needs this machine's own network)`,
);
const local = await node(
`require('http').get('http://127.0.0.1:${port}/', r => { r.setEncoding('utf8'); r.on('data', d => process.stdout.write(d)); }).on('error', e => console.log(String(e)))`,
);
const reached = text(local).includes(canary.service);
record(
sandbox.loopback ? 'Local ports are reachable (WF_HOST_CHECK_LOOPBACK)' : 'Local ports stay unreachable',
reached === Boolean(sandbox.loopback) ? 'pass' : 'FAIL',
first(local),
);
const api = new URL(process.env.WF_API_URL ?? 'http://127.0.0.1:4000');
const platform = await node(
`require('http').get('http://127.0.0.1:${api.port || 80}/health', r => console.log('status', r.statusCode)).on('error', e => console.log(String(e)))`,
);
record(
`The platform's own ports stay unreachable (API ${api.port})`,
/status \d+/.test(platform.stdout) ? 'FAIL' : 'pass',
first(platform),
);
// A throwaway keychain with an item the security tool may read: outside the sandbox it answers.
const keychainFile = path.join(tree, 'check.keychain-db');
const item = ['find-generic-password', '-s', 'wishforge-sandbox-check', '-w', keychainFile];
const security = (args: string[]) =>
executeCommand({ executable: '/usr/bin/security', args, env: { PATH: '/usr/bin:/bin' }, timeoutMs: 30_000 });
const password = secret();
try {
await security(['create-keychain', '-p', password, keychainFile]);
await security([
'add-generic-password',
'-s',
'wishforge-sandbox-check',
'-a',
'check',
'-w',
canary.file,
'-T',
'/usr/bin/security',
keychainFile,
]);
const control = await security(item);
const sandboxed = await execute({ executable: '/usr/bin/security', args: item, cwd: tree, env, timeoutMs: 30_000 });
record(
'The keychain does not answer checks',
!control.stdout.includes(canary.file) ? 'warn' : sandboxed.stdout.includes(canary.file) ? 'FAIL' : 'pass',
control.stdout.includes(canary.file) ? first(sandboxed) : `the control read failed: ${first(control)}`,
);
} finally {
await security(['delete-keychain', keychainFile]);
}
for (const location of sandbox.readable) {
const readable = await node(
`require('fs').statSync(${JSON.stringify(location)}).isDirectory() ? require('fs').readdirSync(${JSON.stringify(location)}) : require('fs').readFileSync(${JSON.stringify(location)})`,
);
record(`Allowed to read ${location}`, readable.exitCode === 0 ? 'pass' : 'FAIL', first(readable));
}
} finally {
server.close();
}
// The POC's own protected installation and checks, on real droboticflow source.
let archive: string | undefined = process.argv[2];
if (!archive) {
const builds = path.join(os.homedir(), '.local/share/wishforge/poc/builds');
const candidates = await Promise.all(
(await readdir(builds).catch(() => [])).map(async name => {
const file = path.join(builds, name, 'source.tar.gz');
return stat(file).then(
info => ({ file, time: info.mtimeMs }),
() => undefined,
);
}),
);
archive = candidates.filter(Boolean).sort((a, b) => b!.time - a!.time)[0]?.file;
}
if (!archive) record('POC checks', 'warn', 'no droboticflow source archive found; pass one as an argument');
else {
const poc = path.join(root, 'poc');
await mkdir(poc, { recursive: true, mode: 0o700 });
execFileSync('tar', ['-xzf', archive, '-C', poc]);
for (const mode of ['install', 'verify'] as const) {
const started = Date.now();
const result = await execute({
executable: process.execPath,
args: [pocCheck, mode],
cwd: poc,
env,
timeoutMs: (mode === 'install' ? 15 : 30) * 60_000,
network: mode === 'install',
});
const seconds = Math.round((Date.now() - started) / 1000);
record(
`POC ${mode} runs sandboxed`,
result.exitCode === 0 ? 'pass' : 'FAIL',
result.exitCode === 0
? `${seconds} s`
: `exit ${result.exitCode} after ${seconds} s:\n${outputTail(result, 2000)}`,
);
if (result.exitCode !== 0) break;
}
}
for (const [label, verdict, detail] of results) console.log(`${verdict.padEnd(5)} ${label}: ${detail}`);
const passed = results.every(([, verdict]) => verdict !== 'FAIL');
if (passed) await rm(root, { recursive: true, force: true });
else console.log(`\nKept for inspection: ${root}`);
process.exitCode = passed ? 0 : 1;

View File

@ -1,5 +1,5 @@
import { resolve } from 'node:path';
import { environment, loadLocalEnvironment, startServices, webListener } from './local-services.mjs';
import { environment, loadLocalEnvironment, platformPorts, startServices, webListener } from './local-services.mjs';
loadLocalEnvironment();
const root = process.cwd();
@ -38,19 +38,26 @@ await startServices(
name: 'provisioner',
cwd: root,
args: ['apps/provisioner/main.mjs'],
env: environment([
/^WF_PROVISIONER_/,
/^WF_LOCAL_/,
/^WF_TENANT_ID$/,
/^WF_RESOURCE_GRANT_SECRET$/,
/^WF_CONTROL_PLANE_URL$/,
/^TEMPORAL_/,
/^WF_GIT_ALLOW_LOCAL_HTTP$/,
/^WF_CODEBUDDY_BIN$/,
/^WF_CLAUDE_BIN$/,
/^WF_CLAUDE_LOCAL_AUTH_HOME$/,
/^WF_CLAUDE_COMMAND_(SANDBOX|READ_PATHS|ENV)$/,
]),
env: environment(
[
/^WF_PROVISIONER_/,
/^WF_LOCAL_/,
/^WF_TENANT_ID$/,
/^WF_RESOURCE_GRANT_SECRET$/,
/^WF_CONTROL_PLANE_URL$/,
/^TEMPORAL_/,
/^WF_GIT_ALLOW_LOCAL_HTTP$/,
/^WF_CODEBUDDY_BIN$/,
/^WF_CLAUDE_BIN$/,
/^WF_CLAUDE_LOCAL_AUTH_HOME$/,
/^WF_CLAUDE_COMMAND_(SANDBOX|READ_PATHS|ENV)$/,
/^WF_HOST_CHECK_(SANDBOX|READ_PATHS|WRITE_PATHS|LOOPBACK)$/,
],
{
// Sandboxed checks never reach the platform's own services, whatever the operator listed.
WF_HOST_CHECK_BLOCKED_PORTS: platformPorts().join(','),
},
),
url: 'http://127.0.0.1:4450/healthz',
identity: 'wishforge-provisioner',
},

View File

@ -42,6 +42,37 @@ const inherited = () =>
.filter(key => process.env[key])
.map(key => [key, process.env[key]]),
);
/**
* The ports of this machine's WishForge services, from their configured addresses: sandboxed host
* checks may never reach them, even when they may use loopback (WF_HOST_CHECK_BLOCKED_PORTS).
* Temporal's web interface (8233) is included; it can stop workflows like Temporal itself.
*/
export function platformPorts(env = process.env) {
const ports = new Set([8233, Number(env.WF_WEB_PORT || 3000), Number(env.WF_GIT_PORT || 4504)]);
const addresses = [
env.WF_API_URL,
env.WF_CONTROL_PLANE_URL,
env.DATABASE_URL,
env.S3_ENDPOINT,
env.WF_PROVISIONER_URL,
env.WF_GIT_SERVICE_URL,
env.WF_IDE_GATEWAY_URL,
env.WF_IDE_PUBLIC_URL,
...(env.WF_IDE_ALLOWED_UPSTREAM_ORIGINS ?? '').split(','),
env.TEMPORAL_ADDRESS && `grpc://${env.TEMPORAL_ADDRESS}`,
];
for (const address of addresses.filter(Boolean)) {
try {
const url = new URL(address.trim());
const port = Number(url.port || { 'http:': 80, 'https:': 443 }[url.protocol]);
if (port) ports.add(port);
} catch {
// Not an address; nothing to block.
}
}
for (const port of (env.WF_HOST_CHECK_BLOCKED_PORTS ?? '').split(',')) if (port.trim()) ports.add(Number(port));
return [...ports].filter(port => Number.isInteger(port) && port > 0 && port < 65536).sort((a, b) => a - b);
}
/** Each process sees only the settings matching its patterns, plus the deployment mode. */
export function environment(patterns, extra = {}) {
return {

View File

@ -3,7 +3,7 @@ import { mkdtemp, readFile, rm } from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import test from 'node:test';
import { startServices, webListener } from './local-services.mjs';
import { platformPorts, startServices, webListener } from './local-services.mjs';
const alive = pid => {
try {
@ -74,3 +74,19 @@ test('only the production web build listens beyond loopback, and readiness is ch
assert.equal(webListener({ WF_WEB_HOST: 'localhost', WF_WEB_SERVE_MODE: 'development' }).host, 'localhost');
assert.throws(() => webListener({ WF_WEB_PORT: '70000' }), /TCP port/);
});
test("the platform's own ports come from its configured addresses", () => {
assert.deepEqual(
platformPorts({
WF_API_URL: 'http://127.0.0.1:4000',
DATABASE_URL: 'postgresql://user:password@127.0.0.1:55432/wishforge',
TEMPORAL_ADDRESS: '127.0.0.1:7233',
S3_ENDPOINT: 'http://127.0.0.1:49000',
WF_PROVISIONER_URL: 'http://127.0.0.1:4450',
WF_IDE_ALLOWED_UPSTREAM_ORIGINS: 'http://127.0.0.1:4300, http://localhost:4301',
WF_HOST_CHECK_BLOCKED_PORTS: '6379',
WF_IDE_PUBLIC_URL: 'not an address',
}),
[3000, 4000, 4300, 4301, 4450, 4504, 6379, 7233, 8233, 49000, 55432],
);
});

49
tests/host-checks.test.ts Normal file
View File

@ -0,0 +1,49 @@
import path from 'node:path';
import { describe, expect, it } from 'vitest';
import type { CommandExecutor, HostCheckSandbox } from '@wishforge/runtime';
import { hostCheckExecutor, hostCheckSandbox } from '../apps/worker/src/host-checks.js';
import type { WorkerConfig } from '../apps/worker/src/common.js';
describe('operator opt-in to sandboxed host checks', () => {
it('reads the paths, loopback and blocked ports, and always blocks the control plane and Temporal', () => {
expect(hostCheckSandbox({})).toBeUndefined();
expect(hostCheckSandbox({ WF_HOST_CHECK_SANDBOX: 'false', WF_HOST_CHECK_READ_PATHS: 'relative' })).toBeUndefined();
expect(
hostCheckSandbox({
WF_HOST_CHECK_SANDBOX: 'true',
WF_HOST_CHECK_READ_PATHS: ['/opt/check.mjs', '/opt/tools'].join(path.delimiter),
WF_HOST_CHECK_WRITE_PATHS: '/opt/cache',
WF_HOST_CHECK_LOOPBACK: 'true',
WF_HOST_CHECK_BLOCKED_PORTS: '55432,3000',
WF_CONTROL_PLANE_URL: 'http://127.0.0.1:4100',
TEMPORAL_ADDRESS: '127.0.0.1:7233',
}),
).toEqual({
readable: ['/opt/check.mjs', '/opt/tools'],
writable: ['/opt/cache'],
loopback: true,
blockedPorts: [3000, 4100, 7233, 55432],
});
expect(hostCheckSandbox({ WF_HOST_CHECK_SANDBOX: 'true' })).toMatchObject({
loopback: false,
blockedPorts: [4000, 7233],
});
expect(() => hostCheckSandbox({ WF_HOST_CHECK_SANDBOX: 'yes' })).toThrow(/true or false/);
expect(() => hostCheckSandbox({ WF_HOST_CHECK_SANDBOX: 'true', WF_HOST_CHECK_READ_PATHS: 'relative' })).toThrow(
/absolute paths/,
);
expect(() => hostCheckSandbox({ WF_HOST_CHECK_SANDBOX: 'true', WF_HOST_CHECK_WRITE_PATHS: '/a"b' })).toThrow(
/absolute paths/,
);
expect(() => hostCheckSandbox({ WF_HOST_CHECK_SANDBOX: 'true', WF_HOST_CHECK_BLOCKED_PORTS: '99999' })).toThrow(
/TCP ports/,
);
});
it('sandboxes only the checks of the development host mode', () => {
const execute: CommandExecutor = async () => ({ exitCode: 0, stdout: '', stderr: '' });
const hostCheckSandbox: HostCheckSandbox = { readable: [], writable: [] };
expect(hostCheckExecutor({ sandboxMode: 'host' } as WorkerConfig, execute)).toBe(execute);
expect(hostCheckExecutor({ sandboxMode: 'kata', hostCheckSandbox } as WorkerConfig, execute)).toBe(execute);
expect(hostCheckExecutor({ sandboxMode: 'host', hostCheckSandbox } as WorkerConfig, execute)).not.toBe(execute);
});
});

View File

@ -13,7 +13,7 @@ import os from 'node:os';
import path from 'node:path';
import { promisify } from 'node:util';
import type { Actor, CheckpointRecord, Evidence, Lesson, Project, Run } from '@wishforge/contracts';
import { executeCommand, type CommandExecutor, type ProviderStart } from '@wishforge/runtime';
import { executeCommand, type CommandExecutor, type HostCheckSandbox, type ProviderStart } from '@wishforge/runtime';
import { ManagedProvider } from '../packages/runtime/src/providers/base.js';
import { ControlPlane } from '../apps/api/src/domain/control-plane.js';
import { Services } from '../apps/api/src/http.js';
@ -187,6 +187,8 @@ async function provision(
stoppedUsage?: Record<string, unknown>;
fetcher?: typeof fetch;
heartbeatMs?: number;
execute?: CommandExecutor;
hostCheckSandbox?: HostCheckSandbox;
} = {},
) {
const generation = (await services.control.run(owner, runId)).pendingStartId!;
@ -781,6 +783,32 @@ describe('local main loop', () => {
// The stopped session is counted once, though continuing settles its saved usage again.
expect((await services.control.run(owner, runId)).usedTokens).toBe(1120);
});
it('records the checks as sandboxed when the operator sandboxes host checks', async () => {
const platform = Object.getOwnPropertyDescriptor(process, 'platform')!;
Object.defineProperty(process, 'platform', { value: 'darwin' });
try {
const project = await createProject();
const runId = await submit(project.id, 'Make the greeting say hello world', 'wish-host-sandbox-0001');
const sandboxed: string[][] = [];
// Stands in for sandbox-exec: notes the command it was given and runs it as it is.
const seatbelt: CommandExecutor = async command => {
if (command.executable !== '/usr/bin/sandbox-exec') return execute(command);
expect(command.args[0]).toBe('-p');
sandboxed.push(command.args.slice(2));
return execute({ ...command, executable: command.args[2]!, args: command.args.slice(3) });
};
const session = await provision(runId, writeGreeting('hello world', 'Updated the greeting.'), {
execute: seatbelt,
hostCheckSandbox: { readable: [], writable: [] },
});
expect(await drive(session)).toEqual({ status: 'complete' });
expect(sandboxed).toContainEqual([process.execPath, 'check.mjs']);
const verification = (await evidenceOf(runId)).find(evidence => evidence.type === 'verification');
expect(verification).toMatchObject({ passed: true, isolation: 'host', checkSandbox: 'seatbelt' });
} finally {
Object.defineProperty(process, 'platform', platform);
}
});
it('waits for a source branch that was not pushed, and starts once it exists', async () => {
const project = await createProject();
const runId = await submit(project.id, 'Make the greeting say hello world', 'wish-ide-0002', {