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
-1
View File
@@ -24,7 +24,6 @@ codex-protocol = { workspace = true }
codex-sandboxing = { workspace = true }
codex-shell-command = { workspace = true }
codex-utils-absolute-path = { workspace = true }
codex-utils-path = { workspace = true }
codex-utils-pty = { workspace = true }
codex-utils-rustls-provider = { workspace = true }
futures = { workspace = true }
@@ -236,14 +236,6 @@ mod tests {
Ok(b"skill contents".to_vec())
}
async fn read_file_no_follow(
&self,
path: &AbsolutePathBuf,
sandbox: Option<&FileSystemSandboxContext>,
) -> io::Result<Vec<u8>> {
self.read_file(path, sandbox).await
}
async fn write_file(
&self,
_path: &AbsolutePathBuf,
+4 -8
View File
@@ -189,14 +189,10 @@ pub(crate) async fn run_direct_request(
let file_system = DirectFileSystem;
match request {
FsHelperRequest::ReadFile(params) => {
let data = if params.no_follow_symlinks {
file_system
.read_file_no_follow(&params.path, /*sandbox*/ None)
.await
} else {
file_system.read_file(&params.path, /*sandbox*/ None).await
}
.map_err(map_fs_error)?;
let data = file_system
.read_file(&params.path, /*sandbox*/ None)
.await
.map_err(map_fs_error)?;
Ok(FsHelperPayload::ReadFile(FsReadFileResponse {
data_base64: STANDARD.encode(data),
}))
@@ -7,8 +7,6 @@ use std::sync::LazyLock;
use std::time::SystemTime;
use std::time::UNIX_EPOCH;
use tokio::io;
#[cfg(unix)]
use tokio::io::AsyncReadExt;
use crate::CopyOptions;
use crate::CreateDirectoryOptions;
@@ -111,15 +109,6 @@ impl ExecutorFileSystem for LocalFileSystem {
file_system.read_file(path, sandbox).await
}
async fn read_file_no_follow(
&self,
path: &AbsolutePathBuf,
sandbox: Option<&FileSystemSandboxContext>,
) -> FileSystemResult<Vec<u8>> {
let (file_system, sandbox) = self.file_system_for(sandbox)?;
file_system.read_file_no_follow(path, sandbox).await
}
async fn write_file(
&self,
path: &AbsolutePathBuf,
@@ -214,17 +203,6 @@ impl ExecutorFileSystem for UnsandboxedFileSystem {
self.file_system.read_file(path, /*sandbox*/ None).await
}
async fn read_file_no_follow(
&self,
path: &AbsolutePathBuf,
sandbox: Option<&FileSystemSandboxContext>,
) -> FileSystemResult<Vec<u8>> {
reject_platform_sandbox_context(sandbox)?;
self.file_system
.read_file_no_follow(path, /*sandbox*/ None)
.await
}
async fn write_file(
&self,
path: &AbsolutePathBuf,
@@ -339,33 +317,6 @@ impl ExecutorFileSystem for DirectFileSystem {
tokio::fs::read(path.as_path()).await
}
async fn read_file_no_follow(
&self,
path: &AbsolutePathBuf,
sandbox: Option<&FileSystemSandboxContext>,
) -> FileSystemResult<Vec<u8>> {
reject_sandbox_context(sandbox)?;
#[cfg(unix)]
{
let file = codex_utils_path::open_file_for_read_no_follow(path.as_path())?;
let metadata = file.metadata()?;
if metadata.len() > MAX_READ_FILE_BYTES {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
format!("file is too large to read: limit is {MAX_READ_FILE_BYTES} bytes"),
));
}
let mut file = tokio::fs::File::from_std(file);
let mut contents = Vec::new();
file.read_to_end(&mut contents).await?;
Ok(contents)
}
#[cfg(not(unix))]
{
self.read_file(path, /*sandbox*/ None).await
}
}
async fn write_file(
&self,
path: &AbsolutePathBuf,
-3
View File
@@ -183,9 +183,6 @@ pub struct TerminateResponse {
pub struct FsReadFileParams {
pub path: AbsolutePathBuf,
pub sandbox: Option<FileSystemSandboxContext>,
/// Reject symlinks in every path component on Unix executors.
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub no_follow_symlinks: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
@@ -98,30 +98,6 @@ impl ExecutorFileSystem for RemoteFileSystem {
.fs_read_file(FsReadFileParams {
path: path.clone(),
sandbox: remote_sandbox_context(sandbox),
no_follow_symlinks: false,
})
.await
.map_err(map_remote_error)?;
STANDARD.decode(response.data_base64).map_err(|err| {
io::Error::new(
io::ErrorKind::InvalidData,
format!("remote fs/readFile returned invalid base64 dataBase64: {err}"),
)
})
}
async fn read_file_no_follow(
&self,
path: &AbsolutePathBuf,
sandbox: Option<&FileSystemSandboxContext>,
) -> FileSystemResult<Vec<u8>> {
trace!("remote fs read_file_no_follow");
let client = self.client.get().await.map_err(map_remote_error)?;
let response = client
.fs_read_file(FsReadFileParams {
path: path.clone(),
sandbox: remote_sandbox_context(sandbox),
no_follow_symlinks: true,
})
.await
.map_err(map_remote_error)?;
@@ -97,33 +97,6 @@ impl ExecutorFileSystem for SandboxedFileSystem {
FsHelperRequest::ReadFile(FsReadFileParams {
path: path.clone(),
sandbox: None,
no_follow_symlinks: false,
}),
)
.await?
.expect_read_file()
.map_err(map_sandbox_error)?;
STANDARD.decode(response.data_base64).map_err(|err| {
io::Error::new(
io::ErrorKind::InvalidData,
format!("fs/readFile returned invalid base64 dataBase64: {err}"),
)
})
}
async fn read_file_no_follow(
&self,
path: &AbsolutePathBuf,
sandbox: Option<&FileSystemSandboxContext>,
) -> FileSystemResult<Vec<u8>> {
let sandbox = require_platform_sandbox(sandbox)?;
let response = self
.run_sandboxed(
sandbox,
FsHelperRequest::ReadFile(FsReadFileParams {
path: path.clone(),
sandbox: None,
no_follow_symlinks: true,
}),
)
.await?
@@ -52,16 +52,11 @@ impl FileSystemHandler {
&self,
params: FsReadFileParams,
) -> Result<FsReadFileResponse, JSONRPCErrorError> {
let bytes = if params.no_follow_symlinks {
self.file_system
.read_file_no_follow(&params.path, params.sandbox.as_ref())
.await
} else {
self.file_system
.read_file(&params.path, params.sandbox.as_ref())
.await
}
.map_err(map_fs_error)?;
let bytes = self
.file_system
.read_file(&params.path, params.sandbox.as_ref())
.await
.map_err(map_fs_error)?;
Ok(FsReadFileResponse {
data_base64: STANDARD.encode(bytes),
})
@@ -274,7 +269,6 @@ mod tests {
sandbox_policy,
sandbox_cwd.clone(),
)),
no_follow_symlinks: false,
})
.await
.expect("read file");
-35
View File
@@ -735,41 +735,6 @@ async fn file_system_sandboxed_read_rejects_symlink_escape(use_remote: bool) ->
Ok(())
}
#[test_case(false ; "local")]
#[test_case(true ; "remote")]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn file_system_read_file_no_follow_rejects_symlinked_parent(use_remote: bool) -> Result<()> {
let context = create_file_system_context(use_remote).await?;
let file_system = context.file_system;
let tmp = TempDir::new()?;
let tmp = tmp.path().canonicalize()?;
let target_dir = tmp.join("target");
let symlinked_dir = tmp.join("link");
std::fs::create_dir(&target_dir)?;
let target_path = target_dir.join("payload");
std::fs::write(&target_path, "original")?;
symlink(&target_dir, &symlinked_dir)?;
let contents = file_system
.read_file_no_follow(&absolute_path(target_path), /*sandbox*/ None)
.await
.with_context(|| format!("mode={use_remote}"))?;
assert_eq!(contents, b"original");
if let Ok(contents) = file_system
.read_file_no_follow(
&absolute_path(symlinked_dir.join("payload")),
/*sandbox*/ None,
)
.await
{
anyhow::bail!("symlinked parent should fail, but read {contents:?}");
}
Ok(())
}
#[test_case(false ; "local")]
#[test_case(true ; "remote")]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]