mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Prevent apply_patch from widening write permissions (#39614)
## Why Deriving permissions from the parent of an already-writable patch target can unnecessarily grant write access outside the intended workspace. ## What changed Skip targets already covered by the active filesystem sandbox policy before deriving additional parent-directory permissions. Targets outside the writable area continue to request the required parent access. ## Testing Added unit and CLI regression coverage for workspace-directory targets, already-writable parents, and symlink escapes outside the workspace. GitOrigin-RevId: 187109ff0b56a1a399cb8a9981b7e822977025d5
This commit is contained in:
1 parent
929e2b9c1d
commit
530c1aed58
3 files changed
+104
No files matched your search
@@ -239,6 +239,11 @@ fn write_permissions_for_paths(
|
||||
) -> Option<AdditionalPermissionProfile> {
|
||||
let write_paths = file_paths
|
||||
.iter()
|
||||
// Skip already-writable targets before deriving parent permissions.
|
||||
// Otherwise, a writable directory could grant access to its parent.
|
||||
.filter(|path| {
|
||||
!file_system_sandbox_policy.can_write_path_with_cwd(path.as_path(), cwd.as_path())
|
||||
})
|
||||
.map(|path| {
|
||||
path.parent()
|
||||
.unwrap_or_else(|| path.clone())
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
use super::*;
|
||||
use codex_apply_patch::MaybeApplyPatchVerified;
|
||||
use codex_exec_server::LOCAL_FS;
|
||||
use codex_protocol::permissions::FileSystemAccessMode;
|
||||
use codex_protocol::permissions::FileSystemSandboxEntry;
|
||||
use codex_protocol::permissions::FileSystemSandboxPolicy;
|
||||
use codex_protocol::protocol::FileChange;
|
||||
use core_test_support::PathBufExt;
|
||||
@@ -312,3 +314,35 @@ fn write_permissions_for_paths_keep_dirs_outside_workspace_root() {
|
||||
Some(vec![expected_outside])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn write_permissions_for_paths_do_not_widen_workspace_root_target() {
|
||||
let tmp = TempDir::new().expect("tmp");
|
||||
let cwd = tmp.path().join("workspace").abs();
|
||||
std::fs::create_dir_all(&cwd).expect("create workspace");
|
||||
let sandbox_policy = FileSystemSandboxPolicy::workspace_write(
|
||||
&[],
|
||||
/*exclude_tmpdir_env_var*/ true,
|
||||
/*exclude_slash_tmp*/ true,
|
||||
);
|
||||
|
||||
let permissions =
|
||||
write_permissions_for_paths(std::slice::from_ref(&cwd), &sandbox_policy, &cwd);
|
||||
|
||||
assert_eq!(permissions, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn write_permissions_for_paths_do_not_regrant_an_already_writable_parent() {
|
||||
let tmp = TempDir::new().expect("tmp");
|
||||
let cwd = tmp.path().abs();
|
||||
let file_path = cwd.join("protected.txt");
|
||||
let sandbox_policy = FileSystemSandboxPolicy::restricted(vec![
|
||||
FileSystemSandboxEntry::new(cwd.clone().into(), FileSystemAccessMode::Write),
|
||||
FileSystemSandboxEntry::new(file_path.clone().into(), FileSystemAccessMode::Read),
|
||||
]);
|
||||
|
||||
let permissions = write_permissions_for_paths(&[file_path], &sandbox_policy, &cwd);
|
||||
|
||||
assert_eq!(permissions, None);
|
||||
}
|
||||
Reference in new issue
Block a user