Make native sandbox checks work across macOS temporary paths and Linux filtered network proxies

Add a narrow Seatbelt metadata grant for the /var symlink and force the loopback integration fixture through the runtime proxy.
This commit is contained in:
Wayner Barrios
2026-08-17 00:48:28 -05:00
parent f6677eec96
commit 932bd0314d
4 changed files with 60 additions and 15 deletions
+18 -6
View File
@@ -27,6 +27,15 @@ const MACOS_INTERPRETER_PROCESS_RULES = [
"(allow process-exec-interpreter)",
"(allow process-fork)",
].join("\n");
const MACOS_DIRECTORY_METADATA_RULE = [
"(allow file-read-metadata",
" (vnode-type DIRECTORY))",
].join("\n");
const MACOS_TEMP_SYMLINK_METADATA_RULE = [
MACOS_DIRECTORY_METADATA_RULE,
"(allow file-read-metadata",
" (literal \"/var\"))",
].join("\n");
const SUPPORTED_PLATFORMS = new Set(["linux", "darwin"]);
const READ_ONLY_METHODS = new Set(["GET", "HEAD", "OPTIONS"]);
@@ -102,7 +111,7 @@ export function commandToShellString(command) {
return command.map(quotePosixArgument).join(" ");
}
export function enableMacOSInterpreterExecution(descriptor, platform) {
export function applyMacOSCompatibilityProfile(descriptor, platform) {
if (platform !== "darwin") return descriptor;
if (
!descriptor
@@ -113,16 +122,19 @@ export function enableMacOSInterpreterExecution(descriptor, platform) {
) {
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.");
const profile = descriptor.argv[2];
const anchors = [MACOS_PROCESS_RULES, MACOS_DIRECTORY_METADATA_RULE];
if (anchors.some((anchor) => profile.split(anchor).length - 1 !== 1)) {
throw new Error("Pinned macOS sandbox runtime 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),
profile
.replace(MACOS_PROCESS_RULES, MACOS_INTERPRETER_PROCESS_RULES)
.replace(MACOS_DIRECTORY_METADATA_RULE, MACOS_TEMP_SYMLINK_METADATA_RULE),
],
};
}
@@ -1296,7 +1308,7 @@ async function runSandboxedCommandOnce(options, context = {}) {
executionCwd,
{ commandId, commandText: options.command[0] },
);
descriptor = enableMacOSInterpreterExecution(descriptor, platform);
descriptor = applyMacOSCompatibilityProfile(descriptor, platform);
} finally {
if (previousTemp === undefined) delete process.env.CLAUDE_CODE_TMPDIR;
else process.env.CLAUDE_CODE_TMPDIR = previousTemp;
+1 -1
View File
@@ -83,7 +83,7 @@ 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.
The pinned native runtime needs two narrow macOS Seatbelt compatibility rules. It omits the separate `process-exec-interpreter` permission even though it permits native executable files, and its directory-only metadata rule cannot traverse the `/var` symlink used in temporary command paths. The adapter adds the interpreter operation and metadata access to the literal `/var` symlink after verifying the pinned descriptor shape. It does not expose the `/var` subtree and fails closed if the upstream profile changes. Filesystem read rules still decide which scripts, interpreters, and canonical temporary paths are visible. The compatibility shim should be reduced or removed when a verified upstream release covers these cases 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.
@@ -346,13 +346,25 @@ test("real runtime enforces filesystem, environment, network, argv, and exit bou
assert.ok(address && typeof address !== "string");
const destination = `127.0.0.1:${address.port}`;
try {
// The upstream proxy excludes loopback by default. Remove that client-side
// bypass so this host-side fixture exercises the filtered proxy path.
assert.equal(await run(NETWORK_READ, workspace, [
"env",
"-u",
"NO_PROXY",
"-u",
"no_proxy",
"curl",
"--fail",
"--silent",
`http://${destination}/read`,
], { allowedDomains: [destination] }), 0);
assert.notEqual(await run(NETWORK_READ, workspace, [
"env",
"-u",
"NO_PROXY",
"-u",
"no_proxy",
"curl",
"--fail",
"--silent",
+29 -8
View File
@@ -20,7 +20,7 @@ import {
buildChildEnvironment,
commandToShellString,
compileSandboxExecution,
enableMacOSInterpreterExecution,
applyMacOSCompatibilityProfile,
assertNoExternalHardlinks,
existingSystemReadRoots,
probeSandboxRuntime,
@@ -318,25 +318,46 @@ function fakeMacSandboxDescriptor() {
argv: [
"/bin/sh",
"-c",
"sandbox-exec -p '(allow process-exec)\n(allow process-fork)' /bin/sh -c true",
"sandbox-exec -p '(allow file-read-metadata\n (vnode-type DIRECTORY))\n(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", () => {
test("macOS descriptor shim enables scripts and raw temp paths, then fails closed on drift", () => {
const original = fakeMacSandboxDescriptor();
const patched = enableMacOSInterpreterExecution(original, "darwin");
const patched = applyMacOSCompatibilityProfile(original, "darwin");
assert.notEqual(patched, original);
assert.match(patched.argv[2], /\(allow process-exec-interpreter\)/);
assert.match(
patched.argv[2],
/\(vnode-type DIRECTORY\)\)\n\(allow file-read-metadata\n \(literal "\/var"\)\)/,
);
assert.doesNotMatch(patched.argv[2], /\(subpath "\/var"\)/);
assert.doesNotMatch(original.argv[2], /process-exec-interpreter/);
assert.equal(enableMacOSInterpreterExecution(original, "linux"), original);
assert.equal(applyMacOSCompatibilityProfile(original, "linux"), original);
assert.throws(
() => enableMacOSInterpreterExecution({ argv: ["/bin/sh", "-c", "changed"] }, "darwin"),
/process rules no longer match/,
() => applyMacOSCompatibilityProfile({
argv: [
"/bin/sh",
"-c",
"(allow file-read-metadata\n (vnode-type DIRECTORY))\nchanged",
],
}, "darwin"),
/rules no longer match/,
);
assert.throws(
() => enableMacOSInterpreterExecution({ argv: ["unexpected"] }, "darwin"),
() => applyMacOSCompatibilityProfile({
argv: [
"/bin/sh",
"-c",
"(allow process-exec)\n(allow process-fork)",
],
}, "darwin"),
/rules no longer match/,
);
assert.throws(
() => applyMacOSCompatibilityProfile({ argv: ["unexpected"] }, "darwin"),
/unexpected command descriptor/,
);
});