Files
codex/codex-rs/exec-server/src/no_follow/mod.rs
T
pakrym-oai e3e5ad2847 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
2026-08-20 08:10:08 +00:00

61 lines
1.7 KiB
Rust

use crate::FileMetadata;
use std::io;
use std::path::Path;
#[cfg(windows)]
use std::time::SystemTime;
#[cfg(windows)]
use std::time::UNIX_EPOCH;
#[cfg(unix)]
mod unix;
#[cfg(windows)]
mod windows;
#[cfg(unix)]
use unix as imp;
#[cfg(windows)]
use windows as imp;
pub(crate) async fn open_file(path: &Path) -> io::Result<tokio::fs::File> {
imp::open_file(path.to_path_buf()).await
}
pub(crate) async fn write_file(path: &Path, contents: Vec<u8>) -> io::Result<()> {
imp::write_file(path.to_path_buf(), contents).await
}
#[cfg(unix)]
pub(crate) async fn metadata(path: &Path) -> io::Result<FileMetadata> {
imp::metadata(path.to_path_buf()).await
}
#[cfg(windows)]
pub(crate) async fn metadata(path: &Path) -> io::Result<FileMetadata> {
imp::metadata(path.to_path_buf())
.await
.map(|metadata| FileMetadata {
is_directory: metadata.is_dir(),
is_file: metadata.is_file(),
is_symlink: false,
size: metadata.len(),
created_at_ms: metadata.created().ok().map_or(0, system_time_to_unix_ms),
modified_at_ms: metadata.modified().ok().map_or(0, system_time_to_unix_ms),
})
}
#[cfg(windows)]
fn system_time_to_unix_ms(time: SystemTime) -> i64 {
time.duration_since(UNIX_EPOCH)
.ok()
.and_then(|duration| i64::try_from(duration.as_millis()).ok())
.unwrap_or(0)
}
pub(crate) async fn create_directory(path: &Path, recursive: bool) -> io::Result<()> {
imp::create_directory(path.to_path_buf(), recursive).await
}
pub(crate) async fn remove(path: &Path, recursive: bool, force: bool) -> io::Result<()> {
imp::remove(path.to_path_buf(), recursive, force).await
}