From df7905769613509193aaa4b551217f749b525955 Mon Sep 17 00:00:00 2001 From: David Wiesen Date: Tue, 7 Jul 2026 15:46:54 -0700 Subject: [PATCH] Consolidate Windows network validation tests --- codex-rs/app-server/src/lib.rs | 15 -- codex-rs/config/src/config_requirements.rs | 42 ----- codex-rs/core/src/config/config_tests.rs | 193 +++++---------------- 3 files changed, 42 insertions(+), 208 deletions(-) diff --git a/codex-rs/app-server/src/lib.rs b/codex-rs/app-server/src/lib.rs index 3bdedcac2f..5b89b22d9b 100644 --- a/codex-rs/app-server/src/lib.rs +++ b/codex-rs/app-server/src/lib.rs @@ -1427,21 +1427,6 @@ sandbox = "elevated" assert!(is_unrecoverable_windows_network_config_error(&err)); } - #[test] - fn windows_network_config_errors_are_unrecoverable() { - for error in [ - codex_config::ConstraintError::NetworkProxyRequiresElevatedWindowsSandboxRequirement, - codex_config::ConstraintError::NetworkProxyIncompatibleWithUnelevatedWindowsSandbox, - ] { - let error = std::io::Error::from(error); - assert!(is_unrecoverable_windows_network_config_error(&error)); - } - - assert!(!is_unrecoverable_windows_network_config_error( - &std::io::Error::other("unrelated config error") - )); - } - #[test] fn log_format_from_env_value_matches_json_values_case_insensitively() { assert_eq!(LogFormat::from_env_value(Some("json")), LogFormat::Json); diff --git a/codex-rs/config/src/config_requirements.rs b/codex-rs/config/src/config_requirements.rs index d12065f154..fd19c79a78 100644 --- a/codex-rs/config/src/config_requirements.rs +++ b/codex-rs/config/src/config_requirements.rs @@ -2810,42 +2810,6 @@ allowed_approvals_reviewers = ["user"] Ok(()) } - #[test] - fn managed_network_parsing_does_not_require_windows_sandbox_policy() -> Result<()> { - let config: ConfigRequirementsToml = from_str( - r#" - [experimental_network] - enabled = false - "#, - )?; - - let requirements = ConfigRequirements::try_from(with_unknown_source(config))?; - assert!(requirements.network.is_some()); - Ok(()) - } - - #[test] - fn managed_network_accepts_elevated_only_windows_sandbox() -> Result<()> { - let config: ConfigRequirementsToml = from_str( - r#" - [experimental_network] - enabled = true - - [windows] - allowed_sandbox_implementations = ["elevated"] - "#, - )?; - - let requirements = ConfigRequirements::try_from(with_unknown_source(config))?; - assert!(requirements.network.is_some()); - assert_eq!( - requirements.windows_sandbox_mode.value(), - Some(WindowsSandboxModeToml::Elevated) - ); - - Ok(()) - } - #[test] fn deserialize_legacy_allowed_approvals_reviewer() -> Result<()> { let toml_str = r#" @@ -3386,9 +3350,6 @@ command = "python3 /enterprise/hooks/pre.py" [experimental_network.unix_sockets] "/tmp/example.sock" = "allow" "/tmp/blocked.sock" = "deny" - - [windows] - allowed_sandbox_implementations = ["elevated"] "#; let source = RequirementSource::LegacyManagedConfigTomlFromMdm; @@ -3462,9 +3423,6 @@ command = "python3 /enterprise/hooks/pre.py" denied_domains = ["blocked.example.com"] allow_unix_sockets = ["/tmp/example.sock"] allow_local_binding = false - - [windows] - allowed_sandbox_implementations = ["elevated"] "#; let source = RequirementSource::LegacyManagedConfigTomlFromMdm; diff --git a/codex-rs/core/src/config/config_tests.rs b/codex-rs/core/src/config/config_tests.rs index 9ff67529f2..3865c16e10 100644 --- a/codex-rs/core/src/config/config_tests.rs +++ b/codex-rs/core/src/config/config_tests.rs @@ -129,171 +129,62 @@ async fn load_config_with_elevated_only_windows_sandbox_requirement( } #[test] -fn windows_network_proxy_requires_elevated_only_sandbox_requirement() { - let err = validate_windows_network_proxy_requirements_for_platform( - /*is_windows*/ true, - &ConfigRequirementsToml::default(), - /*network_proxy_configured*/ true, - ) - .expect_err("Windows network proxy should require elevated-only sandbox requirements"); - - assert!(matches!( - err.get_ref() - .and_then(|source| source.downcast_ref::()), - Some(ConstraintError::NetworkProxyRequiresElevatedWindowsSandboxRequirement) - )); - - for (requirements, network_proxy_configured) in [ - (ConfigRequirementsToml::default(), false), +fn windows_network_proxy_validation() { + let elevated_only = ConfigRequirementsToml { + windows: Some(codex_config::WindowsRequirementsToml { + allowed_sandbox_implementations: Some(vec![WindowsSandboxModeToml::Elevated]), + }), + ..Default::default() + }; + for (is_windows, requirements, network_proxy_enabled, allowed) in [ + (true, ConfigRequirementsToml::default(), true, false), + (true, ConfigRequirementsToml::default(), false, true), + (true, elevated_only, true, true), ( + true, ConfigRequirementsToml { windows: Some(codex_config::WindowsRequirementsToml { - allowed_sandbox_implementations: Some(vec![WindowsSandboxModeToml::Elevated]), + allowed_sandbox_implementations: Some(vec![ + WindowsSandboxModeToml::Elevated, + WindowsSandboxModeToml::Unelevated, + ]), }), ..Default::default() }, true, + false, ), + (false, ConfigRequirementsToml::default(), true, true), ] { - validate_windows_network_proxy_requirements_for_platform( - /*is_windows*/ true, - &requirements, - network_proxy_configured, - ) - .expect("valid Windows network proxy requirements"); + assert_eq!( + validate_windows_network_proxy_requirements_for_platform( + is_windows, + &requirements, + network_proxy_enabled, + ) + .is_ok(), + allowed + ); } - validate_windows_network_proxy_requirements_for_platform( - /*is_windows*/ false, - &ConfigRequirementsToml::default(), - /*network_proxy_configured*/ true, - ) - .expect("non-Windows network proxy should not require Windows sandbox policy"); -} - -#[test] -fn windows_network_proxy_requires_elevated_sandbox() { - for windows_sandbox_level in [ - WindowsSandboxLevel::Disabled, - WindowsSandboxLevel::RestrictedToken, + for (is_windows, sandbox_level, network_proxy_enabled, allowed) in [ + (true, WindowsSandboxLevel::Disabled, true, false), + (true, WindowsSandboxLevel::RestrictedToken, true, false), + (true, WindowsSandboxLevel::Elevated, true, true), + (true, WindowsSandboxLevel::RestrictedToken, false, true), + (false, WindowsSandboxLevel::RestrictedToken, true, true), ] { - let err = validate_windows_sandbox_network_proxy_compatibility_for_platform( - /*is_windows*/ true, - windows_sandbox_level, - /*network_proxy_configured*/ true, - ) - .expect_err("non-elevated Windows sandbox should reject configured network proxy"); - - assert!(matches!( - err.get_ref() - .and_then(|source| source.downcast_ref::()), - Some(ConstraintError::NetworkProxyIncompatibleWithUnelevatedWindowsSandbox) - )); + assert_eq!( + validate_windows_sandbox_network_proxy_compatibility_for_platform( + is_windows, + sandbox_level, + network_proxy_enabled, + ) + .is_ok(), + allowed + ); } } - -#[test] -fn windows_network_proxy_compatibility_allows_valid_combinations() -> std::io::Result<()> { - for (windows_sandbox_level, network_proxy_configured) in [ - (WindowsSandboxLevel::Disabled, false), - (WindowsSandboxLevel::RestrictedToken, false), - (WindowsSandboxLevel::Elevated, false), - (WindowsSandboxLevel::Elevated, true), - ] { - validate_windows_sandbox_network_proxy_compatibility_for_platform( - /*is_windows*/ true, - windows_sandbox_level, - network_proxy_configured, - )?; - } - validate_windows_sandbox_network_proxy_compatibility_for_platform( - /*is_windows*/ false, - WindowsSandboxLevel::RestrictedToken, - /*network_proxy_configured*/ true, - )?; - - Ok(()) -} - -#[cfg(target_os = "windows")] -#[tokio::test] -async fn network_proxy_without_elevated_only_sandbox_requirement_is_rejected() -> std::io::Result<()> -{ - let codex_home = TempDir::new()?; - let cwd = TempDir::new()?; - std::fs::write(cwd.path().join(".git"), "gitdir: nowhere")?; - - let err = Config::load_from_base_config_with_overrides( - ConfigToml { - sandbox_mode: Some(SandboxMode::WorkspaceWrite), - sandbox_workspace_write: Some(SandboxWorkspaceWrite { - network_access: true, - ..Default::default() - }), - features: Some(toml::from_str("network_proxy = true").expect("valid features")), - 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 - .expect_err("network proxy without elevated-only sandbox requirements should fail"); - - assert!(matches!( - err.get_ref() - .and_then(|source| source.downcast_ref::()), - Some(ConstraintError::NetworkProxyRequiresElevatedWindowsSandboxRequirement) - )); - Ok(()) -} - -#[cfg(target_os = "windows")] -#[tokio::test] -async fn disabled_managed_network_allows_unelevated_windows_sandbox() -> std::io::Result<()> { - let codex_home = TempDir::new()?; - std::fs::write( - codex_home.path().join(CONFIG_TOML_FILE), - r#" -[windows] -sandbox = "unelevated" -"#, - )?; - - let config = ConfigBuilder::without_managed_config_for_tests() - .codex_home(codex_home.path().to_path_buf()) - .fallback_cwd(Some(codex_home.path().to_path_buf())) - .cloud_config_bundle( - CloudConfigBundleFixture::loader_with_enterprise_requirement( - r#" -[experimental_network] -enabled = false -"#, - ), - ) - .build() - .await?; - - assert_eq!( - WindowsSandboxLevel::from_config(&config), - WindowsSandboxLevel::RestrictedToken - ); - assert!( - !config - .permissions - .network - .as_ref() - .expect("managed network requirements should remain available") - .enabled() - ); - Ok(()) -} use core_test_support::PathBufExt; use core_test_support::PathExt; use core_test_support::TempDirExt;