mirror of
https://github.com/openai/codex.git
synced 2026-11-26 23:40:43 +00:00
Fix missing git sandbox mount
This commit is contained in:
@@ -1377,9 +1377,8 @@ pub(crate) fn default_read_only_subpaths_for_writable_root(
|
||||
// writable root itself.
|
||||
let top_level_git_is_file = top_level_git.as_path().is_file();
|
||||
let top_level_git_is_dir = top_level_git.as_path().is_dir();
|
||||
let should_protect_top_level_git = top_level_git_is_dir
|
||||
|| top_level_git_is_file
|
||||
|| (protect_missing_preserved_paths && !has_ancestor_git_metadata(writable_root.as_path()));
|
||||
let should_protect_top_level_git =
|
||||
top_level_git_is_dir || top_level_git_is_file || protect_missing_preserved_paths;
|
||||
if should_protect_top_level_git {
|
||||
if top_level_git_is_file
|
||||
&& is_git_pointer_file(&top_level_git)
|
||||
@@ -1558,34 +1557,6 @@ fn has_explicit_write_entry_for_preserved_path(
|
||||
})
|
||||
}
|
||||
|
||||
fn has_ancestor_git_metadata(path: &Path) -> bool {
|
||||
let Some(parent) = path.parent() else {
|
||||
return false;
|
||||
};
|
||||
parent
|
||||
.ancestors()
|
||||
.any(|ancestor| git_metadata_dir(&ancestor.join(PRESERVED_GIT_PATH_NAME)).is_some())
|
||||
}
|
||||
|
||||
fn git_metadata_dir(path: &Path) -> Option<AbsolutePathBuf> {
|
||||
let Ok(metadata) = std::fs::symlink_metadata(path) else {
|
||||
return None;
|
||||
};
|
||||
if metadata.is_dir() {
|
||||
return AbsolutePathBuf::from_absolute_path(path).ok();
|
||||
}
|
||||
if !metadata.is_file() {
|
||||
return None;
|
||||
}
|
||||
let Ok(dot_git) = AbsolutePathBuf::from_absolute_path(path) else {
|
||||
return None;
|
||||
};
|
||||
if !is_git_pointer_file(&dot_git) {
|
||||
return None;
|
||||
}
|
||||
resolve_gitdir_from_file(&dot_git)
|
||||
}
|
||||
|
||||
fn is_git_pointer_file(path: &AbsolutePathBuf) -> bool {
|
||||
path.as_path().is_file()
|
||||
&& path.as_path().file_name() == Some(OsStr::new(PRESERVED_GIT_PATH_NAME))
|
||||
@@ -1788,7 +1759,7 @@ mod tests {
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn writable_roots_keep_missing_git_absent_under_parent_git_repo() {
|
||||
fn writable_roots_protect_missing_git_under_parent_git_repo() {
|
||||
let repo = TempDir::new().expect("tempdir");
|
||||
fs::create_dir(repo.path().join(".git")).expect("create parent .git");
|
||||
let cwd = repo.path().join("sub");
|
||||
@@ -1811,10 +1782,10 @@ mod tests {
|
||||
assert_eq!(writable_roots.len(), 1);
|
||||
assert_eq!(writable_roots[0].root, expected_root);
|
||||
assert!(
|
||||
!writable_roots[0]
|
||||
writable_roots[0]
|
||||
.read_only_subpaths
|
||||
.contains(&expected_dot_git),
|
||||
"missing child .git under an existing parent repo stays absent so Git discovery still works"
|
||||
"missing child .git under an existing parent repo should be read-only so git init fails"
|
||||
);
|
||||
assert!(
|
||||
writable_roots[0]
|
||||
@@ -1975,10 +1946,6 @@ mod tests {
|
||||
let extra_root = AbsolutePathBuf::from_absolute_path(extra.path()).expect("absolute extra");
|
||||
let policy = SandboxPolicy::WorkspaceWrite {
|
||||
writable_roots: vec![extra_root],
|
||||
read_only_access: ReadOnlyAccess::Restricted {
|
||||
include_platform_defaults: false,
|
||||
readable_roots: vec![],
|
||||
},
|
||||
network_access: false,
|
||||
exclude_tmpdir_env_var: true,
|
||||
exclude_slash_tmp: true,
|
||||
@@ -2019,12 +1986,20 @@ mod tests {
|
||||
let file_system_policy =
|
||||
FileSystemSandboxPolicy::from_legacy_sandbox_policy_for_cwd(&policy, relative_cwd);
|
||||
|
||||
let mut expected_entries = vec![FileSystemSandboxEntry {
|
||||
path: FileSystemPath::Special {
|
||||
value: FileSystemSpecialPath::CurrentWorkingDirectory,
|
||||
let mut expected_entries = vec![
|
||||
FileSystemSandboxEntry {
|
||||
path: FileSystemPath::Special {
|
||||
value: FileSystemSpecialPath::Root,
|
||||
},
|
||||
access: FileSystemAccessMode::Read,
|
||||
},
|
||||
access: FileSystemAccessMode::Write,
|
||||
}];
|
||||
FileSystemSandboxEntry {
|
||||
path: FileSystemPath::Special {
|
||||
value: FileSystemSpecialPath::CurrentWorkingDirectory,
|
||||
},
|
||||
access: FileSystemAccessMode::Write,
|
||||
},
|
||||
];
|
||||
expected_entries.extend(
|
||||
default_read_only_subpaths_for_writable_root(
|
||||
&expected_root,
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
//! between user and agent.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::collections::HashSet;
|
||||
use std::fmt;
|
||||
use std::ops::Mul;
|
||||
use std::path::Path;
|
||||
@@ -4436,22 +4435,20 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn workspace_write_keeps_missing_git_absent_under_parent_repo() {
|
||||
fn workspace_write_protects_missing_git_under_parent_repo() {
|
||||
let repo = TempDir::new().expect("tempdir");
|
||||
std::fs::create_dir(repo.path().join(".git")).expect("create parent .git");
|
||||
let cwd = repo.path().join("sub");
|
||||
std::fs::create_dir(&cwd).expect("create subdir");
|
||||
let expected_root = AbsolutePathBuf::from_absolute_path(&cwd).expect("absolute cwd");
|
||||
let expected_dot_git =
|
||||
AbsolutePathBuf::from_absolute_path(cwd.join(".git")).expect("canonical .git");
|
||||
let expected_dot_codex =
|
||||
AbsolutePathBuf::from_absolute_path(cwd.join(".codex")).expect("canonical .codex");
|
||||
let expected_dot_agents =
|
||||
AbsolutePathBuf::from_absolute_path(cwd.join(".agents")).expect("canonical .agents");
|
||||
let policy = SandboxPolicy::WorkspaceWrite {
|
||||
writable_roots: vec![],
|
||||
read_only_access: ReadOnlyAccess::Restricted {
|
||||
include_platform_defaults: false,
|
||||
readable_roots: vec![],
|
||||
},
|
||||
network_access: false,
|
||||
exclude_tmpdir_env_var: true,
|
||||
exclude_slash_tmp: true,
|
||||
@@ -4463,7 +4460,8 @@ mod tests {
|
||||
expected_root.to_path_buf(),
|
||||
vec![
|
||||
expected_dot_agents.to_path_buf(),
|
||||
expected_dot_codex.to_path_buf()
|
||||
expected_dot_codex.to_path_buf(),
|
||||
expected_dot_git.to_path_buf()
|
||||
],
|
||||
)]
|
||||
);
|
||||
@@ -4481,10 +4479,6 @@ mod tests {
|
||||
AbsolutePathBuf::from_absolute_path(&extra).expect("absolute extra root");
|
||||
let policy = SandboxPolicy::WorkspaceWrite {
|
||||
writable_roots: vec![expected_extra.clone()],
|
||||
read_only_access: ReadOnlyAccess::Restricted {
|
||||
include_platform_defaults: false,
|
||||
readable_roots: vec![],
|
||||
},
|
||||
network_access: false,
|
||||
exclude_tmpdir_env_var: true,
|
||||
exclude_slash_tmp: true,
|
||||
@@ -4513,46 +4507,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_workspace_write_nested_readable_root_stays_writable() {
|
||||
let cwd = TempDir::new().expect("tempdir");
|
||||
let docs = AbsolutePathBuf::resolve_path_against_base("docs", cwd.path());
|
||||
let canonical_cwd = codex_utils_absolute_path::canonicalize_preserving_symlinks(cwd.path())
|
||||
.expect("canonicalize cwd");
|
||||
let expected_dot_codex = AbsolutePathBuf::from_absolute_path(canonical_cwd.join(".codex"))
|
||||
.expect("canonical .codex");
|
||||
let expected_dot_git = AbsolutePathBuf::from_absolute_path(canonical_cwd.join(".git"))
|
||||
.expect("canonical .git");
|
||||
let expected_dot_agents =
|
||||
AbsolutePathBuf::from_absolute_path(canonical_cwd.join(".agents"))
|
||||
.expect("canonical .agents");
|
||||
let policy = SandboxPolicy::WorkspaceWrite {
|
||||
writable_roots: vec![],
|
||||
read_only_access: ReadOnlyAccess::Restricted {
|
||||
include_platform_defaults: true,
|
||||
readable_roots: vec![docs],
|
||||
},
|
||||
network_access: false,
|
||||
exclude_tmpdir_env_var: true,
|
||||
exclude_slash_tmp: true,
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
sorted_writable_roots(
|
||||
FileSystemSandboxPolicy::from_legacy_sandbox_policy_for_cwd(&policy, cwd.path())
|
||||
.get_writable_roots_with_cwd(cwd.path())
|
||||
),
|
||||
vec![(
|
||||
canonical_cwd,
|
||||
vec![
|
||||
expected_dot_agents.to_path_buf(),
|
||||
expected_dot_codex.to_path_buf(),
|
||||
expected_dot_git.to_path_buf()
|
||||
]
|
||||
)]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn file_system_policy_rejects_legacy_bridge_for_non_workspace_writes() {
|
||||
let cwd = if cfg!(windows) {
|
||||
|
||||
Reference in New Issue
Block a user