diff --git a/codex-rs/windows-sandbox-rs/src/helper_materialization.rs b/codex-rs/windows-sandbox-rs/src/helper_materialization.rs index 375199f5cc..4be4942dbd 100644 --- a/codex-rs/windows-sandbox-rs/src/helper_materialization.rs +++ b/codex-rs/windows-sandbox-rs/src/helper_materialization.rs @@ -1,6 +1,6 @@ -use anyhow::anyhow; use anyhow::Context; use anyhow::Result; +use anyhow::anyhow; use std::collections::HashMap; use std::fs; use std::io::Write; @@ -16,18 +16,21 @@ use crate::sandbox_bin_dir; #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] pub(crate) enum HelperExecutable { CommandRunner, + Setup, } impl HelperExecutable { fn file_name(self) -> &'static str { match self { Self::CommandRunner => "codex-command-runner.exe", + Self::Setup => "codex-windows-sandbox-setup.exe", } } fn label(self) -> &'static str { match self { Self::CommandRunner => "command-runner", + Self::Setup => "setup-helper", } } } @@ -49,7 +52,7 @@ pub(crate) fn legacy_lookup(kind: HelperExecutable) -> PathBuf { && let Some(dir) = exe.parent() { let candidate = dir.join(kind.file_name()); - if candidate.exists() { + if candidate.exists() && !is_windows_apps_dir(dir) { return candidate; } } @@ -88,10 +91,7 @@ pub(crate) fn resolve_helper_for_launch( } } -pub fn resolve_current_exe_for_launch( - codex_home: &Path, - fallback_executable: &str, -) -> PathBuf { +pub fn resolve_current_exe_for_launch(codex_home: &Path, fallback_executable: &str) -> PathBuf { let source = match std::env::current_exe() { Ok(path) => path, Err(_) => return PathBuf::from(fallback_executable), @@ -198,9 +198,12 @@ fn copy_from_source_if_needed(source: &Path, destination: &Path) -> Result Result { Ok(meta) => meta, Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(false), Err(err) => { - return Err(err) - .with_context(|| format!("read helper destination metadata {}", destination.display())); + return Err(err).with_context(|| { + format!("read helper destination metadata {}", destination.display()) + }); } }; @@ -287,15 +291,37 @@ fn destination_is_fresh(source: &Path, destination: &Path) -> Result { .modified() .with_context(|| format!("read helper destination mtime {}", destination.display()))?; - Ok(destination_modified >= source_modified) + if destination_modified < source_modified { + return Ok(false); + } + + files_match(source, destination) +} + +pub(crate) fn is_windows_apps_dir(path: &Path) -> bool { + path.components().any(|component| { + component + .as_os_str() + .to_string_lossy() + .eq_ignore_ascii_case("WindowsApps") + }) +} + +fn files_match(source: &Path, destination: &Path) -> Result { + let source_bytes = fs::read(source) + .with_context(|| format!("read helper source bytes {}", source.display()))?; + let destination_bytes = fs::read(destination) + .with_context(|| format!("read helper destination bytes {}", destination.display()))?; + Ok(source_bytes == destination_bytes) } #[cfg(test)] mod tests { + use super::CopyOutcome; + use super::copy_from_source_if_needed; use super::destination_is_fresh; use super::helper_bin_dir; - use super::copy_from_source_if_needed; - use super::CopyOutcome; + use super::is_windows_apps_dir; use pretty_assertions::assert_eq; use std::fs; use std::path::Path; @@ -313,21 +339,29 @@ mod tests { let outcome = copy_from_source_if_needed(&source, &destination).expect("copy helper"); assert_eq!(CopyOutcome::ReCopied, outcome); - assert_eq!(b"runner-v1".as_slice(), fs::read(&destination).expect("read destination")); + assert_eq!( + b"runner-v1".as_slice(), + fs::read(&destination).expect("read destination") + ); } #[test] - fn destination_is_fresh_uses_size_and_mtime() { + fn destination_is_fresh_requires_matching_content_when_metadata_looks_fresh() { let tmp = TempDir::new().expect("tempdir"); let source = tmp.path().join("source.exe"); let destination = tmp.path().join("destination.exe"); - fs::write(&destination, b"same-size").expect("write destination"); + fs::write(&destination, b"dest-v001").expect("write destination"); std::thread::sleep(std::time::Duration::from_secs(1)); - fs::write(&source, b"same-size").expect("write source"); + fs::write(&source, b"src--v001").expect("write source"); assert!(!destination_is_fresh(&source, &destination).expect("stale metadata")); - fs::write(&destination, b"same-size").expect("rewrite destination"); + fs::write(&destination, b"dest-v002").expect("rewrite destination"); + assert!( + !destination_is_fresh(&source, &destination).expect("content drift should be stale") + ); + + fs::write(&destination, b"src--v001").expect("rewrite destination with matching content"); assert!(destination_is_fresh(&source, &destination).expect("fresh metadata")); } @@ -340,11 +374,13 @@ mod tests { fs::write(&source, b"runner-v1").expect("write source"); copy_from_source_if_needed(&source, &destination).expect("initial copy"); - let outcome = - copy_from_source_if_needed(&source, &destination).expect("revalidate helper"); + let outcome = copy_from_source_if_needed(&source, &destination).expect("revalidate helper"); assert_eq!(CopyOutcome::Reused, outcome); - assert_eq!(b"runner-v1".as_slice(), fs::read(&destination).expect("read destination")); + assert_eq!( + b"runner-v1".as_slice(), + fs::read(&destination).expect("read destination") + ); } #[test] @@ -376,4 +412,17 @@ mod tests { fs::read(&runner_destination).expect("read runner") ); } + + #[test] + fn detects_windows_apps_install_dirs() { + assert!(is_windows_apps_dir(Path::new( + r"C:\Program Files\WindowsApps\OpenAI.Codex_1.2.3_x64__abc" + ))); + assert!(is_windows_apps_dir(Path::new( + r"E:\WindowsApps\OpenAI.Codex_1.2.3_x64__abc\app\resources" + ))); + assert!(!is_windows_apps_dir(Path::new( + r"C:\Users\example\.codex\.sandbox-bin" + ))); + } } diff --git a/codex-rs/windows-sandbox-rs/src/setup_orchestrator.rs b/codex-rs/windows-sandbox-rs/src/setup_orchestrator.rs index f6583e0de4..ee531f80bc 100644 --- a/codex-rs/windows-sandbox-rs/src/setup_orchestrator.rs +++ b/codex-rs/windows-sandbox-rs/src/setup_orchestrator.rs @@ -12,7 +12,10 @@ use std::process::Stdio; use crate::allow::AllowDenyPaths; use crate::allow::compute_allow_paths; +use crate::helper_materialization::HelperExecutable; use crate::helper_materialization::helper_bin_dir; +use crate::helper_materialization::is_windows_apps_dir; +use crate::helper_materialization::resolve_helper_for_launch; use crate::logging::log_note; use crate::path_normalization::canonical_path_key; use crate::policy::SandboxPolicy; @@ -172,7 +175,7 @@ fn run_setup_refresh_inner( }; let json = serde_json::to_vec(&payload)?; let b64 = BASE64_STANDARD.encode(json); - let exe = find_setup_exe(); + let exe = find_setup_exe(request.codex_home); // Refresh should never request elevation; ensure verb isn't set and we don't trigger UAC. let mut cmd = Command::new(&exe); cmd.arg(&b64).stdout(Stdio::null()).stderr(Stdio::null()); @@ -330,12 +333,20 @@ fn profile_read_roots(user_profile: &Path) -> Vec { .collect() } +fn current_exe_helper_dir() -> Option { + let exe = std::env::current_exe().ok()?; + current_exe_helper_dir_from_exe_path(&exe) +} + +fn current_exe_helper_dir_from_exe_path(exe: &Path) -> Option { + let dir = exe.parent()?; + (!is_windows_apps_dir(dir)).then(|| dir.to_path_buf()) +} + fn gather_helper_read_roots(codex_home: &Path) -> Vec { let mut roots = Vec::new(); - if let Ok(exe) = std::env::current_exe() - && let Some(dir) = exe.parent() - { - roots.push(dir.to_path_buf()); + if let Some(dir) = current_exe_helper_dir() { + roots.push(dir); } let helper_dir = helper_bin_dir(codex_home); let _ = std::fs::create_dir_all(&helper_dir); @@ -569,16 +580,12 @@ fn quote_arg(arg: &str) -> String { out } -fn find_setup_exe() -> PathBuf { - if let Ok(exe) = std::env::current_exe() - && let Some(dir) = exe.parent() - { - let candidate = dir.join("codex-windows-sandbox-setup.exe"); - if candidate.exists() { - return candidate; - } - } - PathBuf::from("codex-windows-sandbox-setup.exe") +fn find_setup_exe(codex_home: &Path) -> PathBuf { + resolve_helper_for_launch( + HelperExecutable::Setup, + codex_home, + Some(&sandbox_dir(codex_home)), + ) } fn report_helper_failure( @@ -611,7 +618,7 @@ fn run_setup_exe( use windows_sys::Win32::UI::Shell::SEE_MASK_NOCLOSEPROCESS; use windows_sys::Win32::UI::Shell::SHELLEXECUTEINFOW; use windows_sys::Win32::UI::Shell::ShellExecuteExW; - let exe = find_setup_exe(); + let exe = find_setup_exe(codex_home); let payload_json = serde_json::to_string(payload).map_err(|err| { failure( SetupErrorCode::OrchestratorPayloadSerializeFailed, @@ -802,6 +809,8 @@ fn filter_sensitive_write_roots(mut roots: Vec, codex_home: &Path) -> V #[cfg(test)] mod tests { use super::WINDOWS_PLATFORM_DEFAULT_READ_ROOTS; + use super::current_exe_helper_dir_from_exe_path; + use super::gather_helper_read_roots; use super::gather_legacy_full_read_roots; use super::gather_read_roots; use super::loopback_proxy_port_from_url; @@ -826,6 +835,30 @@ mod tests { .collect() } + #[test] + fn helper_read_roots_include_materialized_helper_dir() { + let tmp = TempDir::new().expect("tempdir"); + let codex_home = tmp.path().join("codex-home"); + + let roots = gather_helper_read_roots(&codex_home); + + assert!(roots.contains(&helper_bin_dir(&codex_home))); + } + + #[test] + fn current_exe_helper_dir_skips_windows_apps_parent() { + assert_eq!( + None, + current_exe_helper_dir_from_exe_path(Path::new( + r"C:\Program Files\WindowsApps\OpenAI.Codex_1.2.3_x64__abc\app\resources\codex.exe" + )) + ); + assert_eq!( + Some(PathBuf::from(r"C:\tools\codex")), + current_exe_helper_dir_from_exe_path(Path::new(r"C:\tools\codex\codex.exe")) + ); + } + #[test] fn loopback_proxy_url_parsing_supports_common_forms() { assert_eq!(