mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
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
This commit is contained in:
1 parent
1afffeabb2
commit
5c013177d8
2 files changed
+202
-6
No files matched your search
@@ -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<String>,
|
||||
volume_roots: &[PathBuf],
|
||||
_platform_read_roots: &[PathBuf],
|
||||
platform_read_roots: &[PathBuf],
|
||||
) -> Result<ExecutionRequest, PolicyError> {
|
||||
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::<std::io::Result<Vec<_>>>()?;
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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::<String>::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::<String>::new());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn relative_working_directories_fail_before_launch() -> Result<()> {
|
||||
let root = tempfile::tempdir()?;
|
||||
|
||||
Reference in new issue
Block a user