diff --git a/codex-rs/.config/nextest.toml b/codex-rs/.config/nextest.toml index 6d87360563..17decdc0b8 100644 --- a/codex-rs/.config/nextest.toml +++ b/codex-rs/.config/nextest.toml @@ -43,6 +43,9 @@ max-threads = 1 [test-groups.windows_sandbox_legacy_sessions] max-threads = 1 +[test-groups.windows_sandbox_accounts] +max-threads = 1 + [test-groups.windows_process_heavy] max-threads = 2 @@ -82,6 +85,23 @@ test-group = 'app_server_integration_local' filter = 'package(codex-core) & kind(test) & test(apply_patch_cli)' test-group = 'core_apply_patch_cli_integration' +[[profile.default.overrides]] +# These cases share the runner's elevated sandbox accounts. Queue them before +# starting their timeout; their file lock still coordinates other test processes. +platform = 'cfg(windows)' +filter = ''' + (package(codex-core) & ( + test(windows_sandbox_cli_preserves_managed_deny_reads_across_launches) | + test(windows_elevated_does_not_create_missing_workspace_metadata) | + test(windows_elevated_enforces_deny_read_and_protects_setup_marker) | + test(windows_elevated_unified_exec_enforces_large_recursive_deny_reads) | + test(windows_elevated_approved_git_pull_preserves_deny_read) + )) | + (package(codex-exec-server) & test(file_system_elevated_relative_read_denial_uses_policy_cwd)) | + (package(codex-windows-sandbox) & test(elevated_non_tty_cmd_forwards_env_output_and_exit)) +''' +test-group = 'windows_sandbox_accounts' + [[profile.default.overrides]] # These tests create restricted-token Windows child processes and private desktops. # Serialize them to avoid exhausting Windows session/global desktop resources in CI. diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index c81c7467c7..234023d280 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -2976,6 +2976,7 @@ dependencies = [ "codex-utils-string", "codex-web-search-extension", "codex-windows-sandbox", + "codex-windows-sandbox-test-support", "core_test_support", "ctor 0.6.3", "dirs", @@ -3213,6 +3214,7 @@ dependencies = [ "codex-utils-rustls-provider", "codex-websocket-auth", "codex-websocket-client", + "codex-windows-sandbox-test-support", "ctor 0.6.3", "dirs", "futures", @@ -5282,6 +5284,7 @@ dependencies = [ "codex-utils-process", "codex-utils-pty", "codex-utils-string", + "codex-windows-sandbox-test-support", "dirs-next", "dunce", "glob", @@ -5313,6 +5316,13 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "codex-windows-sandbox-test-support" +version = "0.0.0" +dependencies = [ + "dirs", +] + [[package]] name = "codex-workload-identity" version = "0.0.0" diff --git a/codex-rs/Cargo.toml b/codex-rs/Cargo.toml index 4a063dc3a8..0e01c4534b 100644 --- a/codex-rs/Cargo.toml +++ b/codex-rs/Cargo.toml @@ -112,6 +112,7 @@ members = [ "v8-poc", "websocket-auth", "websocket-client", + "windows-sandbox-rs/tests/support", "windows-sandbox-service", "worktree", "workload-identity", @@ -316,6 +317,7 @@ codex-user-verification = { path = "user-verification" } codex-v8-poc = { path = "v8-poc" } codex-workload-identity = { path = "workload-identity" } codex-windows-sandbox = { path = "windows-sandbox-rs" } +codex-windows-sandbox-test-support = { path = "windows-sandbox-rs/tests/support" } core_test_support = { path = "core/tests/common" } # External diff --git a/codex-rs/core/Cargo.toml b/codex-rs/core/Cargo.toml index 53c64fff36..661bfdd43a 100644 --- a/codex-rs/core/Cargo.toml +++ b/codex-rs/core/Cargo.toml @@ -151,6 +151,7 @@ codex-test-binary-support = { workspace = true } codex-utils-cargo-bin = { workspace = true } codex-utils-redacted-string = { workspace = true } codex-web-search-extension = { workspace = true } +codex-windows-sandbox-test-support = { workspace = true } core_test_support = { workspace = true } ctor = { workspace = true } insta = { workspace = true } diff --git a/codex-rs/core/tests/suite/windows_sandbox.rs b/codex-rs/core/tests/suite/windows_sandbox.rs index ea830aed64..ccbabef97f 100644 --- a/codex-rs/core/tests/suite/windows_sandbox.rs +++ b/codex-rs/core/tests/suite/windows_sandbox.rs @@ -25,6 +25,7 @@ use codex_protocol::protocol::EventMsg; use codex_protocol::protocol::Op; use codex_protocol::protocol::ReviewDecision; use codex_protocol::user_input::UserInput; +use codex_windows_sandbox_test_support::WindowsSandboxAccountTestGuard; use core_test_support::PathExt; use core_test_support::responses::ev_assistant_message; use core_test_support::responses::ev_completed; @@ -41,7 +42,6 @@ use serde_json::json; use serial_test::serial; use std::collections::HashMap; use std::ffi::OsString; -use std::fs::File; use std::path::Path; use std::path::PathBuf; use std::process::Command; @@ -75,32 +75,21 @@ impl Drop for EnvVarGuard { } enum TestCodexHome { - Persistent(PathBuf, File), - Temporary(TempDir, File), + Persistent(PathBuf), + Temporary(TempDir), } impl TestCodexHome { fn path(&self) -> &Path { match self { - Self::Persistent(path, _account_lock) => path.as_path(), - Self::Temporary(temp_dir, _account_lock) => temp_dir.path(), + Self::Persistent(path) => path.as_path(), + Self::Temporary(temp_dir) => temp_dir.path(), } } } fn codex_home_for_windows_sandbox_test(name: &str) -> anyhow::Result { - // Bazel shards and nextest processes share Windows accounts. Hold this lock - // through the test so they cannot rotate each other's credentials mid-launch. - let lock_path = dirs::data_local_dir() - .context("resolve local app data for the Windows sandbox test lock")? - .join("codex-windows-sandbox-integration-test.lock"); - let lock = File::options() - .read(true) - .append(true) - .create(true) - .open(lock_path)?; - lock.lock().context("lock Windows sandbox test accounts")?; - + // Only tests that use the elevated accounts also take the shared account guard. if let Some(test_tmpdir) = std::env::var_os("TEST_TMPDIR") { // The elevated backend provisions machine-local sandbox users. Bazel // retries run in the same Windows VM, so keep CODEX_HOME stable within @@ -108,10 +97,10 @@ fn codex_home_for_windows_sandbox_test(name: &str) -> anyhow::Result anyhow::Result<()> { @@ -205,6 +194,7 @@ fn assert_managed_deny_probe(output: &std::process::Output, launch: usize) -> an #[test] #[serial(codex_home)] fn windows_sandbox_cli_preserves_managed_deny_reads_across_launches() -> anyhow::Result<()> { + let _account_guard = WindowsSandboxAccountTestGuard::acquire()?; let codex_home = codex_home_for_windows_sandbox_test("windows-cli-managed-deny-read-codex-home")?; @@ -415,6 +405,7 @@ async fn windows_restricted_token_rejects_exact_and_glob_deny_read_policy() -> a #[tokio::test] #[serial(codex_home)] async fn windows_elevated_does_not_create_missing_workspace_metadata() -> anyhow::Result<()> { + let _account_guard = WindowsSandboxAccountTestGuard::acquire()?; let codex_home = codex_home_for_windows_sandbox_test("windows-elevated-missing-metadata-codex-home")?; let _codex_home_guard = EnvVarGuard::set("CODEX_HOME", codex_home.path().as_os_str()); @@ -540,6 +531,7 @@ $rules = foreach ($name in @('codex_sandbox_offline_block_inbound', 'codex_sandb #[tokio::test] #[serial(codex_home)] async fn windows_elevated_enforces_deny_read_and_protects_setup_marker() -> anyhow::Result<()> { + let _account_guard = WindowsSandboxAccountTestGuard::acquire()?; let codex_home = codex_home_for_windows_sandbox_test("windows-elevated-deny-read-codex-home")?; let _codex_home_guard = EnvVarGuard::set("CODEX_HOME", codex_home.path().as_os_str()); stage_windows_sandbox_helpers()?; @@ -680,6 +672,7 @@ async fn windows_elevated_enforces_deny_read_and_protects_setup_marker() -> anyh #[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[serial(codex_home)] async fn windows_elevated_unified_exec_enforces_large_recursive_deny_reads() -> anyhow::Result<()> { + let _account_guard = WindowsSandboxAccountTestGuard::acquire()?; let codex_home = codex_home_for_windows_sandbox_test("windows-elevated-tool-runtime-deny-read-codex-home")?; let _codex_home_guard = EnvVarGuard::set("CODEX_HOME", codex_home.path().as_os_str()); @@ -857,6 +850,7 @@ async fn windows_elevated_approved_git_pull_preserves_deny_read() -> anyhow::Res use EventMsg::ExecCommandEnd; use EventMsg::TurnComplete; + let _account_guard = WindowsSandboxAccountTestGuard::acquire()?; let codex_home = codex_home_for_windows_sandbox_test("windows-elevated-git-pull-deny-read")?; let _codex_home_guard = EnvVarGuard::set("CODEX_HOME", codex_home.path().as_os_str()); stage_windows_sandbox_helpers()?; diff --git a/codex-rs/exec-server/Cargo.toml b/codex-rs/exec-server/Cargo.toml index 3c78a5021c..25500aaaf5 100644 --- a/codex-rs/exec-server/Cargo.toml +++ b/codex-rs/exec-server/Cargo.toml @@ -95,6 +95,7 @@ anyhow = { workspace = true } codex-exec-server-test-support = { workspace = true } codex-test-binary-support = { workspace = true } codex-utils-cargo-bin = { workspace = true } +codex-windows-sandbox-test-support = { workspace = true } ctor = { workspace = true } http = { workspace = true } opentelemetry_sdk = { workspace = true } diff --git a/codex-rs/exec-server/tests/file_system_windows.rs b/codex-rs/exec-server/tests/file_system_windows.rs index 1b9830a1ce..8193b570ea 100644 --- a/codex-rs/exec-server/tests/file_system_windows.rs +++ b/codex-rs/exec-server/tests/file_system_windows.rs @@ -33,6 +33,7 @@ use codex_protocol::permissions::NetworkSandboxPolicy; use codex_protocol::protocol::SandboxPolicy; use codex_sandboxing::SandboxType; use codex_utils_path_uri::PathUri; +use codex_windows_sandbox_test_support::WindowsSandboxAccountTestGuard; use futures::TryStreamExt; use pretty_assertions::assert_eq; use test_case::test_case; @@ -456,6 +457,7 @@ async fn file_system_remote_fs_helper_respects_windows_sandbox_write_policy( async fn file_system_elevated_relative_read_denial_uses_policy_cwd( implementation: FileSystemImplementation, ) -> Result<()> { + let _account_guard = WindowsSandboxAccountTestGuard::acquire()?; // Both implementations re-enter this test binary; the elevated backend finds its helpers // next to that binary, while Cargo and Bazel provide them separately. let test_exe = std::env::current_exe()?; diff --git a/codex-rs/windows-sandbox-rs/BUILD.bazel b/codex-rs/windows-sandbox-rs/BUILD.bazel index 50255d4540..8531cb4d66 100644 --- a/codex-rs/windows-sandbox-rs/BUILD.bazel +++ b/codex-rs/windows-sandbox-rs/BUILD.bazel @@ -55,8 +55,4 @@ codex_rust_crate( ":codex-command-runner", ":codex-windows-sandbox-setup", ], - # Setup rotates machine-wide account passwords. The process-local test lock - # cannot prevent exec-server's elevated filesystem tests from rotating them - # between this crate's setup and logon. Windows tests execute locally in CI. - test_tags = ["exclusive-if-local"], ) diff --git a/codex-rs/windows-sandbox-rs/Cargo.toml b/codex-rs/windows-sandbox-rs/Cargo.toml index 65332f3506..356991b1ca 100644 --- a/codex-rs/windows-sandbox-rs/Cargo.toml +++ b/codex-rs/windows-sandbox-rs/Cargo.toml @@ -102,4 +102,5 @@ features = [ version = "0.52" [dev-dependencies] +codex-windows-sandbox-test-support = { workspace = true } pretty_assertions = { workspace = true } diff --git a/codex-rs/windows-sandbox-rs/src/unified_exec/tests.rs b/codex-rs/windows-sandbox-rs/src/unified_exec/tests.rs index f35ee56b38..4520a51a88 100644 --- a/codex-rs/windows-sandbox-rs/src/unified_exec/tests.rs +++ b/codex-rs/windows-sandbox-rs/src/unified_exec/tests.rs @@ -16,6 +16,7 @@ use codex_protocol::models::PermissionProfile; use codex_utils_absolute_path::AbsolutePathBuf; use codex_utils_pty::ProcessDriver; use codex_utils_pty::ProcessSignal; +use codex_windows_sandbox_test_support::WindowsSandboxAccountTestGuard; use pretty_assertions::assert_eq; use std::collections::HashMap; use std::fs; @@ -302,6 +303,8 @@ fn legacy_non_tty_cmd_emits_output() { #[test] fn elevated_non_tty_cmd_forwards_env_output_and_exit() { + let _account_guard = + WindowsSandboxAccountTestGuard::acquire().expect("lock Windows sandbox test accounts"); let _guard = legacy_process_test_guard(); let runtime = current_thread_runtime(); runtime.block_on(async move { diff --git a/codex-rs/windows-sandbox-rs/tests/support/BUILD.bazel b/codex-rs/windows-sandbox-rs/tests/support/BUILD.bazel new file mode 100644 index 0000000000..dea523964c --- /dev/null +++ b/codex-rs/windows-sandbox-rs/tests/support/BUILD.bazel @@ -0,0 +1,8 @@ +load("//:defs.bzl", "codex_rust_crate") + +codex_rust_crate( + name = "support", + crate_name = "codex_windows_sandbox_test_support", + # This support library has no tests of its own. + test_tags = ["manual"], +) diff --git a/codex-rs/windows-sandbox-rs/tests/support/Cargo.toml b/codex-rs/windows-sandbox-rs/tests/support/Cargo.toml new file mode 100644 index 0000000000..6cfb1fb78e --- /dev/null +++ b/codex-rs/windows-sandbox-rs/tests/support/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "codex-windows-sandbox-test-support" +version.workspace = true +edition.workspace = true +license.workspace = true + +[lib] +test = false +doctest = false + +[lints] +workspace = true + +[target.'cfg(windows)'.dependencies] +dirs = { workspace = true } diff --git a/codex-rs/windows-sandbox-rs/tests/support/src/lib.rs b/codex-rs/windows-sandbox-rs/tests/support/src/lib.rs new file mode 100644 index 0000000000..7c69fd1fee --- /dev/null +++ b/codex-rs/windows-sandbox-rs/tests/support/src/lib.rs @@ -0,0 +1,49 @@ +//! Coordinates tests that share the runner's Windows sandbox accounts across processes. + +#![cfg(windows)] + +use std::fs::File; +use std::fs::OpenOptions; +use std::io; + +/// Serializes tests that provision or use the runner's shared Windows sandbox accounts. +/// Hold this from before setup until all sandbox commands and cleanup have finished. +#[must_use = "the account lock is released when the guard is dropped"] +pub struct WindowsSandboxAccountTestGuard { + _file: File, +} + +impl WindowsSandboxAccountTestGuard { + /// Acquires the same file lock across all test processes run by this Windows user. + pub fn acquire() -> io::Result { + let directory = dirs::data_local_dir().ok_or_else(|| { + io::Error::new( + io::ErrorKind::NotFound, + "Windows local application data directory is unavailable", + ) + })?; + // Bazel uses per-test temporary directories, so the lock lives in the runner user's + // Windows known folder instead. It is separate from the production setup lock. + let path = directory.join("codex-windows-sandbox-integration-test.lock"); + // Leave the file in place: deleting it would let processes lock different files at this path. + let file = OpenOptions::new() + .read(true) + .write(true) + .create(true) + .truncate(false) + .open(path) + .map_err(|error| { + io::Error::new( + error.kind(), + format!("open Windows sandbox test lock: {error}"), + ) + })?; + file.lock().map_err(|error| { + io::Error::new( + error.kind(), + format!("lock Windows sandbox test accounts: {error}"), + ) + })?; + Ok(Self { _file: file }) + } +}