Lightweight coding agent that runs in your terminal
Rust
Avoid Windows sandbox setup for irrelevant proxy port changes (#43930)
zm-oai5e3f0ee94b07
diff --git a/codex-rs/windows-sandbox-rs/src/identity.rs b/codex-rs/windows-sandbox-rs/src/identity.rsindex b2efe43ce2..6f3a66d2fd 100644--- a/codex-rs/windows-sandbox-rs/src/identity.rs+++ b/codex-rs/windows-sandbox-rs/src/identity.rs@@ -26,6 +26,10 @@ use std::path::Path;use std::path::PathBuf;use windows_sys::Win32::NetworkManagement::NetManagement::UF_ACCOUNTDISABLE;+#[cfg(test)]+#[path = "identity_integration_tests.rs"]+mod integration_tests;+#[derive(Debug, Clone)]struct SandboxIdentity {username: String,@@ -177,6 +181,48 @@ pub fn require_logon_sandbox_creds(deny_write_paths_override: &[PathBuf],proxy_enforced: bool,proxy_settings_mode: crate::WindowsSandboxProxySettingsMode,+) -> Result<SandboxCreds> {+ require_logon_sandbox_creds_with_setup(+ permissions,+ command_cwd,+ env_map,+ codex_home,+ read_roots_override,+ read_roots_include_platform_defaults,+ write_roots_override,+ deny_read_paths_override,+ deny_write_paths_override,+ proxy_enforced,+ proxy_settings_mode,+ run_elevated_setup_with_proxy_settings,+ run_setup_refresh_with_overrides_and_proxy_settings,+ local_user_flags,+ )+}++#[allow(clippy::too_many_arguments)]+fn require_logon_sandbox_creds_with_setup(+ permissions: &ResolvedWindowsSandboxPermissions,+ command_cwd: &Path,+ env_map: &HashMap<String, String>,+ codex_home: &Path,+ read_roots_override: Option<&[PathBuf]>,+ read_roots_include_platform_defaults: bool,+ write_roots_override: Option<&[PathBuf]>,+ deny_read_paths_override: &[PathBuf],+ deny_write_paths_override: &[PathBuf],+ proxy_enforced: bool,+ proxy_settings_mode: crate::WindowsSandboxProxySettingsMode,+ run_full_setup: impl FnOnce(+ crate::setup::SandboxSetupRequest<'_>,+ &crate::setup::OfflineProxySettings,+ ) -> Result<()>,+ run_refresh_setup: impl FnOnce(+ crate::setup::SandboxSetupRequest<'_>,+ crate::setup::SetupRootOverrides,+ &crate::setup::OfflineProxySettings,+ ) -> Result<()>,+ read_local_user_flags: impl Fn(&str) -> Result<Option<u32>>,) -> Result<SandboxCreds> {let sandbox_dir = crate::setup::sandbox_dir(codex_home);let needed_read = read_roots_override@@ -225,7 +271,7 @@ pub fn require_logon_sandbox_creds(// Cleanup may also have removed the group, so repair missing or disabled accounts before ACL// refresh can fail, not only after a later logon reports ERROR_ACCOUNT_DISABLED.for username in [OFFLINE_USERNAME, ONLINE_USERNAME] {- let needs_repair = match local_user_flags(username) {+ let needs_repair = match read_local_user_flags(username) {Ok(Some(flags)) => flags & UF_ACCOUNTDISABLE != 0,Ok(None) => true,Err(_) => false,@@ -247,7 +293,7 @@ pub fn require_logon_sandbox_creds(} else {crate::logging::log_note("sandbox setup required", Some(&sandbox_dir));}- run_elevated_setup_with_proxy_settings(+ run_full_setup(crate::setup::SandboxSetupRequest {permissions,command_cwd,@@ -260,7 +306,7 @@ pub fn require_logon_sandbox_creds(identity = select_identity(network_identity, codex_home)?;}// Always refresh ACLs (non-elevated) for current roots via the setup binary.- run_setup_refresh_with_overrides_and_proxy_settings(+ run_refresh_setup(crate::setup::SandboxSetupRequest {permissions,command_cwd,diff --git a/codex-rs/windows-sandbox-rs/src/identity_integration_tests.rs b/codex-rs/windows-sandbox-rs/src/identity_integration_tests.rsnew file mode 100644index 0000000000..6f1926ed9e--- /dev/null+++ b/codex-rs/windows-sandbox-rs/src/identity_integration_tests.rs@@ -0,0 +1,116 @@+//! Exercises credential orchestration with real marker files and DPAPI credentials.+//! Setup and account lookup callbacks isolate machine state. This does not exercise+//! the public wrapper, native account queries, payload serialization, singleflight,+//! or helper launches.++use super::require_logon_sandbox_creds_with_setup;+use crate::WindowsSandboxProxySettingsMode;+use crate::resolved_permissions::ResolvedWindowsSandboxPermissions;+use crate::setup::SETUP_VERSION;+use crate::setup::SandboxUserRecord;+use crate::setup::SandboxUsersFile;+use crate::setup::SetupMarker;+use crate::setup::sandbox_users_path;+use crate::setup::setup_marker_path;+use anyhow::Result;+use base64::Engine;+use base64::engine::general_purpose::STANDARD as BASE64;+use codex_protocol::models::PermissionProfile;+use pretty_assertions::assert_eq;+use std::cell::RefCell;+use std::collections::HashMap;+use std::fs;+use windows_sys::Win32::NetworkManagement::NetManagement::UF_NORMAL_ACCOUNT;++#[derive(Debug, Clone, Copy, PartialEq, Eq)]+enum SetupLaunch {+ Full,+ Refresh,+}++#[test]+fn credential_setup_reconciles_effective_firewall_policy() -> Result<()> {+ let permissions = ResolvedWindowsSandboxPermissions::try_from_permission_profile(+ &PermissionProfile::read_only(),+ )?;+ for (stored_binding, desired_binding, ports, expected_launch) in [+ (true, true, "8080", SetupLaunch::Refresh),+ (true, true, "", SetupLaunch::Refresh),+ (true, true, "8080,3129", SetupLaunch::Refresh),+ (false, false, "8080", SetupLaunch::Full),+ (false, true, "3128", SetupLaunch::Full),+ (true, false, "3128", SetupLaunch::Full),+ ] {+ let launches = RefCell::new(Vec::new());+ let home = tempfile::tempdir()?;+ let marker = SetupMarker {+ version: SETUP_VERSION,+ offline_username: "offline".into(),+ online_username: "online".into(),+ created_at: None,+ proxy_ports: vec![3128],+ allow_local_binding: stored_binding,+ };+ let user = SandboxUserRecord {+ username: marker.offline_username.clone(),+ password: BASE64.encode(crate::dpapi::protect(b"test-password")?),+ };+ let users = SandboxUsersFile {+ version: SETUP_VERSION,+ offline: user.clone(),+ online: SandboxUserRecord {+ username: marker.online_username.clone(),+ ..user+ },+ };+ let marker_bytes = serde_json::to_vec(&marker)?;+ for (path, bytes) in [+ (setup_marker_path(home.path()), marker_bytes.clone()),+ (sandbox_users_path(home.path()), serde_json::to_vec(&users)?),+ ] {+ fs::create_dir_all(path.parent().unwrap())?;+ fs::write(path, bytes)?;+ }+ let env = HashMap::from([+ ("CODEX_WINDOWS_SANDBOX_PROXY_PORTS".into(), ports.into()),+ (+ "CODEX_NETWORK_ALLOW_LOCAL_BINDING".into(),+ u8::from(desired_binding).to_string(),+ ),+ ]);+ let result = require_logon_sandbox_creds_with_setup(+ &permissions,+ home.path(),+ &env,+ home.path(),+ Some(&[]),+ /*read_roots_include_platform_defaults*/ false,+ Some(&[]),+ &[],+ &[],+ /*proxy_enforced*/ true,+ WindowsSandboxProxySettingsMode::Reconcile,+ |_, _| {+ launches.borrow_mut().push(SetupLaunch::Full);+ Err(anyhow::anyhow!("full setup intercepted"))+ },+ |_, _, _| {+ launches.borrow_mut().push(SetupLaunch::Refresh);+ Ok(())+ },+ |_| Ok(Some(UF_NORMAL_ACCOUNT)),+ );+ assert_eq!(launches.into_inner(), [expected_launch]);+ assert_eq!(+ result+ .map(|creds| (creds.username, creds.password))+ .map_err(|err| err.to_string()),+ match expected_launch {+ SetupLaunch::Refresh => Ok(("offline".into(), "test-password".into())),+ SetupLaunch::Full => Err("full setup intercepted".into()),+ }+ );+ assert_eq!(fs::read(setup_marker_path(home.path()))?, marker_bytes);+ }+ Ok(())+}diff --git a/codex-rs/windows-sandbox-rs/src/setup.rs b/codex-rs/windows-sandbox-rs/src/setup.rsindex 7926c7e7fd..7eb6279088 100644--- a/codex-rs/windows-sandbox-rs/src/setup.rs+++ b/codex-rs/windows-sandbox-rs/src/setup.rs@@ -450,8 +450,10 @@ impl SetupMarker {if !network_identity.uses_offline_identity() {return None;}- if self.proxy_ports == offline_proxy_settings.proxy_ports- && self.allow_local_binding == offline_proxy_settings.allow_local_binding+ // Local-binding mode has no port-specific loopback rules, so changing proxy+ // listeners does not require a firewall update while that mode stays enabled.+ if self.allow_local_binding == offline_proxy_settings.allow_local_binding+ && (self.allow_local_binding || self.proxy_ports == offline_proxy_settings.proxy_ports){return None;}