Merge commit '2b8ce702a715059adc338c5b098b72ae4fdb2089' into codex/psec-4394-git-06-merge-guard

This commit is contained in:
Chris Bookholt
2026-07-02 02:59:51 -07:00
42 changed files with 8483 additions and 742 deletions
+39 -7
View File
@@ -13,6 +13,7 @@ use std::path::PathBuf;
use crate::FsmonitorOverride;
use crate::apply_output::parse_git_apply_output;
use crate::git_command::GitRunner;
use crate::git_config_sources::ensure_no_worktree_config_sources;
use crate::merge_driver::ensure_no_selected_merge_drivers;
use crate::patch_paths::extract_patch_path_inventory;
use crate::reverse_staging::stage_effective_paths_for_reverse;
@@ -49,13 +50,19 @@ pub struct ApplyGitResult {
pub fn apply_git_patch(req: &ApplyGitRequest) -> io::Result<ApplyGitResult> {
let git = GitRunner::for_cwd_io(&req.cwd)?;
let mut cfg_parts = configured_git_config_parts();
ensure_no_worktree_config_sources(&git, &req.cwd, &cfg_parts)?;
let requested_cwd = std::fs::canonicalize(&req.cwd)?;
let git_root = resolve_git_root(&git, &req.cwd, &cfg_parts)?;
if git_root != requested_cwd {
ensure_no_worktree_config_sources(&git, &git_root, &cfg_parts)?;
}
// Write unified diff into a temporary file
let (tmpdir, patch_path) = write_temp_patch(&req.diff)?;
// Keep tmpdir alive until function end to ensure the file exists
let _guard = tmpdir;
let patch_path_inventory = extract_patch_path_inventory(&git, &patch_path, req.revert)?;
let patch_path_inventory =
extract_patch_path_inventory(&git, &git_root, &patch_path, req.revert, &cfg_parts)?;
let patch_paths = &patch_path_inventory.effective_paths;
let filter_guard =
ensure_no_selected_executable_git_filters(&git, &git_root, patch_paths, &cfg_parts)?;
@@ -160,12 +167,11 @@ fn resolve_git_root(
git_config_args: &[String],
) -> io::Result<PathBuf> {
let requested_cwd = std::fs::canonicalize(cwd)?;
let mut command = git.command();
let mut command = git.command_for_cwd(&requested_cwd)?;
command
.args(git_config_args)
.arg("rev-parse")
.arg("--show-toplevel")
.current_dir(&requested_cwd);
.arg("--show-toplevel");
let out = git.output(command)?;
let code = out.status.code().unwrap_or(-1);
if code != 0 {
@@ -203,7 +209,7 @@ fn resolve_git_root(
Ok(root)
}
fn configured_git_config_parts() -> Vec<String> {
pub(crate) fn configured_git_config_parts() -> Vec<String> {
let mut cfg_parts = Vec::new();
if let Ok(cfg) = std::env::var("CODEX_APPLY_GIT_CFG") {
for pair in cfg.split(',') {
@@ -231,14 +237,13 @@ pub(crate) fn run_git(
git_cfg: &[String],
args: &[String],
) -> io::Result<(i32, String, String)> {
let mut cmd = git.command();
let mut cmd = git.command_for_cwd(cwd)?;
for p in git_cfg {
cmd.arg(p);
}
for a in args {
cmd.arg(a);
}
cmd.current_dir(cwd);
let out = git.output(cmd)?;
let code = out.status.code().unwrap_or(-1);
let stdout = String::from_utf8_lossy(&out.stdout).into_owned();
@@ -848,4 +853,31 @@ diff --git a/ghost.txt b/ghost.txt\n--- a/ghost.txt\n+++ b/ghost.txt\n@@ -1,1 +1
assert!(error.to_string().contains("instead of expected worktree"));
}
}
#[cfg(unix)]
#[test]
fn apply_propagates_unsafe_repository_metadata_before_git_launch() {
use std::os::unix::fs::symlink;
let repo = init_repo();
let root = repo.path();
let external = tempfile::tempdir().expect("external metadata parent");
let admin = external.path().join("admin");
std::fs::rename(root.join(".git"), &admin).expect("move metadata external");
symlink(&admin, root.join("switch")).expect("worktree metadata switch");
std::fs::write(root.join(".git"), "gitdir: switch\n").expect("write gitdir marker");
let (code, _, stderr) = run(root, &["git", "rev-parse", "--absolute-git-dir"]);
assert_eq!(code, 0, "native Git fixture: {stderr}");
let request = ApplyGitRequest {
cwd: root.to_path_buf(),
diff: String::new(),
revert: false,
preflight: true,
};
let error = apply_git_patch(&request).expect_err("unsafe metadata route");
assert_eq!(error.kind(), io::ErrorKind::PermissionDenied);
assert!(error.to_string().contains("Git metadata route crosses"));
assert!(!error.to_string().contains("PATH"));
}
}