use std::collections::BTreeSet; use std::collections::HashMap; use std::path::Path; use std::path::PathBuf; use codex_protocol::config_types::WindowsSandboxLevel; use codex_protocol::models::PermissionProfile; use codex_protocol::permissions::FileSystemPolicyProjection; use codex_protocol::permissions::FileSystemSandboxPolicy; use codex_protocol::protocol::WritableRoot; use codex_utils_absolute_path::AbsolutePathBuf; use crate::SandboxType; use crate::compatibility_sandbox_policy_for_permission_profile; /// Resolved filesystem overrides for the Windows sandbox backends. /// /// The elevated Windows backend consumes extra deny-read paths plus explicit /// read and write roots during setup/refresh. The unelevated restricted-token /// backend only consumes extra deny-write carveouts on top of the legacy /// `WorkspaceWrite` allow set. Read-root overrides are layered on top of the /// baseline helper roots that the elevated setup path needs to launch the /// sandboxed command; split policies that opt into platform defaults carry /// that explicitly with the override. #[derive(Debug, Clone, PartialEq, Eq)] pub struct WindowsSandboxFilesystemOverrides { pub read_roots_override: Option>, pub read_roots_include_platform_defaults: bool, pub write_roots_override: Option>, pub additional_deny_read_paths: Vec, pub additional_deny_write_paths: Vec, } impl WindowsSandboxFilesystemOverrides { /// Resolve filesystem overrides for the selected Windows sandbox using the child's environment. pub fn resolve( sandbox: SandboxType, permission_profile: &PermissionProfile, sandbox_policy_cwd: &AbsolutePathBuf, windows_sandbox_level: WindowsSandboxLevel, workload_env: &HashMap, ) -> Result, String> { let use_elevated = windows_sandbox_uses_elevated_backend(windows_sandbox_level); if use_elevated { resolve_windows_elevated_filesystem_overrides( sandbox, permission_profile, sandbox_policy_cwd, use_elevated, workload_env, ) } else { resolve_windows_restricted_token_filesystem_overrides( sandbox, permission_profile, sandbox_policy_cwd, windows_sandbox_level, ) } } } pub fn windows_sandbox_uses_elevated_backend(sandbox_level: WindowsSandboxLevel) -> bool { matches!(sandbox_level, WindowsSandboxLevel::Elevated) } pub fn permission_profile_supports_windows_restricted_token_sandbox( permission_profile: &PermissionProfile, ) -> bool { match permission_profile { PermissionProfile::Managed { file_system, .. } => { !file_system.to_sandbox_policy().has_full_disk_write_access() } PermissionProfile::Disabled | PermissionProfile::External { .. } => false, } } pub fn resolve_windows_restricted_token_filesystem_overrides( sandbox: SandboxType, permission_profile: &PermissionProfile, sandbox_policy_cwd: &AbsolutePathBuf, windows_sandbox_level: WindowsSandboxLevel, ) -> std::result::Result, String> { if sandbox != SandboxType::WindowsRestrictedToken || windows_sandbox_level == WindowsSandboxLevel::Elevated { return Ok(None); } let (mut file_system_sandbox_policy, network_sandbox_policy) = permission_profile.to_runtime_permissions(); let needs_direct_runtime_enforcement = file_system_sandbox_policy .needs_direct_runtime_enforcement(network_sandbox_policy, sandbox_policy_cwd); if permission_profile_supports_windows_restricted_token_sandbox(permission_profile) && !needs_direct_runtime_enforcement { return Ok(None); } if !permission_profile_supports_windows_restricted_token_sandbox(permission_profile) { let permission_profile_name = permission_profile_display_name(permission_profile); return Err(format!( "windows sandbox backend cannot enforce file_system={:?}, network={network_sandbox_policy:?}, permission_profile={permission_profile_name}; refusing to run unsandboxed", file_system_sandbox_policy.kind, )); } // Windows protects existing metadata paths through the legacy writable root // projection. Do not turn skip-missing entries into newly-created // deny-write sentinels. file_system_sandbox_policy.remove_skip_missing_path_entries(); // The restricted-token backend can still enforce split write restrictions, // but its WRITE_RESTRICTED token does not make capability SID deny-read ACEs // participate in read access checks. Read restrictions therefore require the // elevated backend, even when the filesystem root remains readable. if !windows_policy_has_root_read_access(&file_system_sandbox_policy, sandbox_policy_cwd) { return Err( "windows unelevated restricted-token sandbox cannot enforce split filesystem read restrictions directly; refusing to run unsandboxed" .to_string(), ); } let resolved = file_system_sandbox_policy .resolve_for_enforcement(sandbox_policy_cwd, FileSystemPolicyProjection::Native) .map_err(|err| err.to_string())?; let additional_deny_read_paths = codex_windows_sandbox::expand_windows_deny_read_paths(&resolved, sandbox_policy_cwd)?; if !additional_deny_read_paths.is_empty() { return Err( "windows unelevated restricted-token sandbox cannot enforce deny-read restrictions directly; refusing to run unsandboxed" .to_string(), ); } let legacy_projection = compatibility_sandbox_policy_for_permission_profile( permission_profile, sandbox_policy_cwd.as_path(), ); let legacy_writable_roots = legacy_projection.get_writable_roots_with_cwd(sandbox_policy_cwd); let split_writable_roots = resolved.writable_roots; let legacy_root_paths: BTreeSet = legacy_writable_roots .iter() .map(|root| normalize_windows_override_path(root.root.as_path())) .collect::>()?; let split_root_paths: BTreeSet = split_writable_roots .iter() .map(|root| normalize_windows_override_path(root.root.as_path())) .collect::>()?; if legacy_root_paths != split_root_paths { return Err( "windows unelevated restricted-token sandbox cannot enforce split writable root sets directly; refusing to run unsandboxed" .to_string(), ); } if has_reopened_writable_descendant(&split_writable_roots) { return Err( "windows unelevated restricted-token sandbox cannot reopen writable descendants under read-only carveouts directly; refusing to run unsandboxed" .to_string(), ); } let mut additional_deny_write_paths = BTreeSet::new(); for split_root in &split_writable_roots { let split_root_path = normalize_windows_override_path(split_root.root.as_path())?; let Some(legacy_root) = legacy_writable_roots.iter().find(|candidate| { normalize_windows_override_path(candidate.root.as_path()) .is_ok_and(|candidate_path| candidate_path == split_root_path) }) else { return Err( "windows unelevated restricted-token sandbox cannot enforce split writable root sets directly; refusing to run unsandboxed" .to_string(), ); }; for read_only_subpath in &split_root.read_only_subpaths { if !legacy_root .read_only_subpaths .iter() .any(|candidate| candidate == read_only_subpath) { additional_deny_write_paths.insert(normalize_windows_override_path( read_only_subpath.as_path(), )?); } } } if additional_deny_read_paths.is_empty() && additional_deny_write_paths.is_empty() { return Ok(None); } Ok(Some(WindowsSandboxFilesystemOverrides { read_roots_override: None, read_roots_include_platform_defaults: false, write_roots_override: None, additional_deny_read_paths, additional_deny_write_paths: additional_deny_write_paths .into_iter() .map(|path| AbsolutePathBuf::from_absolute_path(path).map_err(|err| err.to_string())) .collect::>()?, })) } pub fn resolve_windows_elevated_filesystem_overrides( sandbox: SandboxType, permission_profile: &PermissionProfile, sandbox_policy_cwd: &AbsolutePathBuf, use_windows_elevated_backend: bool, workload_env: &HashMap, ) -> std::result::Result, String> { if sandbox != SandboxType::WindowsRestrictedToken || !use_windows_elevated_backend { return Ok(None); } let (mut file_system_sandbox_policy, network_sandbox_policy) = permission_profile.to_runtime_permissions(); if !permission_profile_supports_windows_restricted_token_sandbox(permission_profile) { let permission_profile_name = permission_profile_display_name(permission_profile); return Err(format!( "windows sandbox backend cannot enforce file_system={:?}, network={network_sandbox_policy:?}, permission_profile={permission_profile_name}; refusing to run unsandboxed", file_system_sandbox_policy.kind, )); } // Windows protects existing metadata paths through the legacy writable root // projection. Do not turn skip-missing entries into newly-created // deny-write sentinels. file_system_sandbox_policy.remove_skip_missing_path_entries(); #[cfg(target_os = "windows")] codex_windows_sandbox::resolve_workload_temp_paths( &mut file_system_sandbox_policy, workload_env, ); #[cfg(not(target_os = "windows"))] let _ = workload_env; let resolved = file_system_sandbox_policy .resolve_for_enforcement(sandbox_policy_cwd, FileSystemPolicyProjection::Native) .map_err(|err| err.to_string())?; let additional_deny_read_paths = codex_windows_sandbox::expand_windows_deny_read_paths(&resolved, sandbox_policy_cwd)?; let split_writable_roots = resolved.writable_roots; if has_reopened_writable_descendant(&split_writable_roots) { return Err( "windows elevated sandbox cannot reopen writable descendants under read-only carveouts directly; refusing to run unsandboxed" .to_string(), ); } let needs_direct_runtime_enforcement = file_system_sandbox_policy .needs_direct_runtime_enforcement(network_sandbox_policy, sandbox_policy_cwd); let has_explicit_write_carveouts = split_writable_roots.iter().any(|writable_root| { writable_root.read_only_subpaths.iter().any(|path| { file_system_sandbox_policy.has_explicit_non_write_entry_for_local_path_with_cwd( path.as_path(), sandbox_policy_cwd, ) }) }); let normalize_path = |path: PathBuf| dunce::canonicalize(&path).unwrap_or(path); let legacy_projection = compatibility_sandbox_policy_for_permission_profile( permission_profile, sandbox_policy_cwd.as_path(), ); let legacy_writable_roots = legacy_projection.get_writable_roots_with_cwd(sandbox_policy_cwd); let legacy_root_paths: BTreeSet = legacy_writable_roots .iter() .map(|root| normalize_path(root.root.to_path_buf())) .collect(); let split_readable_roots: Vec = resolved .readable_roots .into_iter() .map(AbsolutePathBuf::into_path_buf) .map(&normalize_path) .collect(); let split_root_paths: Vec = split_writable_roots .iter() .map(|root| normalize_path(root.root.to_path_buf())) .collect(); let split_root_path_set: BTreeSet = split_root_paths.iter().cloned().collect(); // `has_full_disk_read_access()` is intentionally false when deny-read // entries exist. For Windows setup overrides, the important question is // whether the baseline still reads from the filesystem root and only needs // additional deny ACLs layered on top. let split_has_root_read_access = windows_policy_has_root_read_access(&file_system_sandbox_policy, sandbox_policy_cwd); let read_roots_override = if split_has_root_read_access { None } else { Some(split_readable_roots) }; let write_roots_override = if split_root_path_set == legacy_root_paths { None } else { Some(split_root_paths) }; let additional_deny_write_paths = if needs_direct_runtime_enforcement || has_explicit_write_carveouts { let mut deny_paths = BTreeSet::new(); for writable_root in &split_writable_roots { let writable_root_path = normalize_path(writable_root.root.to_path_buf()); let legacy_root = legacy_writable_roots.iter().find(|candidate| { normalize_path(candidate.root.to_path_buf()) == writable_root_path }); for read_only_subpath in &writable_root.read_only_subpaths { let read_only_subpath_suffix = read_only_subpath .as_path() .strip_prefix(writable_root.root.as_path()) .ok(); let already_denied_by_legacy = legacy_root.is_some_and(|legacy_root| { legacy_root.read_only_subpaths.iter().any(|candidate| { candidate .as_path() .strip_prefix(legacy_root.root.as_path()) .ok() == read_only_subpath_suffix }) }); let explicitly_configured = file_system_sandbox_policy .has_explicit_non_write_entry_for_local_path_with_cwd( read_only_subpath.as_path(), sandbox_policy_cwd, ); if explicitly_configured || !already_denied_by_legacy { deny_paths.insert(normalize_path(read_only_subpath.to_path_buf())); } } } deny_paths .into_iter() .map(|path| AbsolutePathBuf::from_absolute_path(path).map_err(|err| err.to_string())) .collect::>()? } else { Vec::new() }; if read_roots_override.is_none() && write_roots_override.is_none() && additional_deny_read_paths.is_empty() && additional_deny_write_paths.is_empty() { return Ok(None); } Ok(Some(WindowsSandboxFilesystemOverrides { read_roots_include_platform_defaults: read_roots_override.is_some() && resolved.include_platform_defaults, read_roots_override, write_roots_override, additional_deny_read_paths, additional_deny_write_paths, })) } fn normalize_windows_override_path(path: &Path) -> std::result::Result { AbsolutePathBuf::from_absolute_path(dunce::simplified(path)) .map(AbsolutePathBuf::into_path_buf) .map_err(|err| err.to_string()) } fn windows_policy_has_root_read_access( file_system_sandbox_policy: &FileSystemSandboxPolicy, cwd: &AbsolutePathBuf, ) -> bool { let Some(root) = cwd.as_path().ancestors().last() else { return false; }; file_system_sandbox_policy.can_read_local_path_with_cwd(root, cwd.as_path()) } fn permission_profile_display_name(permission_profile: &PermissionProfile) -> &'static str { match permission_profile { PermissionProfile::Managed { .. } => "Managed", PermissionProfile::Disabled => "Disabled", PermissionProfile::External { .. } => "External", } } fn has_reopened_writable_descendant(writable_roots: &[WritableRoot]) -> bool { writable_roots.iter().any(|writable_root| { writable_root .read_only_subpaths .iter() .any(|read_only_subpath| { writable_roots.iter().any(|candidate| { candidate.root.as_path() != writable_root.root.as_path() && candidate .root .as_path() .starts_with(read_only_subpath.as_path()) }) }) }) } #[cfg(all(test, target_os = "windows"))] #[path = "windows_tests.rs"] mod tests;