Fix macOS system-alias matching in patch permission checks (#47879)

## Why

Local `apply_patch` permission checks can treat a macOS system alias and its canonical spelling as different locations, sending writes covered by temporary-directory grants for unnecessary approval.

## What changed

- Normalize trusted top-level aliases in local policy roots and patch targets, reusing a prepared matcher for permission requests and patch safety checks.
- Share alias normalization with Seatbelt and the macOS executor sandbox while preserving mutable symlinks and missing descendants.
- Preserve read-only and deny precedence, protected metadata restrictions, and remote URI matching. Propagate normalization failures from fallible permission checks.

## Testing

Add macOS regressions for multi-file patches with missing parent directories through both direct `apply_patch` calls and shell interception, asserting automatic approval and file contents. Add unit coverage for alias restriction precedence, mutable symlink preservation, missing descendants, and remote permission isolation.

GitOrigin-RevId: 0822ff951eaab728cb1ad91281745fa1b8809f15
This commit is contained in:
felixxia-oai
2026-09-24 16:36:57 +00:00
committed by copyberry
parent 4021746a15
commit 6f51c65958
17 changed files with 792 additions and 304 deletions
+7 -10
View File
@@ -1,5 +1,5 @@
use crate::function_tool::FunctionCallError;
use crate::safety::PatchSandboxRoute;
use crate::safety::PatchPolicyMatcher;
use crate::safety::SafetyCheck;
use crate::safety::assess_patch_safety;
use crate::session::step_context::StepContext;
@@ -7,9 +7,7 @@ use crate::session::turn_context::TurnEnvironment;
use crate::tools::sandboxing::ExecApprovalRequirement;
use codex_apply_patch::ApplyPatchAction;
use codex_apply_patch::ApplyPatchFileChange;
use codex_protocol::permissions::FileSystemSandboxPolicyContext;
use codex_protocol::protocol::FileChange;
use codex_protocol::protocol::FileSystemSandboxPolicy;
use codex_utils_path_uri::PathUri;
use std::collections::HashMap;
use std::path::PathBuf;
@@ -24,19 +22,18 @@ pub(crate) struct ApplyPatchRuntimeInvocation {
pub(crate) fn prepare_apply_patch(
step_context: &StepContext,
turn_environment: &TurnEnvironment,
file_system_sandbox_policy: &FileSystemSandboxPolicy,
context: &FileSystemSandboxPolicyContext<'_>,
sandbox_route: PatchSandboxRoute,
matching: &PatchPolicyMatcher<'_>,
action: ApplyPatchAction,
) -> Result<ApplyPatchRuntimeInvocation, FunctionCallError> {
match assess_patch_safety(
&action,
step_context.settings.approval_policy(),
turn_environment.permission_profile(),
file_system_sandbox_policy,
context,
sandbox_route,
) {
matching,
)
.map_err(|error| {
FunctionCallError::RespondToModel(format!("failed to check patch permissions: {error}"))
})? {
SafetyCheck::AutoApprove => Ok(ApplyPatchRuntimeInvocation {
action,
auto_approved: true,