Report native sandbox violations and verify disposable temporary mounts

Enable correlated macOS Seatbelt diagnostics for failed commands so runtime denials identify the exact blocked operation while preserving exit codes and cleanup. Update the Linux shared temporary-path integration assertion to verify its isolated tmpfs behavior and confirm that no host file is created.
This commit is contained in:
Wayner Barrios
2026-08-17 00:33:11 -05:00
parent f45f7cfc3d
commit f6677eec96
3 changed files with 64 additions and 4 deletions
+10 -2
View File
@@ -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;
}
+5 -2
View File
@@ -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 = [];
+49
View File
@@ -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");