fix(sandbox): preserve symlinked project config

Co-authored-by: Codex noreply@openai.com
This commit is contained in:
viyatb-oai
2026-06-08 22:19:01 -07:00
co-authored by Codex noreply@openai.com
parent fa73ec83f3
commit b32bd8d514
20 changed files with 171 additions and 495 deletions
-3
View File
@@ -12,9 +12,6 @@ codex-utils-absolute-path = { workspace = true }
dunce = { workspace = true }
tempfile = { workspace = true }
[target.'cfg(unix)'.dependencies]
libc = { workspace = true }
[dev-dependencies]
pretty_assertions = { workspace = true }
tempfile = { workspace = true }
-83
View File
@@ -5,19 +5,7 @@ pub use env::is_wsl;
use codex_utils_absolute_path::AbsolutePathBuf;
use std::collections::HashSet;
#[cfg(unix)]
use std::ffi::CString;
#[cfg(unix)]
use std::fs::File;
use std::io;
#[cfg(unix)]
use std::os::fd::AsRawFd;
#[cfg(unix)]
use std::os::fd::FromRawFd;
#[cfg(unix)]
use std::os::unix::ffi::OsStrExt;
#[cfg(unix)]
use std::path::Component;
use std::path::Path;
use std::path::PathBuf;
use tempfile::NamedTempFile;
@@ -144,77 +132,6 @@ pub fn write_atomically(write_path: &Path, contents: &str) -> io::Result<()> {
Ok(())
}
/// Opens a file for reading without following symlinks in any path component.
///
/// Relative paths are resolved from the process's current directory.
#[cfg(unix)]
pub fn open_file_for_read_no_follow(path: &Path) -> io::Result<File> {
let components = path
.components()
.filter_map(|component| match component {
Component::RootDir | Component::CurDir => None,
Component::ParentDir => Some(Ok(std::ffi::OsStr::new(".."))),
Component::Normal(component) => Some(Ok(component)),
Component::Prefix(_) => Some(Err(io::Error::new(
io::ErrorKind::InvalidInput,
format!("unsupported path prefix in {}", path.display()),
))),
})
.collect::<io::Result<Vec<_>>>()?;
let Some((file_name, parent_components)) = components.split_last() else {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
format!("path {} does not name a file", path.display()),
));
};
let mut current_dir = if path.is_absolute() {
File::open("/")?
} else {
File::open(".")?
};
for component in parent_components {
current_dir = openat(
&current_dir,
component,
libc::O_RDONLY | libc::O_CLOEXEC | libc::O_DIRECTORY | libc::O_NOFOLLOW,
/*mode*/ 0,
)?;
}
openat(
&current_dir,
file_name,
libc::O_RDONLY | libc::O_CLOEXEC | libc::O_NOFOLLOW,
/*mode*/ 0,
)
}
#[cfg(unix)]
fn openat(
directory: &File,
path: &std::ffi::OsStr,
flags: libc::c_int,
mode: libc::mode_t,
) -> io::Result<File> {
let path = CString::new(path.as_bytes())
.map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?;
// SAFETY: `path` is NUL-terminated, `directory` owns a valid fd, and the
// returned fd is transferred into a `File` exactly once.
let fd = unsafe {
libc::openat(
directory.as_raw_fd(),
path.as_ptr(),
flags,
libc::c_uint::from(mode),
)
};
if fd == -1 {
return Err(io::Error::last_os_error());
}
// SAFETY: `openat` returned a new owned file descriptor.
Ok(unsafe { File::from_raw_fd(fd) })
}
fn normalize_for_wsl(path: PathBuf) -> PathBuf {
normalize_for_wsl_with_flag(path, env::is_wsl())
}
@@ -1,9 +1,7 @@
#[cfg(unix)]
mod symlinks {
use super::super::open_file_for_read_no_follow;
use super::super::resolve_symlink_write_paths;
use pretty_assertions::assert_eq;
use std::io::Read;
use std::os::unix::fs::symlink;
#[test]
@@ -21,53 +19,6 @@ mod symlinks {
assert_eq!(resolved.write_path, a);
Ok(())
}
#[test]
fn no_follow_read_rejects_symlinked_parent_directory() -> std::io::Result<()> {
let dir = tempfile::tempdir()?;
let dir = dir.path().canonicalize()?;
let target_dir = dir.join("target");
let symlinked_dir = dir.join("link");
std::fs::create_dir(&target_dir)?;
std::fs::write(target_dir.join("payload"), "secret")?;
symlink(&target_dir, &symlinked_dir)?;
let err = open_file_for_read_no_follow(&symlinked_dir.join("payload"))
.expect_err("symlinked parent should fail");
assert!(err.raw_os_error().is_some());
Ok(())
}
#[test]
fn no_follow_read_rejects_symlinked_file() -> std::io::Result<()> {
let dir = tempfile::tempdir()?;
let dir = dir.path().canonicalize()?;
let target = dir.join("target");
let symlink_path = dir.join("link");
std::fs::write(&target, "secret")?;
symlink(&target, &symlink_path)?;
let err =
open_file_for_read_no_follow(&symlink_path).expect_err("symlinked file should fail");
assert_eq!(err.raw_os_error(), Some(libc::ELOOP));
Ok(())
}
#[test]
fn no_follow_read_reads_regular_file() -> std::io::Result<()> {
let dir = tempfile::tempdir()?;
let path = dir.path().canonicalize()?.join("payload");
std::fs::write(&path, "hello")?;
let mut reader = open_file_for_read_no_follow(&path)?;
let mut contents = String::new();
reader.read_to_string(&mut contents)?;
assert_eq!(contents, "hello");
Ok(())
}
}
#[cfg(target_os = "linux")]