From dcc592e216f6d59fb7190eec1dcd15c00b72fa1a Mon Sep 17 00:00:00 2001 From: viyatb-oai Date: Tue, 28 Apr 2026 18:39:34 -0700 Subject: [PATCH] feat(cli): add sandbox profile config controls Co-authored-by: Codex noreply@openai.com --- codex-rs/cli/src/debug_sandbox.rs | 140 +++++++++++++++--- codex-rs/cli/src/lib.rs | 52 +++++++ codex-rs/cli/src/main.rs | 35 +++++ codex-rs/config/src/loader/mod.rs | 51 ++++--- codex-rs/config/src/state.rs | 2 + .../core/src/config/config_loader_tests.rs | 46 ++++++ 6 files changed, 280 insertions(+), 46 deletions(-) diff --git a/codex-rs/cli/src/debug_sandbox.rs b/codex-rs/cli/src/debug_sandbox.rs index f458d22fdf..bbd4707a5e 100644 --- a/codex-rs/cli/src/debug_sandbox.rs +++ b/codex-rs/cli/src/debug_sandbox.rs @@ -6,6 +6,7 @@ mod seatbelt; use std::path::PathBuf; use std::process::Stdio; +use codex_config::LoaderOverrides; use codex_core::config::Config; use codex_core::config::ConfigBuilder; use codex_core::config::ConfigOverrides; @@ -45,14 +46,20 @@ pub async fn run_command_under_seatbelt( let SeatbeltCommand { full_auto, permissions_profile, + cwd, + include_managed_config, allow_unix_sockets, log_denials, config_overrides, command, } = command; run_command_under_sandbox( - full_auto, - permissions_profile, + DebugSandboxConfigOptions { + full_auto, + permissions_profile, + cwd, + include_managed_config, + }, command, config_overrides, codex_linux_sandbox_exe, @@ -79,12 +86,18 @@ pub async fn run_command_under_landlock( let LandlockCommand { full_auto, permissions_profile, + cwd, + include_managed_config, config_overrides, command, } = command; run_command_under_sandbox( - full_auto, - permissions_profile, + DebugSandboxConfigOptions { + full_auto, + permissions_profile, + cwd, + include_managed_config, + }, command, config_overrides, codex_linux_sandbox_exe, @@ -100,12 +113,18 @@ pub async fn run_command_under_windows( let WindowsCommand { full_auto, permissions_profile, + cwd, + include_managed_config, config_overrides, command, } = command; run_command_under_sandbox( - full_auto, - permissions_profile, + DebugSandboxConfigOptions { + full_auto, + permissions_profile, + cwd, + include_managed_config, + }, command, config_overrides, codex_linux_sandbox_exe, @@ -124,9 +143,22 @@ enum SandboxType { Windows, } -async fn run_command_under_sandbox( +#[derive(Debug)] +struct DebugSandboxConfigOptions { full_auto: bool, permissions_profile: Option, + cwd: Option, + include_managed_config: bool, +} + +#[derive(Debug, Clone, Copy)] +enum ManagedRequirementsMode { + Include, + Ignore, +} + +async fn run_command_under_sandbox( + config_options: DebugSandboxConfigOptions, command: Vec, config_overrides: CliConfigOverrides, codex_linux_sandbox_exe: Option, @@ -137,8 +169,7 @@ async fn run_command_under_sandbox( .parse_overrides() .map_err(anyhow::Error::msg)?, codex_linux_sandbox_exe, - full_auto, - permissions_profile, + config_options, ) .await?; @@ -588,14 +619,12 @@ mod windows_stdio_bridge { async fn load_debug_sandbox_config( cli_overrides: Vec<(String, TomlValue)>, codex_linux_sandbox_exe: Option, - full_auto: bool, - permissions_profile: Option, + options: DebugSandboxConfigOptions, ) -> anyhow::Result { load_debug_sandbox_config_with_codex_home( cli_overrides, codex_linux_sandbox_exe, - full_auto, - permissions_profile, + options, /*codex_home*/ None, ) .await @@ -604,10 +633,22 @@ async fn load_debug_sandbox_config( async fn load_debug_sandbox_config_with_codex_home( mut cli_overrides: Vec<(String, TomlValue)>, codex_linux_sandbox_exe: Option, - full_auto: bool, - permissions_profile: Option, + options: DebugSandboxConfigOptions, codex_home: Option, ) -> anyhow::Result { + let DebugSandboxConfigOptions { + full_auto, + permissions_profile, + cwd, + include_managed_config, + } = options; + + let managed_requirements_mode = if permissions_profile.is_some() && !include_managed_config { + ManagedRequirementsMode::Ignore + } else { + ManagedRequirementsMode::Include + }; + if let Some(permissions_profile) = permissions_profile { cli_overrides.push(( "default_permissions".to_string(), @@ -618,10 +659,12 @@ async fn load_debug_sandbox_config_with_codex_home( let config = build_debug_sandbox_config( cli_overrides.clone(), ConfigOverrides { + cwd: cwd.clone(), codex_linux_sandbox_exe: codex_linux_sandbox_exe.clone(), ..Default::default() }, codex_home.clone(), + managed_requirements_mode, ) .await?; @@ -638,10 +681,12 @@ async fn load_debug_sandbox_config_with_codex_home( cli_overrides, ConfigOverrides { sandbox_mode: Some(create_sandbox_mode(full_auto)), + cwd, codex_linux_sandbox_exe, ..Default::default() }, codex_home, + managed_requirements_mode, ) .await .map_err(Into::into) @@ -651,10 +696,17 @@ async fn build_debug_sandbox_config( cli_overrides: Vec<(String, TomlValue)>, harness_overrides: ConfigOverrides, codex_home: Option, + managed_requirements_mode: ManagedRequirementsMode, ) -> std::io::Result { let mut builder = ConfigBuilder::default() .cli_overrides(cli_overrides) .harness_overrides(harness_overrides); + if let ManagedRequirementsMode::Ignore = managed_requirements_mode { + builder = builder.loader_overrides(LoaderOverrides { + ignore_managed_requirements: true, + ..Default::default() + }); + } if let Some(codex_home) = codex_home { builder = builder .codex_home(codex_home.clone()) @@ -715,6 +767,7 @@ mod tests { Vec::new(), ConfigOverrides::default(), Some(codex_home_path.clone()), + ManagedRequirementsMode::Include, ) .await?; let legacy_config = build_debug_sandbox_config( @@ -724,14 +777,19 @@ mod tests { ..Default::default() }, Some(codex_home_path.clone()), + ManagedRequirementsMode::Include, ) .await?; let config = load_debug_sandbox_config_with_codex_home( Vec::new(), /*codex_linux_sandbox_exe*/ None, - /*full_auto*/ false, - /*permissions_profile*/ None, + DebugSandboxConfigOptions { + full_auto: false, + permissions_profile: None, + cwd: None, + include_managed_config: false, + }, Some(codex_home_path), ) .await?; @@ -765,8 +823,12 @@ mod tests { let err = load_debug_sandbox_config_with_codex_home( Vec::new(), /*codex_linux_sandbox_exe*/ None, - /*full_auto*/ true, - /*permissions_profile*/ None, + DebugSandboxConfigOptions { + full_auto: true, + permissions_profile: None, + cwd: None, + include_managed_config: false, + }, Some(codex_home.path().to_path_buf()), ) .await @@ -787,8 +849,12 @@ mod tests { let config = load_debug_sandbox_config_with_codex_home( Vec::new(), /*codex_linux_sandbox_exe*/ None, - /*full_auto*/ false, - Some(":workspace".to_string()), + DebugSandboxConfigOptions { + full_auto: false, + permissions_profile: Some(":workspace".to_string()), + cwd: None, + include_managed_config: false, + }, Some(codex_home.path().to_path_buf()), ) .await?; @@ -813,8 +879,12 @@ mod tests { let config = load_debug_sandbox_config_with_codex_home( Vec::new(), /*codex_linux_sandbox_exe*/ None, - /*full_auto*/ false, - Some("limited-read-test".to_string()), + DebugSandboxConfigOptions { + full_auto: false, + permissions_profile: Some("limited-read-test".to_string()), + cwd: None, + include_managed_config: false, + }, Some(codex_home.path().to_path_buf()), ) .await?; @@ -826,6 +896,7 @@ mod tests { )], ConfigOverrides::default(), Some(codex_home.path().to_path_buf()), + ManagedRequirementsMode::Include, ) .await?; @@ -836,4 +907,27 @@ mod tests { Ok(()) } + + #[tokio::test] + async fn debug_sandbox_uses_explicit_profile_cwd() -> anyhow::Result<()> { + let codex_home = TempDir::new()?; + let cwd = TempDir::new()?; + + let config = load_debug_sandbox_config_with_codex_home( + Vec::new(), + /*codex_linux_sandbox_exe*/ None, + DebugSandboxConfigOptions { + full_auto: false, + permissions_profile: Some(":workspace".to_string()), + cwd: Some(cwd.path().to_path_buf()), + include_managed_config: false, + }, + Some(codex_home.path().to_path_buf()), + ) + .await?; + + assert_eq!(config.cwd.as_path(), cwd.path()); + + Ok(()) + } } diff --git a/codex-rs/cli/src/lib.rs b/codex-rs/cli/src/lib.rs index 816ada1d53..b6d153361a 100644 --- a/codex-rs/cli/src/lib.rs +++ b/codex-rs/cli/src/lib.rs @@ -5,6 +5,7 @@ pub(crate) mod login; use clap::Parser; use codex_utils_absolute_path::AbsolutePathBuf; use codex_utils_cli::CliConfigOverrides; +use std::path::PathBuf; pub use debug_sandbox::run_command_under_landlock; pub use debug_sandbox::run_command_under_seatbelt; @@ -29,6 +30,23 @@ pub struct SeatbeltCommand { #[arg(long = "permissions-profile", value_name = "NAME")] pub permissions_profile: Option, + /// Working directory used for profile resolution and command execution. + #[arg( + short = 'C', + long = "cd", + value_name = "DIR", + requires = "permissions_profile" + )] + pub cwd: Option, + + /// Include managed requirements while resolving an explicit permissions profile. + #[arg( + long = "include-managed-config", + default_value_t = false, + requires = "permissions_profile" + )] + pub include_managed_config: bool, + /// Allow the sandboxed command to bind/connect AF_UNIX sockets rooted at this path. Relative paths are resolved against the current directory. Repeat to allow multiple paths. #[arg(long = "allow-unix-socket", value_parser = parse_allow_unix_socket_path)] pub allow_unix_sockets: Vec, @@ -60,6 +78,23 @@ pub struct LandlockCommand { #[arg(long = "permissions-profile", value_name = "NAME")] pub permissions_profile: Option, + /// Working directory used for profile resolution and command execution. + #[arg( + short = 'C', + long = "cd", + value_name = "DIR", + requires = "permissions_profile" + )] + pub cwd: Option, + + /// Include managed requirements while resolving an explicit permissions profile. + #[arg( + long = "include-managed-config", + default_value_t = false, + requires = "permissions_profile" + )] + pub include_managed_config: bool, + #[clap(skip)] pub config_overrides: CliConfigOverrides, @@ -78,6 +113,23 @@ pub struct WindowsCommand { #[arg(long = "permissions-profile", value_name = "NAME")] pub permissions_profile: Option, + /// Working directory used for profile resolution and command execution. + #[arg( + short = 'C', + long = "cd", + value_name = "DIR", + requires = "permissions_profile" + )] + pub cwd: Option, + + /// Include managed requirements while resolving an explicit permissions profile. + #[arg( + long = "include-managed-config", + default_value_t = false, + requires = "permissions_profile" + )] + pub include_managed_config: bool, + #[clap(skip)] pub config_overrides: CliConfigOverrides, diff --git a/codex-rs/cli/src/main.rs b/codex-rs/cli/src/main.rs index c1bebc408d..d98d163a38 100644 --- a/codex-rs/cli/src/main.rs +++ b/codex-rs/cli/src/main.rs @@ -1950,6 +1950,41 @@ mod tests { assert_eq!(command.command, vec!["echo"]); } + #[test] + fn sandbox_macos_parses_explicit_profile_controls() { + let cli = MultitoolCli::try_parse_from([ + "codex", + "sandbox", + "macos", + "--permissions-profile", + ":workspace", + "-C", + "/tmp", + "--include-managed-config", + "--", + "echo", + ]) + .expect("parse"); + + let Some(Subcommand::Sandbox(SandboxArgs { + cmd: SandboxCommand::Macos(command), + })) = cli.subcommand + else { + panic!("expected sandbox macos command"); + }; + + assert_eq!(command.cwd.as_deref(), Some(std::path::Path::new("/tmp"))); + assert!(command.include_managed_config); + } + + #[test] + fn sandbox_macos_rejects_explicit_profile_controls_without_profile() { + let err = MultitoolCli::try_parse_from(["codex", "sandbox", "macos", "-C", "/tmp"]) + .expect_err("parse should fail"); + + assert_eq!(err.kind(), clap::error::ErrorKind::MissingRequiredArgument); + } + #[test] fn plugin_marketplace_remove_parses_under_plugin() { let cli = diff --git a/codex-rs/config/src/loader/mod.rs b/codex-rs/config/src/loader/mod.rs index 28e5ff3426..74dd258eb9 100644 --- a/codex-rs/config/src/loader/mod.rs +++ b/codex-rs/config/src/loader/mod.rs @@ -92,41 +92,46 @@ pub async fn load_config_layers_state( cloud_requirements: CloudRequirementsLoader, thread_config_loader: &dyn ThreadConfigLoader, ) -> io::Result { + let ignore_managed_requirements = overrides.ignore_managed_requirements; let ignore_user_config = overrides.ignore_user_config; let ignore_user_and_project_exec_policy_rules = overrides.ignore_user_and_project_exec_policy_rules; let mut config_requirements_toml = ConfigRequirementsWithSources::default(); - if let Some(requirements) = cloud_requirements.get().await.map_err(io::Error::other)? { - merge_requirements_with_remote_sandbox_config( + if !ignore_managed_requirements { + if let Some(requirements) = cloud_requirements.get().await.map_err(io::Error::other)? { + merge_requirements_with_remote_sandbox_config( + &mut config_requirements_toml, + RequirementSource::CloudRequirements, + requirements, + ); + } + + #[cfg(target_os = "macos")] + macos::load_managed_admin_requirements_toml( &mut config_requirements_toml, - RequirementSource::CloudRequirements, - requirements, - ); + overrides + .macos_managed_config_requirements_base64 + .as_deref(), + ) + .await?; + + // Honor the system requirements.toml location. + let requirements_toml_file = system_requirements_toml_file_with_overrides(&overrides)?; + load_requirements_toml(fs, &mut config_requirements_toml, &requirements_toml_file).await?; } - #[cfg(target_os = "macos")] - macos::load_managed_admin_requirements_toml( - &mut config_requirements_toml, - overrides - .macos_managed_config_requirements_base64 - .as_deref(), - ) - .await?; - - // Honor the system requirements.toml location. - let requirements_toml_file = system_requirements_toml_file_with_overrides(&overrides)?; - load_requirements_toml(fs, &mut config_requirements_toml, &requirements_toml_file).await?; - // Make a best-effort to support the legacy `managed_config.toml` as a // requirements specification. let loaded_config_layers = layer_io::load_config_layers_internal(fs, codex_home, overrides.clone()).await?; - load_requirements_from_legacy_scheme( - &mut config_requirements_toml, - loaded_config_layers.clone(), - ) - .await?; + if !ignore_managed_requirements { + load_requirements_from_legacy_scheme( + &mut config_requirements_toml, + loaded_config_layers.clone(), + ) + .await?; + } let thread_config_context = ThreadConfigContext { thread_id: None, diff --git a/codex-rs/config/src/state.rs b/codex-rs/config/src/state.rs index 6bb846edd9..08ddbca95e 100644 --- a/codex-rs/config/src/state.rs +++ b/codex-rs/config/src/state.rs @@ -20,6 +20,7 @@ pub struct LoaderOverrides { pub managed_config_path: Option, pub system_config_path: Option, pub system_requirements_path: Option, + pub ignore_managed_requirements: bool, pub ignore_user_config: bool, pub ignore_user_and_project_exec_policy_rules: bool, //TODO(gt): Add a macos_ prefix to this field and remove the target_os check. @@ -38,6 +39,7 @@ impl LoaderOverrides { managed_config_path: Some(base.join("managed_config.toml")), system_config_path: Some(base.join("config.toml")), system_requirements_path: Some(base.join("requirements.toml")), + ignore_managed_requirements: false, ignore_user_config: false, ignore_user_and_project_exec_policy_rules: false, #[cfg(target_os = "macos")] diff --git a/codex-rs/core/src/config/config_loader_tests.rs b/codex-rs/core/src/config/config_loader_tests.rs index cc465d42b1..b0c9670ae4 100644 --- a/codex-rs/core/src/config/config_loader_tests.rs +++ b/codex-rs/core/src/config/config_loader_tests.rs @@ -1084,6 +1084,52 @@ async fn load_config_layers_includes_cloud_requirements() -> anyhow::Result<()> Ok(()) } +#[tokio::test] +async fn load_config_layers_can_ignore_managed_requirements() -> anyhow::Result<()> { + let tmp = tempdir()?; + let codex_home = tmp.path().join("home"); + tokio::fs::create_dir_all(&codex_home).await?; + let cwd = AbsolutePathBuf::from_absolute_path(tmp.path())?; + + let managed_config_path = tmp.path().join("managed_config.toml"); + tokio::fs::write(&managed_config_path, "approval_policy = \"never\"\n").await?; + let system_requirements_path = tmp.path().join("requirements.toml"); + tokio::fs::write( + &system_requirements_path, + "allowed_sandbox_modes = [\"read-only\"]\n", + ) + .await?; + + let mut overrides = LoaderOverrides::with_managed_config_path_for_tests(managed_config_path); + overrides.system_requirements_path = Some(system_requirements_path); + overrides.ignore_managed_requirements = true; + + let cloud_requirements = CloudRequirementsLoader::new(async { + Ok(Some(ConfigRequirementsToml { + allowed_approval_policies: Some(vec![AskForApproval::Never]), + ..Default::default() + })) + }); + + let layers = load_config_layers_state( + LOCAL_FS.as_ref(), + &codex_home, + Some(cwd), + &[] as &[(String, TomlValue)], + overrides, + cloud_requirements, + &codex_config::NoopThreadConfigLoader, + ) + .await?; + + assert_eq!( + layers.requirements_toml(), + &ConfigRequirementsToml::default() + ); + + Ok(()) +} + #[tokio::test] async fn load_config_layers_includes_cloud_hook_requirements() -> anyhow::Result<()> { let tmp = tempdir()?;