From a8ab08ff2a6b1f33e455a11fa057cad178079905 Mon Sep 17 00:00:00 2001 From: colby-oai <228809017+colby-oai@users.noreply.github.com> Date: Tue, 28 Apr 2026 14:40:57 -0400 Subject: [PATCH] clean up redundant old check --- codex-rs/core/src/mcp_openai_file.rs | 65 ------------------- codex-rs/exec-server/src/fs_sandbox.rs | 4 +- .../exec-server/src/sandboxed_file_system.rs | 32 ++++++++- codex-rs/exec-server/tests/file_system.rs | 32 +++++++++ 4 files changed, 65 insertions(+), 68 deletions(-) diff --git a/codex-rs/core/src/mcp_openai_file.rs b/codex-rs/core/src/mcp_openai_file.rs index 736646379a..88fc37f79b 100644 --- a/codex-rs/core/src/mcp_openai_file.rs +++ b/codex-rs/core/src/mcp_openai_file.rs @@ -112,19 +112,6 @@ async fn build_uploaded_local_argument_value( file_path: &str, ) -> Result { let resolved_path = turn_context.resolve_path(Some(file_path.to_string())); - if !turn_context - .file_system_sandbox_policy() - .can_read_path_with_cwd(resolved_path.as_path(), turn_context.cwd.as_path()) - { - return Err(match index { - Some(index) => format!( - "reading `{file_path}` for `{field_name}[{index}]` is blocked by the filesystem sandbox policy" - ), - None => format!( - "reading `{file_path}` for `{field_name}` is blocked by the filesystem sandbox policy" - ), - }); - } let Some(auth) = auth else { return Err( "ChatGPT auth is required to upload local files for Codex Apps tools".to_string(), @@ -185,11 +172,7 @@ mod tests { use super::*; use crate::session::tests::make_session_and_context; use codex_protocol::models::PermissionProfile; - use codex_protocol::permissions::FileSystemAccessMode; - use codex_protocol::permissions::FileSystemPath; - use codex_protocol::permissions::FileSystemSandboxEntry; use codex_protocol::permissions::FileSystemSandboxPolicy; - use codex_protocol::permissions::FileSystemSpecialPath; use codex_protocol::permissions::NetworkSandboxPolicy; use codex_utils_absolute_path::AbsolutePathBuf; use pretty_assertions::assert_eq; @@ -301,54 +284,6 @@ mod tests { ); } - #[tokio::test] - async fn build_uploaded_local_argument_value_respects_filesystem_read_policy() { - let (session, mut turn_context) = make_session_and_context().await; - let auth = CodexAuth::create_dummy_chatgpt_auth_for_testing(); - let dir = tempdir().expect("temp dir"); - let local_path = dir.path().join("blocked.csv"); - tokio::fs::write(&local_path, b"secret") - .await - .expect("write local file"); - turn_context.cwd = AbsolutePathBuf::try_from(dir.path()).expect("absolute path"); - - let file_system_sandbox_policy = FileSystemSandboxPolicy::restricted(vec![ - FileSystemSandboxEntry { - path: FileSystemPath::Special { - value: FileSystemSpecialPath::Root, - }, - access: FileSystemAccessMode::Read, - }, - FileSystemSandboxEntry { - path: FileSystemPath::Path { - path: AbsolutePathBuf::try_from(local_path.as_path()) - .expect("absolute blocked file path"), - }, - access: FileSystemAccessMode::None, - }, - ]); - turn_context.permission_profile = PermissionProfile::from_runtime_permissions( - &file_system_sandbox_policy, - NetworkSandboxPolicy::Enabled, - ); - - let error = build_uploaded_local_argument_value( - &session, - &turn_context, - Some(&auth), - "file", - /*index*/ None, - "blocked.csv", - ) - .await - .expect_err("deny-read policy should block local file upload"); - - assert_eq!( - error, - "reading `blocked.csv` for `file` is blocked by the filesystem sandbox policy" - ); - } - #[tokio::test] async fn rewrite_argument_value_for_openai_files_rewrites_scalar_path() { use wiremock::Mock; diff --git a/codex-rs/exec-server/src/fs_sandbox.rs b/codex-rs/exec-server/src/fs_sandbox.rs index a1c46f29e7..24e9d2e7da 100644 --- a/codex-rs/exec-server/src/fs_sandbox.rs +++ b/codex-rs/exec-server/src/fs_sandbox.rs @@ -198,7 +198,9 @@ fn add_helper_runtime_permissions( } } -fn normalize_file_system_policy_root_aliases(file_system_policy: &mut FileSystemSandboxPolicy) { +pub(crate) fn normalize_file_system_policy_root_aliases( + file_system_policy: &mut FileSystemSandboxPolicy, +) { for entry in &mut file_system_policy.entries { if let FileSystemPath::Path { path } = &mut entry.path { *path = normalize_top_level_alias(path.clone()); diff --git a/codex-rs/exec-server/src/sandboxed_file_system.rs b/codex-rs/exec-server/src/sandboxed_file_system.rs index a895584e95..50a00f2007 100644 --- a/codex-rs/exec-server/src/sandboxed_file_system.rs +++ b/codex-rs/exec-server/src/sandboxed_file_system.rs @@ -18,6 +18,8 @@ use crate::RemoveOptions; use crate::fs_helper::FsHelperPayload; use crate::fs_helper::FsHelperRequest; use crate::fs_sandbox::FileSystemSandboxRunner; +use crate::fs_sandbox::normalize_file_system_policy_root_aliases; +use crate::local_file_system::current_sandbox_cwd; use crate::local_file_system::file_name_from_path; use crate::protocol::FsCopyParams; use crate::protocol::FsCreateDirectoryParams; @@ -84,11 +86,12 @@ impl ExecutorFileSystem for SandboxedFileSystem { sandbox: Option<&FileSystemSandboxContext>, ) -> FileSystemResult { let sandbox = require_platform_sandbox(sandbox)?; + let resolved_path = resolve_read_path_for_sandbox(path, sandbox).await?; let file_info = self .run_sandboxed( sandbox, FsHelperRequest::ReadFileInfo(FsReadFileParams { - path: path.clone(), + path: resolved_path.clone(), sandbox: None, }), ) @@ -100,7 +103,7 @@ impl ExecutorFileSystem for SandboxedFileSystem { .run_stream( sandbox, FsHelperRequest::ReadFileStream(FsReadFileParams { - path: path.clone(), + path: resolved_path, sandbox: None, }), ) @@ -255,6 +258,31 @@ impl ExecutorFileSystem for SandboxedFileSystem { } } +async fn resolve_read_path_for_sandbox( + path: &AbsolutePathBuf, + sandbox: &FileSystemSandboxContext, +) -> io::Result { + let resolved = tokio::fs::canonicalize(path.as_path()).await?; + let resolved = AbsolutePathBuf::from_absolute_path(resolved)?; + let cwd = match sandbox.cwd.as_ref() { + Some(cwd) => cwd.clone(), + None => AbsolutePathBuf::from_absolute_path(current_sandbox_cwd()?)?, + }; + let mut file_system_policy = sandbox.permissions.file_system_sandbox_policy(); + normalize_file_system_policy_root_aliases(&mut file_system_policy); + if !file_system_policy.can_read_path_with_cwd(resolved.as_path(), cwd.as_path()) { + return Err(io::Error::new( + io::ErrorKind::PermissionDenied, + format!( + "reading `{}` resolved to `{}` which is not permitted by the filesystem sandbox policy", + path.display(), + resolved.display() + ), + )); + } + Ok(resolved) +} + fn require_platform_sandbox( sandbox: Option<&FileSystemSandboxContext>, ) -> FileSystemResult<&FileSystemSandboxContext> { diff --git a/codex-rs/exec-server/tests/file_system.rs b/codex-rs/exec-server/tests/file_system.rs index f82b645547..0f27e2be06 100644 --- a/codex-rs/exec-server/tests/file_system.rs +++ b/codex-rs/exec-server/tests/file_system.rs @@ -680,6 +680,38 @@ 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_sandboxed_read_body_rejects_symlink_to_denied_file( + use_remote: bool, +) -> Result<()> { + let context = create_file_system_context(use_remote).await?; + let file_system = context.file_system; + + let tmp = TempDir::new()?; + let allowed_dir = tmp.path().join("allowed"); + let outside_dir = tmp.path().join("outside"); + let secret_path = outside_dir.join("secret.csv"); + let symlink_path = allowed_dir.join("report.csv"); + std::fs::create_dir_all(&allowed_dir)?; + std::fs::create_dir_all(&outside_dir)?; + std::fs::write(&secret_path, "secret")?; + symlink(&secret_path, &symlink_path)?; + + let sandbox = read_only_sandbox(allowed_dir); + let error = match file_system + .read_file_body(&absolute_path(symlink_path), Some(&sandbox)) + .await + { + Ok(_) => anyhow::bail!("read body should be blocked"), + Err(error) => error, + }; + assert_sandbox_denied(&error); + + Ok(()) +} + #[test_case(false ; "local")] #[test_case(true ; "remote")] #[tokio::test(flavor = "multi_thread", worker_threads = 2)]