diff --git a/codex-rs/mxc-sandbox/src/policy.rs b/codex-rs/mxc-sandbox/src/policy.rs index f2e4253840..4fb07c5e9d 100644 --- a/codex-rs/mxc-sandbox/src/policy.rs +++ b/codex-rs/mxc-sandbox/src/policy.rs @@ -1,6 +1,6 @@ //! Translate canonical permissions into MXC path grants. //! Denies and read-only carveouts remain separate kernel policy lists. -//! Missing deny parents are created before deny globs and volume grants are expanded. +//! Missing deny parents are created and marked before deny globs and volume grants are expanded. //! Alias limitations are documented in the crate README. use std::collections::HashMap; @@ -134,13 +134,49 @@ pub(super) fn build_request( let policy = materialize_temporary_paths(permissions.file_system_sandbox_policy(), &env)?; #[cfg(windows)] for denied in policy.get_unreadable_roots_with_cwd(cwd) { - if let Some(parent) = denied.as_path().parent() - && matches!(parent.try_exists(), Ok(false)) - { - std::fs::create_dir_all(parent).map_err(|error| { + let Some(parent) = denied.as_path().parent() else { + continue; + }; + if parent.components().any(|component| { + component + .as_os_str() + .to_string_lossy() + .trim_end_matches(['.', ' ']) + .eq_ignore_ascii_case(".chatgptcreated") + }) { + return Err(PolicyError::PolicyResolution(format!( + "MXC deny path {} uses the reserved directory name .chatgptcreated", + denied.display() + ))); + } + let mut missing = parent + .ancestors() + .take_while(|path| matches!(path.try_exists(), Ok(false))) + .collect::>(); + missing.reverse(); + for directory in missing { + // Only mark directories this launcher actually created. + match std::fs::create_dir(directory) { + Ok(()) => {} + Err(error) + if error.kind() == std::io::ErrorKind::AlreadyExists && directory.is_dir() => + { + continue; + } + Err(error) => { + return Err(PolicyError::PolicyResolution(format!( + "MXC currently requires deny paths to have existing parent directories. Could not create {}: {error}. Create this directory manually or correct the path/permissions, then retry.", + directory.display() + ))); + } + } + let sentinel = directory.join(".chatgptcreated"); + std::fs::File::create_new(&sentinel).map_err(|error| { + // Leave a failed preparation retryable when the directory is still empty. + let _ = std::fs::remove_dir(directory); PolicyError::PolicyResolution(format!( - "MXC currently requires deny paths to have existing parent directories. Could not create {}: {error}. Create this directory manually or correct the path/permissions, then retry.", - parent.display() + "cannot create MXC directory sentinel {}: {error}", + sentinel.display() )) })?; } diff --git a/codex-rs/mxc-sandbox/src/policy_tests.rs b/codex-rs/mxc-sandbox/src/policy_tests.rs index 5aafe3926f..95ebeefc82 100644 --- a/codex-rs/mxc-sandbox/src/policy_tests.rs +++ b/codex-rs/mxc-sandbox/src/policy_tests.rs @@ -313,6 +313,12 @@ fn volume_expansion_does_not_turn_read_only_child_writable() -> Result<()> { expected_write_paths.insert(1, missing.to_str().unwrap()); assert!(denied.parent().unwrap().is_dir()); assert!(!denied.exists()); + for directory in [&missing, &missing.join("parent")] { + assert_eq!(std::fs::read(directory.join(".chatgptcreated"))?, b""); + } + for directory in [root, readonly.as_path(), writable.as_path()] { + assert!(!directory.join(".chatgptcreated").exists()); + } } assert_eq!(request.policy.readwrite_paths, expected_write_paths); assert_eq!(request.policy.denied_paths, vec![denied.to_str().unwrap()]); @@ -576,11 +582,46 @@ fn denied_parents_are_created_before_glob_expansion() -> Result<()> { missing.join("child").to_str().unwrap() ] ); + let sentinel = missing.join(".chatgptcreated"); + assert_eq!(std::fs::read(&sentinel)?, b""); + std::fs::write(&sentinel, "preserve existing contents")?; + build_request(&command(&profile, &root), &root, Vec::new(), &[], &[])?; + assert_eq!( + std::fs::read_to_string(&sentinel)?, + "preserve existing contents" + ); + std::fs::remove_file(sentinel)?; std::fs::remove_dir(&missing)?; } Ok(()) } +#[cfg(windows)] +#[test] +fn deny_parent_cannot_use_sentinel_name() -> Result<()> { + let temp = tempfile::tempdir()?; + let root = canonical_root(&temp)?; + let missing = root.join("missing"); + for name in [".chatgptcreated", ".CHATGPTCREATED", ".chatgptcreated. "] { + let denied = missing.join(name).join("secret"); + let fs = FileSystemSandboxPolicy::restricted(vec![ + entry(&root, FileSystemAccessMode::Write)?, + entry(&denied, FileSystemAccessMode::Deny)?, + ]); + let profile = + PermissionProfile::from_runtime_permissions(&fs, NetworkSandboxPolicy::Restricted); + let error = build_request(&command(&profile, &root), &root, Vec::new(), &[], &[]) + .expect_err("sentinel directory names must be rejected before creating parents"); + assert!( + error + .to_string() + .contains("reserved directory name .chatgptcreated") + ); + assert!(!missing.exists()); + } + Ok(()) +} + #[test] fn root_deny_keeps_only_narrow_explicit_grants() -> Result<()> { let temp = tempfile::tempdir()?;