Skip to content

fix: realpathSafe to safely resolve symlinks for seatbelt #378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions codex-cli/src/utils/agent/sandbox/macos-seatbelt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,18 @@ import type { SpawnOptions } from "child_process";

import { exec } from "./raw-exec.js";
import { log } from "../log.js";
import { realpath } from "fs/promises";
import { CONFIG_DIR } from "src/utils/config.js";

async function realpathSafe(path: string): Promise<string> {
try {
return realpath(path);
} catch (err) {
// fallback to the literal path if file not found or other exception
return path;
}
}

function getCommonRoots() {
return [
CONFIG_DIR,
Expand All @@ -14,7 +24,7 @@ function getCommonRoots() {
];
}

export function execWithSeatbelt(
export async function execWithSeatbelt(
cmd: Array<string>,
opts: SpawnOptions,
writableRoots: Array<string>,
Expand All @@ -25,8 +35,11 @@ export function execWithSeatbelt(
if (writableRoots.length > 0) {
// Add `~/.codex` to the list of writable roots
// (if there's any already, not in read-only mode)
getCommonRoots().map((root) => writableRoots.push(root));
const { policies, params } = writableRoots
writableRoots.push(...getCommonRoots());

const resolvedRoots = await Promise.all(writableRoots.map(realpathSafe));

const { policies, params } = resolvedRoots
.map((root, index) => ({
policy: `(subpath (param "WRITABLE_ROOT_${index}"))`,
param: `-DWRITABLE_ROOT_${index}=${root}`,
Expand Down