From fd59b42a09df58d301839cac889c2db69c40f6cb Mon Sep 17 00:00:00 2001 From: zm-oai Date: Sat, 19 Sep 2026 00:41:14 +0000 Subject: [PATCH] Preserve Windows package identity for sandboxed descendants (#46575) ## Why Package context propagation was limited to verified `codex-command-runner.exe` processes, preventing other packaged callers from passing their OS-assigned identity to sandboxed children. ## What changed Use the current process's OS package identity to decide whether to preserve desktop app context in both regular and ConPTY sandbox launches. Remove executable-name and staged-runner verification from this decision. Reject callers without package identity when `CODEX_WINDOWS_REGISTERED_CORE=1`; otherwise, unpackaged callers continue without preserving app context. GitOrigin-RevId: 52eb2fe33c8746d898b5d7ebf53bd001a55b5506 --- .../windows-sandbox-rs/src/app_package.rs | 27 ++++++------------- codex-rs/windows-sandbox-rs/src/conpty/mod.rs | 2 +- codex-rs/windows-sandbox-rs/src/process.rs | 2 +- 3 files changed, 10 insertions(+), 21 deletions(-) diff --git a/codex-rs/windows-sandbox-rs/src/app_package.rs b/codex-rs/windows-sandbox-rs/src/app_package.rs index 3e4d5d09d3..da121c3e12 100644 --- a/codex-rs/windows-sandbox-rs/src/app_package.rs +++ b/codex-rs/windows-sandbox-rs/src/app_package.rs @@ -78,25 +78,14 @@ pub(crate) fn query_package_name( Ok(Some(String::from_utf16(family)?)) } -/// Only an OS-verified packaged runner may propagate package context to sandbox children. -pub(crate) fn current_process_is_registered_core_runner() -> Result { - let executable = std::env::current_exe().context("resolve current Core runner image")?; - if !executable - .file_name() - .is_some_and(|name| name.eq_ignore_ascii_case("codex-command-runner.exe")) - { - return Ok(false); - } - let process = unsafe { GetCurrentProcess() }; - if process_package_name(process)?.is_none() { - ensure!( - !registered_core_requested(), - "registered Core runner has no package identity" - ); - return Ok(false); - } - verify_registered_core_runner(process, &executable)?; - Ok(true) +/// Preserve the caller's OS-assigned package identity for sandboxed descendants. +pub(crate) fn current_process_has_package_identity() -> Result { + let has_identity = current_package_full_name()?.is_some(); + ensure!( + has_identity || !registered_core_requested(), + "registered Core process has no package identity" + ); + Ok(has_identity) } fn staged_package_root(name: &[u16]) -> Result { diff --git a/codex-rs/windows-sandbox-rs/src/conpty/mod.rs b/codex-rs/windows-sandbox-rs/src/conpty/mod.rs index 363421ec19..352105a040 100644 --- a/codex-rs/windows-sandbox-rs/src/conpty/mod.rs +++ b/codex-rs/windows-sandbox-rs/src/conpty/mod.rs @@ -134,7 +134,7 @@ pub fn spawn_conpty_process_as_user( job: Some(Arc::clone(&job)), _desktop: Some(desktop), }; - let preserve_app_context = crate::app_package::current_process_is_registered_core_runner()?; + let preserve_app_context = crate::app_package::current_process_has_package_identity()?; let mut attrs = ProcThreadAttributeList::new(2 + u32::from(preserve_app_context))?; attrs.set_pseudoconsole(hpc)?; attrs.set_job(job.as_raw_handle() as HANDLE)?; diff --git a/codex-rs/windows-sandbox-rs/src/process.rs b/codex-rs/windows-sandbox-rs/src/process.rs index a6d088ef41..76e664a937 100644 --- a/codex-rs/windows-sandbox-rs/src/process.rs +++ b/codex-rs/windows-sandbox-rs/src/process.rs @@ -112,7 +112,7 @@ pub unsafe fn create_process_as_user( | (None, ConsoleMode::Inherit) | (None, ConsoleMode::NoWindow) => 0, }; - let preserve_app_context = crate::app_package::current_process_is_registered_core_runner()?; + let preserve_app_context = crate::app_package::current_process_has_package_identity()?; let attr_count = if stdio.is_some() { 2 } else { 1 } + u32::from(preserve_app_context); let mut attrs = ProcThreadAttributeList::new(attr_count)?; attrs.set_job(job.as_raw_handle() as HANDLE)?;