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
+1
View File
@@ -12,6 +12,7 @@ use std::path::PathBuf;
use ts_rs::TS;
mod absolutize;
mod system_aliases;
/// A path that is guaranteed to be absolute and normalized (though it is not
/// guaranteed to be canonicalized or exist on the filesystem).
@@ -0,0 +1,35 @@
//! Normalize aliases rooted directly in the trusted filesystem root.
//! Never resolve mutable components below that root, including missing descendants.
use crate::AbsolutePathBuf;
use std::io;
use std::path::Path;
impl AbsolutePathBuf {
/// Resolve a top-level system alias, such as macOS `/tmp -> /private/tmp`.
/// All remaining components keep their logical spelling, even if they are
/// symlinks or do not exist. Only call this on the host that owns the path.
pub fn normalize_system_aliases(&self) -> io::Result<Self> {
let Some(top_level) = self.as_path().ancestors().find(|ancestor| {
ancestor.parent().is_some() && ancestor.parent().and_then(Path::parent).is_none()
}) else {
return Ok(self.clone());
};
match std::fs::symlink_metadata(top_level) {
Ok(metadata) if metadata.file_type().is_symlink() => {}
Ok(_) => return Ok(self.clone()),
Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(self.clone()),
Err(error) => return Err(error),
}
let canonical_top_level = dunce::canonicalize(top_level)?;
let suffix = self
.as_path()
.strip_prefix(top_level)
.map_err(io::Error::other)?;
Self::from_absolute_path(canonical_top_level.join(suffix))
}
}
#[cfg(test)]
#[path = "system_aliases_tests.rs"]
mod tests;
@@ -0,0 +1,29 @@
use crate::AbsolutePathBuf;
use pretty_assertions::assert_eq;
#[cfg(target_os = "macos")]
#[test]
fn system_aliases_preserve_mutable_symlinks_and_missing_descendants() -> std::io::Result<()> {
let directory = tempfile::tempdir_in("/tmp")?;
let outside = tempfile::tempdir()?;
let logical = AbsolutePathBuf::from_absolute_path(directory.path())?;
let physical = logical.canonicalize()?;
std::os::unix::fs::symlink(outside.path(), logical.join("mutable"))?;
for suffix in ["", "missing/child", "mutable", "mutable/missing/child"] {
assert_eq!(
logical.join(suffix).normalize_system_aliases()?,
physical.join(suffix),
);
}
Ok(())
}
#[test]
fn ordinary_paths_keep_missing_descendants() -> std::io::Result<()> {
let directory = tempfile::tempdir()?;
let physical = AbsolutePathBuf::from_absolute_path(directory.path())?.canonicalize()?;
let missing = physical.join("missing/child");
assert_eq!(missing.normalize_system_aliases()?, missing);
Ok(())
}