diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index 308e9b24a0..5d17c0f603 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -2816,6 +2816,7 @@ dependencies = [ "cc", "clap", "codex-core", + "codex-process-hardening", "codex-protocol", "codex-sandboxing", "codex-utils-absolute-path", @@ -4094,6 +4095,7 @@ dependencies = [ "assert_cmd", "base64 0.22.1", "codex-arg0", + "codex-config", "codex-core", "codex-exec-server", "codex-features", diff --git a/codex-rs/config/src/config_toml.rs b/codex-rs/config/src/config_toml.rs index a1692b2cb1..3bd19f5568 100644 --- a/codex-rs/config/src/config_toml.rs +++ b/codex-rs/config/src/config_toml.rs @@ -114,7 +114,8 @@ pub struct ConfigToml { /// Sandbox configuration to apply if `sandbox` is `WorkspaceWrite`. pub sandbox_workspace_write: Option, - /// Default named permissions profile to apply from the `[permissions]` + /// Default permissions profile to apply. Names starting with `:` refer to + /// built-in profiles; other names are resolved from the `[permissions]` /// table. pub default_permissions: Option, diff --git a/codex-rs/core/config.schema.json b/codex-rs/core/config.schema.json index 70e49d34ec..f0e7c23c3b 100644 --- a/codex-rs/core/config.schema.json +++ b/codex-rs/core/config.schema.json @@ -3194,7 +3194,7 @@ "type": "string" }, "default_permissions": { - "description": "Default named permissions profile to apply from the `[permissions]` table.", + "description": "Default permissions profile to apply. Names starting with `:` refer to built-in profiles; other names are resolved from the `[permissions]` table.", "type": "string" }, "developer_instructions": { diff --git a/codex-rs/core/src/config/config_tests.rs b/codex-rs/core/src/config/config_tests.rs index 17246ae774..c9ddaa1bab 100644 --- a/codex-rs/core/src/config/config_tests.rs +++ b/codex-rs/core/src/config/config_tests.rs @@ -50,6 +50,8 @@ use codex_config::types::ToolSuggestDiscoverableType; use codex_config::types::Tui; use codex_config::types::TuiKeymap; use codex_config::types::TuiNotificationSettings; +use codex_config::types::WindowsSandboxModeToml; +use codex_config::types::WindowsToml; use codex_exec_server::LOCAL_FS; use codex_features::Feature; use codex_features::FeaturesToml; @@ -666,8 +668,55 @@ allow_upstream_proxy = false } #[tokio::test] -async fn permissions_profiles_network_populates_runtime_network_proxy_spec() -> std::io::Result<()> -{ +async fn permissions_profiles_network_enabled_allows_runtime_network_without_proxy() +-> std::io::Result<()> { + let codex_home = TempDir::new()?; + let cwd = TempDir::new()?; + std::fs::write(cwd.path().join(".git"), "gitdir: nowhere")?; + + let config = Config::load_from_base_config_with_overrides( + ConfigToml { + default_permissions: Some("workspace".to_string()), + permissions: Some(PermissionsToml { + entries: BTreeMap::from([( + "workspace".to_string(), + PermissionProfileToml { + filesystem: Some(FilesystemPermissionsToml { + glob_scan_max_depth: None, + entries: BTreeMap::from([( + ":minimal".to_string(), + FilesystemPermissionToml::Access(FileSystemAccessMode::Read), + )]), + }), + network: Some(NetworkToml { + enabled: Some(true), + ..Default::default() + }), + }, + )]), + }), + ..Default::default() + }, + ConfigOverrides { + cwd: Some(cwd.path().to_path_buf()), + ..Default::default() + }, + codex_home.abs(), + ) + .await?; + assert_eq!( + config.permissions.network_sandbox_policy(), + NetworkSandboxPolicy::Enabled + ); + assert!( + config.permissions.network.is_none(), + "bare profile network.enabled should not start the managed network proxy" + ); + Ok(()) +} + +#[tokio::test] +async fn permissions_profiles_proxy_policy_starts_managed_network_proxy() -> std::io::Result<()> { let codex_home = TempDir::new()?; let cwd = TempDir::new()?; std::fs::write(cwd.path().join(".git"), "gitdir: nowhere")?; @@ -704,14 +753,20 @@ async fn permissions_profiles_network_populates_runtime_network_proxy_spec() -> codex_home.abs(), ) .await?; + assert_eq!( + config.permissions.network_sandbox_policy(), + NetworkSandboxPolicy::Enabled + ); let network = config .permissions .network .as_ref() - .expect("enabled profile network should produce a NetworkProxySpec"); - + .expect("profile proxy policy should start the managed network proxy"); assert_eq!(network.proxy_host_and_port(), "127.0.0.1:43128"); - assert!(!network.socks_enabled()); + assert!( + !network.socks_enabled(), + "profile proxy policy should preserve SOCKS config" + ); Ok(()) } @@ -1019,7 +1074,8 @@ async fn permission_profile_override_applies_runtime_roots_to_legacy_projection( } #[tokio::test] -async fn permission_profile_override_preserves_configured_network_proxy() -> std::io::Result<()> { +async fn permission_profile_override_preserves_configured_network_policy_without_starting_proxy() +-> std::io::Result<()> { let codex_home = TempDir::new()?; let cwd = TempDir::new()?; let permission_profile = PermissionProfile::Disabled; @@ -1064,14 +1120,10 @@ async fn permission_profile_override_preserves_configured_network_proxy() -> std codex_home.abs(), ) .await?; - let network = config - .permissions - .network - .as_ref() - .expect("network-enabled override should preserve configured proxy"); - - assert_eq!(network.proxy_host_and_port(), "127.0.0.1:43128"); - assert!(!network.socks_enabled()); + assert!( + config.permissions.network.is_none(), + "profile network.enabled should not start the managed network proxy" + ); assert_eq!(config.permissions.permission_profile(), permission_profile); Ok(()) } @@ -1194,6 +1246,306 @@ async fn permissions_profiles_require_default_permissions() -> std::io::Result<( Ok(()) } +#[tokio::test] +async fn default_permissions_can_select_builtin_profile_without_permissions_table() +-> std::io::Result<()> { + let codex_home = TempDir::new()?; + let cwd = TempDir::new()?; + + let config = Config::load_from_base_config_with_overrides( + ConfigToml { + default_permissions: Some(":workspace".to_string()), + ..Default::default() + }, + ConfigOverrides { + cwd: Some(cwd.path().to_path_buf()), + ..Default::default() + }, + codex_home.abs(), + ) + .await?; + + let policy = config.permissions.file_system_sandbox_policy(); + assert!( + policy.can_write_path_with_cwd(cwd.path(), cwd.path()), + "expected :workspace to allow writing the project root, policy: {policy:?}" + ); + assert!( + !policy.can_write_path_with_cwd(&cwd.path().join(".git"), cwd.path()), + "expected :workspace to protect project metadata, policy: {policy:?}" + ); + Ok(()) +} + +#[tokio::test] +async fn empty_config_defaults_to_builtin_profile_for_trusted_project() -> std::io::Result<()> { + let codex_home = TempDir::new()?; + let cwd = TempDir::new()?; + let project_key = cwd.path().to_string_lossy().to_string(); + + let config = Config::load_from_base_config_with_overrides( + ConfigToml { + projects: Some(HashMap::from([( + project_key, + ProjectConfig { + trust_level: Some(TrustLevel::Trusted), + }, + )])), + ..Default::default() + }, + ConfigOverrides { + cwd: Some(cwd.path().to_path_buf()), + ..Default::default() + }, + codex_home.abs(), + ) + .await?; + + let policy = config.permissions.file_system_sandbox_policy(); + if cfg!(target_os = "windows") { + assert!( + !policy.can_write_path_with_cwd(cwd.path(), cwd.path()), + "expected trusted project fallback to stay read-only without Windows sandbox support, policy: {policy:?}" + ); + } else { + assert!( + policy.can_write_path_with_cwd(cwd.path(), cwd.path()), + "expected trusted project fallback to use :workspace, policy: {policy:?}" + ); + assert!( + !policy.can_write_path_with_cwd(&cwd.path().join(".codex"), cwd.path()), + "expected :workspace metadata carveouts, policy: {policy:?}" + ); + } + Ok(()) +} + +#[tokio::test] +async fn implicit_builtin_workspace_profile_preserves_sandbox_workspace_write_settings() +-> std::io::Result<()> { + let codex_home = TempDir::new()?; + let cwd = TempDir::new()?; + let extra_root = TempDir::new()?; + let extra_root = extra_root.path().abs(); + let project_key = cwd.path().to_string_lossy().to_string(); + + let config = Config::load_from_base_config_with_overrides( + ConfigToml { + projects: Some(HashMap::from([( + project_key, + ProjectConfig { + trust_level: Some(TrustLevel::Trusted), + }, + )])), + sandbox_workspace_write: Some(SandboxWorkspaceWrite { + writable_roots: vec![extra_root.clone()], + network_access: true, + exclude_tmpdir_env_var: true, + exclude_slash_tmp: false, + }), + windows: Some(WindowsToml { + sandbox: Some(WindowsSandboxModeToml::Elevated), + sandbox_private_desktop: None, + }), + ..Default::default() + }, + ConfigOverrides { + cwd: Some(cwd.path().to_path_buf()), + ..Default::default() + }, + codex_home.abs(), + ) + .await?; + + let policy = config.permissions.file_system_sandbox_policy(); + assert!( + policy.can_write_path_with_cwd(extra_root.as_path(), cwd.path()), + "expected implicit :workspace to preserve sandbox_workspace_write.writable_roots, policy: {policy:?}" + ); + assert_eq!( + config.permissions.network_sandbox_policy(), + NetworkSandboxPolicy::Enabled + ); + match config.legacy_sandbox_policy() { + SandboxPolicy::WorkspaceWrite { + writable_roots, + network_access, + exclude_tmpdir_env_var, + exclude_slash_tmp, + } => { + assert!(writable_roots.contains(&extra_root)); + assert!(network_access); + assert!(exclude_tmpdir_env_var); + assert!(!exclude_slash_tmp); + } + sandbox_policy => panic!("expected workspace-write projection, got {sandbox_policy:?}"), + } + Ok(()) +} + +#[tokio::test] +async fn implicit_builtin_workspace_profile_preserves_add_dir_metadata_carveouts() +-> std::io::Result<()> { + let codex_home = TempDir::new()?; + let cwd = TempDir::new()?; + let extra_root = TempDir::new()?; + for subpath in [".git", ".agents", ".codex"] { + std::fs::create_dir_all(extra_root.path().join(subpath))?; + } + let project_key = cwd.path().to_string_lossy().to_string(); + + let config = Config::load_from_base_config_with_overrides( + ConfigToml { + projects: Some(HashMap::from([( + project_key, + ProjectConfig { + trust_level: Some(TrustLevel::Trusted), + }, + )])), + windows: Some(WindowsToml { + sandbox: Some(WindowsSandboxModeToml::Elevated), + sandbox_private_desktop: None, + }), + ..Default::default() + }, + ConfigOverrides { + cwd: Some(cwd.path().to_path_buf()), + additional_writable_roots: vec![extra_root.path().to_path_buf()], + ..Default::default() + }, + codex_home.abs(), + ) + .await?; + + let policy = config.permissions.file_system_sandbox_policy(); + let extra_root = extra_root.path().abs(); + assert!( + policy.can_write_path_with_cwd(extra_root.as_path(), cwd.path()), + "expected implicit :workspace to preserve additional writable roots, policy: {policy:?}" + ); + for subpath in [".git", ".agents", ".codex"] { + assert!( + !policy.can_write_path_with_cwd(&extra_root.join(subpath), cwd.path()), + "expected implicit :workspace to preserve legacy metadata carveout for {subpath}, \ + policy: {policy:?}" + ); + } + Ok(()) +} + +#[tokio::test] +async fn empty_config_defaults_to_builtin_read_only_without_trust_decision() -> std::io::Result<()> +{ + let codex_home = TempDir::new()?; + let cwd = TempDir::new()?; + + let config = Config::load_from_base_config_with_overrides( + ConfigToml::default(), + ConfigOverrides { + cwd: Some(cwd.path().to_path_buf()), + ..Default::default() + }, + codex_home.abs(), + ) + .await?; + + let policy = config.permissions.file_system_sandbox_policy(); + assert!( + policy.can_read_path_with_cwd(cwd.path(), cwd.path()), + "expected :read-only to allow reads, policy: {policy:?}" + ); + assert!( + !policy.can_write_path_with_cwd(cwd.path(), cwd.path()), + "expected :read-only to deny writes, policy: {policy:?}" + ); + Ok(()) +} + +#[tokio::test] +async fn default_permissions_can_select_builtin_no_sandbox_profile() -> std::io::Result<()> { + let codex_home = TempDir::new()?; + let cwd = TempDir::new()?; + + let config = Config::load_from_base_config_with_overrides( + ConfigToml { + default_permissions: Some(":danger-no-sandbox".to_string()), + ..Default::default() + }, + ConfigOverrides { + cwd: Some(cwd.path().to_path_buf()), + ..Default::default() + }, + codex_home.abs(), + ) + .await?; + + assert_eq!( + config.permissions.permission_profile(), + PermissionProfile::Disabled + ); + Ok(()) +} + +#[tokio::test] +async fn user_defined_permission_profile_names_cannot_use_builtin_prefix() -> std::io::Result<()> { + let codex_home = TempDir::new()?; + let cwd = TempDir::new()?; + + let err = Config::load_from_base_config_with_overrides( + ConfigToml { + default_permissions: Some(":custom".to_string()), + permissions: Some(PermissionsToml { + entries: BTreeMap::from([( + ":custom".to_string(), + PermissionProfileToml::default(), + )]), + }), + ..Default::default() + }, + ConfigOverrides { + cwd: Some(cwd.path().to_path_buf()), + ..Default::default() + }, + codex_home.abs(), + ) + .await + .expect_err("reserved profile name should be rejected"); + + assert_eq!(err.kind(), std::io::ErrorKind::InvalidInput); + assert_eq!( + err.to_string(), + "permissions profile `:custom` uses a reserved built-in profile prefix" + ); + Ok(()) +} + +#[tokio::test] +async fn unknown_builtin_permission_profile_name_is_rejected() -> std::io::Result<()> { + let codex_home = TempDir::new()?; + let cwd = TempDir::new()?; + + let err = Config::load_from_base_config_with_overrides( + ConfigToml { + default_permissions: Some(":unknown".to_string()), + ..Default::default() + }, + ConfigOverrides { + cwd: Some(cwd.path().to_path_buf()), + ..Default::default() + }, + codex_home.abs(), + ) + .await + .expect_err("unknown built-in profile name should be rejected"); + + assert_eq!(err.kind(), std::io::ErrorKind::InvalidInput); + assert_eq!( + err.to_string(), + "default_permissions refers to unknown built-in profile `:unknown`" + ); + Ok(()) +} + #[tokio::test] async fn permissions_profiles_allow_direct_write_roots_outside_workspace_root() -> std::io::Result<()> { diff --git a/codex-rs/core/src/config/mod.rs b/codex-rs/core/src/config/mod.rs index ef5ba7cadc..dcf443b84e 100644 --- a/codex-rs/core/src/config/mod.rs +++ b/codex-rs/core/src/config/mod.rs @@ -100,9 +100,12 @@ use std::path::Path; use std::path::PathBuf; use std::sync::Arc; -use crate::config::permissions::compile_permission_profile; +use crate::config::permissions::builtin_permission_profile; +use crate::config::permissions::compile_permission_profile_selection; +use crate::config::permissions::default_builtin_permission_profile_name; use crate::config::permissions::get_readable_roots_required_for_codex_runtime; -use crate::config::permissions::network_proxy_config_from_profile_network; +use crate::config::permissions::network_proxy_config_for_profile_selection; +use crate::config::permissions::validate_user_permission_profile_names; use codex_network_proxy::NetworkProxyConfig; use toml::Value as TomlValue; use toml_edit::DocumentMut; @@ -319,6 +322,18 @@ impl Permissions { } } +// A profile override only inherits the selected profile's proxy/allowlist config +// when Codex is still responsible for the network policy. `Disabled` means no +// outer sandbox, so starting the managed proxy would narrow the override. +fn profile_allows_configured_network_proxy(permission_profile: &PermissionProfile) -> bool { + match permission_profile { + PermissionProfile::Managed { network, .. } | PermissionProfile::External { network } => { + network.is_enabled() + } + PermissionProfile::Disabled => false, + } +} + /// Configured thread persistence backend. #[derive(Debug, Clone, PartialEq, Eq, Default)] pub enum ThreadStoreConfig { @@ -1886,6 +1901,7 @@ impl Config { .permissions .as_ref() .is_some_and(|profiles| !profiles.is_empty()); + validate_user_permission_profile_names(cfg.permissions.as_ref())?; if has_permission_profiles && !matches!( permission_config_syntax, @@ -1916,8 +1932,8 @@ impl Config { let profiles_are_active = matches!( permission_config_syntax, Some(PermissionConfigSyntax::Profiles) - ) || (permission_config_syntax.is_none() - && has_permission_profiles); + ) || permission_config_syntax.is_none(); + let using_implicit_builtin_profile = permission_config_syntax.is_none(); let ( configured_network_proxy_config, permission_profile, @@ -1926,25 +1942,22 @@ impl Config { let (mut file_system_sandbox_policy, network_sandbox_policy) = permission_profile.to_runtime_permissions(); let configured_network_proxy_config = - if network_sandbox_policy.is_enabled() && profiles_are_active { - let permissions = cfg.permissions.as_ref().ok_or_else(|| { - std::io::Error::new( - std::io::ErrorKind::InvalidInput, - "default_permissions requires a `[permissions]` table", - ) - })?; - let default_permissions = cfg.default_permissions.as_deref().ok_or_else(|| { - std::io::Error::new( - std::io::ErrorKind::InvalidInput, - "default_permissions requires a named permissions profile", - ) - })?; - let profile = resolve_permission_profile(permissions, default_permissions)?; - + if profile_allows_configured_network_proxy(&permission_profile) + && profiles_are_active + { // PermissionProfile carries the active network sandbox bit, not the configured // proxy/allowlist policy. Keep that config so active profiles can round-trip // without broadening network behavior. - network_proxy_config_from_profile_network(profile.network.as_ref()) + let default_permissions = cfg.default_permissions.as_deref().unwrap_or_else(|| { + default_builtin_permission_profile_name( + &active_project, + windows_sandbox_level, + ) + }); + network_proxy_config_for_profile_selection( + cfg.permissions.as_ref(), + default_permissions, + )? } else { NetworkProxyConfig::default() }; @@ -1972,32 +1985,31 @@ impl Config { file_system_sandbox_policy, ) } else if profiles_are_active { - let permissions = cfg.permissions.as_ref().ok_or_else(|| { - std::io::Error::new( - std::io::ErrorKind::InvalidInput, - "default_permissions requires a `[permissions]` table", - ) - })?; - let default_permissions = cfg.default_permissions.as_deref().ok_or_else(|| { - std::io::Error::new( - std::io::ErrorKind::InvalidInput, - "default_permissions requires a named permissions profile", - ) - })?; - let profile = resolve_permission_profile(permissions, default_permissions)?; - let configured_network_proxy_config = - network_proxy_config_from_profile_network(profile.network.as_ref()); + let default_permissions = cfg.default_permissions.as_deref().unwrap_or_else(|| { + default_builtin_permission_profile_name(&active_project, windows_sandbox_level) + }); + let configured_network_proxy_config = network_proxy_config_for_profile_selection( + cfg.permissions.as_ref(), + default_permissions, + )?; let (mut file_system_sandbox_policy, network_sandbox_policy) = - compile_permission_profile( - permissions, + compile_permission_profile_selection( + cfg.permissions.as_ref(), default_permissions, + cfg.sandbox_workspace_write.as_ref(), resolved_cwd.as_path(), &mut startup_warnings, )?; - let mut permission_profile = PermissionProfile::from_runtime_permissions( - &file_system_sandbox_policy, - network_sandbox_policy, - ); + let mut permission_profile = if let Some(permission_profile) = + builtin_permission_profile(default_permissions, cfg.sandbox_workspace_write.as_ref()) + { + permission_profile + } else { + PermissionProfile::from_runtime_permissions( + &file_system_sandbox_policy, + network_sandbox_policy, + ) + }; let sandbox_policy = compatibility_sandbox_policy_for_permission_profile( &permission_profile, &file_system_sandbox_policy, @@ -2005,11 +2017,17 @@ impl Config { resolved_cwd.as_path(), ); if matches!(sandbox_policy, SandboxPolicy::WorkspaceWrite { .. }) { - file_system_sandbox_policy = file_system_sandbox_policy - .with_additional_writable_roots( + file_system_sandbox_policy = if using_implicit_builtin_profile { + file_system_sandbox_policy + .with_additional_legacy_workspace_writable_roots( + &additional_writable_roots, + ) + } else { + file_system_sandbox_policy.with_additional_writable_roots( resolved_cwd.as_path(), &additional_writable_roots, - ); + ) + }; permission_profile = PermissionProfile::from_runtime_permissions( &file_system_sandbox_policy, network_sandbox_policy, diff --git a/codex-rs/core/src/config/permissions.rs b/codex-rs/core/src/config/permissions.rs index 6d938e9185..b51a8973a3 100644 --- a/codex-rs/core/src/config/permissions.rs +++ b/codex-rs/core/src/config/permissions.rs @@ -9,9 +9,12 @@ use codex_config::permissions_toml::FilesystemPermissionsToml; use codex_config::permissions_toml::NetworkToml; use codex_config::permissions_toml::PermissionProfileToml; use codex_config::permissions_toml::PermissionsToml; +use codex_config::types::SandboxWorkspaceWrite; use codex_network_proxy::NetworkProxyConfig; #[cfg(test)] use codex_network_proxy::NetworkUnixSocketPermission as ProxyNetworkUnixSocketPermission; +use codex_protocol::config_types::WindowsSandboxLevel; +use codex_protocol::models::PermissionProfile; use codex_protocol::permissions::FileSystemAccessMode; use codex_protocol::permissions::FileSystemPath; use codex_protocol::permissions::FileSystemSandboxEntry; @@ -20,13 +23,120 @@ use codex_protocol::permissions::FileSystemSpecialPath; use codex_protocol::permissions::NetworkSandboxPolicy; use codex_utils_absolute_path::AbsolutePathBuf; +use super::ProjectConfig; + +pub(crate) const BUILT_IN_READ_ONLY_PROFILE: &str = ":read-only"; +pub(crate) const BUILT_IN_WORKSPACE_PROFILE: &str = ":workspace"; +pub(crate) const BUILT_IN_DANGER_NO_SANDBOX_PROFILE: &str = ":danger-no-sandbox"; + +pub(crate) fn default_builtin_permission_profile_name( + active_project: &ProjectConfig, + windows_sandbox_level: WindowsSandboxLevel, +) -> &'static str { + if (active_project.is_trusted() || active_project.is_untrusted()) + && !(cfg!(target_os = "windows") && windows_sandbox_level == WindowsSandboxLevel::Disabled) + { + BUILT_IN_WORKSPACE_PROFILE + } else { + BUILT_IN_READ_ONLY_PROFILE + } +} + +pub(crate) fn is_builtin_permission_profile_name(profile_name: &str) -> bool { + matches!( + profile_name, + BUILT_IN_READ_ONLY_PROFILE + | BUILT_IN_WORKSPACE_PROFILE + | BUILT_IN_DANGER_NO_SANDBOX_PROFILE + ) +} + +pub(crate) fn builtin_permission_profile( + profile_name: &str, + workspace_write: Option<&SandboxWorkspaceWrite>, +) -> Option { + match profile_name { + BUILT_IN_READ_ONLY_PROFILE => Some(PermissionProfile::read_only()), + BUILT_IN_WORKSPACE_PROFILE => Some(match workspace_write { + Some(SandboxWorkspaceWrite { + writable_roots, + network_access, + exclude_tmpdir_env_var, + exclude_slash_tmp, + }) => PermissionProfile::workspace_write_with( + writable_roots, + if *network_access { + NetworkSandboxPolicy::Enabled + } else { + NetworkSandboxPolicy::Restricted + }, + *exclude_tmpdir_env_var, + *exclude_slash_tmp, + ), + None => PermissionProfile::workspace_write(), + }), + BUILT_IN_DANGER_NO_SANDBOX_PROFILE => Some(PermissionProfile::Disabled), + _ => None, + } +} + +pub(crate) fn validate_user_permission_profile_names( + permissions: Option<&PermissionsToml>, +) -> io::Result<()> { + let Some(permissions) = permissions else { + return Ok(()); + }; + + for profile_name in permissions.entries.keys() { + if profile_name.starts_with(':') { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!( + "permissions profile `{profile_name}` uses a reserved built-in profile prefix" + ), + )); + } + } + + Ok(()) +} + pub(crate) fn network_proxy_config_from_profile_network( network: Option<&NetworkToml>, ) -> NetworkProxyConfig { - network.map_or_else( + let mut config = network.map_or_else( NetworkProxyConfig::default, NetworkToml::to_network_proxy_config, - ) + ); + // Profile `network.enabled` controls sandbox network access. Do not start a + // managed proxy for that bit alone, but keep the proxy enabled when the + // profile also supplied policy that only the proxy can enforce. + config.network.enabled = network.is_some_and(profile_network_requires_proxy); + config +} + +fn profile_network_requires_proxy(network: &NetworkToml) -> bool { + if network.enabled != Some(true) { + return false; + } + + network.proxy_url.is_some() + || network.enable_socks5 == Some(true) + || network.socks_url.is_some() + || network.enable_socks5_udp == Some(true) + || network.allow_upstream_proxy == Some(true) + || network.dangerously_allow_non_loopback_proxy == Some(true) + || network.dangerously_allow_all_unix_sockets == Some(true) + || network.mode.is_some() + || network + .domains + .as_ref() + .is_some_and(|domains| !domains.is_empty()) + || network + .unix_sockets + .as_ref() + .is_some_and(|unix_sockets| !unix_sockets.is_empty()) + || network.allow_local_binding == Some(true) } pub(crate) fn resolve_permission_profile<'a>( @@ -41,6 +151,27 @@ pub(crate) fn resolve_permission_profile<'a>( }) } +pub(crate) fn network_proxy_config_for_profile_selection( + permissions: Option<&PermissionsToml>, + profile_name: &str, +) -> io::Result { + if is_builtin_permission_profile_name(profile_name) { + return Ok(NetworkProxyConfig::default()); + } + reject_unknown_builtin_permission_profile(profile_name)?; + + let permissions = permissions.ok_or_else(|| { + io::Error::new( + io::ErrorKind::InvalidInput, + "default_permissions requires a `[permissions]` table", + ) + })?; + let profile = resolve_permission_profile(permissions, profile_name)?; + Ok(network_proxy_config_from_profile_network( + profile.network.as_ref(), + )) +} + pub(crate) fn compile_permission_profile( permissions: &PermissionsToml, profile_name: &str, @@ -103,6 +234,38 @@ pub(crate) fn compile_permission_profile( Ok((file_system_sandbox_policy, network_sandbox_policy)) } +pub(crate) fn compile_permission_profile_selection( + permissions: Option<&PermissionsToml>, + profile_name: &str, + workspace_write: Option<&SandboxWorkspaceWrite>, + policy_cwd: &Path, + startup_warnings: &mut Vec, +) -> io::Result<(FileSystemSandboxPolicy, NetworkSandboxPolicy)> { + if let Some(permission_profile) = builtin_permission_profile(profile_name, workspace_write) { + return Ok(permission_profile.to_runtime_permissions()); + } + reject_unknown_builtin_permission_profile(profile_name)?; + + let permissions = permissions.ok_or_else(|| { + io::Error::new( + io::ErrorKind::InvalidInput, + "default_permissions requires a `[permissions]` table", + ) + })?; + compile_permission_profile(permissions, profile_name, policy_cwd, startup_warnings) +} + +fn reject_unknown_builtin_permission_profile(profile_name: &str) -> io::Result<()> { + if profile_name.starts_with(':') { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!("default_permissions refers to unknown built-in profile `{profile_name}`"), + )); + } + + Ok(()) +} + /// Returns a list of paths that must be readable by shell tools in order /// for Codex to function. These should always be added to the /// `FileSystemSandboxPolicy` for a thread. diff --git a/codex-rs/core/src/config/permissions_tests.rs b/codex-rs/core/src/config/permissions_tests.rs index e021db3d2d..7dd2bcdefe 100644 --- a/codex-rs/core/src/config/permissions_tests.rs +++ b/codex-rs/core/src/config/permissions_tests.rs @@ -236,6 +236,45 @@ fn network_toml_overlays_unix_socket_permissions_by_path() { ); } +#[test] +fn profile_network_proxy_config_keeps_proxy_disabled_for_bare_network_access() { + let config = network_proxy_config_from_profile_network(Some(&NetworkToml { + enabled: Some(true), + ..Default::default() + })); + + assert!(!config.network.enabled); +} + +#[test] +fn profile_network_proxy_config_enables_proxy_for_proxy_policy() { + let config = network_proxy_config_from_profile_network(Some(&NetworkToml { + enabled: Some(true), + proxy_url: Some("http://127.0.0.1:43128".to_string()), + enable_socks5: Some(false), + domains: Some(NetworkDomainPermissionsToml { + entries: BTreeMap::from([( + "openai.com".to_string(), + NetworkDomainPermissionToml::Allow, + )]), + }), + ..Default::default() + })); + + assert!(config.network.enabled); + assert_eq!(config.network.proxy_url, "http://127.0.0.1:43128"); + assert!(!config.network.enable_socks5); + assert_eq!( + config.network.domains, + Some(codex_network_proxy::NetworkDomainPermissions { + entries: vec![codex_network_proxy::NetworkDomainPermissionEntry { + pattern: "openai.com".to_string(), + permission: codex_network_proxy::NetworkDomainPermission::Allow, + }], + }) + ); +} + #[test] fn read_write_glob_warnings_skip_supported_deny_read_globs_and_trailing_subpaths() { let filesystem = FilesystemPermissionsToml { diff --git a/codex-rs/core/src/session/turn.rs b/codex-rs/core/src/session/turn.rs index ec5705adfc..823dcbfc3d 100644 --- a/codex-rs/core/src/session/turn.rs +++ b/codex-rs/core/src/session/turn.rs @@ -1864,6 +1864,11 @@ async fn try_run_sampling_request( otel.name = field::Empty, tool_name = field::Empty, from = field::Empty, + gen_ai.usage.input_tokens = field::Empty, + gen_ai.usage.cache_read.input_tokens = field::Empty, + gen_ai.usage.output_tokens = field::Empty, + codex.usage.reasoning_output_tokens = field::Empty, + codex.usage.total_tokens = field::Empty, ); let event = match stream diff --git a/codex-rs/core/src/state/turn.rs b/codex-rs/core/src/state/turn.rs index 1439e28bb3..0dd0c09379 100644 --- a/codex-rs/core/src/state/turn.rs +++ b/codex-rs/core/src/state/turn.rs @@ -79,17 +79,25 @@ pub(crate) struct RunningTask { pub(crate) _timer: Option, } +pub(crate) struct RemovedTask { + pub(crate) records_turn_token_usage_on_span: bool, + pub(crate) active_turn_is_empty: bool, +} + impl ActiveTurn { pub(crate) fn add_task(&mut self, task: RunningTask) { let sub_id = task.turn_context.sub_id.clone(); self.tasks.insert(sub_id, task); } - pub(crate) fn remove_task(&mut self, sub_id: &str) -> bool { - if let Some(task) = self.tasks.swap_remove(sub_id) { - task.handle.detach(); - } - self.tasks.is_empty() + pub(crate) fn remove_task(&mut self, sub_id: &str) -> Option { + let task = self.tasks.swap_remove(sub_id)?; + let records_turn_token_usage_on_span = task.task.records_turn_token_usage_on_span(); + task.handle.detach(); + Some(RemovedTask { + records_turn_token_usage_on_span, + active_turn_is_empty: self.tasks.is_empty(), + }) } pub(crate) fn drain_tasks(&mut self) -> Vec { diff --git a/codex-rs/core/src/tasks/mod.rs b/codex-rs/core/src/tasks/mod.rs index e0286f3090..91078c50ce 100644 --- a/codex-rs/core/src/tasks/mod.rs +++ b/codex-rs/core/src/tasks/mod.rs @@ -14,6 +14,8 @@ use tokio::sync::Notify; use tokio_util::sync::CancellationToken; use tokio_util::task::AbortOnDropHandle; use tracing::Instrument; +use tracing::Span; +use tracing::field; use tracing::info_span; use tracing::trace; use tracing::warn; @@ -188,6 +190,11 @@ pub(crate) trait SessionTask: Send + Sync + 'static { /// Returns the tracing name for a spawned task span. fn span_name(&self) -> &'static str; + /// Returns whether turn token usage should be recorded on this task's turn span. + fn records_turn_token_usage_on_span(&self) -> bool { + false + } + /// Executes the task until completion or cancellation. /// /// Implementations typically stream protocol events using `session` and @@ -225,6 +232,8 @@ pub(crate) trait AnySessionTask: Send + Sync + 'static { fn span_name(&self) -> &'static str; + fn records_turn_token_usage_on_span(&self) -> bool; + fn run( self: Arc, session: Arc, @@ -252,6 +261,10 @@ where SessionTask::span_name(self) } + fn records_turn_token_usage_on_span(&self) -> bool { + SessionTask::records_turn_token_usage_on_span(self) + } + fn run( self: Arc, session: Arc, @@ -361,6 +374,12 @@ impl Session { thread.id = %self.conversation_id, turn.id = %turn_context.sub_id, model = %turn_context.model_info.slug, + codex.turn.token_usage.input_tokens = field::Empty, + codex.turn.token_usage.cached_input_tokens = field::Empty, + codex.turn.token_usage.non_cached_input_tokens = field::Empty, + codex.turn.token_usage.output_tokens = field::Empty, + codex.turn.token_usage.reasoning_output_tokens = field::Empty, + codex.turn.token_usage.total_tokens = field::Empty, ); let handle = tokio::spawn( async move { @@ -548,14 +567,20 @@ impl Session { let mut token_usage_at_turn_start = None; let mut turn_had_memory_citation = false; let mut turn_tool_calls = 0_u64; + let mut records_turn_token_usage_on_span = false; let turn_state = { let mut active = self.active_turn.lock().await; if let Some(at) = active.as_mut() - && at.remove_task(&turn_context.sub_id) + && let Some(removed_task) = at.remove_task(&turn_context.sub_id) { - should_clear_active_turn = true; - let turn_state = Arc::clone(&at.turn_state); - Some(turn_state) + records_turn_token_usage_on_span = removed_task.records_turn_token_usage_on_span; + if removed_task.active_turn_is_empty { + should_clear_active_turn = true; + let turn_state = Arc::clone(&at.turn_state); + Some(turn_state) + } else { + None + } } else { None } @@ -634,6 +659,33 @@ impl Session { - token_usage_at_turn_start.total_tokens) .max(0), }; + if records_turn_token_usage_on_span { + let current_span = Span::current(); + current_span.record( + "codex.turn.token_usage.input_tokens", + turn_token_usage.input_tokens, + ); + current_span.record( + "codex.turn.token_usage.cached_input_tokens", + turn_token_usage.cached_input(), + ); + current_span.record( + "codex.turn.token_usage.non_cached_input_tokens", + turn_token_usage.non_cached_input(), + ); + current_span.record( + "codex.turn.token_usage.output_tokens", + turn_token_usage.output_tokens, + ); + current_span.record( + "codex.turn.token_usage.reasoning_output_tokens", + turn_token_usage.reasoning_output_tokens, + ); + current_span.record( + "codex.turn.token_usage.total_tokens", + turn_token_usage.total_tokens, + ); + } self.services .analytics_events_client .track_turn_token_usage(TurnTokenUsageFact { diff --git a/codex-rs/core/src/tasks/regular.rs b/codex-rs/core/src/tasks/regular.rs index d64b83f01b..08c4933488 100644 --- a/codex-rs/core/src/tasks/regular.rs +++ b/codex-rs/core/src/tasks/regular.rs @@ -33,6 +33,10 @@ impl SessionTask for RegularTask { "session_task.turn" } + fn records_turn_token_usage_on_span(&self) -> bool { + true + } + async fn run( self: Arc, session: Arc, diff --git a/codex-rs/core/tests/common/Cargo.toml b/codex-rs/core/tests/common/Cargo.toml index e2765e8be6..f710aa36cc 100644 --- a/codex-rs/core/tests/common/Cargo.toml +++ b/codex-rs/core/tests/common/Cargo.toml @@ -15,6 +15,7 @@ anyhow = { workspace = true } assert_cmd = { workspace = true } base64 = { workspace = true } codex-arg0 = { workspace = true } +codex-config = { workspace = true } codex-core = { workspace = true } codex-exec-server = { workspace = true } codex-features = { workspace = true } diff --git a/codex-rs/core/tests/common/lib.rs b/codex-rs/core/tests/common/lib.rs index a11d5ee6a4..c89e6a5188 100644 --- a/codex-rs/core/tests/common/lib.rs +++ b/codex-rs/core/tests/common/lib.rs @@ -8,6 +8,9 @@ use ctor::ctor; use std::sync::OnceLock; use tempfile::TempDir; +use codex_config::CloudRequirementsLoader; +use codex_config::ConfigRequirementsToml; +use codex_config::NetworkRequirementsToml; use codex_core::CodexThread; use codex_core::config::Config; use codex_core::config::ConfigBuilder; @@ -164,14 +167,41 @@ pub fn fetch_dotslash_file( /// temporary directory. Using a per-test directory keeps tests hermetic and /// avoids clobbering a developer’s real `~/.codex`. pub async fn load_default_config_for_test(codex_home: &TempDir) -> Config { + load_default_config_for_test_with_cloud_requirements( + codex_home, + CloudRequirementsLoader::default(), + ) + .await +} + +/// Returns a default `Config` with test-provided cloud requirements applied +/// during config construction. +pub async fn load_default_config_for_test_with_cloud_requirements( + codex_home: &TempDir, + cloud_requirements: CloudRequirementsLoader, +) -> Config { ConfigBuilder::default() .codex_home(codex_home.path().to_path_buf()) .harness_overrides(default_test_overrides()) + .cloud_requirements(cloud_requirements) .build() .await .expect("defaults for test should always succeed") } +pub fn managed_network_requirements_loader() -> CloudRequirementsLoader { + CloudRequirementsLoader::new(async { + Ok(Some(ConfigRequirementsToml { + network: Some(NetworkRequirementsToml { + enabled: Some(true), + allow_local_binding: Some(true), + ..Default::default() + }), + ..Default::default() + })) + }) +} + #[cfg(target_os = "linux")] fn default_test_overrides() -> ConfigOverrides { ConfigOverrides { diff --git a/codex-rs/core/tests/common/test_codex.rs b/codex-rs/core/tests/common/test_codex.rs index 9a010417fb..d77d55956a 100644 --- a/codex-rs/core/tests/common/test_codex.rs +++ b/codex-rs/core/tests/common/test_codex.rs @@ -12,6 +12,7 @@ use std::time::Duration; use anyhow::Context; use anyhow::Result; use anyhow::anyhow; +use codex_config::CloudRequirementsLoader; use codex_core::CodexThread; use codex_core::ThreadManager; use codex_core::config::Config; @@ -47,6 +48,7 @@ use crate::PathBufExt; use crate::TempDirExt; use crate::get_remote_test_env; use crate::load_default_config_for_test; +use crate::load_default_config_for_test_with_cloud_requirements; use crate::responses::WebSocketTestServer; use crate::responses::output_value_to_text; use crate::responses::start_mock_server; @@ -206,6 +208,7 @@ pub struct TestCodexBuilder { pre_build_hooks: Vec>, workspace_setups: Vec>, home: Option>, + cloud_requirements: Option, user_shell_override: Option, exec_server_url: Option, } @@ -254,6 +257,11 @@ impl TestCodexBuilder { self } + pub fn with_cloud_requirements(mut self, cloud_requirements: CloudRequirementsLoader) -> Self { + self.cloud_requirements = Some(cloud_requirements); + self + } + pub fn with_user_shell(mut self, user_shell: Shell) -> Self { self.user_shell_override = Some(user_shell); self @@ -485,7 +493,11 @@ impl TestCodexBuilder { for hook in self.pre_build_hooks.drain(..) { hook(home.path()); } - let mut config = load_default_config_for_test(home).await; + let mut config = if let Some(cloud_requirements) = self.cloud_requirements.take() { + load_default_config_for_test_with_cloud_requirements(home, cloud_requirements).await + } else { + load_default_config_for_test(home).await + }; config.cwd = cwd_override; config.model_provider = model_provider; if let Ok(path) = codex_utils_cargo_bin::cargo_bin("codex") { @@ -923,6 +935,7 @@ pub fn test_codex() -> TestCodexBuilder { pre_build_hooks: vec![], workspace_setups: vec![], home: None, + cloud_requirements: None, user_shell_override: None, exec_server_url: None, } diff --git a/codex-rs/core/tests/suite/approvals.rs b/codex-rs/core/tests/suite/approvals.rs index 4209fc2100..ef22cfbfc1 100644 --- a/codex-rs/core/tests/suite/approvals.rs +++ b/codex-rs/core/tests/suite/approvals.rs @@ -2,12 +2,6 @@ use anyhow::Context; use anyhow::Result; -use codex_config::ConfigLayerStack; -use codex_config::ConfigLayerStackOrdering; -use codex_config::NetworkConstraints; -use codex_config::NetworkRequirementsToml; -use codex_config::RequirementSource; -use codex_config::Sourced; use codex_config::types::ApprovalsReviewer; use codex_core::CodexThread; use codex_core::config::Constrained; @@ -25,6 +19,7 @@ use codex_protocol::protocol::Op; use codex_protocol::protocol::ReviewDecision; use codex_protocol::protocol::SandboxPolicy; use codex_protocol::user_input::UserInput; +use core_test_support::managed_network_requirements_loader; use core_test_support::responses::ev_apply_patch_function_call; use core_test_support::responses::ev_assistant_message; use core_test_support::responses::ev_completed; @@ -2590,38 +2585,15 @@ allow_local_binding = true exclude_slash_tmp: false, }; let sandbox_policy_for_config = sandbox_policy.clone(); - let mut builder = test_codex().with_home(home).with_config(move |config| { - config.permissions.approval_policy = Constrained::allow_any(approval_policy); - config - .set_legacy_sandbox_policy(sandbox_policy_for_config) - .expect("set sandbox policy"); - let layers = config - .config_layer_stack - .get_layers( - ConfigLayerStackOrdering::LowestPrecedenceFirst, - /*include_disabled*/ true, - ) - .into_iter() - .cloned() - .collect(); - let mut requirements = config.config_layer_stack.requirements().clone(); - requirements.network = Some(Sourced::new( - NetworkConstraints { - enabled: Some(true), - allow_local_binding: Some(true), - ..Default::default() - }, - RequirementSource::CloudRequirements, - )); - let mut requirements_toml = config.config_layer_stack.requirements_toml().clone(); - requirements_toml.network = Some(NetworkRequirementsToml { - enabled: Some(true), - allow_local_binding: Some(true), - ..Default::default() + let mut builder = test_codex() + .with_home(home) + .with_cloud_requirements(managed_network_requirements_loader()) + .with_config(move |config| { + config.permissions.approval_policy = Constrained::allow_any(approval_policy); + config + .set_legacy_sandbox_policy(sandbox_policy_for_config) + .expect("set sandbox policy"); }); - config.config_layer_stack = ConfigLayerStack::new(layers, requirements, requirements_toml) - .expect("rebuild config layer stack with network requirements"); - }); let test = builder.build(&server).await?; assert!( test.config.managed_network_requirements_enabled(), @@ -2892,40 +2864,17 @@ allow_local_binding = true exclude_tmpdir_env_var: false, exclude_slash_tmp: false, }; - let mut builder = test_codex().with_home(home).with_config(move |config| { - config.permissions.approval_policy = Constrained::allow_any(approval_policy); - let cwd = config.cwd.clone(); - config - .permissions - .set_legacy_sandbox_policy(SandboxPolicy::DangerFullAccess, cwd.as_path()) - .expect("test setup should allow sandbox policy"); - let layers = config - .config_layer_stack - .get_layers( - ConfigLayerStackOrdering::LowestPrecedenceFirst, - /*include_disabled*/ true, - ) - .into_iter() - .cloned() - .collect(); - let mut requirements = config.config_layer_stack.requirements().clone(); - requirements.network = Some(Sourced::new( - NetworkConstraints { - enabled: Some(true), - allow_local_binding: Some(true), - ..Default::default() - }, - RequirementSource::CloudRequirements, - )); - let mut requirements_toml = config.config_layer_stack.requirements_toml().clone(); - requirements_toml.network = Some(NetworkRequirementsToml { - enabled: Some(true), - allow_local_binding: Some(true), - ..Default::default() + let mut builder = test_codex() + .with_home(home) + .with_cloud_requirements(managed_network_requirements_loader()) + .with_config(move |config| { + config.permissions.approval_policy = Constrained::allow_any(approval_policy); + let cwd = config.cwd.clone(); + config + .permissions + .set_legacy_sandbox_policy(SandboxPolicy::DangerFullAccess, cwd.as_path()) + .expect("test setup should allow sandbox policy"); }); - config.config_layer_stack = ConfigLayerStack::new(layers, requirements, requirements_toml) - .expect("rebuild config layer stack with network requirements"); - }); let test = builder.build(&server).await?; assert!( !test.config.managed_network_requirements_enabled(), diff --git a/codex-rs/core/tests/suite/hooks.rs b/codex-rs/core/tests/suite/hooks.rs index 74e9a7a682..28185a0a5c 100644 --- a/codex-rs/core/tests/suite/hooks.rs +++ b/codex-rs/core/tests/suite/hooks.rs @@ -3,12 +3,6 @@ use std::path::Path; use anyhow::Context; use anyhow::Result; -use codex_config::ConfigLayerStack; -use codex_config::ConfigLayerStackOrdering; -use codex_config::NetworkConstraints; -use codex_config::NetworkRequirementsToml; -use codex_config::RequirementSource; -use codex_config::Sourced; use codex_core::config::Constrained; use codex_features::Feature; use codex_protocol::items::parse_hook_prompt_fragment; @@ -21,6 +15,7 @@ use codex_protocol::protocol::RolloutItem; use codex_protocol::protocol::RolloutLine; use codex_protocol::protocol::SandboxPolicy; use codex_protocol::user_input::UserInput; +use core_test_support::managed_network_requirements_loader; use core_test_support::responses::ev_apply_patch_function_call; use core_test_support::responses::ev_assistant_message; use core_test_support::responses::ev_completed; @@ -1577,6 +1572,7 @@ allow_local_binding = true panic!("failed to write permission request hook test fixture: {error}"); } }) + .with_cloud_requirements(managed_network_requirements_loader()) .with_config(move |config| { config .features @@ -1586,33 +1582,6 @@ allow_local_binding = true config .set_legacy_sandbox_policy(sandbox_policy_for_config) .expect("set sandbox policy"); - let layers = config - .config_layer_stack - .get_layers( - ConfigLayerStackOrdering::LowestPrecedenceFirst, - /*include_disabled*/ true, - ) - .into_iter() - .cloned() - .collect(); - let mut requirements = config.config_layer_stack.requirements().clone(); - requirements.network = Some(Sourced::new( - NetworkConstraints { - enabled: Some(true), - allow_local_binding: Some(true), - ..Default::default() - }, - RequirementSource::CloudRequirements, - )); - let mut requirements_toml = config.config_layer_stack.requirements_toml().clone(); - requirements_toml.network = Some(NetworkRequirementsToml { - enabled: Some(true), - allow_local_binding: Some(true), - ..Default::default() - }); - config.config_layer_stack = - ConfigLayerStack::new(layers, requirements, requirements_toml) - .expect("rebuild config layer stack with network requirements"); }) .build(&server) .await?; diff --git a/codex-rs/core/tests/suite/otel.rs b/codex-rs/core/tests/suite/otel.rs index 3d2f5102e2..8d0d4d79a7 100644 --- a/codex-rs/core/tests/suite/otel.rs +++ b/codex-rs/core/tests/suite/otel.rs @@ -563,6 +563,91 @@ async fn process_sse_emits_completed_telemetry() { }); } +#[tokio::test(flavor = "current_thread")] +async fn turn_and_completed_response_spans_record_token_usage() { + let buffer: &'static Mutex> = Box::leak(Box::new(Mutex::new(Vec::new()))); + let subscriber = tracing_subscriber::fmt() + .with_level(true) + .with_ansi(false) + .with_max_level(Level::TRACE) + .with_span_events(FmtSpan::FULL) + .with_writer(MockWriter::new(buffer)) + .finish(); + let _guard = tracing::subscriber::set_default(subscriber); + + let server = start_mock_server().await; + + mount_sse_once( + &server, + sse(vec![serde_json::json!({ + "type": "response.completed", + "response": { + "id": "resp1", + "usage": { + "input_tokens": 3, + "input_tokens_details": { "cached_tokens": 1 }, + "output_tokens": 5, + "output_tokens_details": { "reasoning_tokens": 2 }, + "total_tokens": 9 + } + } + })]), + ) + .await; + + let TestCodex { codex, .. } = test_codex() + .with_config(|config| { + config + .features + .disable(Feature::GhostCommit) + .expect("test config should allow feature update"); + }) + .build(&server) + .await + .unwrap(); + + codex + .submit(Op::UserInput { + environments: None, + items: vec![UserInput::Text { + text: "hello".into(), + text_elements: Vec::new(), + }], + final_output_json_schema: None, + responsesapi_client_metadata: None, + }) + .await + .unwrap(); + + wait_for_event(&codex, |ev| matches!(ev, EventMsg::TurnComplete(_))).await; + + let logs = String::from_utf8(buffer.lock().unwrap().clone()).unwrap(); + + assert!( + logs.lines().any(|line| { + line.contains("handle_responses{otel.name=\"completed\"") + && line.contains("gen_ai.usage.input_tokens=3") + && line.contains("gen_ai.usage.cache_read.input_tokens=1") + && line.contains("gen_ai.usage.output_tokens=5") + && line.contains("codex.usage.reasoning_output_tokens=2") + && line.contains("codex.usage.total_tokens=9") + }), + "missing completed response span token usage\nlogs:\n{logs}" + ); + assert!( + logs.lines().any(|line| { + line.contains("turn{otel.name=\"session_task.turn\"") + && line.contains("codex.turn.token_usage.input_tokens=3") + && line.contains("codex.turn.token_usage.cached_input_tokens=1") + && line.contains("codex.turn.token_usage.non_cached_input_tokens=2") + && line.contains("codex.turn.token_usage.output_tokens=5") + && line.contains("codex.turn.token_usage.reasoning_output_tokens=2") + && line.contains("codex.turn.token_usage.total_tokens=9") + }), + "missing regular turn span token usage\nlogs:\n{logs}" + ); +} + #[tokio::test] async fn handle_responses_span_records_response_kind_and_tool_name() { let buffer: &'static Mutex> = Box::leak(Box::new(Mutex::new(Vec::new()))); diff --git a/codex-rs/linux-sandbox/Cargo.toml b/codex-rs/linux-sandbox/Cargo.toml index 519ae5138e..05967661e2 100644 --- a/codex-rs/linux-sandbox/Cargo.toml +++ b/codex-rs/linux-sandbox/Cargo.toml @@ -17,6 +17,7 @@ workspace = true [target.'cfg(target_os = "linux")'.dependencies] clap = { workspace = true, features = ["derive"] } +codex-process-hardening = { workspace = true } codex-protocol = { workspace = true } codex-sandboxing = { workspace = true } codex-utils-absolute-path = { workspace = true } diff --git a/codex-rs/linux-sandbox/src/landlock.rs b/codex-rs/linux-sandbox/src/landlock.rs index 5794982530..50ea87bd1d 100644 --- a/codex-rs/linux-sandbox/src/landlock.rs +++ b/codex-rs/linux-sandbox/src/landlock.rs @@ -176,6 +176,8 @@ fn install_network_seccomp_filter_on_current_thread( let mut rules: BTreeMap> = BTreeMap::new(); deny_syscall(&mut rules, libc::SYS_ptrace); + deny_syscall(&mut rules, libc::SYS_process_vm_readv); + deny_syscall(&mut rules, libc::SYS_process_vm_writev); deny_syscall(&mut rules, libc::SYS_io_uring_setup); deny_syscall(&mut rules, libc::SYS_io_uring_enter); deny_syscall(&mut rules, libc::SYS_io_uring_register); diff --git a/codex-rs/linux-sandbox/src/proxy_routing.rs b/codex-rs/linux-sandbox/src/proxy_routing.rs index 07e1893ee3..d28b864666 100644 --- a/codex-rs/linux-sandbox/src/proxy_routing.rs +++ b/codex-rs/linux-sandbox/src/proxy_routing.rs @@ -450,7 +450,7 @@ fn spawn_host_bridge(endpoint: SocketAddr, uds_path: &Path) -> io::Result io::Result<()> { - set_parent_death_signal()?; + harden_bridge_process()?; if uds_path.exists() { std::fs::remove_file(uds_path)?; } @@ -501,7 +501,7 @@ fn spawn_local_bridge(uds_path: &Path) -> io::Result { } fn run_local_bridge(uds_path: &Path, ready_fd: libc::c_int) -> io::Result<()> { - set_parent_death_signal()?; + harden_bridge_process()?; let listener = bind_local_loopback_listener()?; let port = listener.local_addr()?.port(); @@ -614,6 +614,11 @@ fn set_parent_death_signal() -> io::Result<()> { } } +fn harden_bridge_process() -> io::Result<()> { + set_parent_death_signal()?; + codex_process_hardening::disable_process_dumping() +} + fn proxy_bidirectional(mut tcp_stream: TcpStream, mut unix_stream: UnixStream) -> io::Result<()> { let mut tcp_reader = tcp_stream.try_clone()?; let mut unix_writer = unix_stream.try_clone()?; diff --git a/codex-rs/otel/src/events/session_telemetry.rs b/codex-rs/otel/src/events/session_telemetry.rs index 38d361e7a7..bb64bb2bba 100644 --- a/codex-rs/otel/src/events/session_telemetry.rs +++ b/codex-rs/otel/src/events/session_telemetry.rs @@ -305,6 +305,23 @@ impl SessionTelemetry { handle_responses_span.record("tool_name", name.as_str()); } } + ResponseEvent::Completed { + token_usage: Some(token_usage), + .. + } => { + handle_responses_span.record("gen_ai.usage.input_tokens", token_usage.input_tokens); + handle_responses_span.record( + "gen_ai.usage.cache_read.input_tokens", + token_usage.cached_input(), + ); + handle_responses_span + .record("gen_ai.usage.output_tokens", token_usage.output_tokens); + handle_responses_span.record( + "codex.usage.reasoning_output_tokens", + token_usage.reasoning_output_tokens, + ); + handle_responses_span.record("codex.usage.total_tokens", token_usage.total_tokens); + } _ => {} } } diff --git a/codex-rs/process-hardening/src/lib.rs b/codex-rs/process-hardening/src/lib.rs index 1c206aae87..f9695fcbdc 100644 --- a/codex-rs/process-hardening/src/lib.rs +++ b/codex-rs/process-hardening/src/lib.rs @@ -61,6 +61,17 @@ pub(crate) fn pre_main_hardening_linux() { remove_env_vars_with_prefix(b"LD_"); } +/// Mark the current Linux process non-dumpable so same-user processes cannot attach with ptrace. +#[cfg(target_os = "linux")] +pub fn disable_process_dumping() -> std::io::Result<()> { + let ret_code = unsafe { libc::prctl(libc::PR_SET_DUMPABLE, 0, 0, 0, 0) }; + if ret_code == 0 { + Ok(()) + } else { + Err(std::io::Error::last_os_error()) + } +} + #[cfg(any(target_os = "freebsd", target_os = "openbsd"))] pub(crate) fn pre_main_hardening_bsd() { // FreeBSD/OpenBSD: set RLIMIT_CORE to 0 and clear LD_* env vars