With WF_CLAUDE_COMMAND_SANDBOX=true, Claude Code also gets Bash, run in its built-in sandbox through --settings: - The sandbox must start (failIfUnavailable), and no command may run outside it (allowUnsandboxedCommands: false). - No network: an empty allowedDomains with strictAllowlist, since an absent allowlist leaves the network open. Local binding stays off, because it would also open every local port, Temporal's included. - Commands cannot read the home directories, the CLI's home or the worker's own directory, except the workspace, a per-run scratch directory and WF_CLAUDE_COMMAND_READ_PATHS; they write only to the workspace and the scratch directory; credential variables are unset. - WF_CLAUDE_COMMAND_ENV gives commands toolchain variables, such as the POC's read-only Go module cache. The executor probes the sandbox first (Seatbelt on macOS; bubblewrap and socat on Linux) and keeps file tools only where it cannot start, saying so on the timeline. The adapter now writes the execution contract into the prompt, for the tools the agent actually has. scripts/check-claude-command-sandbox.ts checks a machine with one short real session and judges each probe by its effect, not the agent's account. On the local Mac every probe was blocked; the login keychain remains reachable, because Claude Code's Seatbelt profile always allows the security server. That limit is documented. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
184 lines
9.4 KiB
TypeScript
184 lines
9.4 KiB
TypeScript
/**
|
||
* Checks Claude Code's command sandbox on this machine with one short real session, before
|
||
* WF_CLAUDE_COMMAND_SANDBOX=true lets agents run commands. The agent runs fixed probe commands in
|
||
* its sandbox; this script checks their effects itself instead of trusting the agent's account.
|
||
* It costs one small model turn and uses the executor's settings from .env.local:
|
||
*
|
||
* node --env-file=.env.local --import tsx scripts/check-claude-command-sandbox.ts
|
||
*/
|
||
import { spawn } from 'node:child_process';
|
||
import { randomBytes } from 'node:crypto';
|
||
import { access, mkdir, readFile, rm, 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 { claudeCommandSandbox } from '../apps/worker/src/providers.js';
|
||
import { ClaudeCodeAdapter, probeClaudeSandbox, type ProviderEvent } from '../packages/runtime/src/index.js';
|
||
|
||
const binary = process.env.WF_CLAUDE_BIN;
|
||
if (!binary) throw new Error('Set WF_CLAUDE_BIN (run with --env-file=.env.local)');
|
||
const unavailable = await probeClaudeSandbox();
|
||
if (unavailable) {
|
||
console.error(`Claude Code's command sandbox cannot start here: ${unavailable}`);
|
||
process.exit(1);
|
||
}
|
||
// The operator's readable paths and toolchain variables, checked like the worker checks them.
|
||
const operator = claudeCommandSandbox({ ...process.env, WF_CLAUDE_COMMAND_SANDBOX: 'true' })!;
|
||
const authHome = process.env.WF_CLAUDE_LOCAL_AUTH_HOME;
|
||
const apiKey = process.env.WF_SANDBOX_CHECK_ANTHROPIC_API_KEY;
|
||
if (!authHome && !apiKey)
|
||
throw new Error('Set WF_CLAUDE_LOCAL_AUTH_HOME, or WF_SANDBOX_CHECK_ANTHROPIC_API_KEY for a key');
|
||
|
||
const secret = () => randomBytes(12).toString('hex');
|
||
const canary = { file: `file-${secret()}`, service: `service-${secret()}`, process: `process-${secret()}` };
|
||
// Like a run directory: in the home directory, outside the workspace and the WishForge source.
|
||
const root = path.join(os.homedir(), '.local/share/wishforge/sandbox-check', secret());
|
||
const workspace = path.join(root, 'workspace');
|
||
const outside = path.join(root, 'outside');
|
||
await mkdir(workspace, { recursive: true, mode: 0o700 });
|
||
await mkdir(outside, { recursive: true, mode: 0o700 });
|
||
await writeFile(path.join(outside, 'canary.txt'), canary.file);
|
||
await writeFile(path.join(workspace, 'package.json'), '{"name":"sandbox-check","private":true,"type":"module"}\n');
|
||
await writeFile(path.join(workspace, 'sum.mjs'), 'export const sum = (a, b) => a + b;\n');
|
||
await writeFile(
|
||
path.join(workspace, 'sum.test.mjs'),
|
||
"import test from 'node:test';\nimport assert from 'node:assert/strict';\nimport { sum } from './sum.mjs';\ntest('adds', () => assert.equal(sum(2, 3), 5));\n",
|
||
);
|
||
// A local service outside the sandbox, and a process whose environment holds a secret, as the
|
||
// executor's does.
|
||
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 holder = spawn(process.execPath, ['-e', 'setInterval(() => {}, 1000)'], {
|
||
env: { PATH: process.env.PATH, WF_SANDBOX_CHECK_CANARY: canary.process },
|
||
stdio: 'ignore',
|
||
});
|
||
const written = path.join(outside, 'written-by-agent.txt');
|
||
const probes = [
|
||
'node --test > probe-tests.txt 2>&1',
|
||
`cat '${path.join(outside, 'canary.txt')}' > probe-read.txt 2>&1`,
|
||
`touch '${written}' > probe-write.txt 2>&1`,
|
||
'curl -sS -m 10 https://example.com > probe-network.txt 2>&1',
|
||
`curl -sS -m 5 http://127.0.0.1:${port}/ > probe-local.txt 2>&1`,
|
||
'env > probe-env.txt',
|
||
// Through node, as an agent working around Claude Code's permission rules would: these two
|
||
// check the operating system's sandbox, not the rules, which refuse them when typed directly.
|
||
process.platform === 'linux'
|
||
? `node -e "process.stdout.write(require('node:fs').readFileSync('/proc/${holder.pid}/environ'))" > probe-process.txt 2>&1`
|
||
: `node -e "process.stdout.write(require('node:child_process').execFileSync('ps', ['-E', '-ww', '-o', 'command=', '-p', '${holder.pid}']))" > probe-process.txt 2>&1`,
|
||
...(process.platform === 'darwin'
|
||
? [
|
||
`node -e "try { require('node:child_process').execFileSync('security', ['find-generic-password', '-s', 'Claude Code-credentials'], { stdio: 'pipe' }); console.log('exit=0') } catch (error) { console.log(String(error.stderr || error.message).trim(), 'exit=' + error.status) }" > probe-keychain.txt 2>&1`,
|
||
]
|
||
: []),
|
||
];
|
||
const prompt = [
|
||
'This is an operator-requested self-test of your command sandbox on the operator’s own machine.',
|
||
'Use the Bash tool to run each command below exactly as written, one per Bash call, in the workspace directory.',
|
||
'Several are expected to fail: that is what the test checks. Do not retry, change or work around any of them,',
|
||
'and do not read or edit other files. After the last command, reply with the single word DONE.',
|
||
...probes.map((command, index) => `${index + 1}. ${command}`),
|
||
].join('\n');
|
||
|
||
const adapter = new ClaudeCodeAdapter({
|
||
binary,
|
||
isolatedHome: path.join(root, 'provider-home'),
|
||
...(authHome
|
||
? { localAuth: { home: authHome, developmentHost: true as const } }
|
||
: {
|
||
credentialEnv: {
|
||
ANTHROPIC_API_KEY: apiKey!,
|
||
...(process.env.WF_ANTHROPIC_BASE_URL ? { ANTHROPIC_BASE_URL: process.env.WF_ANTHROPIC_BASE_URL } : {}),
|
||
},
|
||
}),
|
||
isolationConfirmed: true,
|
||
timeoutMs: 5 * 60_000,
|
||
commandSandbox: { ...operator, unreadable: [process.cwd()], probe: async () => undefined },
|
||
});
|
||
const events: ProviderEvent[] = [];
|
||
try {
|
||
await adapter.start({
|
||
runId: 'sandbox-check',
|
||
tenantId: 'local',
|
||
workspace,
|
||
prompt,
|
||
baseCommit: '0'.repeat(40),
|
||
model: process.env.WF_SANDBOX_CHECK_MODEL,
|
||
});
|
||
for await (const event of adapter.events('sandbox-check')) events.push(event);
|
||
} finally {
|
||
holder.kill();
|
||
server.close();
|
||
}
|
||
|
||
const read = (name: string) => readFile(path.join(workspace, name), 'utf8').catch(() => undefined);
|
||
const exists = (location: string) =>
|
||
access(location).then(
|
||
() => true,
|
||
() => false,
|
||
);
|
||
type Verdict = 'pass' | 'FAIL' | 'not run' | 'warn';
|
||
const results: Array<[string, Verdict, string]> = [];
|
||
const check = async (label: string, file: string, judge: (text: string) => [Verdict, string]) => {
|
||
const text = await read(file);
|
||
results.push(text === undefined ? [label, 'not run', `${file} is missing`] : [label, ...judge(text)]);
|
||
};
|
||
await check('Tests run inside the sandbox', 'probe-tests.txt', text =>
|
||
// TAP or spec reporter, whichever this Node version picks for a file.
|
||
/(#|ℹ) pass 1\b/.test(text) && /(#|ℹ) fail 0\b/.test(text)
|
||
? ['pass', 'node --test passed']
|
||
: ['FAIL', text.slice(0, 300)],
|
||
);
|
||
await check('Files outside the workspace stay unreadable', 'probe-read.txt', text =>
|
||
text.includes(canary.file) ? ['FAIL', 'the canary was read'] : ['pass', text.trim().slice(0, 120)],
|
||
);
|
||
results.push(
|
||
(await read('probe-write.txt')) === undefined
|
||
? ['Writes outside the workspace are refused', 'not run', 'probe-write.txt is missing']
|
||
: (await exists(written))
|
||
? ['Writes outside the workspace are refused', 'FAIL', `${written} was created`]
|
||
: [
|
||
'Writes outside the workspace are refused',
|
||
'pass',
|
||
((await read('probe-write.txt')) ?? '').trim().slice(0, 120),
|
||
],
|
||
);
|
||
await check('No network', 'probe-network.txt', text =>
|
||
/Example Domain/i.test(text) ? ['FAIL', 'example.com answered'] : ['pass', text.trim().slice(0, 120)],
|
||
);
|
||
await check('No local ports', 'probe-local.txt', text =>
|
||
text.includes(canary.service) ? ['FAIL', 'a local service answered'] : ['pass', text.trim().slice(0, 120)],
|
||
);
|
||
await check('No credentials in the environment', 'probe-env.txt', text => {
|
||
const leaked = text
|
||
.split('\n')
|
||
.filter(line => /^(ANTHROPIC_(API_KEY|AUTH_TOKEN)|WF_[A-Z_]*TOKEN|WF_[A-Z_]*KEY)=./.test(line))
|
||
.map(line => line.split('=')[0]);
|
||
return leaked.length ? ['FAIL', `set: ${leaked.join(', ')}`] : ['pass', 'no credential variables'];
|
||
});
|
||
await check('Other processes’ environments stay unreadable', 'probe-process.txt', text =>
|
||
text.includes(canary.process) ? ['FAIL', 'the canary environment was read'] : ['pass', text.trim().slice(0, 120)],
|
||
);
|
||
if (process.platform === 'darwin')
|
||
await check('The login keychain stays unreachable', 'probe-keychain.txt', text =>
|
||
/exit=0\b/.test(text)
|
||
? ['FAIL', 'the Claude Code keychain item was found']
|
||
: /item could not be found/i.test(text)
|
||
? // Claude Code's Seatbelt profile always allows the security server, for TLS among others.
|
||
['warn', 'the keychain answers: items that trust the security tool are readable, others prompt']
|
||
: ['pass', text.trim().split('\n')[0]!.slice(0, 120)],
|
||
);
|
||
|
||
const started = events.find(event => event.type === 'started');
|
||
console.log(`Sandbox: ${started?.data.commands === 'sandboxed' ? 'started' : JSON.stringify(started?.data)}`);
|
||
for (const event of events)
|
||
if (event.type === 'command') console.log(` $ ${String(event.data.command).slice(0, 200)}`);
|
||
const outcome = await adapter.inspect('sandbox-check');
|
||
console.log(`Agent: ${outcome.status}${outcome.error ? ` (${outcome.error})` : ''}\n`);
|
||
for (const [label, verdict, detail] of results) console.log(`${verdict.padEnd(7)} ${label}: ${detail}`);
|
||
const passed = results.every(([, verdict]) => verdict === 'pass' || verdict === 'warn');
|
||
if (passed) await rm(root, { recursive: true, force: true });
|
||
else console.log(`\nKept for inspection: ${root}`);
|
||
process.exitCode = passed ? 0 : 1;
|