mirror of
https://github.com/openai/codex.git
synced 2026-11-17 22:10:37 +00:00
Mark deny-path parent directories created by the Windows MXC launcher (#59240)
## What changed Create an empty `.chatgptcreated` sentinel in each missing deny-path parent directory created by the Windows MXC launcher. Leave existing directories and sentinel contents untouched, including when another process creates a directory first. Reject deny-path parents that use the reserved `.chatgptcreated` name, accounting for case and trailing dots or spaces, before creating directories. If sentinel creation fails, attempt to remove the newly created directory so preparation can be retried. ## Testing Extend Windows policy tests to cover sentinel creation, preservation of existing directories and sentinel contents, and rejection of reserved directory names before any parents are created. GitOrigin-RevId: 640a1f3e05c118cca357643745e0d3b437354ffb
This commit is contained in:
@@ -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::<Vec<_>>();
|
||||
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()
|
||||
))
|
||||
})?;
|
||||
}
|
||||
|
||||
@@ -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()?;
|
||||
|
||||
Reference in New Issue
Block a user