diff --git a/bin/sandbox/runtime.mjs b/bin/sandbox/runtime.mjs index 88254b2..f5b80a2 100644 --- a/bin/sandbox/runtime.mjs +++ b/bin/sandbox/runtime.mjs @@ -1278,13 +1278,14 @@ async function runSandboxedCommandOnce(options, context = {}) { const runtimeSignalSource = context.runtimeSignalSource || process; const cleanupListenerSnapshot = snapshotRuntimeCleanupListeners(runtimeSignalSource); try { - await manager.initialize(runtimeConfig); + await manager.initialize(runtimeConfig, undefined, platform === "darwin"); } finally { removeAddedRuntimeCleanupListeners(runtimeSignalSource, cleanupListenerSnapshot); } const previousTemp = process.env.CLAUDE_CODE_TMPDIR; process.env.CLAUDE_CODE_TMPDIR = path.join(runRoot, "tmp"); + const commandId = randomUUID(); let descriptor; try { descriptor = await manager.wrapWithSandboxArgv( @@ -1293,7 +1294,7 @@ async function runSandboxedCommandOnce(options, context = {}) { undefined, undefined, executionCwd, - { commandId: randomUUID(), commandText: options.command[0] }, + { commandId, commandText: options.command[0] }, ); descriptor = enableMacOSInterpreterExecution(descriptor, platform); } finally { @@ -1312,6 +1313,13 @@ async function runSandboxedCommandOnce(options, context = {}) { killProcess: context.killProcess, signalGraceMs: context.signalGraceMs, }); + if (platform === "darwin" && childResult.code !== 0 && manager.getSandboxViolationStore) { + await new Promise((resolve) => setTimeout(resolve, 200)); + const violations = manager.getSandboxViolationStore().getViolationsForCommand(commandId); + for (const violation of violations) { + context.writeError?.(`Sandbox violation: ${violation.line}\n`); + } + } } catch (error) { operationError = error; } diff --git a/tests/sandbox-runtime.integration.test.mjs b/tests/sandbox-runtime.integration.test.mjs index fcef373..9cf5096 100644 --- a/tests/sandbox-runtime.integration.test.mjs +++ b/tests/sandbox-runtime.integration.test.mjs @@ -61,6 +61,7 @@ async function run(profileValue, workspace, command, overrides = {}) { ...contextEnvironment, OPP_INTEGRATION_SECRET: "must-not-leak", }, + writeError: process.platform === "darwin" ? (message) => process.stderr.write(message) : undefined, }); } @@ -323,11 +324,13 @@ test("real runtime enforces filesystem, environment, network, argv, and exit bou assert.equal(networkDenied, 7); const sharedTemporaryProbe = `/tmp/claude/opp-${process.pid}-probe`; - assert.notEqual(await run(OBSERVE, workspace, [ + const sharedTemporaryExit = await run(OBSERVE, workspace, [ process.execPath, "-e", `require('fs').mkdirSync('/tmp/claude',{recursive:true});require('fs').writeFileSync(${JSON.stringify(sharedTemporaryProbe)},'bad')`, - ]), 0); + ]); + if (process.platform === "linux") assert.equal(sharedTemporaryExit, 0); + else assert.notEqual(sharedTemporaryExit, 0); await assert.rejects(stat(sharedTemporaryProbe), /ENOENT/); let requests = []; diff --git a/tests/sandbox-runtime.test.mjs b/tests/sandbox-runtime.test.mjs index 73ef917..3a8c2a5 100644 --- a/tests/sandbox-runtime.test.mjs +++ b/tests/sandbox-runtime.test.mjs @@ -402,6 +402,55 @@ test("runner initializes before spawn, uses shell false, propagates exit codes, assert.doesNotMatch(diagnostics, /GH_TOKEN|secret|literal/); }); +test("macOS runner enables correlated violation diagnostics for failed commands", async () => { + const fixture = await mkdtemp(path.join(os.tmpdir(), "opp-macos-diagnostic-")); + const workspace = path.join(fixture, "workspace"); + await mkdir(workspace); + let monitorEnabled; + let commandId; + let diagnostics = ""; + const manager = { + isSupportedPlatform: () => true, + initialize: async (_config, _callback, enabled) => { monitorEnabled = enabled; }, + wrapWithSandboxArgv: async (_command, _shell, _a, _b, _cwd, options) => { + commandId = options.commandId; + return fakeMacSandboxDescriptor(); + }, + getSandboxViolationStore: () => ({ + getViolationsForCommand: (id) => id === commandId + ? [{ line: "deny process-exec-interpreter /bin/sh" }] + : [], + }), + reset: async () => {}, + }; + try { + assert.equal(await runSandboxedCommand({ + profile: profile("observe"), + allowedDomains: [], + allowedEnvironment: [], + confirmExternalSideEffects: false, + command: ["true"], + }, { + cwd: workspace, + home: os.homedir(), + temporaryRoot: fixture, + env: { PATH: "/usr/bin:/bin" }, + platform: "darwin", + loadRuntime: async () => ({ + SandboxManager: manager, + SandboxRuntimeConfigSchema: { parse: (config) => config }, + }), + spawn: fakeSpawn(126), + writeError: (message) => { diagnostics += message; }, + }), 126); + assert.equal(monitorEnabled, true); + assert.match(commandId, /^[0-9a-f-]{36}$/); + assert.match(diagnostics, /Sandbox violation: deny process-exec-interpreter \/bin\/sh/); + } finally { + await rm(fixture, { recursive: true, force: true }); + } +}); + test("runner canonicalizes a symlinked temporary root before compiling private paths", async () => { const fixture = await mkdtemp(path.join(os.tmpdir(), "opp-canonical-temp-")); const actualTemporaryRoot = path.join(fixture, "actual-temp");