From 689107592d0b279edd7fc1c3d78d5dd923ef3d2a Mon Sep 17 00:00:00 2001 From: starr-openai Date: Mon, 6 Apr 2026 17:01:56 -0700 Subject: [PATCH] codex: route view_image through sandboxed fs options --- codex-rs/Cargo.lock | 1 + .../core/src/tools/handlers/view_image.rs | 33 +++++++++++++++++-- codex-rs/exec-server/Cargo.toml | 2 ++ codex-rs/exec-server/src/lib.rs | 1 + 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index 18e9aa434c..9f5c8fd7c4 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -2048,6 +2048,7 @@ dependencies = [ "base64 0.22.1", "clap", "codex-app-server-protocol", + "codex-protocol", "codex-utils-absolute-path", "codex-utils-cargo-bin", "codex-utils-pty", diff --git a/codex-rs/core/src/tools/handlers/view_image.rs b/codex-rs/core/src/tools/handlers/view_image.rs index ec34fafb36..526a1a9743 100644 --- a/codex-rs/core/src/tools/handlers/view_image.rs +++ b/codex-rs/core/src/tools/handlers/view_image.rs @@ -1,3 +1,4 @@ +use codex_exec_server::FileSystemOperationOptions; use codex_protocol::models::FunctionCallOutputBody; use codex_protocol::models::FunctionCallOutputContentItem; use codex_protocol::models::FunctionCallOutputPayload; @@ -8,6 +9,7 @@ use codex_utils_absolute_path::AbsolutePathBuf; use codex_utils_image::PromptImageMode; use codex_utils_image::load_for_prompt_bytes; use serde::Deserialize; +use std::path::Path; use crate::function_tool::FunctionCallError; use crate::original_image_detail::can_request_original_image_detail; @@ -91,11 +93,13 @@ impl ToolHandler for ViewImageHandler { AbsolutePathBuf::try_from(turn.resolve_path(Some(args.path))).map_err(|error| { FunctionCallError::RespondToModel(format!("unable to resolve image path: {error}")) })?; + let file_system_options = + view_image_file_system_options(turn.cwd.as_path(), turn.sandbox_policy.get().clone()); let metadata = turn .environment .get_filesystem() - .get_metadata(&abs_path) + .get_metadata_with_options(&abs_path, &file_system_options) .await .map_err(|error| { FunctionCallError::RespondToModel(format!( @@ -113,7 +117,7 @@ impl ToolHandler for ViewImageHandler { let file_bytes = turn .environment .get_filesystem() - .read_file(&abs_path) + .read_file_with_options(&abs_path, &file_system_options) .await .map_err(|error| { FunctionCallError::RespondToModel(format!( @@ -160,6 +164,16 @@ impl ToolHandler for ViewImageHandler { } } +fn view_image_file_system_options( + cwd: &Path, + sandbox_policy: codex_protocol::protocol::SandboxPolicy, +) -> FileSystemOperationOptions { + FileSystemOperationOptions { + sandbox_policy: Some(sandbox_policy), + cwd: AbsolutePathBuf::from_absolute_path(cwd).ok(), + } +} + pub struct ViewImageOutput { image_url: String, image_detail: Option, @@ -204,6 +218,7 @@ mod tests { use super::*; use pretty_assertions::assert_eq; use serde_json::json; + use tempfile::tempdir; #[test] fn code_mode_result_returns_image_url_object() { @@ -224,4 +239,18 @@ mod tests { }) ); } + + #[test] + fn view_image_file_system_options_include_sandbox_policy_and_cwd() { + let temp_dir = tempdir().expect("temp dir"); + let sandbox_policy = codex_protocol::protocol::SandboxPolicy::new_workspace_write_policy(); + + let options = view_image_file_system_options(temp_dir.path(), sandbox_policy.clone()); + + assert_eq!(options.sandbox_policy, Some(sandbox_policy)); + assert_eq!( + options.cwd, + AbsolutePathBuf::from_absolute_path(temp_dir.path()).ok() + ); + } } diff --git a/codex-rs/exec-server/Cargo.toml b/codex-rs/exec-server/Cargo.toml index f064a82dca..1b40aad319 100644 --- a/codex-rs/exec-server/Cargo.toml +++ b/codex-rs/exec-server/Cargo.toml @@ -20,6 +20,7 @@ async-trait = { workspace = true } base64 = { workspace = true } clap = { workspace = true, features = ["derive"] } codex-app-server-protocol = { workspace = true } +codex-protocol = { workspace = true } codex-utils-absolute-path = { workspace = true } codex-utils-pty = { workspace = true } futures = { workspace = true } @@ -42,6 +43,7 @@ tracing = { workspace = true } [dev-dependencies] anyhow = { workspace = true } +codex-protocol = { workspace = true } codex-utils-cargo-bin = { workspace = true } pretty_assertions = { workspace = true } tempfile = { workspace = true } diff --git a/codex-rs/exec-server/src/lib.rs b/codex-rs/exec-server/src/lib.rs index 14414accb3..8c03c60451 100644 --- a/codex-rs/exec-server/src/lib.rs +++ b/codex-rs/exec-server/src/lib.rs @@ -39,6 +39,7 @@ pub use file_system::CopyOptions; pub use file_system::CreateDirectoryOptions; pub use file_system::ExecutorFileSystem; pub use file_system::FileMetadata; +pub use file_system::FileSystemOperationOptions; pub use file_system::FileSystemResult; pub use file_system::ReadDirectoryEntry; pub use file_system::RemoveOptions;