mirror of
https://github.com/openai/codex.git
synced 2026-12-06 01:10:58 +00:00
fix(git-utils): validate linked worktree trust targets
Co-authored-by: Codex noreply@openai.com
This commit is contained in:
co-authored by
Codex noreply@openai.com
parent
d36a3ead3c
commit
7a857a306a
@@ -610,6 +610,11 @@ async fn resolve_root_git_project_for_trust_detects_worktree_pointer_without_git
|
||||
std::fs::create_dir_all(&worktree_git_dir).unwrap();
|
||||
std::fs::create_dir_all(&worktree_root).unwrap();
|
||||
std::fs::create_dir_all(worktree_root.join("nested")).unwrap();
|
||||
std::fs::write(
|
||||
worktree_git_dir.join("gitdir"),
|
||||
format!("{}\n", worktree_root.join(".git").display()),
|
||||
)
|
||||
.unwrap();
|
||||
std::fs::write(
|
||||
worktree_root.join(".git"),
|
||||
format!("gitdir: {}\n", worktree_git_dir.display()),
|
||||
@@ -629,6 +634,35 @@ async fn resolve_root_git_project_for_trust_detects_worktree_pointer_without_git
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn resolve_root_git_project_for_trust_rejects_unrelated_worktree_pointer() {
|
||||
let tmp = TempDir::new().expect("tempdir");
|
||||
let repo_root = tmp.path().join("repo");
|
||||
let worktree_git_dir = repo_root.join(".git").join("worktrees").join("feature-x");
|
||||
let trusted_worktree_root = tmp.path().join("trusted-wt");
|
||||
let unrelated_worktree_root = tmp.path().join("unrelated-wt");
|
||||
std::fs::create_dir_all(&worktree_git_dir).unwrap();
|
||||
std::fs::create_dir_all(&trusted_worktree_root).unwrap();
|
||||
std::fs::create_dir_all(&unrelated_worktree_root).unwrap();
|
||||
std::fs::write(trusted_worktree_root.join(".git"), "").unwrap();
|
||||
std::fs::write(
|
||||
worktree_git_dir.join("gitdir"),
|
||||
format!("{}\n", trusted_worktree_root.join(".git").display()),
|
||||
)
|
||||
.unwrap();
|
||||
std::fs::write(
|
||||
unrelated_worktree_root.join(".git"),
|
||||
format!("gitdir: {}\n", worktree_git_dir.display()),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert!(
|
||||
resolve_root_git_project_for_trust(LOCAL_FS.as_ref(), &unrelated_worktree_root.abs())
|
||||
.await
|
||||
.is_none()
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn resolve_root_git_project_for_trust_non_worktrees_gitdir_returns_none() {
|
||||
let tmp = TempDir::new().expect("tempdir");
|
||||
|
||||
@@ -769,6 +769,25 @@ pub async fn resolve_root_git_project_for_trust(
|
||||
return None;
|
||||
}
|
||||
|
||||
let worktree_dot_git_s = fs
|
||||
.read_file_text(&git_dir_path.join("gitdir"), /*sandbox*/ None)
|
||||
.await
|
||||
.ok()?;
|
||||
let worktree_dot_git_rel = worktree_dot_git_s.trim();
|
||||
if worktree_dot_git_rel.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let worktree_dot_git =
|
||||
AbsolutePathBuf::resolve_path_against_base(worktree_dot_git_rel, git_dir_path.as_path());
|
||||
if fs
|
||||
.canonicalize(&worktree_dot_git, /*sandbox*/ None)
|
||||
.await
|
||||
.ok()?
|
||||
!= fs.canonicalize(&dot_git, /*sandbox*/ None).await.ok()?
|
||||
{
|
||||
return None;
|
||||
}
|
||||
|
||||
let common_dir = worktrees_dir.parent()?;
|
||||
common_dir.parent()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user