From f45f7cfc3d209f58ff9e80b5ff580fa3bce283f8 Mon Sep 17 00:00:00 2001 From: Wayner Barrios Date: Mon, 17 Aug 2026 00:20:19 -0500 Subject: [PATCH] Complete portable script execution under native macOS containment Add the missing Seatbelt interpreter permission through an exact-version, fail-closed compatibility shim so shell and env shebang commands work without weakening filesystem visibility. Align the Linux credential-directory test with its disposable tmpfs behavior and document when the temporary upstream compatibility layer can be removed. --- bin/sandbox/runtime.mjs | 33 ++++++++++++++++++++ docs/sandbox-compatibility.md | 2 ++ tests/sandbox-runtime.integration.test.mjs | 2 +- tests/sandbox-runtime.test.mjs | 35 ++++++++++++++++++++-- 4 files changed, 68 insertions(+), 4 deletions(-) diff --git a/bin/sandbox/runtime.mjs b/bin/sandbox/runtime.mjs index 0f22392..88254b2 100644 --- a/bin/sandbox/runtime.mjs +++ b/bin/sandbox/runtime.mjs @@ -21,6 +21,13 @@ export const SANDBOX_RUNTIME_PACKAGE = "@anthropic-ai/sandbox-runtime"; export const SANDBOX_RUNTIME_VERSION = "0.0.73"; export const SANDBOX_EXECUTION_LEVEL = "shell-contained"; +const MACOS_PROCESS_RULES = "(allow process-exec)\n(allow process-fork)"; +const MACOS_INTERPRETER_PROCESS_RULES = [ + "(allow process-exec)", + "(allow process-exec-interpreter)", + "(allow process-fork)", +].join("\n"); + const SUPPORTED_PLATFORMS = new Set(["linux", "darwin"]); const READ_ONLY_METHODS = new Set(["GET", "HEAD", "OPTIONS"]); const PROTECTED_ENVIRONMENT = new Set([ @@ -95,6 +102,31 @@ export function commandToShellString(command) { return command.map(quotePosixArgument).join(" "); } +export function enableMacOSInterpreterExecution(descriptor, platform) { + if (platform !== "darwin") return descriptor; + if ( + !descriptor + || !Array.isArray(descriptor.argv) + || descriptor.argv.length !== 3 + || descriptor.argv[1] !== "-c" + || typeof descriptor.argv[2] !== "string" + ) { + throw new Error("Pinned macOS sandbox runtime returned an unexpected command descriptor."); + } + const occurrences = descriptor.argv[2].split(MACOS_PROCESS_RULES).length - 1; + if (occurrences !== 1) { + throw new Error("Pinned macOS sandbox runtime process rules no longer match the verified profile."); + } + return { + ...descriptor, + argv: [ + descriptor.argv[0], + descriptor.argv[1], + descriptor.argv[2].replace(MACOS_PROCESS_RULES, MACOS_INTERPRETER_PROCESS_RULES), + ], + }; +} + export function buildChildEnvironment({ sourceEnvironment, allowedEnvironment, workspace, runRoot }) { const childEnvironment = { HOME: path.join(runRoot, "home"), @@ -1263,6 +1295,7 @@ async function runSandboxedCommandOnce(options, context = {}) { executionCwd, { commandId: randomUUID(), commandText: options.command[0] }, ); + descriptor = enableMacOSInterpreterExecution(descriptor, platform); } finally { if (previousTemp === undefined) delete process.env.CLAUDE_CODE_TMPDIR; else process.env.CLAUDE_CODE_TMPDIR = previousTemp; diff --git a/docs/sandbox-compatibility.md b/docs/sandbox-compatibility.md index fa34ed9..112bac3 100644 --- a/docs/sandbox-compatibility.md +++ b/docs/sandbox-compatibility.md @@ -83,6 +83,8 @@ Host filesystem reads are denied by default. The runner exposes the workspace, i Selecting a command grants read access to that executable. If it belongs to a package or Python virtual environment identified by a nearby `package.json` or `pyvenv.cfg`, the bounded package root is also readable so resource-bearing CLIs can start. The runner also resolves bounded shebang dependencies, including `#!/usr/bin/env node` and `env -S`, without exposing a whole version-manager or system prefix. Commands selected from `~/.local/bin` can read `~/.local/lib` for user-level Python packages, but not user configuration, cache, or credential directories. Installing project-specific tools inside the workspace remains the most predictable option. +The pinned native runtime omits macOS Seatbelt's separate `process-exec-interpreter` permission even though it permits native executable files. The adapter adds exactly that narrow operation to the generated macOS profile after verifying the pinned descriptor shape. It fails closed if the upstream profile changes. Filesystem read rules still decide which scripts and interpreters are visible, and the compatibility shim should be removed when a verified upstream release includes the permission directly. + When the command starts inside a Git repository, the runner treats the containing worktree root as the workspace while preserving the original working directory. Existing credential-shaped files are denied recursively, and exact credential paths in both the repository root and original working directory stay protected even when absent at startup. The runner also exposes validated linked-worktree and submodule metadata, so ordinary inspection commands work from nested directories, linked worktrees, and standard submodules. The repository's `.git/config` remains readable while writes to it and hooks remain denied. Do not store credentials in Git remote URLs or custom repository configuration. Use an explicitly granted environment variable for network credentials instead. The runner is intentionally lightweight and on demand. It uses native Seatbelt or Bubblewrap primitives for one command tree, creates no persistent container or daemon, and removes its private directory afterward. Because the pinned upstream manager has process-global state, a second command in the same Node.js process is rejected while one is active. Independent CLI processes have separate managers. diff --git a/tests/sandbox-runtime.integration.test.mjs b/tests/sandbox-runtime.integration.test.mjs index 8705685..fcef373 100644 --- a/tests/sandbox-runtime.integration.test.mjs +++ b/tests/sandbox-runtime.integration.test.mjs @@ -209,7 +209,7 @@ test("real runtime enforces filesystem, environment, network, argv, and exit bou "require('fs').writeFileSync('../sibling/.npmrc','changed')", ]), 0); assert.equal(await readFile(path.join(credentialSibling, ".npmrc"), "utf8"), "sibling-token"); - assert.notEqual(await run(DEVELOP, nestedWorkspace, [ + assert.equal(await run(DEVELOP, nestedWorkspace, [ process.execPath, "-e", "require('fs').writeFileSync('../sibling/.ssh/id_test','changed')", diff --git a/tests/sandbox-runtime.test.mjs b/tests/sandbox-runtime.test.mjs index ac740cf..73ef917 100644 --- a/tests/sandbox-runtime.test.mjs +++ b/tests/sandbox-runtime.test.mjs @@ -20,6 +20,7 @@ import { buildChildEnvironment, commandToShellString, compileSandboxExecution, + enableMacOSInterpreterExecution, assertNoExternalHardlinks, existingSystemReadRoots, probeSandboxRuntime, @@ -312,6 +313,34 @@ function fakeSpawn(exitCode, onSpawn) { }; } +function fakeMacSandboxDescriptor() { + return { + argv: [ + "/bin/sh", + "-c", + "sandbox-exec -p '(allow process-exec)\n(allow process-fork)' /bin/sh -c true", + ], + env: {}, + }; +} + +test("macOS descriptor shim enables shebang interpreters and fails closed on upstream drift", () => { + const original = fakeMacSandboxDescriptor(); + const patched = enableMacOSInterpreterExecution(original, "darwin"); + assert.notEqual(patched, original); + assert.match(patched.argv[2], /\(allow process-exec-interpreter\)/); + assert.doesNotMatch(original.argv[2], /process-exec-interpreter/); + assert.equal(enableMacOSInterpreterExecution(original, "linux"), original); + assert.throws( + () => enableMacOSInterpreterExecution({ argv: ["/bin/sh", "-c", "changed"] }, "darwin"), + /process rules no longer match/, + ); + assert.throws( + () => enableMacOSInterpreterExecution({ argv: ["unexpected"] }, "darwin"), + /unexpected command descriptor/, + ); +}); + test("runner initializes before spawn, uses shell false, propagates exit codes, and resets once", async () => { let initialized; let resetCount = 0; @@ -385,7 +414,7 @@ test("runner canonicalizes a symlinked temporary root before compiling private p const manager = { isSupportedPlatform: () => true, initialize: async () => {}, - wrapWithSandboxArgv: async () => ({ argv: ["/fake/sandbox"], env: {} }), + wrapWithSandboxArgv: async () => fakeMacSandboxDescriptor(), reset: async () => {}, }; @@ -541,7 +570,7 @@ test("runner exposes an env shebang interpreter and its bounded runtime", async const manager = { isSupportedPlatform: () => true, initialize: async (config) => { runtimeConfig = config; }, - wrapWithSandboxArgv: async () => ({ argv: ["/fake/sandbox"], env: {} }), + wrapWithSandboxArgv: async () => fakeMacSandboxDescriptor(), reset: async () => {}, }; try { @@ -698,7 +727,7 @@ test("runner discovers Git roots from nested directories and validates linked wo initialize: async (config) => { runtimeConfig = config; }, wrapWithSandboxArgv: async (_command, _shell, _a, _b, cwd) => { wrappedCwd = cwd; - return { argv: ["/fake/sandbox"], env: {} }; + return fakeMacSandboxDescriptor(); }, reset: async () => {}, };