From 5c013177d85b72d61dc35e0634511c6d55af5cc5 Mon Sep 17 00:00:00 2001 From: iceweasel-oai Date: Thu, 10 Sep 2026 18:44:55 +0000 Subject: [PATCH] Support temporary and minimal filesystem grants in MXC (#44620) ## Why MXC rejected symbolic temporary-directory and minimal filesystem entries, preventing policies using them from reaching execution. ## What changed - Resolve `Tmpdir` entries from absolute `TEMP` and `TMP` paths in the filtered command environment, matching variable names case-insensitively. - Ignore `SlashTmp` entries because `/tmp` has no special meaning on the Windows executor. - Include platform read roots when the policy requests platform defaults, preserving explicit denies. ## Testing Add coverage for temporary paths and invalid environment values, optional platform roots, explicit denies, empty commands, and Windows case-insensitive read/write overrides. GitOrigin-RevId: feb69157ff35bcf648fa0ed0e7d4376818bc1941 --- codex-rs/mxc-sandbox/src/policy.rs | 51 +++++++- codex-rs/mxc-sandbox/src/policy_tests.rs | 157 +++++++++++++++++++++++ 2 files changed, 202 insertions(+), 6 deletions(-) diff --git a/codex-rs/mxc-sandbox/src/policy.rs b/codex-rs/mxc-sandbox/src/policy.rs index 46b767794a..55ee549fa8 100644 --- a/codex-rs/mxc-sandbox/src/policy.rs +++ b/codex-rs/mxc-sandbox/src/policy.rs @@ -44,7 +44,7 @@ pub enum PolicyError { RelativePolicyCwd, #[error("MXC requires an absolute command working directory")] RelativeCommandCwd, - #[error("MXC symbolic filesystem paths other than :root are not implemented")] + #[error("MXC policy contains an unresolved symbolic filesystem path")] UnsupportedSymbolicPath, #[error("MXC requires a Unicode command working directory")] NonUnicodeCommandCwd, @@ -69,7 +69,7 @@ pub fn build_request( command_cwd: &Path, env: Vec, volume_roots: &[PathBuf], - _platform_read_roots: &[PathBuf], + platform_read_roots: &[PathBuf], ) -> Result { if command.command.is_empty() { return Err(PolicyError::EmptyCommand); @@ -82,7 +82,44 @@ pub fn build_request( if !command_cwd.is_absolute() { return Err(PolicyError::RelativeCommandCwd); } - let policy = permissions.file_system_sandbox_policy(); + let mut policy = permissions.file_system_sandbox_policy(); + // Resolve Windows temporary directories from the filtered command + // environment, including case-insensitive names. + let mut temp_values = HashMap::new(); + for (key, value) in env.iter().filter_map(|entry| entry.split_once('=')) { + if key.eq_ignore_ascii_case("TEMP") || key.eq_ignore_ascii_case("TMP") { + temp_values.insert(key.to_ascii_uppercase(), value); + } + } + let temp_paths = ["TEMP", "TMP"] + .into_iter() + .filter_map(|key| temp_values.get(key).copied()) + .filter(|path| Path::new(path).is_absolute()) + .map(AbsolutePathBuf::from_absolute_path) + .collect::>>()?; + policy.entries = policy + .entries + .into_iter() + .flat_map(|entry| { + match &entry.path { + FileSystemPath::Special { + value: FileSystemSpecialPath::Tmpdir, + } => temp_paths + .iter() + .map(|path| { + let mut entry = entry.clone(); + entry.path = path.clone().into(); + entry + }) + .collect(), + // /tmp has no special meaning on the Windows executor. + FileSystemPath::Special { + value: FileSystemSpecialPath::SlashTmp, + } => Vec::new(), + _ => vec![entry], + } + }) + .collect(); let full_disk_write = policy.has_full_disk_write_access(); let volumes = volume_roots .iter() @@ -140,6 +177,9 @@ pub fn build_request( .filter(|(_, path)| fs.can_read_local_path_with_cwd(path.as_path(), cwd)) .map(|(key, path)| (key.clone(), path.clone())), ); + if policy.include_platform_defaults() { + read.extend(collect_paths(platform_read_roots.iter().cloned())?); + } write.retain(|key, _| !carveouts.contains_key(key) && !deny.contains_key(key)); read.retain(|key, _| !write.contains_key(key) && !deny.contains_key(key)); let volume_roots = volume_roots @@ -268,15 +308,14 @@ pub(super) fn materialize_volume_roots( })), FileSystemPath::Special { value: - FileSystemSpecialPath::Minimal - | FileSystemSpecialPath::ProjectRoots { .. } + FileSystemSpecialPath::ProjectRoots { .. } | FileSystemSpecialPath::Tmpdir | FileSystemSpecialPath::SlashTmp, } => return Err(PolicyError::UnsupportedSymbolicPath), FileSystemPath::Path { .. } | FileSystemPath::GlobPattern { .. } | FileSystemPath::Special { - value: FileSystemSpecialPath::Unknown { .. }, + value: FileSystemSpecialPath::Minimal | FileSystemSpecialPath::Unknown { .. }, } => entries.push(entry), } } diff --git a/codex-rs/mxc-sandbox/src/policy_tests.rs b/codex-rs/mxc-sandbox/src/policy_tests.rs index 5df6230e59..dd684ccd9b 100644 --- a/codex-rs/mxc-sandbox/src/policy_tests.rs +++ b/codex-rs/mxc-sandbox/src/policy_tests.rs @@ -95,6 +95,16 @@ fn command(permissions: &PermissionProfile, cwd: &Path) -> MxcCommand { } } +#[test] +fn empty_command_is_rejected_before_native_launch() -> Result<()> { + let root = tempfile::tempdir()?; + let mut parsed = command(&PermissionProfile::read_only(), root.path()); + parsed.command.clear(); + let error = build_request(&parsed, root.path(), Vec::new(), &[], &[]).unwrap_err(); + assert_eq!(error.to_string(), "MXC command must not be empty"); + Ok(()) +} + #[test] fn native_grants_preserve_denies_and_read_only_carveouts() -> Result<()> { let temp = tempfile::tempdir()?; @@ -424,6 +434,111 @@ fn root_deny_keeps_only_narrow_explicit_grants() -> Result<()> { Ok(()) } +#[test] +fn windows_temp_roots_use_filtered_environment_and_keep_explicit_denies() -> Result<()> { + let temp = tempfile::tempdir()?; + let canonical = canonical_root(&temp)?; + let root = canonical.as_path(); + let first = root.join("temp").join("~"); + let second = root.join("tmp with spaces"); + let denied = first.join("secret"); + let fs = FileSystemSandboxPolicy::restricted(vec![ + FileSystemSandboxEntry::new( + FileSystemPath::Special { + value: FileSystemSpecialPath::Tmpdir, + }, + FileSystemAccessMode::Write, + ), + FileSystemSandboxEntry::new( + FileSystemPath::Special { + value: FileSystemSpecialPath::SlashTmp, + }, + FileSystemAccessMode::Write, + ), + entry(&denied, FileSystemAccessMode::Deny)?, + ]); + let profile = + PermissionProfile::from_runtime_permissions(&fs, NetworkSandboxPolicy::Restricted); + let request = build_request( + &command(&profile, root), + root, + vec![ + format!("temp={}", first.display()), + format!("Tmp={}", second.display()), + ], + &[], + &[], + )?; + assert_eq!( + request.policy.readwrite_paths, + vec![first.to_str().unwrap(), second.to_str().unwrap()] + ); + assert_eq!(request.policy.denied_paths, vec![denied.to_str().unwrap()]); + + for value in [ + None, + Some(""), + Some("relative"), + Some("~/scratch"), + Some("~\\scratch"), + Some("C:temp"), + Some("\\temp"), + ] { + let request = build_request( + &command(&profile, root), + root, + value + .map(|value| vec![format!("TEMP={value}"), format!("TMP={value}")]) + .unwrap_or_default(), + &[], + &[], + )?; + assert_eq!(request.policy.readwrite_paths, Vec::::new()); + assert_eq!(request.policy.denied_paths, vec![denied.to_str().unwrap()]); + } + Ok(()) +} + +#[test] +fn minimal_platform_roots_preserve_explicit_denies() -> Result<()> { + let temp = tempfile::tempdir()?; + let root = canonical_root(&temp)?; + let allowed = root.join("platform"); + let denied = root.join("secret"); + for include_defaults in [false, true] { + let mut entries = vec![entry(&denied, FileSystemAccessMode::Deny)?]; + if include_defaults { + entries.push(FileSystemSandboxEntry::new( + FileSystemPath::Special { + value: FileSystemSpecialPath::Minimal, + }, + FileSystemAccessMode::Read, + )); + } + let profile = PermissionProfile::from_runtime_permissions( + &FileSystemSandboxPolicy::restricted(entries), + NetworkSandboxPolicy::Restricted, + ); + let request = build_request( + &command(&profile, &root), + &root, + Vec::new(), + &[], + &[allowed.clone(), denied.clone()], + )?; + let expected_read = if include_defaults { + vec![allowed.to_str().unwrap().to_owned()] + } else { + Vec::new() + }; + assert_eq!( + (request.policy.readonly_paths, request.policy.denied_paths), + (expected_read, vec![denied.to_str().unwrap().to_owned()]) + ); + } + Ok(()) +} + #[test] fn symbolic_root_preserves_equal_path_precedence_and_denies() -> Result<()> { let temp = tempfile::tempdir()?; @@ -538,6 +653,48 @@ fn narrowing_one_volume_keeps_the_other_volume_root_grant() -> Result<()> { Ok(()) } +#[cfg(windows)] +#[test] +fn volume_enumeration_uses_windows_identity_for_read_write_overrides() -> Result<()> { + let temp = tempfile::tempdir()?; + let root = canonical_root(&temp)?; + let child = root.join("MiXeD"); + std::fs::create_dir(&child)?; + let alias = child.to_str().unwrap().to_ascii_lowercase(); + let fs = FileSystemSandboxPolicy::restricted(vec![ + entry(&root, FileSystemAccessMode::Read)?, + entry(Path::new(&alias), FileSystemAccessMode::Write)?, + ]); + let profile = + PermissionProfile::from_runtime_permissions(&fs, NetworkSandboxPolicy::Restricted); + let request = build_request( + &command(&profile, &root), + &root, + Vec::new(), + std::slice::from_ref(&root), + &[], + )?; + assert_eq!( + request.policy.readwrite_paths, + vec![ + PathUri::from_host_native_path(&alias)? + .to_abs_path()? + .as_path() + .to_str() + .unwrap() + .to_owned() + ] + ); + let readonly_child: Vec<_> = request + .policy + .readonly_paths + .into_iter() + .filter(|path| path.eq_ignore_ascii_case(child.to_str().unwrap())) + .collect(); + assert_eq!(readonly_child, Vec::::new()); + Ok(()) +} + #[test] fn relative_working_directories_fail_before_launch() -> Result<()> { let root = tempfile::tempdir()?;