diff --git a/bin/sandbox/runtime.mjs b/bin/sandbox/runtime.mjs index 07a60ea..4d08fc7 100644 --- a/bin/sandbox/runtime.mjs +++ b/bin/sandbox/runtime.mjs @@ -15,6 +15,7 @@ import { import { createServer } from "node:net"; import os from "node:os"; import path from "node:path"; +import { fileURLToPath } from "node:url"; export const SANDBOX_RUNTIME_PACKAGE = "@anthropic-ai/sandbox-runtime"; export const SANDBOX_RUNTIME_VERSION = "0.0.73"; @@ -55,8 +56,12 @@ function unique(values) { return [...new Set(values)]; } +function isEnvironmentName(name) { + return /^[A-Za-z_][A-Za-z0-9_]*$/.test(name); +} + function assertEnvironmentName(name) { - if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(name)) { + if (!isEnvironmentName(name)) { throw new Error(`Invalid environment variable name: ${name}`); } if (PROTECTED_ENVIRONMENT.has(name) || /^(?:DYLD_|LD_)/.test(name)) { @@ -226,7 +231,7 @@ export function compileSandboxExecution({ runRoot, }); const deniedEnvironment = Object.keys(sourceEnvironment) - .filter((name) => !Object.hasOwn(childEnvironment, name)) + .filter((name) => isEnvironmentName(name) && !Object.hasOwn(childEnvironment, name)) .map((name) => ({ name, mode: "deny" })); const workspaceWrites = capabilities.workspace === "write" ? [workspace] : []; const network = { @@ -273,6 +278,9 @@ export function compileSandboxExecution({ ...(helperPaths.rg ? { ripgrep: { command: helperPaths.rg } } : {}), ...(helperPaths.bwrap ? { bwrapPath: helperPaths.bwrap } : {}), ...(helperPaths.socat ? { socatPath: helperPaths.socat } : {}), + ...(helperPaths.applySeccomp + ? { seccomp: { applyPath: helperPaths.applySeccomp } } + : {}), }; return { @@ -855,6 +863,43 @@ async function resolveHelperPaths(platform, environmentPath, blockedRoots) { return { bwrap, rg, socat }; } +async function resolveSandboxRuntimeResources(platform, architecture = process.arch) { + if (platform !== "linux") return {}; + const architectureDirectory = { x64: "x64", arm64: "arm64" }[architecture]; + if (!architectureDirectory) { + throw new Error(`Sandbox seccomp helper is unsupported on architecture: ${architecture}`); + } + let current = path.dirname(fileURLToPath(import.meta.resolve(SANDBOX_RUNTIME_PACKAGE))); + for (let depth = 0; depth <= 3; depth += 1) { + const manifestPath = path.join(current, "package.json"); + try { + const manifest = JSON.parse(await readFile(manifestPath, "utf8")); + if ( + manifest.name === SANDBOX_RUNTIME_PACKAGE + && manifest.version === SANDBOX_RUNTIME_VERSION + ) { + const applySeccomp = await realpath(path.join( + current, + "vendor", + "seccomp", + architectureDirectory, + "apply-seccomp", + )); + const info = await stat(applySeccomp); + await access(applySeccomp, 1); + if (!info.isFile()) throw new Error("Sandbox seccomp helper is not a regular file."); + return { applySeccomp }; + } + } catch (error) { + if (error?.code !== "ENOENT") throw error; + } + const parent = path.dirname(current); + if (parent === current) break; + current = parent; + } + throw new Error(`Could not locate pinned ${SANDBOX_RUNTIME_PACKAGE} runtime resources.`); +} + async function createDenyProxy() { const server = createServer((socket) => socket.destroy()); await new Promise((resolve, reject) => { @@ -1002,7 +1047,9 @@ export async function probeSandboxRuntime({ platform = process.platform, cwd = process.cwd(), env = process.env, + architecture = process.arch, hostProbe = probeHostIsolation, + resourceProbe = resolveSandboxRuntimeResources, } = {}) { const backend = `${SANDBOX_RUNTIME_PACKAGE}@${SANDBOX_RUNTIME_VERSION}`; if (!SUPPORTED_PLATFORMS.has(platform)) { @@ -1026,9 +1073,12 @@ export async function probeSandboxRuntime({ }; } const dependencies = await SandboxManager.checkDependenciesAsync(); - const host = dependencies.errors.length === 0 - ? await hostProbe({ platform, cwd: await realpath(cwd), env }) - : { errors: [], warnings: [] }; + const [host] = dependencies.errors.length === 0 + ? await Promise.all([ + hostProbe({ platform, cwd: await realpath(cwd), env }), + resourceProbe(platform, architecture), + ]) + : [{ errors: [], warnings: [] }]; const errors = [...dependencies.errors, ...host.errors]; const warnings = [...dependencies.warnings, ...host.warnings]; return { @@ -1080,12 +1130,13 @@ async function runSandboxedCommandOnce(options, context = {}) { ]); const sourceEnvironment = context.env || process.env; - const [helperPaths, systemReadRoots, executableReadRoots] = await Promise.all([ + const [containmentHelpers, runtimeResources, systemReadRoots, executableReadRoots] = await Promise.all([ context.helperPaths || resolveHelperPaths( platform, sourceEnvironment.PATH, [workspace, originalHome, temporaryRoot], ), + resolveSandboxRuntimeResources(platform, context.architecture), existingSystemReadRoots(platform), commandReadRoots(options.command, sourceEnvironment.PATH, executionCwd, { workspace, @@ -1093,7 +1144,12 @@ async function runSandboxedCommandOnce(options, context = {}) { temporaryRoot, }), ]); - const allowedReadRoots = unique([...systemReadRoots, ...executableReadRoots]); + const helperPaths = { ...containmentHelpers, ...runtimeResources }; + const allowedReadRoots = unique([ + ...systemReadRoots, + ...executableReadRoots, + ...Object.values(runtimeResources), + ]); const runRoot = await realpath(await mkdtemp(path.join(temporaryRoot, "opp-sandbox-"))); let denyProxy; let manager; diff --git a/tests/sandbox-runtime.test.mjs b/tests/sandbox-runtime.test.mjs index 708cd0a..7302aa5 100644 --- a/tests/sandbox-runtime.test.mjs +++ b/tests/sandbox-runtime.test.mjs @@ -273,6 +273,20 @@ test("runtime probing reports readiness and fails closed on unsupported or missi assert.equal(ready.executionLevel, "shell-contained"); assert.deepEqual(ready.warnings, ["preview"]); + const unsupportedArchitecture = await probeSandboxRuntime({ + platform: "linux", + architecture: "s390x", + loadRuntime: async () => ({ + SandboxManager: { + isSupportedPlatform: () => true, + checkDependenciesAsync: async () => ({ errors: [], warnings: [] }), + }, + }), + hostProbe: async () => ({ errors: [], warnings: [] }), + }); + assert.equal(unsupportedArchitecture.runnerReady, false); + assert.match(unsupportedArchitecture.errors[0], /unsupported on architecture/); + const unsupported = await probeSandboxRuntime({ platform: "win32" }); assert.equal(unsupported.runnerReady, false); assert.match(unsupported.errors[0], /Unsupported platform/); @@ -318,7 +332,11 @@ test("runner initializes before spawn, uses shell false, propagates exit codes, }, { cwd: REPO, home: os.homedir(), - env: { PATH: "/usr/bin:/bin", GH_TOKEN: "secret" }, + env: { + PATH: "/usr/bin:/bin", + GH_TOKEN: "secret", + "JAVA_HOME_17.0.16_x64": "/invalid-host-key", + }, helperPaths: { bwrap: "/usr/bin/bwrap", rg: "/usr/bin/rg", socat: "/usr/bin/socat" }, platform: "linux", loadRuntime: async () => ({ @@ -333,8 +351,19 @@ test("runner initializes before spawn, uses shell false, propagates exit codes, assert.equal(resetCount, 1); assert.equal(spawnOptions.shell, false); assert.equal(spawnOptions.env.GH_TOKEN, undefined); + assert.equal(spawnOptions.env["JAVA_HOME_17.0.16_x64"], undefined); assert.equal(spawnOptions.env.SHOULD_NOT_LEAK, undefined); assert.equal(initialized.enableWeakerNestedSandbox, false); + assert.ok(initialized.seccomp.applyPath.endsWith(path.join( + "vendor", + "seccomp", + process.arch === "arm64" ? "arm64" : "x64", + "apply-seccomp", + ))); + assert.ok(initialized.filesystem.allowRead.includes(initialized.seccomp.applyPath)); + assert.equal(initialized.credentials.envVars.some( + (entry) => entry.name === "JAVA_HOME_17.0.16_x64", + ), false); assert.equal(wrappedCommand, `'printf' '%s' 'literal;$(not-run)'`); assert.match(diagnostics, /Sandbox: observe, shell-contained/); assert.doesNotMatch(diagnostics, /GH_TOKEN|secret|literal/); @@ -478,8 +507,10 @@ test("runner exposes an explicitly selected executable outside static system roo assert.ok(runtimeConfig.filesystem.allowRead.some( (allowed) => allowed === executable || isParent(allowed, executable), )); + const canonicalRuntimeResource = await realpath(runtimeResource); assert.ok(runtimeConfig.filesystem.allowRead.some( - (allowed) => allowed === runtimeResource || isParent(allowed, runtimeResource), + (allowed) => allowed === canonicalRuntimeResource + || isParent(allowed, canonicalRuntimeResource), )); } finally { await rm(fixture, { recursive: true, force: true }); @@ -608,6 +639,16 @@ test("runner discovers Git roots from nested directories and validates linked wo ]); const linkedNested = path.join(linkedWorktree, "src"); await mkdir(linkedNested); + const canonicalRepository = await realpath(repository); + const canonicalNested = await realpath(nested); + const canonicalCredentialSibling = await realpath(credentialSibling); + const canonicalDeepCredential = await realpath(deepCredential); + const canonicalLinkedWorktree = await realpath(linkedWorktree); + const canonicalLinkedNested = await realpath(linkedNested); + const canonicalSubmodule = await realpath(submodule); + const canonicalSubmoduleNested = await realpath(submoduleNested); + const canonicalSubmoduleGitDirectory = await realpath(submoduleGitDirectory); + const canonicalRepositoryGitDirectory = await realpath(path.join(repository, ".git")); let runtimeConfig; let wrappedCwd; const manager = { @@ -639,35 +680,37 @@ test("runner discovers Git roots from nested directories and validates linked wo }); try { assert.equal(await execute(nested), 0); - assert.equal(wrappedCwd, nested); - assert.ok(runtimeConfig.filesystem.allowRead.includes(repository)); + assert.equal(wrappedCwd, canonicalNested); + assert.ok(runtimeConfig.filesystem.allowRead.includes(canonicalRepository)); assert.ok(runtimeConfig.credentials.files.some( - (entry) => entry.path === path.join(nested, ".env") && entry.mode === "deny", + (entry) => entry.path === path.join(canonicalNested, ".env") && entry.mode === "deny", )); assert.ok(runtimeConfig.credentials.files.some( - (entry) => entry.path === path.join(repository, "**", ".env") && entry.mode === "deny", + (entry) => entry.path === path.join(canonicalRepository, "**", ".env") && entry.mode === "deny", )); - assert.ok(runtimeConfig.filesystem.denyWrite.includes(path.join(nested, ".npmrc"))); - assert.ok(runtimeConfig.filesystem.denyWrite.includes(path.join(credentialSibling, ".npmrc"))); - assert.ok(runtimeConfig.filesystem.denyWrite.includes(path.join(credentialSibling, ".ssh"))); - assert.ok(runtimeConfig.filesystem.denyWrite.includes(deepCredential)); - assert.ok(runtimeConfig.filesystem.denyWrite.includes(path.join(submodule, ".git"))); + assert.ok(runtimeConfig.filesystem.denyWrite.includes(path.join(canonicalNested, ".npmrc"))); + assert.ok(runtimeConfig.filesystem.denyWrite.includes(path.join(canonicalCredentialSibling, ".npmrc"))); + assert.ok(runtimeConfig.filesystem.denyWrite.includes(path.join(canonicalCredentialSibling, ".ssh"))); + assert.ok(runtimeConfig.filesystem.denyWrite.includes(canonicalDeepCredential)); + assert.ok(runtimeConfig.filesystem.denyWrite.includes(path.join(canonicalSubmodule, ".git"))); assert.ok(runtimeConfig.filesystem.denyWrite.includes( - path.join(submoduleGitDirectory, "config"), + path.join(canonicalSubmoduleGitDirectory, "config"), )); assert.equal(await execute(linkedNested), 0); - assert.equal(wrappedCwd, linkedNested); - assert.ok(runtimeConfig.filesystem.allowRead.includes(linkedWorktree)); - assert.ok(runtimeConfig.filesystem.allowRead.includes(path.join(repository, ".git"))); - assert.ok(runtimeConfig.filesystem.denyWrite.includes(path.join(repository, ".git", "config"))); + assert.equal(wrappedCwd, canonicalLinkedNested); + assert.ok(runtimeConfig.filesystem.allowRead.includes(canonicalLinkedWorktree)); + assert.ok(runtimeConfig.filesystem.allowRead.includes(canonicalRepositoryGitDirectory)); + assert.ok(runtimeConfig.filesystem.denyWrite.includes( + path.join(canonicalRepositoryGitDirectory, "config"), + )); assert.equal(await execute(submoduleNested), 0); - assert.equal(wrappedCwd, submoduleNested); - assert.ok(runtimeConfig.filesystem.allowRead.includes(submodule)); - assert.ok(runtimeConfig.filesystem.allowRead.includes(submoduleGitDirectory)); - assert.equal(runtimeConfig.filesystem.allowRead.includes(repository), false); - assert.ok(runtimeConfig.filesystem.denyWrite.includes(path.join(submodule, ".git"))); + assert.equal(wrappedCwd, canonicalSubmoduleNested); + assert.ok(runtimeConfig.filesystem.allowRead.includes(canonicalSubmodule)); + assert.ok(runtimeConfig.filesystem.allowRead.includes(canonicalSubmoduleGitDirectory)); + assert.equal(runtimeConfig.filesystem.allowRead.includes(canonicalRepository), false); + assert.ok(runtimeConfig.filesystem.denyWrite.includes(path.join(canonicalSubmodule, ".git"))); await writeFile( path.join(submodule, ".git"),