Harden unsandboxed patch filesystem access (#39659)

## Why

An `apply_patch` path can be replaced with a symlink after verification, allowing an unsandboxed patch operation to reach a different file than the one that was approved.

## What changed

- Add `follow_symlinks` options to executor filesystem reads, writes, metadata lookups, directory creation, and removal, including the corresponding `followSymlinks` protocol fields.
- Implement no-follow filesystem operations on Unix and Windows that reject links in any path component and restrict file access to regular files.
- Run `apply_patch` with symlink traversal disabled when an otherwise-required sandbox is bypassed, while retaining the existing follow-symlink default for standalone callers.

## Testing

- Cover leaf and ancestor symlinks across patch add, update, delete, and move operations, including a path swap after verification.
- Exercise local and remote no-follow filesystem behavior, concurrent directory creation, special-file rejection, and Windows reparse points.

GitOrigin-RevId: 43fd479084891493ce13564fbd894b98f329c6dd
This commit is contained in:
pakrym-oai authored and copyberry committed 2026-08-20 08:10:08 +00:00
1 parent 4e1a772a7d
commit e3e5ad2847
88 files changed
+2782 -302

No files matched your search

@@ -16,6 +16,7 @@ use crate::tools::sandboxing::ToolRuntime;
use crate::tools::sandboxing::executor_windows_sandbox_level;
use codex_apply_patch::AppliedPatchDelta;
use codex_apply_patch::ApplyPatchAction;
use codex_apply_patch::ApplyPatchOptions;
use codex_exec_server::FileSystemSandboxContext;
use codex_protocol::error::CodexErr;
use codex_protocol::error::SandboxErr;
@@ -173,9 +174,19 @@ impl ToolRuntime<ApplyPatchRequest, ApplyPatchRuntimeOutput> for ApplyPatchRunti
let sandbox = Self::file_system_sandbox_context_for_attempt(req, attempt);
let mut stdout = Vec::new();
let mut stderr = Vec::new();
let result = codex_apply_patch::apply_patch_with_mode(
let result = codex_apply_patch::apply_patch_with_options(
&req.action.patch,
req.action.update_file_mode(),
ApplyPatchOptions {
update_file_mode: req.action.update_file_mode(),
// Only reject links when an otherwise-required sandbox was bypassed.
// Executor-managed sandboxes can have SandboxType::None.
follow_symlinks: attempt.sandbox_requested
|| !attempt.manager.should_sandbox(
attempt.permissions,
self.sandbox_preference(),
attempt.enforce_managed_network,
),
},
&req.action.cwd,
&mut stdout,
&mut stderr,