mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Make permission transforms aware of executor path context (#41909)
## What changed - Add context-aware normalization, materialization, and intersection helpers for additional filesystem permissions. - Resolve project roots, home-relative deny globs, temporary directories, and filesystem roots from the executor's `FileSystemSandboxPolicyContext` while preserving URI path conventions and symlinked paths. - Keep deny constraints when intersecting grants across POSIX, Windows, and UNC paths, and reject incompatible path conventions when executor context is available. ## Testing - Cover cross-convention validation, empty temporary-directory contexts, home-relative deny globs, and rooted deny globs for POSIX, Windows, and UNC paths. GitOrigin-RevId: 14fd866a17a0ac4a595aa4f0e2fb82bfae1a8838
This commit is contained in:
1 parent
2f0a5d5516
commit
b51b07785b
6 files changed
+534
-246
No files matched your search
@@ -1,8 +1,11 @@
|
||||
use super::effective_file_system_sandbox_policy;
|
||||
use super::intersect_permission_profiles;
|
||||
use super::intersect_permission_profiles_with_context;
|
||||
use super::materialize_additional_permissions;
|
||||
use super::materialize_additional_permissions_with_context;
|
||||
use super::merge_file_system_policy_with_additional_permissions;
|
||||
use super::normalize_additional_permissions;
|
||||
use super::normalize_additional_permissions_with_context;
|
||||
use super::should_require_platform_sandbox;
|
||||
use codex_protocol::models::AdditionalPermissionProfile as PermissionProfile;
|
||||
use codex_protocol::models::FileSystemPermissions;
|
||||
@@ -11,9 +14,11 @@ use codex_protocol::permissions::FileSystemAccessMode;
|
||||
use codex_protocol::permissions::FileSystemPath;
|
||||
use codex_protocol::permissions::FileSystemSandboxEntry;
|
||||
use codex_protocol::permissions::FileSystemSandboxPolicy;
|
||||
use codex_protocol::permissions::FileSystemSandboxPolicyContext;
|
||||
use codex_protocol::permissions::FileSystemSpecialPath;
|
||||
use codex_protocol::permissions::NetworkSandboxPolicy;
|
||||
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||
use codex_utils_path_uri::PathUri;
|
||||
use dunce::canonicalize;
|
||||
use pretty_assertions::assert_eq;
|
||||
#[cfg(unix)]
|
||||
@@ -129,6 +134,39 @@ fn normalize_additional_permissions_preserves_network() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normalize_additional_permissions_only_checks_convention_with_context() {
|
||||
let windows_path = PathUri::parse("file:///C:/workspace/out").expect("Windows path URI");
|
||||
let permissions = PermissionProfile {
|
||||
file_system: Some(FileSystemPermissions {
|
||||
entries: vec![FileSystemSandboxEntry::new(
|
||||
FileSystemPath::Path { path: windows_path },
|
||||
FileSystemAccessMode::Write,
|
||||
)],
|
||||
glob_scan_max_depth: None,
|
||||
}),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
normalize_additional_permissions(permissions.clone()).expect("context-free permissions"),
|
||||
permissions
|
||||
);
|
||||
|
||||
let cwd = PathUri::parse("file:///workspace").expect("POSIX cwd URI");
|
||||
let workspace_roots = [cwd.clone()];
|
||||
let context = FileSystemSandboxPolicyContext {
|
||||
cwd: &cwd,
|
||||
workspace_roots: &workspace_roots,
|
||||
user_home_dir: None,
|
||||
temporary_directories: None,
|
||||
};
|
||||
assert!(
|
||||
normalize_additional_permissions_with_context(permissions, &context).is_err(),
|
||||
"context-aware normalization should reject mismatched conventions"
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn normalize_additional_permissions_preserves_symlinked_write_paths() {
|
||||
@@ -242,6 +280,7 @@ fn materialize_additional_permissions_preserves_authority_and_constraints() {
|
||||
canonicalize(temp_dir.path()).expect("canonicalize temp dir"),
|
||||
)
|
||||
.expect("absolute temp dir");
|
||||
let home = PathUri::from_host_native_path("~").expect("host home");
|
||||
let project_path = |subpath: &str| FileSystemPath::Special {
|
||||
value: FileSystemSpecialPath::project_roots(Some(subpath.to_owned())),
|
||||
};
|
||||
@@ -263,6 +302,8 @@ fn materialize_additional_permissions_preserves_authority_and_constraints() {
|
||||
FileSystemSandboxEntry::new(project_path("private/reopened"), Write),
|
||||
reopened.clone(),
|
||||
deny_glob("**/*.env".to_owned()),
|
||||
deny_glob("~".to_owned()),
|
||||
deny_glob("~/private/*.env".to_owned()),
|
||||
]);
|
||||
let expected = profile(vec![
|
||||
FileSystemSandboxEntry::new(cwd.clone().into(), Write),
|
||||
@@ -270,6 +311,12 @@ fn materialize_additional_permissions_preserves_authority_and_constraints() {
|
||||
FileSystemSandboxEntry::skip_missing_path(cwd.join("readonly").into(), Read),
|
||||
reopened,
|
||||
deny_glob(cwd.join("**/*.env").to_string_lossy().into_owned()),
|
||||
deny_glob(home.inferred_native_path_string()),
|
||||
deny_glob(
|
||||
home.join("private/*.env")
|
||||
.expect("home-relative deny glob")
|
||||
.inferred_native_path_string(),
|
||||
),
|
||||
]);
|
||||
|
||||
assert_eq!(
|
||||
@@ -279,6 +326,50 @@ fn materialize_additional_permissions_preserves_authority_and_constraints() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn materialize_additional_permissions_ignores_empty_tmpdir_deny() {
|
||||
let cwd = PathUri::parse("file:///workspace").expect("cwd URI");
|
||||
let workspace_roots = [cwd.clone()];
|
||||
let temporary_directories = [];
|
||||
let context = FileSystemSandboxPolicyContext {
|
||||
cwd: &cwd,
|
||||
workspace_roots: &workspace_roots,
|
||||
user_home_dir: None,
|
||||
temporary_directories: Some(&temporary_directories),
|
||||
};
|
||||
let write = FileSystemSandboxEntry::new(
|
||||
FileSystemPath::Path { path: cwd.clone() },
|
||||
FileSystemAccessMode::Write,
|
||||
);
|
||||
let permissions = PermissionProfile {
|
||||
file_system: Some(FileSystemPermissions {
|
||||
entries: vec![
|
||||
write.clone(),
|
||||
FileSystemSandboxEntry::new(
|
||||
FileSystemPath::Special {
|
||||
value: FileSystemSpecialPath::Tmpdir,
|
||||
},
|
||||
FileSystemAccessMode::Deny,
|
||||
),
|
||||
],
|
||||
glob_scan_max_depth: None,
|
||||
}),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
materialize_additional_permissions_with_context(permissions, &context)
|
||||
.expect("permissions"),
|
||||
PermissionProfile {
|
||||
file_system: Some(FileSystemPermissions {
|
||||
entries: vec![write],
|
||||
glob_scan_max_depth: None,
|
||||
}),
|
||||
..Default::default()
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn intersect_permission_profiles_preserves_explicit_empty_requested_reads() {
|
||||
let temp_dir = TempDir::new().expect("create temp dir");
|
||||
@@ -449,6 +540,58 @@ fn intersect_permission_profiles_preserves_deny_across_case_variant_grant() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn intersect_permission_profiles_preserves_rooted_first_segment_glob_deny() {
|
||||
use FileSystemAccessMode::Deny;
|
||||
use FileSystemAccessMode::Write;
|
||||
|
||||
for (cwd_uri, grant_uri, pattern) in [
|
||||
("file:///workspace", "file:///fooX", "/foo*/*"),
|
||||
("file:///workspace", "file:///foo%5Cbar", r"/foo\\*/*"),
|
||||
("file:///C:/workspace", "file:///C:/fooX", r"C:\foo*\*"),
|
||||
(
|
||||
"file://server/share/workspace",
|
||||
"file://server/share/fooX",
|
||||
r"\\server\share\foo*\*",
|
||||
),
|
||||
] {
|
||||
let cwd = PathUri::parse(cwd_uri).expect("cwd URI");
|
||||
let grant_path = PathUri::parse(grant_uri).expect("grant URI");
|
||||
let workspace_roots = [cwd.clone()];
|
||||
let context = FileSystemSandboxPolicyContext {
|
||||
cwd: &cwd,
|
||||
workspace_roots: &workspace_roots,
|
||||
user_home_dir: None,
|
||||
temporary_directories: None,
|
||||
};
|
||||
let grant_entry =
|
||||
FileSystemSandboxEntry::new(FileSystemPath::Path { path: grant_path }, Write);
|
||||
let deny_entry = FileSystemSandboxEntry::new(
|
||||
FileSystemPath::GlobPattern {
|
||||
pattern: pattern.to_string(),
|
||||
},
|
||||
Deny,
|
||||
);
|
||||
let profile = |entries| PermissionProfile {
|
||||
file_system: Some(FileSystemPermissions {
|
||||
entries,
|
||||
glob_scan_max_depth: None,
|
||||
}),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
intersect_permission_profiles_with_context(
|
||||
profile(vec![grant_entry.clone(), deny_entry.clone()]),
|
||||
profile(vec![grant_entry.clone()]),
|
||||
&context,
|
||||
),
|
||||
profile(vec![grant_entry, deny_entry]),
|
||||
"deny glob should survive for {pattern}",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn intersect_permission_profiles_preserves_opaque_child_deny() {
|
||||
use std::ffi::OsString;
|
||||
|
||||
Reference in new issue
Block a user