fix: introduce AbsolutePathBuf as part of sandbox config

This commit is contained in:
Michael Bolin
2025-12-10 19:02:35 -08:00
parent 3fc8b2894f
commit 39c5b2acfa
26 changed files with 174 additions and 66 deletions
+8
View File
@@ -865,6 +865,7 @@ dependencies = [
"anyhow", "anyhow",
"clap", "clap",
"codex-protocol", "codex-protocol",
"codex-utils-absolute-path",
"mcp-types", "mcp-types",
"pretty_assertions", "pretty_assertions",
"schemars 0.8.22", "schemars 0.8.22",
@@ -1183,6 +1184,7 @@ dependencies = [
"codex-common", "codex-common",
"codex-core", "codex-core",
"codex-protocol", "codex-protocol",
"codex-utils-absolute-path",
"core_test_support", "core_test_support",
"libc", "libc",
"mcp-types", "mcp-types",
@@ -1322,6 +1324,7 @@ version = "0.0.0"
dependencies = [ dependencies = [
"clap", "clap",
"codex-core", "codex-core",
"codex-utils-absolute-path",
"landlock", "landlock",
"libc", "libc",
"seccompiler", "seccompiler",
@@ -1446,6 +1449,7 @@ version = "0.0.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"codex-git", "codex-git",
"codex-utils-absolute-path",
"codex-utils-image", "codex-utils-image",
"icu_decimal", "icu_decimal",
"icu_locale_core", "icu_locale_core",
@@ -1544,6 +1548,7 @@ dependencies = [
"codex-file-search", "codex-file-search",
"codex-login", "codex-login",
"codex-protocol", "codex-protocol",
"codex-utils-absolute-path",
"codex-windows-sandbox", "codex-windows-sandbox",
"color-eyre", "color-eyre",
"crossterm", "crossterm",
@@ -1613,6 +1618,7 @@ dependencies = [
"codex-login", "codex-login",
"codex-protocol", "codex-protocol",
"codex-tui", "codex-tui",
"codex-utils-absolute-path",
"codex-windows-sandbox", "codex-windows-sandbox",
"color-eyre", "color-eyre",
"crossterm", "crossterm",
@@ -1665,9 +1671,11 @@ name = "codex-utils-absolute-path"
version = "0.0.0" version = "0.0.0"
dependencies = [ dependencies = [
"path-absolutize", "path-absolutize",
"schemars 0.8.22",
"serde", "serde",
"serde_json", "serde_json",
"tempfile", "tempfile",
"ts-rs",
] ]
[[package]] [[package]]
+1
View File
@@ -15,6 +15,7 @@ workspace = true
anyhow = { workspace = true } anyhow = { workspace = true }
clap = { workspace = true, features = ["derive"] } clap = { workspace = true, features = ["derive"] }
codex-protocol = { workspace = true } codex-protocol = { workspace = true }
codex-utils-absolute-path = { workspace = true }
mcp-types = { workspace = true } mcp-types = { workspace = true }
schemars = { workspace = true } schemars = { workspace = true }
serde = { workspace = true, features = ["derive"] } serde = { workspace = true, features = ["derive"] }
@@ -16,6 +16,7 @@ use codex_protocol::protocol::ReviewDecision;
use codex_protocol::protocol::SandboxPolicy; use codex_protocol::protocol::SandboxPolicy;
use codex_protocol::protocol::SessionSource; use codex_protocol::protocol::SessionSource;
use codex_protocol::protocol::TurnAbortReason; use codex_protocol::protocol::TurnAbortReason;
use codex_utils_absolute_path::AbsolutePathBuf;
use schemars::JsonSchema; use schemars::JsonSchema;
use serde::Deserialize; use serde::Deserialize;
use serde::Serialize; use serde::Serialize;
@@ -359,7 +360,7 @@ pub struct Tools {
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct SandboxSettings { pub struct SandboxSettings {
#[serde(default)] #[serde(default)]
pub writable_roots: Vec<PathBuf>, pub writable_roots: Vec<AbsolutePathBuf>,
pub network_access: Option<bool>, pub network_access: Option<bool>,
pub exclude_tmpdir_env_var: Option<bool>, pub exclude_tmpdir_env_var: Option<bool>,
pub exclude_slash_tmp: Option<bool>, pub exclude_slash_tmp: Option<bool>,
@@ -24,6 +24,7 @@ use codex_protocol::protocol::SessionSource as CoreSessionSource;
use codex_protocol::protocol::TokenUsage as CoreTokenUsage; use codex_protocol::protocol::TokenUsage as CoreTokenUsage;
use codex_protocol::protocol::TokenUsageInfo as CoreTokenUsageInfo; use codex_protocol::protocol::TokenUsageInfo as CoreTokenUsageInfo;
use codex_protocol::user_input::UserInput as CoreUserInput; use codex_protocol::user_input::UserInput as CoreUserInput;
use codex_utils_absolute_path::AbsolutePathBuf;
use mcp_types::ContentBlock as McpContentBlock; use mcp_types::ContentBlock as McpContentBlock;
use mcp_types::Resource as McpResource; use mcp_types::Resource as McpResource;
use mcp_types::ResourceTemplate as McpResourceTemplate; use mcp_types::ResourceTemplate as McpResourceTemplate;
@@ -420,7 +421,7 @@ pub enum SandboxPolicy {
#[ts(rename_all = "camelCase")] #[ts(rename_all = "camelCase")]
WorkspaceWrite { WorkspaceWrite {
#[serde(default)] #[serde(default)]
writable_roots: Vec<PathBuf>, writable_roots: Vec<AbsolutePathBuf>,
#[serde(default)] #[serde(default)]
network_access: bool, network_access: bool,
#[serde(default)] #[serde(default)]
@@ -410,7 +410,7 @@ async fn test_send_user_turn_updates_sandbox_and_cwd_between_turns() -> Result<(
cwd: first_cwd.clone(), cwd: first_cwd.clone(),
approval_policy: AskForApproval::Never, approval_policy: AskForApproval::Never,
sandbox_policy: SandboxPolicy::WorkspaceWrite { sandbox_policy: SandboxPolicy::WorkspaceWrite {
writable_roots: vec![first_cwd.clone()], writable_roots: vec![first_cwd.try_into()?],
network_access: false, network_access: false,
exclude_tmpdir_env_var: false, exclude_tmpdir_env_var: false,
exclude_slash_tmp: false, exclude_slash_tmp: false,
+1 -1
View File
@@ -80,7 +80,7 @@ async fn get_config_toml_parses_all_fields() -> Result<()> {
approval_policy: Some(AskForApproval::OnRequest), approval_policy: Some(AskForApproval::OnRequest),
sandbox_mode: Some(SandboxMode::WorkspaceWrite), sandbox_mode: Some(SandboxMode::WorkspaceWrite),
sandbox_settings: Some(SandboxSettings { sandbox_settings: Some(SandboxSettings {
writable_roots: vec!["/tmp".into()], writable_roots: vec!["/tmp".try_into()?],
network_access: Some(true), network_access: Some(true),
exclude_tmpdir_env_var: Some(true), exclude_tmpdir_env_var: Some(true),
exclude_slash_tmp: Some(true), exclude_slash_tmp: Some(true),
@@ -532,7 +532,7 @@ async fn turn_start_updates_sandbox_and_cwd_between_turns_v2() -> Result<()> {
cwd: Some(first_cwd.clone()), cwd: Some(first_cwd.clone()),
approval_policy: Some(codex_app_server_protocol::AskForApproval::Never), approval_policy: Some(codex_app_server_protocol::AskForApproval::Never),
sandbox_policy: Some(codex_app_server_protocol::SandboxPolicy::WorkspaceWrite { sandbox_policy: Some(codex_app_server_protocol::SandboxPolicy::WorkspaceWrite {
writable_roots: vec![first_cwd.clone()], writable_roots: vec![first_cwd.try_into()?],
network_access: false, network_access: false,
exclude_tmpdir_env_var: false, exclude_tmpdir_env_var: false,
exclude_slash_tmp: false, exclude_slash_tmp: false,
+13 -10
View File
@@ -40,9 +40,9 @@ use codex_protocol::config_types::TrustLevel;
use codex_protocol::config_types::Verbosity; use codex_protocol::config_types::Verbosity;
use codex_protocol::openai_models::ReasoningEffort; use codex_protocol::openai_models::ReasoningEffort;
use codex_rmcp_client::OAuthCredentialsStoreMode; use codex_rmcp_client::OAuthCredentialsStoreMode;
use codex_utils_absolute_path::AbsolutePathBuf;
use codex_utils_absolute_path::AbsolutePathBufGuard; use codex_utils_absolute_path::AbsolutePathBufGuard;
use dirs::home_dir; use dirs::home_dir;
use dunce::canonicalize;
use serde::Deserialize; use serde::Deserialize;
use similar::DiffableStr; use similar::DiffableStr;
use std::collections::BTreeMap; use std::collections::BTreeMap;
@@ -1022,13 +1022,10 @@ impl Config {
} }
} }
}; };
let additional_writable_roots: Vec<PathBuf> = additional_writable_roots let additional_writable_roots: Vec<AbsolutePathBuf> = additional_writable_roots
.into_iter() .into_iter()
.map(|path| { .map(|path| AbsolutePathBuf::resolve_path_against_base(path, &resolved_cwd))
let absolute = resolve_path(&resolved_cwd, &path); .collect::<Result<Vec<_>, _>>()?;
canonicalize(&absolute).unwrap_or(absolute)
})
.collect();
let active_project = cfg let active_project = cfg
.get_active_project(&resolved_cwd) .get_active_project(&resolved_cwd)
.unwrap_or(ProjectConfig { trust_level: None }); .unwrap_or(ProjectConfig { trust_level: None });
@@ -1488,7 +1485,7 @@ exclude_slash_tmp = true
resolution, resolution,
SandboxPolicyResolution { SandboxPolicyResolution {
policy: SandboxPolicy::WorkspaceWrite { policy: SandboxPolicy::WorkspaceWrite {
writable_roots: vec![PathBuf::from("/my/workspace")], writable_roots: vec!["/my/workspace".try_into().unwrap()],
network_access: false, network_access: false,
exclude_tmpdir_env_var: true, exclude_tmpdir_env_var: true,
exclude_slash_tmp: true, exclude_slash_tmp: true,
@@ -1533,7 +1530,10 @@ trust_level = "trusted"
resolution, resolution,
SandboxPolicyResolution { SandboxPolicyResolution {
policy: SandboxPolicy::WorkspaceWrite { policy: SandboxPolicy::WorkspaceWrite {
writable_roots: vec![PathBuf::from("/my/workspace")], writable_roots: vec![
AbsolutePathBuf::from_absolute_path("/my/workspace")
.expect("absolute path")
],
network_access: false, network_access: false,
exclude_tmpdir_env_var: true, exclude_tmpdir_env_var: true,
exclude_slash_tmp: true, exclude_slash_tmp: true,
@@ -1565,7 +1565,10 @@ trust_level = "trusted"
temp_dir.path().to_path_buf(), temp_dir.path().to_path_buf(),
)?; )?;
let expected_backend = canonicalize(&backend).expect("canonicalize backend directory"); let expected_backend = AbsolutePathBuf::try_from(
dunce::canonicalize(&backend).expect("canonicalize backend directory"),
)
.unwrap();
if cfg!(target_os = "windows") { if cfg!(target_os = "windows") {
assert!( assert!(
config.forced_auto_mode_downgraded_on_windows, config.forced_auto_mode_downgraded_on_windows,
+1 -1
View File
@@ -406,7 +406,7 @@ impl Notice {
#[derive(Deserialize, Debug, Clone, PartialEq, Default)] #[derive(Deserialize, Debug, Clone, PartialEq, Default)]
pub struct SandboxWorkspaceWrite { pub struct SandboxWorkspaceWrite {
#[serde(default)] #[serde(default)]
pub writable_roots: Vec<PathBuf>, pub writable_roots: Vec<AbsolutePathBuf>,
#[serde(default)] #[serde(default)]
pub network_access: bool, pub network_access: bool,
#[serde(default)] #[serde(default)]
+6 -2
View File
@@ -1,3 +1,4 @@
use codex_utils_absolute_path::AbsolutePathBuf;
use serde::Deserialize; use serde::Deserialize;
use serde::Serialize; use serde::Serialize;
use strum_macros::Display as DeriveDisplay; use strum_macros::Display as DeriveDisplay;
@@ -27,7 +28,7 @@ pub(crate) struct EnvironmentContext {
pub approval_policy: Option<AskForApproval>, pub approval_policy: Option<AskForApproval>,
pub sandbox_mode: Option<SandboxMode>, pub sandbox_mode: Option<SandboxMode>,
pub network_access: Option<NetworkAccess>, pub network_access: Option<NetworkAccess>,
pub writable_roots: Option<Vec<PathBuf>>, pub writable_roots: Option<Vec<AbsolutePathBuf>>,
pub shell: Shell, pub shell: Shell,
} }
@@ -203,7 +204,10 @@ mod tests {
fn workspace_write_policy(writable_roots: Vec<&str>, network_access: bool) -> SandboxPolicy { fn workspace_write_policy(writable_roots: Vec<&str>, network_access: bool) -> SandboxPolicy {
SandboxPolicy::WorkspaceWrite { SandboxPolicy::WorkspaceWrite {
writable_roots: writable_roots.into_iter().map(PathBuf::from).collect(), writable_roots: writable_roots
.into_iter()
.map(|s| AbsolutePathBuf::try_from(s).unwrap())
.collect(),
network_access, network_access,
exclude_tmpdir_env_var: false, exclude_tmpdir_env_var: false,
exclude_slash_tmp: false, exclude_slash_tmp: false,
+2 -1
View File
@@ -188,6 +188,7 @@ fn is_write_patch_constrained_to_writable_paths(
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use codex_utils_absolute_path::AbsolutePathBuf;
use tempfile::TempDir; use tempfile::TempDir;
#[test] #[test]
@@ -228,7 +229,7 @@ mod tests {
// With the parent dir explicitly added as a writable root, the // With the parent dir explicitly added as a writable root, the
// outside write should be permitted. // outside write should be permitted.
let policy_with_parent = SandboxPolicy::WorkspaceWrite { let policy_with_parent = SandboxPolicy::WorkspaceWrite {
writable_roots: vec![parent], writable_roots: vec![AbsolutePathBuf::try_from(parent).unwrap()],
network_access: false, network_access: false,
exclude_tmpdir_env_var: true, exclude_tmpdir_env_var: true,
exclude_slash_tmp: true, exclude_slash_tmp: true,
+13 -3
View File
@@ -63,7 +63,11 @@ pub(crate) fn create_seatbelt_command_args(
for (index, wr) in writable_roots.iter().enumerate() { for (index, wr) in writable_roots.iter().enumerate() {
// Canonicalize to avoid mismatches like /var vs /private/var on macOS. // Canonicalize to avoid mismatches like /var vs /private/var on macOS.
let canonical_root = wr.root.canonicalize().unwrap_or_else(|_| wr.root.clone()); let canonical_root = wr
.root
.as_path()
.canonicalize()
.unwrap_or_else(|_| wr.root.to_path_buf());
let root_param = format!("WRITABLE_ROOT_{index}"); let root_param = format!("WRITABLE_ROOT_{index}");
file_write_params.push((root_param.clone(), canonical_root)); file_write_params.push((root_param.clone(), canonical_root));
@@ -75,7 +79,10 @@ pub(crate) fn create_seatbelt_command_args(
let mut require_parts: Vec<String> = Vec::new(); let mut require_parts: Vec<String> = Vec::new();
require_parts.push(format!("(subpath (param \"{root_param}\"))")); require_parts.push(format!("(subpath (param \"{root_param}\"))"));
for (subpath_index, ro) in wr.read_only_subpaths.iter().enumerate() { for (subpath_index, ro) in wr.read_only_subpaths.iter().enumerate() {
let canonical_ro = ro.canonicalize().unwrap_or_else(|_| ro.clone()); let canonical_ro = ro
.as_path()
.canonicalize()
.unwrap_or_else(|_| ro.to_path_buf());
let ro_param = format!("WRITABLE_ROOT_{index}_RO_{subpath_index}"); let ro_param = format!("WRITABLE_ROOT_{index}_RO_{subpath_index}");
require_parts require_parts
.push(format!("(require-not (subpath (param \"{ro_param}\")))")); .push(format!("(require-not (subpath (param \"{ro_param}\")))"));
@@ -182,7 +189,10 @@ mod tests {
// Build a policy that only includes the two test roots as writable and // Build a policy that only includes the two test roots as writable and
// does not automatically include defaults TMPDIR or /tmp. // does not automatically include defaults TMPDIR or /tmp.
let policy = SandboxPolicy::WorkspaceWrite { let policy = SandboxPolicy::WorkspaceWrite {
writable_roots: vec![root_with_git, root_without_git], writable_roots: vec![root_with_git, root_without_git]
.into_iter()
.map(|p| p.try_into().unwrap())
.collect(),
network_access: false, network_access: false,
exclude_tmpdir_env_var: true, exclude_tmpdir_env_var: true,
exclude_slash_tmp: true, exclude_slash_tmp: true,
+3 -2
View File
@@ -11,6 +11,7 @@ use codex_core::shell::Shell;
use codex_core::shell::default_user_shell; use codex_core::shell::default_user_shell;
use codex_protocol::openai_models::ReasoningEffort; use codex_protocol::openai_models::ReasoningEffort;
use codex_protocol::user_input::UserInput; use codex_protocol::user_input::UserInput;
use codex_utils_absolute_path::AbsolutePathBuf;
use core_test_support::load_sse_fixture_with_id; use core_test_support::load_sse_fixture_with_id;
use core_test_support::responses::mount_sse_once; use core_test_support::responses::mount_sse_once;
use core_test_support::responses::start_mock_server; use core_test_support::responses::start_mock_server;
@@ -317,7 +318,7 @@ async fn overrides_turn_context_but_keeps_cached_prefix_and_key_constant() -> an
cwd: None, cwd: None,
approval_policy: Some(AskForApproval::Never), approval_policy: Some(AskForApproval::Never),
sandbox_policy: Some(SandboxPolicy::WorkspaceWrite { sandbox_policy: Some(SandboxPolicy::WorkspaceWrite {
writable_roots: vec![writable.path().to_path_buf()], writable_roots: vec![writable.path().try_into().unwrap()],
network_access: true, network_access: true,
exclude_tmpdir_env_var: true, exclude_tmpdir_env_var: true,
exclude_slash_tmp: true, exclude_slash_tmp: true,
@@ -507,7 +508,7 @@ async fn per_turn_overrides_keep_cached_prefix_and_key_constant() -> anyhow::Res
cwd: new_cwd.path().to_path_buf(), cwd: new_cwd.path().to_path_buf(),
approval_policy: AskForApproval::Never, approval_policy: AskForApproval::Never,
sandbox_policy: SandboxPolicy::WorkspaceWrite { sandbox_policy: SandboxPolicy::WorkspaceWrite {
writable_roots: vec![writable.path().to_path_buf()], writable_roots: vec![AbsolutePathBuf::try_from(writable.path()).unwrap()],
network_access: true, network_access: true,
exclude_tmpdir_env_var: true, exclude_tmpdir_env_var: true,
exclude_slash_tmp: true, exclude_slash_tmp: true,
+2 -2
View File
@@ -76,7 +76,7 @@ async fn if_parent_of_repo_is_writable_then_dot_git_folder_is_writable() {
let tmp = TempDir::new().expect("should be able to create temp dir"); let tmp = TempDir::new().expect("should be able to create temp dir");
let test_scenario = create_test_scenario(&tmp); let test_scenario = create_test_scenario(&tmp);
let policy = SandboxPolicy::WorkspaceWrite { let policy = SandboxPolicy::WorkspaceWrite {
writable_roots: vec![test_scenario.repo_parent.clone()], writable_roots: vec![test_scenario.repo_parent.as_path().try_into().unwrap()],
network_access: false, network_access: false,
exclude_tmpdir_env_var: true, exclude_tmpdir_env_var: true,
exclude_slash_tmp: true, exclude_slash_tmp: true,
@@ -102,7 +102,7 @@ async fn if_git_repo_is_writable_root_then_dot_git_folder_is_read_only() {
let tmp = TempDir::new().expect("should be able to create temp dir"); let tmp = TempDir::new().expect("should be able to create temp dir");
let test_scenario = create_test_scenario(&tmp); let test_scenario = create_test_scenario(&tmp);
let policy = SandboxPolicy::WorkspaceWrite { let policy = SandboxPolicy::WorkspaceWrite {
writable_roots: vec![test_scenario.repo_root.clone()], writable_roots: vec![test_scenario.repo_root.as_path().try_into().unwrap()],
network_access: false, network_access: false,
exclude_tmpdir_env_var: true, exclude_tmpdir_env_var: true,
exclude_slash_tmp: true, exclude_slash_tmp: true,
+1
View File
@@ -26,6 +26,7 @@ codex-common = { workspace = true, features = [
] } ] }
codex-core = { workspace = true } codex-core = { workspace = true }
codex-protocol = { workspace = true } codex-protocol = { workspace = true }
codex-utils-absolute-path = { workspace = true }
mcp-types = { workspace = true } mcp-types = { workspace = true }
opentelemetry-appender-tracing = { workspace = true } opentelemetry-appender-tracing = { workspace = true }
owo-colors = { workspace = true } owo-colors = { workspace = true }
+3 -2
View File
@@ -1,6 +1,7 @@
#![cfg(unix)] #![cfg(unix)]
use codex_core::protocol::SandboxPolicy; use codex_core::protocol::SandboxPolicy;
use codex_core::spawn::StdioPolicy; use codex_core::spawn::StdioPolicy;
use codex_utils_absolute_path::AbsolutePathBuf;
use std::collections::HashMap; use std::collections::HashMap;
use std::future::Future; use std::future::Future;
use std::io; use std::io;
@@ -58,14 +59,14 @@ async fn spawn_command_under_sandbox(
async fn python_multiprocessing_lock_works_under_sandbox() { async fn python_multiprocessing_lock_works_under_sandbox() {
core_test_support::skip_if_sandbox!(); core_test_support::skip_if_sandbox!();
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
let writable_roots = Vec::<PathBuf>::new(); let writable_roots = Vec::<AbsolutePathBuf>::new();
// From https://man7.org/linux/man-pages/man7/sem_overview.7.html // From https://man7.org/linux/man-pages/man7/sem_overview.7.html
// //
// > On Linux, named semaphores are created in a virtual filesystem, // > On Linux, named semaphores are created in a virtual filesystem,
// > normally mounted under /dev/shm. // > normally mounted under /dev/shm.
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
let writable_roots = vec![PathBuf::from("/dev/shm")]; let writable_roots: Vec<AbsolutePathBuf> = vec!["/dev/shm".try_into().unwrap()];
let policy = SandboxPolicy::WorkspaceWrite { let policy = SandboxPolicy::WorkspaceWrite {
writable_roots, writable_roots,
+1
View File
@@ -18,6 +18,7 @@ workspace = true
[target.'cfg(target_os = "linux")'.dependencies] [target.'cfg(target_os = "linux")'.dependencies]
clap = { workspace = true, features = ["derive"] } clap = { workspace = true, features = ["derive"] }
codex-core = { workspace = true } codex-core = { workspace = true }
codex-utils-absolute-path = { workspace = true }
landlock = { workspace = true } landlock = { workspace = true }
libc = { workspace = true } libc = { workspace = true }
seccompiler = { workspace = true } seccompiler = { workspace = true }
+4 -1
View File
@@ -6,6 +6,7 @@ use codex_core::error::CodexErr;
use codex_core::error::Result; use codex_core::error::Result;
use codex_core::error::SandboxErr; use codex_core::error::SandboxErr;
use codex_core::protocol::SandboxPolicy; use codex_core::protocol::SandboxPolicy;
use codex_utils_absolute_path::AbsolutePathBuf;
use landlock::ABI; use landlock::ABI;
use landlock::Access; use landlock::Access;
@@ -56,7 +57,9 @@ pub(crate) fn apply_sandbox_policy_to_current_thread(
/// ///
/// # Errors /// # Errors
/// Returns [`CodexErr::Sandbox`] variants when the ruleset fails to apply. /// Returns [`CodexErr::Sandbox`] variants when the ruleset fails to apply.
fn install_filesystem_landlock_rules_on_current_thread(writable_roots: Vec<PathBuf>) -> Result<()> { fn install_filesystem_landlock_rules_on_current_thread(
writable_roots: Vec<AbsolutePathBuf>,
) -> Result<()> {
let abi = ABI::V5; let abi = ABI::V5;
let access_rw = AccessFs::from_all(abi); let access_rw = AccessFs::from_all(abi);
let access_ro = AccessFs::from_read(abi); let access_ro = AccessFs::from_read(abi);
+1 -1
View File
@@ -13,7 +13,7 @@ workspace = true
[dependencies] [dependencies]
codex-git = { workspace = true } codex-git = { workspace = true }
codex-utils-absolute-path = { workspace = true }
codex-utils-image = { workspace = true } codex-utils-image = { workspace = true }
icu_decimal = { workspace = true } icu_decimal = { workspace = true }
icu_locale_core = { workspace = true } icu_locale_core = { workspace = true }
+37 -12
View File
@@ -23,6 +23,7 @@ use crate::openai_models::ReasoningEffort as ReasoningEffortConfig;
use crate::parse_command::ParsedCommand; use crate::parse_command::ParsedCommand;
use crate::plan_tool::UpdatePlanArgs; use crate::plan_tool::UpdatePlanArgs;
use crate::user_input::UserInput; use crate::user_input::UserInput;
use codex_utils_absolute_path::AbsolutePathBuf;
use mcp_types::CallToolResult; use mcp_types::CallToolResult;
use mcp_types::RequestId; use mcp_types::RequestId;
use mcp_types::Resource as McpResource; use mcp_types::Resource as McpResource;
@@ -34,6 +35,7 @@ use serde::Serialize;
use serde_json::Value; use serde_json::Value;
use serde_with::serde_as; use serde_with::serde_as;
use strum_macros::Display; use strum_macros::Display;
use tracing::error;
use ts_rs::TS; use ts_rs::TS;
pub use crate::approvals::ApplyPatchApprovalRequestEvent; pub use crate::approvals::ApplyPatchApprovalRequestEvent;
@@ -273,7 +275,7 @@ pub enum SandboxPolicy {
/// Additional folders (beyond cwd and possibly TMPDIR) that should be /// Additional folders (beyond cwd and possibly TMPDIR) that should be
/// writable from within the sandbox. /// writable from within the sandbox.
#[serde(default, skip_serializing_if = "Vec::is_empty")] #[serde(default, skip_serializing_if = "Vec::is_empty")]
writable_roots: Vec<PathBuf>, writable_roots: Vec<AbsolutePathBuf>,
/// When set to `true`, outbound network access is allowed. `false` by /// When set to `true`, outbound network access is allowed. `false` by
/// default. /// default.
@@ -299,11 +301,9 @@ pub enum SandboxPolicy {
/// not modified by the agent. /// not modified by the agent.
#[derive(Debug, Clone, PartialEq, Eq, JsonSchema)] #[derive(Debug, Clone, PartialEq, Eq, JsonSchema)]
pub struct WritableRoot { pub struct WritableRoot {
/// Absolute path, by construction. pub root: AbsolutePathBuf,
pub root: PathBuf,
/// Also absolute paths, by construction. pub read_only_subpaths: Vec<AbsolutePathBuf>,
pub read_only_subpaths: Vec<PathBuf>,
} }
impl WritableRoot { impl WritableRoot {
@@ -385,16 +385,30 @@ impl SandboxPolicy {
network_access: _, network_access: _,
} => { } => {
// Start from explicitly configured writable roots. // Start from explicitly configured writable roots.
let mut roots: Vec<PathBuf> = writable_roots.clone(); let mut roots: Vec<AbsolutePathBuf> = writable_roots.clone();
// Always include defaults: cwd, /tmp (if present on Unix), and // Always include defaults: cwd, /tmp (if present on Unix), and
// on macOS, the per-user TMPDIR unless explicitly excluded. // on macOS, the per-user TMPDIR unless explicitly excluded.
roots.push(cwd.to_path_buf()); // TODO(mbolin): cwd param should be AbsolutePathBuf.
let cwd_absolute = AbsolutePathBuf::from_absolute_path(cwd);
match cwd_absolute {
Ok(cwd) => {
roots.push(cwd);
}
Err(e) => {
error!(
"Ignoring invalid cwd {:?} for sandbox writable root: {}",
cwd, e
);
}
}
// Include /tmp on Unix unless explicitly excluded. // Include /tmp on Unix unless explicitly excluded.
if cfg!(unix) && !exclude_slash_tmp { if cfg!(unix) && !exclude_slash_tmp {
let slash_tmp = PathBuf::from("/tmp"); #[allow(clippy::expect_used)]
if slash_tmp.is_dir() { let slash_tmp =
AbsolutePathBuf::from_absolute_path("/tmp").expect("/tmp is absolute");
if slash_tmp.as_path().is_dir() {
roots.push(slash_tmp); roots.push(slash_tmp);
} }
} }
@@ -411,7 +425,15 @@ impl SandboxPolicy {
&& let Some(tmpdir) = std::env::var_os("TMPDIR") && let Some(tmpdir) = std::env::var_os("TMPDIR")
&& !tmpdir.is_empty() && !tmpdir.is_empty()
{ {
roots.push(PathBuf::from(tmpdir)); if let Ok(tmpdir_path) =
AbsolutePathBuf::from_absolute_path(PathBuf::from(&tmpdir))
{
roots.push(tmpdir_path);
} else {
error!(
"Ignoring invalid TMPDIR value {tmpdir:?} for sandbox writable root",
);
}
} }
// For each root, compute subpaths that should remain read-only. // For each root, compute subpaths that should remain read-only.
@@ -419,8 +441,11 @@ impl SandboxPolicy {
.into_iter() .into_iter()
.map(|writable_root| { .map(|writable_root| {
let mut subpaths = Vec::new(); let mut subpaths = Vec::new();
let top_level_git = writable_root.join(".git"); #[allow(clippy::expect_used)]
if top_level_git.is_dir() { let top_level_git = writable_root
.join(".git")
.expect(".git is a valid relative path");
if top_level_git.as_path().is_dir() {
subpaths.push(top_level_git); subpaths.push(top_level_git);
} }
WritableRoot { WritableRoot {
+1
View File
@@ -41,6 +41,7 @@ codex-feedback = { workspace = true }
codex-file-search = { workspace = true } codex-file-search = { workspace = true }
codex-login = { workspace = true } codex-login = { workspace = true }
codex-protocol = { workspace = true } codex-protocol = { workspace = true }
codex-utils-absolute-path = { workspace = true }
color-eyre = { workspace = true } color-eyre = { workspace = true }
crossterm = { workspace = true, features = ["bracketed-paste", "event-stream"] } crossterm = { workspace = true, features = ["bracketed-paste", "event-stream"] }
derive_more = { workspace = true, features = ["is_variant"] } derive_more = { workspace = true, features = ["is_variant"] }
+2 -1
View File
@@ -56,6 +56,7 @@ use codex_protocol::plan_tool::PlanItemArg;
use codex_protocol::plan_tool::StepStatus; use codex_protocol::plan_tool::StepStatus;
use codex_protocol::plan_tool::UpdatePlanArgs; use codex_protocol::plan_tool::UpdatePlanArgs;
use codex_protocol::protocol::CodexErrorInfo; use codex_protocol::protocol::CodexErrorInfo;
use codex_utils_absolute_path::AbsolutePathBuf;
use crossterm::event::KeyCode; use crossterm::event::KeyCode;
use crossterm::event::KeyEvent; use crossterm::event::KeyEvent;
use crossterm::event::KeyModifiers; use crossterm::event::KeyModifiers;
@@ -1805,7 +1806,7 @@ fn preset_matching_ignores_extra_writable_roots() {
.find(|p| p.id == "auto") .find(|p| p.id == "auto")
.expect("auto preset exists"); .expect("auto preset exists");
let current_sandbox = SandboxPolicy::WorkspaceWrite { let current_sandbox = SandboxPolicy::WorkspaceWrite {
writable_roots: vec![PathBuf::from("C:\\extra")], writable_roots: vec![AbsolutePathBuf::try_from("C:\\extra").unwrap()],
network_access: false, network_access: false,
exclude_tmpdir_env_var: false, exclude_tmpdir_env_var: false,
exclude_slash_tmp: false, exclude_slash_tmp: false,
+1
View File
@@ -40,6 +40,7 @@ codex-feedback = { workspace = true }
codex-file-search = { workspace = true } codex-file-search = { workspace = true }
codex-login = { workspace = true } codex-login = { workspace = true }
codex-protocol = { workspace = true } codex-protocol = { workspace = true }
codex-utils-absolute-path = { workspace = true }
codex-tui = { workspace = true } codex-tui = { workspace = true }
color-eyre = { workspace = true } color-eyre = { workspace = true }
crossterm = { workspace = true, features = ["bracketed-paste", "event-stream"] } crossterm = { workspace = true, features = ["bracketed-paste", "event-stream"] }
+2 -1
View File
@@ -56,6 +56,7 @@ use codex_protocol::plan_tool::PlanItemArg;
use codex_protocol::plan_tool::StepStatus; use codex_protocol::plan_tool::StepStatus;
use codex_protocol::plan_tool::UpdatePlanArgs; use codex_protocol::plan_tool::UpdatePlanArgs;
use codex_protocol::protocol::CodexErrorInfo; use codex_protocol::protocol::CodexErrorInfo;
use codex_utils_absolute_path::AbsolutePathBuf;
use crossterm::event::KeyCode; use crossterm::event::KeyCode;
use crossterm::event::KeyEvent; use crossterm::event::KeyEvent;
use crossterm::event::KeyModifiers; use crossterm::event::KeyModifiers;
@@ -1806,7 +1807,7 @@ fn preset_matching_ignores_extra_writable_roots() {
.find(|p| p.id == "auto") .find(|p| p.id == "auto")
.expect("auto preset exists"); .expect("auto preset exists");
let current_sandbox = SandboxPolicy::WorkspaceWrite { let current_sandbox = SandboxPolicy::WorkspaceWrite {
writable_roots: vec![PathBuf::from("C:\\extra")], writable_roots: vec![AbsolutePathBuf::try_from("C:\\extra").unwrap()],
network_access: false, network_access: false,
exclude_tmpdir_env_var: false, exclude_tmpdir_env_var: false,
exclude_slash_tmp: false, exclude_slash_tmp: false,
+5
View File
@@ -10,7 +10,12 @@ workspace = true
[dependencies] [dependencies]
path-absolutize = { workspace = true } path-absolutize = { workspace = true }
schemars = { workspace = true }
serde = { workspace = true, features = ["derive"] } serde = { workspace = true, features = ["derive"] }
ts-rs = { workspace = true, features = [
"serde-json-impl",
"no-serde-warnings",
] }
[dev-dependencies] [dev-dependencies]
serde_json = { workspace = true } serde_json = { workspace = true }
+60 -22
View File
@@ -1,4 +1,5 @@
use path_absolutize::Absolutize; use path_absolutize::Absolutize;
use schemars::JsonSchema;
use serde::Deserialize; use serde::Deserialize;
use serde::Deserializer; use serde::Deserializer;
use serde::Serialize; use serde::Serialize;
@@ -7,6 +8,7 @@ use std::cell::RefCell;
use std::path::Display; use std::path::Display;
use std::path::Path; use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
use ts_rs::TS;
/// A path that is guaranteed to be absolute and normalized (though it is not /// A path that is guaranteed to be absolute and normalized (though it is not
/// guaranteed to be canonicalized or exist on the filesystem). /// guaranteed to be canonicalized or exist on the filesystem).
@@ -15,27 +17,27 @@ use std::path::PathBuf;
/// using `AbsolutePathBufGuard::new(base_path)`. If no base path is set, the /// using `AbsolutePathBufGuard::new(base_path)`. If no base path is set, the
/// deserialization will fail unless the path being deserialized is already /// deserialization will fail unless the path being deserialized is already
/// absolute. /// absolute.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, JsonSchema, TS)]
pub struct AbsolutePathBuf(PathBuf); pub struct AbsolutePathBuf(PathBuf);
impl AbsolutePathBuf { impl AbsolutePathBuf {
pub fn resolve_path_against_base<P, B>(path: P, base_path: B) -> std::io::Result<Self> pub fn resolve_path_against_base<P: AsRef<Path>, B: AsRef<Path>>(
where path: P,
P: AsRef<Path>, base_path: B,
B: AsRef<Path>, ) -> std::io::Result<Self> {
{
let absolute_path = path.as_ref().absolutize_from(base_path.as_ref())?; let absolute_path = path.as_ref().absolutize_from(base_path.as_ref())?;
Ok(Self(absolute_path.into_owned())) Ok(Self(absolute_path.into_owned()))
} }
pub fn from_absolute_path<P>(path: P) -> std::io::Result<Self> pub fn from_absolute_path<P: AsRef<Path>>(path: P) -> std::io::Result<Self> {
where
P: AsRef<Path>,
{
let absolute_path = path.as_ref().absolutize()?; let absolute_path = path.as_ref().absolutize()?;
Ok(Self(absolute_path.into_owned())) Ok(Self(absolute_path.into_owned()))
} }
pub fn join<P: AsRef<Path>>(&self, path: P) -> std::io::Result<Self> {
Self::resolve_path_against_base(path, &self.0)
}
pub fn as_path(&self) -> &Path { pub fn as_path(&self) -> &Path {
&self.0 &self.0
} }
@@ -48,11 +50,59 @@ impl AbsolutePathBuf {
self.0.clone() self.0.clone()
} }
pub fn to_string_lossy(&self) -> std::borrow::Cow<'_, str> {
self.0.to_string_lossy()
}
pub fn display(&self) -> Display<'_> { pub fn display(&self) -> Display<'_> {
self.0.display() self.0.display()
} }
} }
impl AsRef<Path> for AbsolutePathBuf {
fn as_ref(&self) -> &Path {
&self.0
}
}
impl From<AbsolutePathBuf> for PathBuf {
fn from(path: AbsolutePathBuf) -> Self {
path.into_path_buf()
}
}
impl TryFrom<&Path> for AbsolutePathBuf {
type Error = std::io::Error;
fn try_from(value: &Path) -> Result<Self, Self::Error> {
Self::from_absolute_path(value)
}
}
impl TryFrom<PathBuf> for AbsolutePathBuf {
type Error = std::io::Error;
fn try_from(value: PathBuf) -> Result<Self, Self::Error> {
Self::from_absolute_path(value)
}
}
impl TryFrom<&str> for AbsolutePathBuf {
type Error = std::io::Error;
fn try_from(value: &str) -> Result<Self, Self::Error> {
Self::from_absolute_path(value)
}
}
impl TryFrom<String> for AbsolutePathBuf {
type Error = std::io::Error;
fn try_from(value: String) -> Result<Self, Self::Error> {
Self::from_absolute_path(value)
}
}
thread_local! { thread_local! {
static ABSOLUTE_PATH_BASE: RefCell<Option<PathBuf>> = const { RefCell::new(None) }; static ABSOLUTE_PATH_BASE: RefCell<Option<PathBuf>> = const { RefCell::new(None) };
} }
@@ -96,18 +146,6 @@ impl<'de> Deserialize<'de> for AbsolutePathBuf {
} }
} }
impl AsRef<Path> for AbsolutePathBuf {
fn as_ref(&self) -> &Path {
self.as_path()
}
}
impl From<AbsolutePathBuf> for PathBuf {
fn from(path: AbsolutePathBuf) -> Self {
path.into_path_buf()
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;