mirror of
https://github.com/openai/codex.git
synced 2026-11-16 22:00:36 +00:00
Centralize permission profile compatibility helpers in protocol (#61784)
## What changed Move legacy sandbox policy conversion into `codex-protocol` and add a shared helper for resolving compatibility writable roots. Re-export the policy helper from `codex-sandboxing` and use the writable-roots helper in both Windows sandbox backends, preserving the existing workspace-write fallback. ## Testing Add a regression test confirming that an output-only permission profile retains the fallback's output and working-directory writable roots, including protected `.git` and `.codex` subpaths. GitOrigin-RevId: a40474eb74544f34f7eea8135a7eb1b57706dbc2
This commit is contained in:
@@ -20,6 +20,7 @@ use strum_macros::Display;
|
||||
use tracing::error;
|
||||
use ts_rs::TS;
|
||||
|
||||
use crate::models::PermissionProfile;
|
||||
use crate::protocol::NetworkAccess;
|
||||
use crate::protocol::SandboxPolicy;
|
||||
use crate::protocol::WritableRoot;
|
||||
@@ -2424,6 +2425,52 @@ pub(crate) fn default_read_only_subpaths_for_writable_root(
|
||||
dedup_absolute_paths(subpaths, /*normalize_effective_paths*/ false)
|
||||
}
|
||||
|
||||
/// Resolves the compatibility writable roots for a permission profile.
|
||||
pub fn compatibility_writable_roots_for_permission_profile(
|
||||
permissions: &PermissionProfile,
|
||||
cwd: &Path,
|
||||
) -> Vec<WritableRoot> {
|
||||
compatibility_sandbox_policy_for_permission_profile(permissions, cwd)
|
||||
.get_writable_roots_with_cwd(cwd)
|
||||
}
|
||||
|
||||
/// Builds a legacy sandbox policy, using the workspace-write fallback for
|
||||
/// profiles that cannot be expressed directly.
|
||||
pub fn compatibility_sandbox_policy_for_permission_profile(
|
||||
permissions: &PermissionProfile,
|
||||
cwd: &Path,
|
||||
) -> SandboxPolicy {
|
||||
if let Ok(policy) = permissions.to_legacy_sandbox_policy(cwd) {
|
||||
return policy;
|
||||
}
|
||||
|
||||
let (file_system_policy, network_policy) = permissions.to_runtime_permissions();
|
||||
let cwd_abs = AbsolutePathBuf::from_absolute_path(cwd).ok();
|
||||
let writable_roots = file_system_policy
|
||||
.get_writable_roots_with_cwd(cwd)
|
||||
.into_iter()
|
||||
.map(|root| root.root)
|
||||
.filter(|root| cwd_abs.as_ref() != Some(root))
|
||||
.collect();
|
||||
let tmpdir_writable = std::env::var_os("TMPDIR")
|
||||
.filter(|tmpdir| !tmpdir.is_empty())
|
||||
.and_then(|tmpdir| AbsolutePathBuf::from_absolute_path(PathBuf::from(tmpdir)).ok())
|
||||
.is_some_and(|tmpdir| {
|
||||
file_system_policy.can_write_local_path_with_cwd(tmpdir.as_path(), cwd)
|
||||
});
|
||||
let slash_tmp = Path::new("/tmp");
|
||||
let slash_tmp_writable = slash_tmp.is_absolute()
|
||||
&& slash_tmp.is_dir()
|
||||
&& file_system_policy.can_write_local_path_with_cwd(slash_tmp, cwd);
|
||||
|
||||
SandboxPolicy::WorkspaceWrite {
|
||||
writable_roots,
|
||||
network_access: network_policy.is_enabled(),
|
||||
exclude_tmpdir_env_var: !tmpdir_writable,
|
||||
exclude_slash_tmp: !slash_tmp_writable,
|
||||
}
|
||||
}
|
||||
|
||||
/// Rebuilds the filesystem policy that legacy sandbox runtimes enforce for a
|
||||
/// concrete cwd.
|
||||
///
|
||||
|
||||
@@ -1452,6 +1452,39 @@ fn missing_symbolic_metadata_carveouts_need_direct_runtime_enforcement() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compatibility_writable_roots_preserve_the_output_only_fallback() -> io::Result<()> {
|
||||
let temp = TempDir::new()?;
|
||||
let root = AbsolutePathBuf::from_absolute_path(canonicalize_preserving_symlinks(temp.path())?)?;
|
||||
let cwd = root.join("workspace");
|
||||
let output = root.join("output");
|
||||
std::fs::create_dir_all(output.join(".git"))?;
|
||||
let policy = FileSystemSandboxPolicy::restricted(vec![FileSystemSandboxEntry::new(
|
||||
output.clone().into(),
|
||||
FileSystemAccessMode::Write,
|
||||
)]);
|
||||
|
||||
let profile =
|
||||
PermissionProfile::from_runtime_permissions(&policy, NetworkSandboxPolicy::Restricted);
|
||||
|
||||
assert_eq!(
|
||||
compatibility_writable_roots_for_permission_profile(&profile, &cwd),
|
||||
[
|
||||
WritableRoot {
|
||||
root: output.clone(),
|
||||
read_only_subpaths: vec![output.join(".git")],
|
||||
protected_metadata_names: Vec::new(),
|
||||
},
|
||||
WritableRoot {
|
||||
root: cwd.clone(),
|
||||
read_only_subpaths: vec![cwd.join(".codex")],
|
||||
protected_metadata_names: Vec::new(),
|
||||
},
|
||||
]
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn root_write_with_read_only_child_is_not_full_disk_write() {
|
||||
let cwd = TempDir::new().expect("tempdir");
|
||||
|
||||
Reference in New Issue
Block a user