From 6b40c77a0f7ff4679e34282ba5de0ac7f5a5e889 Mon Sep 17 00:00:00 2001 From: Chris Bookholt Date: Fri, 3 Jul 2026 15:55:43 -0700 Subject: [PATCH 01/20] fix(git-utils): parse Git boolean syntax consistently --- codex-rs/git-utils/src/git_config.rs | 65 ++++++++++++++ codex-rs/git-utils/src/git_config_tests.rs | 64 +++++++++++++ .../src/repository_authority/plain_config.rs | 53 +++++------ .../src/repository_authority_tests.rs | 89 +++++++++++++++++-- 4 files changed, 235 insertions(+), 36 deletions(-) diff --git a/codex-rs/git-utils/src/git_config.rs b/codex-rs/git-utils/src/git_config.rs index 236ab89ef7..92496132a1 100644 --- a/codex-rs/git-utils/src/git_config.rs +++ b/codex-rs/git-utils/src/git_config.rs @@ -1,6 +1,71 @@ use std::path::Component; use std::path::Path; +pub(crate) fn parse_git_boolean(value: &[u8]) -> Option { + if value.eq_ignore_ascii_case(b"true") + || value.eq_ignore_ascii_case(b"yes") + || value.eq_ignore_ascii_case(b"on") + { + return Some(true); + } + if value.is_empty() + || value.eq_ignore_ascii_case(b"false") + || value.eq_ignore_ascii_case(b"no") + || value.eq_ignore_ascii_case(b"off") + { + return Some(false); + } + + // Git parses the remaining boolean spellings through `git_parse_int`: C + // base-0 syntax, an optional binary-unit suffix, and signed `int` bounds. + let value = std::str::from_utf8(value) + .ok()? + .trim_start_matches(|value: char| value.is_ascii_whitespace()); + let (negative, unsigned) = match value.as_bytes().first() { + Some(b'-') => (true, &value[1..]), + Some(b'+') => (false, &value[1..]), + Some(_) => (false, value), + None => return None, + }; + let (base, unsigned) = if unsigned.starts_with("0x") || unsigned.starts_with("0X") { + (16, &unsigned[2..]) + } else if unsigned.starts_with('0') { + (8, unsigned) + } else { + (10, unsigned) + }; + let digit_count = unsigned + .bytes() + .take_while(|byte| match base { + 8 => matches!(byte, b'0'..=b'7'), + 10 => byte.is_ascii_digit(), + 16 => byte.is_ascii_hexdigit(), + _ => false, + }) + .count(); + if digit_count == 0 { + return None; + } + let (digits, suffix) = unsigned.split_at(digit_count); + let factor = if suffix.is_empty() { + 1_i128 + } else if suffix.eq_ignore_ascii_case("k") { + 1024 + } else if suffix.eq_ignore_ascii_case("m") { + 1024 * 1024 + } else if suffix.eq_ignore_ascii_case("g") { + 1024 * 1024 * 1024 + } else { + return None; + }; + let magnitude = i128::from_str_radix(digits, base).ok()?; + let signed = if negative { -magnitude } else { magnitude }; + let value = signed.checked_mul(factor)?; + (i128::from(i32::MIN)..=i128::from(i32::MAX)) + .contains(&value) + .then_some(value != 0) +} + pub(crate) fn path_is_within(path: &Path, root: &Path) -> bool { let mut path_components = path.components(); for root_component in root.components() { diff --git a/codex-rs/git-utils/src/git_config_tests.rs b/codex-rs/git-utils/src/git_config_tests.rs index 3795caf2f8..825d9a597f 100644 --- a/codex-rs/git-utils/src/git_config_tests.rs +++ b/codex-rs/git-utils/src/git_config_tests.rs @@ -1,5 +1,69 @@ use super::*; +#[test] +fn git_boolean_accepts_native_git_spellings() { + for (value, expected) in [ + (b"true".as_slice(), true), + (b"TRUE".as_slice(), true), + (b"yes".as_slice(), true), + (b"On".as_slice(), true), + (b"1".as_slice(), true), + (b"2".as_slice(), true), + (b"-1".as_slice(), true), + (b"+1".as_slice(), true), + (b"01".as_slice(), true), + (b"0x1".as_slice(), true), + (b"0Xf".as_slice(), true), + (b"010".as_slice(), true), + (b"1k".as_slice(), true), + (b"1M".as_slice(), true), + (b"1g".as_slice(), true), + (b" 1".as_slice(), true), + (b"2147483647".as_slice(), true), + (b"-2147483648".as_slice(), true), + (b"-2g".as_slice(), true), + (b"false".as_slice(), false), + (b"FALSE".as_slice(), false), + (b"no".as_slice(), false), + (b"Off".as_slice(), false), + (b"".as_slice(), false), + (b"0".as_slice(), false), + (b"-0".as_slice(), false), + (b"+0".as_slice(), false), + (b"00".as_slice(), false), + (b"0x0".as_slice(), false), + (b"0k".as_slice(), false), + ] { + assert_eq!(parse_git_boolean(value), Some(expected), "value {value:?}"); + } +} + +#[test] +fn git_boolean_rejects_values_native_git_rejects() { + for value in [ + b"08".as_slice(), + b"-08".as_slice(), + b"2147483648".as_slice(), + b"-2147483649".as_slice(), + b"9223372036854775807".as_slice(), + b"2g".as_slice(), + b"-3g".as_slice(), + b"0x".as_slice(), + b"0b1".as_slice(), + b"1kb".as_slice(), + b"1foo".as_slice(), + b"1_0".as_slice(), + b"1 ".as_slice(), + b" ".as_slice(), + b"+".as_slice(), + b"-".as_slice(), + b"not-a-bool".as_slice(), + b"\xff".as_slice(), + ] { + assert_eq!(parse_git_boolean(value), None, "value {value:?}"); + } +} + #[test] fn path_containment_uses_component_boundaries() { let root = Path::new("/repo/root"); diff --git a/codex-rs/git-utils/src/repository_authority/plain_config.rs b/codex-rs/git-utils/src/repository_authority/plain_config.rs index b3a4253b77..1464ac9095 100644 --- a/codex-rs/git-utils/src/repository_authority/plain_config.rs +++ b/codex-rs/git-utils/src/repository_authority/plain_config.rs @@ -4,6 +4,7 @@ use std::path::PathBuf; use super::bytes_to_path; use super::read_bounded_file; +use crate::git_config::parse_git_boolean; #[derive(Debug, Eq, PartialEq)] pub(crate) enum CommonConfigAuthority { @@ -50,7 +51,7 @@ pub(crate) fn inspect_plain_common_config_authority( return Ok(CommonConfigAuthority::Unproven); } let bare = match unique_explicit_value(&config, "core", "bare") { - Ok(Some(value)) => match explicit_boolean(value.as_ref()) { + Ok(Some(value)) => match parse_git_boolean(value.as_ref()) { Some(value) => Some(value), None => return Ok(CommonConfigAuthority::Unproven), }, @@ -58,7 +59,7 @@ pub(crate) fn inspect_plain_common_config_authority( Err(()) => return Ok(CommonConfigAuthority::Unproven), }; match unique_explicit_value(&config, "extensions", "worktreeConfig") { - Ok(Some(value)) if explicit_boolean(value.as_ref()) == Some(false) => {} + Ok(Some(value)) if parse_git_boolean(value.as_ref()) == Some(false) => {} Ok(None) => {} Ok(Some(_)) | Err(()) => return Ok(CommonConfigAuthority::Unproven), } @@ -99,39 +100,29 @@ fn unique_explicit_value( let Some(sections) = config.sections_by_name(section_name) else { return Ok(None); }; - let mut occurrences = 0usize; - let mut explicit_values = Vec::new(); + let mut occurrence = None; for section in sections { if section.header().subsection_name().is_some() { return Err(()); } - occurrences += section - .value_names() - .filter(|name| { - let name: &str = name.as_ref(); - name.eq_ignore_ascii_case(value_name) - }) - .count(); - explicit_values.extend( - section - .values(value_name) - .into_iter() - .map(|value| value.as_ref().to_vec()), - ); + for name in section.value_names() { + let name: &str = name.as_ref(); + if !name.eq_ignore_ascii_case(value_name) { + continue; + } + if occurrence.is_some() { + return Err(()); + } + let value = section + .value_implicit(name) + .ok_or(())? + .map(|value| value.as_ref().to_vec()); + occurrence = Some(value); + } } - match (occurrences, explicit_values.len()) { - (0, 0) => Ok(None), - (1, 1) => Ok(explicit_values.pop()), - _ => Err(()), - } -} - -fn explicit_boolean(value: &[u8]) -> Option { - if value.eq_ignore_ascii_case(b"true") { - Some(true) - } else if value.eq_ignore_ascii_case(b"false") { - Some(false) - } else { - None + match occurrence { + None => Ok(None), + Some(Some(value)) => Ok(Some(value)), + Some(None) => Err(()), } } diff --git a/codex-rs/git-utils/src/repository_authority_tests.rs b/codex-rs/git-utils/src/repository_authority_tests.rs index 0447fa0d82..9f07340939 100644 --- a/codex-rs/git-utils/src/repository_authority_tests.rs +++ b/codex-rs/git-utils/src/repository_authority_tests.rs @@ -81,6 +81,36 @@ fn plain_common_bare_parser_matches_native_boolean_syntax_and_is_stricter_on_dup CommonConfigAuthority::Bare, Some(true), ), + ( + "yes", + b"[core]\n\tbare = yes\n".as_slice(), + CommonConfigAuthority::Bare, + Some(true), + ), + ( + "on", + b"[core]\n\tbare = on\n".as_slice(), + CommonConfigAuthority::Bare, + Some(true), + ), + ( + "numeric true", + b"[core]\n\tbare = 1\n".as_slice(), + CommonConfigAuthority::Bare, + Some(true), + ), + ( + "hex true", + b"[core]\n\tbare = 0x1\n".as_slice(), + CommonConfigAuthority::Bare, + Some(true), + ), + ( + "suffixed true", + b"[core]\n\tbare = 1k\n".as_slice(), + CommonConfigAuthority::Bare, + Some(true), + ), ( "false", b"[core]\n\tbare = false\n".as_slice(), @@ -133,13 +163,13 @@ fn plain_common_bare_parser_matches_native_boolean_syntax_and_is_stricter_on_dup } #[test] -fn common_bare_proof_accepts_only_explicit_boolean_literals() { +fn common_bare_proof_rejects_invalid_git_boolean_values() { for (name, body) in [ - ("empty", b"[core]\n\tbare =\n".as_slice()), - ("yes", b"[core]\n\tbare = yes\n".as_slice()), - ("on", b"[core]\n\tbare = on\n".as_slice()), - ("one", b"[core]\n\tbare = 1\n".as_slice()), ("leading zero", b"[core]\n\tbare = 08\n".as_slice()), + ( + "negative leading zero", + b"[core]\n\tbare = -08\n".as_slice(), + ), ( "positive overflow", b"[core]\n\tbare = 2147483648\n".as_slice(), @@ -152,6 +182,8 @@ fn common_bare_proof_accepts_only_explicit_boolean_literals() { "i64 max", b"[core]\n\tbare = 9223372036854775807\n".as_slice(), ), + ("suffix overflow", b"[core]\n\tbare = 2g\n".as_slice()), + ("trailing junk", b"[core]\n\tbare = 1foo\n".as_slice()), ] { let common = write_common_config(body); assert_eq!( @@ -162,6 +194,34 @@ fn common_bare_proof_accepts_only_explicit_boolean_literals() { } } +#[test] +fn common_bare_proof_accepts_explicit_false_worktree_config_spellings() { + for value in ["false", "no", "off", "", "0", "-0", "+0", "00", "0x0"] { + let common = write_common_config( + format!("[core]\n\tbare = yes\n[extensions]\n\tworktreeConfig = {value}\n").as_bytes(), + ); + assert_eq!( + inspect_plain_common_config_authority(common.path()).expect(value), + CommonConfigAuthority::Bare, + "authority result for worktreeConfig={value:?}" + ); + } +} + +#[test] +fn common_bare_proof_rejects_explicit_true_worktree_config_spellings() { + for value in ["true", "yes", "on", "1", "-1", "0x1", "1k"] { + let common = write_common_config( + format!("[core]\n\tbare = yes\n[extensions]\n\tworktreeConfig = {value}\n").as_bytes(), + ); + assert_eq!( + inspect_plain_common_config_authority(common.path()).expect(value), + CommonConfigAuthority::Unproven, + "authority result for worktreeConfig={value:?}" + ); + } +} + #[test] fn common_bare_proof_rejects_includes_worktree_config_and_relative_worktree() { let include_target = tempfile::NamedTempFile::new().expect("included config"); @@ -226,6 +286,25 @@ fn common_config_absolute_worktree_is_returned_as_authority() { ); } +#[test] +fn common_config_false_bare_spellings_allow_absolute_worktree() { + let worktree = tempfile::tempdir().expect("worktree"); + for bare in ["false", "no", "off", "", "0", "-0", "+0", "00", "0x0"] { + let common = write_common_config( + format!( + "[core]\n\tbare = {bare}\n\tworktree = {}\n", + git_config_path(worktree.path()) + ) + .as_bytes(), + ); + assert_eq!( + inspect_plain_common_config_authority(common.path()).expect(bare), + CommonConfigAuthority::Worktree(worktree.path().to_path_buf()), + "authority result for core.bare={bare:?}" + ); + } +} + #[test] fn common_config_rejects_contradictory_or_malformed_bare_with_absolute_worktree() { let worktree = tempfile::tempdir().expect("worktree"); From 49664d3c75f233ddbf151ebcc659f10bc298a240 Mon Sep 17 00:00:00 2001 From: Chris Bookholt Date: Fri, 3 Jul 2026 16:13:55 -0700 Subject: [PATCH 02/20] codex/git: isolate three-way reverse staging --- codex-rs/git-utils/src/apply.rs | 58 +++-- codex-rs/git-utils/src/git_command.rs | 201 ++++++++++++++++++ codex-rs/git-utils/src/guarded_config.rs | 81 ++++++- .../src/guarded_config/merge_overlay.rs | 136 +++++------- .../src/guarded_config/merge_overlay_tests.rs | 5 +- .../git-utils/src/guarded_config_tests.rs | 22 +- .../git-utils/src/merge_driver_race_tests.rs | 197 ++++++++++++++++- codex-rs/git-utils/src/merge_driver_tests.rs | 173 ++++++++++++++- .../src/repository_authority/authority.rs | 11 +- codex-rs/git-utils/src/reverse_apply_tests.rs | 65 +++++- codex-rs/git-utils/src/reverse_staging.rs | 16 ++ 11 files changed, 833 insertions(+), 132 deletions(-) diff --git a/codex-rs/git-utils/src/apply.rs b/codex-rs/git-utils/src/apply.rs index e01903aa16..f26c8e7dd9 100644 --- a/codex-rs/git-utils/src/apply.rs +++ b/codex-rs/git-utils/src/apply.rs @@ -16,6 +16,7 @@ use crate::apply_output::parse_git_apply_output; use crate::git_command::GitRunner; use crate::guarded_config::GuardedGitConfig; use crate::patch_paths::extract_patch_path_inventory_guarded; +use crate::reverse_staging::ReverseApplyMode; use crate::reverse_staging::stage_effective_paths_for_reverse; #[cfg(test)] use crate::safe_git::DISABLED_HOOKS_PATH; @@ -43,6 +44,21 @@ pub struct ApplyGitResult { pub cmd_for_log: String, } +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +enum ApplyStrategy { + Direct, + ThreeWay, +} + +impl ApplyStrategy { + fn reverse_mode(self) -> ReverseApplyMode { + match self { + Self::Direct => ReverseApplyMode::Direct, + Self::ThreeWay => ReverseApplyMode::ThreeWay, + } + } +} + /// Apply a unified diff to the target repository by shelling out to `git apply`. /// /// When [`ApplyGitRequest::preflight`] is `true`, this behaves like `git apply --check` and @@ -111,25 +127,43 @@ pub fn apply_git_patch(req: &ApplyGitRequest) -> io::Result { plain_check_args.push(patch_arg.clone()); let (plain_check_code, _, _) = run_guarded_apply(&config, &plain_check_args)?; - let mut args = vec!["apply".to_string()]; - if plain_check_code != 0 { + let strategy = if plain_check_code != 0 { config.install_three_way_merge_policy()?; - args.push("--3way".to_string()); + ApplyStrategy::ThreeWay } else { - args.push("--index".to_string()); - } + ApplyStrategy::Direct + }; if req.revert { // Stage only after the worktree-only applicability decision. The // guarded config is shared with the final Git command. - stage_effective_paths_for_reverse(&mut config, patch_paths)?; + stage_effective_paths_for_reverse(&mut config, patch_paths, strategy.reverse_mode())?; } - if req.revert { - args.push("-R".to_string()); - } - args.push(patch_arg); - let cmd_for_log = config.render_command_for_log(&args)?; - let (code, stdout, stderr) = run_guarded_apply(&config, &args)?; + let (cmd_for_log, output) = match strategy { + ApplyStrategy::Direct => { + let mut args = vec!["apply".to_string(), "--index".to_string()]; + if req.revert { + args.push("-R".to_string()); + } + args.push(patch_arg); + let rendered = config.render_command_for_log(&args)?; + let (code, stdout, stderr) = run_guarded_apply(&config, &args)?; + (rendered, (code, stdout, stderr)) + } + ApplyStrategy::ThreeWay => { + let rendered = config.render_three_way_apply_for_log(req.revert, &patch_arg)?; + let output = config.run_three_way_apply(req.revert, &patch_arg)?; + ( + rendered, + ( + output.status.code().unwrap_or(-1), + String::from_utf8_lossy(&output.stdout).into_owned(), + String::from_utf8_lossy(&output.stderr).into_owned(), + ), + ) + } + }; + let (code, stdout, stderr) = output; let (mut applied_paths, mut skipped_paths, mut conflicted_paths) = if code == 0 { (patch_path_inventory.primary_paths, Vec::new(), Vec::new()) diff --git a/codex-rs/git-utils/src/git_command.rs b/codex-rs/git-utils/src/git_command.rs index eb39ded9e7..f04bfd7d2d 100644 --- a/codex-rs/git-utils/src/git_command.rs +++ b/codex-rs/git-utils/src/git_command.rs @@ -41,6 +41,73 @@ pub(crate) struct GitCommand { inner: Command, } +/// App-owned common repository metadata for one final three-way apply. +/// +/// The real per-worktree Git directory remains selected for HEAD and index +/// state, while this directory replaces every common config and attribute +/// source that could define a repository-selected executable helper. +pub(crate) struct IsolatedGitCommonDir { + root: tempfile::TempDir, +} + +impl IsolatedGitCommonDir { + pub(crate) fn config_path(&self) -> PathBuf { + self.root.path().join("config") + } + + fn system_config_path(&self) -> PathBuf { + self.root.path().join("system.gitconfig") + } + + fn global_config_path(&self) -> PathBuf { + self.root.path().join("global.gitconfig") + } + + fn home_path(&self) -> PathBuf { + self.root.path().join("home") + } + + fn xdg_config_home(&self) -> PathBuf { + self.root.path().join("xdg") + } + + fn validate(&self) -> io::Result<()> { + for path in [ + self.config_path(), + self.system_config_path(), + self.global_config_path(), + self.root.path().join("info/attributes"), + ] { + let metadata = std::fs::symlink_metadata(&path)?; + if !metadata.is_file() || metadata.file_type().is_symlink() { + return Err(io::Error::new( + io::ErrorKind::PermissionDenied, + format!("isolated Git metadata file changed at {}", path.display()), + )); + } + } + for path in [ + self.root.path().join("objects"), + self.root.path().join("refs"), + self.root.path().join("info"), + self.home_path(), + self.xdg_config_home(), + ] { + let metadata = std::fs::symlink_metadata(&path)?; + if !metadata.is_dir() || metadata.file_type().is_symlink() { + return Err(io::Error::new( + io::ErrorKind::PermissionDenied, + format!( + "isolated Git metadata directory changed at {}", + path.display() + ), + )); + } + } + Ok(()) + } +} + impl GitCommand { pub(crate) fn arg(&mut self, arg: impl AsRef) -> &mut Self { self.inner.arg(arg); @@ -134,6 +201,29 @@ impl GitRunner { self.config_environment.value(name) } + pub(crate) fn create_isolated_common_dir(&self) -> io::Result { + let root = tempfile::tempdir()?; + self.authority + .ensure_config_source_is_not_worktree_controlled( + root.path(), + "owned isolated Git common directory", + )?; + for path in ["objects", "refs", "info", "home", "xdg"] { + std::fs::create_dir_all(root.path().join(path))?; + } + for path in [ + "config", + "system.gitconfig", + "global.gitconfig", + "info/attributes", + ] { + std::fs::write(root.path().join(path), [])?; + } + let isolated = IsolatedGitCommonDir { root }; + isolated.validate()?; + Ok(isolated) + } + pub(crate) fn output(&self, mut command: GitCommand) -> io::Result { self.revalidate_active_repository_metadata()?; isolate_git_command_environment(&mut command.inner); @@ -142,6 +232,60 @@ impl GitRunner { command.inner.output() } + pub(crate) fn output_in_isolated_common_dir( + &self, + mut command: GitCommand, + isolated: &IsolatedGitCommonDir, + ) -> io::Result { + self.revalidate_active_repository_metadata()?; + isolated.validate()?; + self.authority + .ensure_config_source_is_not_worktree_controlled( + isolated.root.path(), + "owned isolated Git common directory", + )?; + isolate_git_command_environment(&mut command.inner); + scrub_repository_and_config_environment(&mut command.inner); + + let git_dir = self.authority.active_git_dir().ok_or_else(|| { + io::Error::new( + io::ErrorKind::PermissionDenied, + "active Git directory is unavailable for isolated three-way apply", + ) + })?; + let common_dir = self.authority.active_common_dir().ok_or_else(|| { + io::Error::new( + io::ErrorKind::PermissionDenied, + "active Git common directory is unavailable for isolated three-way apply", + ) + })?; + let worktree = self.authority.active_worktree_root(); + command + .inner + .env("GIT_DIR", git_dir) + .env("GIT_COMMON_DIR", isolated.root.path()) + .env("GIT_WORK_TREE", worktree) + .env("GIT_INDEX_FILE", git_dir.join("index")) + .env("GIT_OBJECT_DIRECTORY", common_dir.join("objects")) + .env("GIT_CONFIG_NOSYSTEM", "1") + .env("GIT_CONFIG_SYSTEM", isolated.system_config_path()) + .env("GIT_CONFIG_GLOBAL", isolated.global_config_path()) + .env("GIT_CONFIG_COUNT", "0") + .env("GIT_ATTR_NOSYSTEM", "1") + .env("GIT_NO_REPLACE_OBJECTS", "1") + .env("HOME", isolated.home_path()) + .env("XDG_CONFIG_HOME", isolated.xdg_config_home()); + #[cfg(windows)] + command + .inner + .env("APPDATA", isolated.home_path()) + .env("PROGRAMDATA", isolated.home_path()) + .env("USERPROFILE", isolated.home_path()); + command.inner.envs(crate::local_only_git_env()); + harden_git_launch_environment(&mut command.inner, &self.safe_path); + command.inner.output() + } + fn revalidate_active_repository_metadata(&self) -> io::Result<()> { self.authority.revalidate_active_repository_metadata() } @@ -168,6 +312,63 @@ impl GitRunner { } } +fn scrub_repository_and_config_environment(command: &mut Command) { + let mut names = std::env::vars_os() + .map(|(name, _)| name) + .filter(|name| isolated_launch_variable(name)) + .collect::>(); + names.extend( + command + .get_envs() + .filter(|&(name, _)| isolated_launch_variable(name)) + .map(|(name, _)| name.to_os_string()), + ); + names.sort(); + names.dedup(); + for name in names { + command.env_remove(name); + } +} + +fn isolated_launch_variable(name: &OsStr) -> bool { + let name = name.to_string_lossy().to_ascii_uppercase(); + matches!( + name.as_str(), + "GIT_DIR" + | "GIT_COMMON_DIR" + | "GIT_WORK_TREE" + | "GIT_INDEX_FILE" + | "GIT_INDEX_VERSION" + | "GIT_OBJECT_DIRECTORY" + | "GIT_ALTERNATE_OBJECT_DIRECTORIES" + | "GIT_NAMESPACE" + | "GIT_QUARANTINE_PATH" + | "GIT_GRAFT_FILE" + | "GIT_SHALLOW_FILE" + | "GIT_REPLACE_REF_BASE" + | "GIT_NO_REPLACE_OBJECTS" + | "GIT_ATTR_SOURCE" + | "GIT_ATTR_NOSYSTEM" + | "GIT_CONFIG" + | "GIT_CONFIG_GLOBAL" + | "GIT_CONFIG_SYSTEM" + | "GIT_CONFIG_NOSYSTEM" + | "GIT_CONFIG_COUNT" + | "GIT_CONFIG_PARAMETERS" + | "GIT_DEFAULT_HASH" + | "GIT_DEFAULT_REF_FORMAT" + | "GIT_REFERENCE_BACKEND" + | "HOME" + | "XDG_CONFIG_HOME" + | "APPDATA" + | "PROGRAMDATA" + | "USERPROFILE" + | "HOMEDRIVE" + | "HOMEPATH" + ) || name.starts_with("GIT_CONFIG_KEY_") + || name.starts_with("GIT_CONFIG_VALUE_") +} + #[cfg(test)] thread_local! { static GIT_RUNNER_CONSTRUCTION_COUNT: std::cell::Cell = const { std::cell::Cell::new(0) }; diff --git a/codex-rs/git-utils/src/guarded_config.rs b/codex-rs/git-utils/src/guarded_config.rs index 33c82f7b5c..7f5f5f9098 100644 --- a/codex-rs/git-utils/src/guarded_config.rs +++ b/codex-rs/git-utils/src/guarded_config.rs @@ -364,9 +364,6 @@ impl<'git> GuardedGitConfig<'git> { if let Some(neutralizer) = apply.and_then(FilterPolicySnapshot::neutralizer) { neutralizer.append_to(&self.identity, &mut command)?; } - if let Some(merge) = &self.merge { - merge.append_to(&self.identity, &mut command)?; - } if let Some(neutralizer) = git_add.and_then(FilterPolicySnapshot::neutralizer) { neutralizer.append_to(&self.identity, &mut command)?; } @@ -550,11 +547,82 @@ impl<'git> GuardedGitConfig<'git> { Ok(apply.checked_paths()) } + pub(crate) fn run_three_way_apply( + &self, + revert: bool, + patch_path: &str, + ) -> io::Result { + let (apply, _git_add) = self.ordered_filter_snapshots()?; + if apply.is_none() || !self.merge_policy_installed { + return Err(io::Error::new( + io::ErrorKind::PermissionDenied, + "three-way apply policy is not installed", + )); + } + let merge = self.merge.as_ref().ok_or_else(|| { + io::Error::new( + io::ErrorKind::PermissionDenied, + "isolated three-way config is unavailable", + ) + })?; + let isolated = merge.common_dir(&self.identity)?; + let mut command = self + .sources + .git + .command_for_cwd(&self.sources.canonical_root)?; + append_safe_scalar_overrides(&mut command); + BoundSubcommand::Apply.append_to(&mut command); + command.arg("--3way"); + if revert { + command.arg("-R"); + } + command.arg(patch_path); + self.sources + .git + .output_in_isolated_common_dir(command, isolated) + } + + pub(crate) fn render_three_way_apply_for_log( + &self, + revert: bool, + patch_path: &str, + ) -> io::Result { + let merge = self.merge.as_ref().ok_or_else(|| { + io::Error::new( + io::ErrorKind::PermissionDenied, + "isolated three-way config is unavailable", + ) + })?; + let _ = merge.common_dir(&self.identity)?; + let mut parts = vec![ + "env".to_string(), + "GIT_COMMON_DIR=".to_string(), + "git".to_string(), + ]; + parts.extend(safe_scalar_override_args()); + parts.push("apply".to_string()); + parts.push("--3way".to_string()); + if revert { + parts.push("-R".to_string()); + } + parts.push(patch_path.to_string()); + Ok(format!( + "(cd {} && {})", + quote_shell(&self.sources.canonical_root.display().to_string()), + parts + .into_iter() + .map(|part| quote_shell(&part)) + .collect::>() + .join(" ") + )) + } + #[cfg(test)] - pub(crate) fn merge_include_arg(&self) -> Option<&str> { + pub(crate) fn merge_common_config_path(&self) -> Option { self.merge .as_ref() - .map(SealedMergeConfigOverride::include_arg) + .and_then(|merge| merge.common_dir(&self.identity).ok()) + .map(crate::git_command::IsolatedGitCommonDir::config_path) } pub(crate) fn authorize_git_add_filter_paths(&mut self, paths: &[String]) -> io::Result<()> { @@ -581,9 +649,6 @@ impl<'git> GuardedGitConfig<'git> { if let Some(neutralizer) = apply.and_then(FilterPolicySnapshot::neutralizer) { neutralizer.append_rendered_args(&self.identity, &mut parts)?; } - if let Some(merge) = &self.merge { - merge.append_rendered_args(&self.identity, &mut parts)?; - } if let Some(neutralizer) = git_add.and_then(FilterPolicySnapshot::neutralizer) { neutralizer.append_rendered_args(&self.identity, &mut parts)?; } diff --git a/codex-rs/git-utils/src/guarded_config/merge_overlay.rs b/codex-rs/git-utils/src/guarded_config/merge_overlay.rs index 355b6e520b..02f553b562 100644 --- a/codex-rs/git-utils/src/guarded_config/merge_overlay.rs +++ b/codex-rs/git-utils/src/guarded_config/merge_overlay.rs @@ -7,10 +7,11 @@ use std::sync::Arc; use super::BoundSubcommand; use super::CapabilityIdentity; use super::GuardedGitConfig; -use crate::git_command::GitCommand; +use crate::git_command::IsolatedGitCommonDir; use crate::git_config::GitConfigEntry; const MERGE_CONFIG_PATTERN: &str = r"^(merge\.default|merge\..*\.driver)$"; +const SANITIZED_CONFIG_PATTERN: &str = r"^(core\.(repositoryformatversion|filemode|symlinks|ignorecase|precomposeunicode|protecthfs|protectntfs|trustctime|checkstat|longpaths|fscache|splitindex|sparsecheckout|sparsecheckoutcone|autocrlf|eol|safecrlf|checkroundtripencoding|bigfilethreshold|whitespace)|extensions\.(objectformat|compatobjectformat)|index\.(sparse|version)|apply\.(whitespace|ignorewhitespace)|merge\.conflictstyle)$"; /// A complete, fresh merge-config read bound to one authorized operation. /// @@ -36,60 +37,37 @@ impl MergeConfigSnapshot { Ok(()) } - fn nonempty_driver_keys(&self) -> io::Result> { - let mut keys = Vec::new(); - for entry in self.entries.values() { - if entry.key == "merge.default" { - continue; - } - let _driver = entry - .key - .strip_prefix("merge.") - .and_then(|key| key.strip_suffix(".driver")) - .ok_or_else(|| { - io::Error::new( - io::ErrorKind::InvalidData, - "malformed effective Git merge-driver key", - ) - })?; - if !entry.value.is_empty() { - keys.push(entry.key.as_str()); - } + fn sanitized_default_driver(&self) -> &str { + match self + .entries + .get("merge.default") + .map(|entry| entry.value.as_str()) + { + Some("binary") => "binary", + Some("union") => "union", + _ => "text", } - Ok(keys) } } -/// A sealed merge-driver-only include bound to one operation. +/// A sealed, helper-free common repository view bound to one operation. /// -/// Construction accepts only a complete `MergeConfigSnapshot`, and the type -/// exposes neither raw keys nor its include argument outside this module. +/// The real Git directory, index, worktree, and object store remain selected; +/// only common config and attributes are replaced for the final three-way +/// child. Construction accepts a complete merge snapshot so a caller cannot +/// attach an unreviewed or partial view. pub(super) struct SealedMergeConfigOverride { owner: Arc, - include_arg: String, - _config_dir: tempfile::TempDir, + common_dir: IsolatedGitCommonDir, } impl SealedMergeConfigOverride { - pub(super) fn append_to( + pub(super) fn common_dir( &self, owner: &Arc, - command: &mut GitCommand, - ) -> io::Result<()> { + ) -> io::Result<&IsolatedGitCommonDir> { self.ensure_owner(owner)?; - command.args(["-c", &self.include_arg]); - Ok(()) - } - - pub(super) fn append_rendered_args( - &self, - owner: &Arc, - args: &mut Vec, - ) -> io::Result<()> { - self.ensure_owner(owner)?; - args.push("-c".to_string()); - args.push(self.include_arg.clone()); - Ok(()) + Ok(&self.common_dir) } fn ensure_owner(&self, owner: &Arc) -> io::Result<()> { @@ -101,11 +79,6 @@ impl SealedMergeConfigOverride { } Ok(()) } - - #[cfg(test)] - pub(super) fn include_arg(&self) -> &str { - &self.include_arg - } } impl<'git> GuardedGitConfig<'git> { @@ -135,8 +108,8 @@ impl<'git> GuardedGitConfig<'git> { ), )); } - let neutralizer = self.build_merge_override(&snapshot)?; - self.attach_merge_override(neutralizer) + let isolated = self.build_merge_override(&snapshot)?; + self.attach_merge_override(isolated) } /// Read merge-driver policy from the frozen, authorized base invocation. @@ -188,48 +161,43 @@ impl<'git> GuardedGitConfig<'git> { fn build_merge_override( &self, snapshot: &MergeConfigSnapshot, - ) -> io::Result> { + ) -> io::Result { snapshot.ensure_owner(&self.identity)?; if self.merge_policy_installed { return Err(io::Error::new( io::ErrorKind::PermissionDenied, - "a merge neutralizer is already attached", + "an isolated three-way config is already attached", )); } - let driver_keys = snapshot.nonempty_driver_keys()?; - if driver_keys.is_empty() { - return Ok(None); - } - let config_dir = tempfile::tempdir()?; - let config_path = config_dir - .path() - .join("merge-driver-neutralization.gitconfig"); - self.ensure_owned_config_path(&config_path, "owned Git merge-driver neutralization")?; - std::fs::write(&config_path, [])?; - for key in driver_keys { - self.write_merge_override_value(&config_path, key)?; + let common_dir = self.sources.git.create_isolated_common_dir()?; + let config_path = common_dir.config_path(); + self.ensure_owned_config_path(&config_path, "owned isolated Git common config")?; + let entries = self + .sources + .read_effective(SANITIZED_CONFIG_PATTERN, "three-way allowlist")?; + for entry in entries.values() { + self.write_sanitized_config_value(&config_path, &entry.key, &entry.value)?; } - let config_path = config_path.to_str().ok_or_else(|| { - io::Error::new(io::ErrorKind::InvalidData, "non-UTF-8 merge guard path") - })?; + self.write_sanitized_config_value(&config_path, "core.bare", "false")?; + self.write_sanitized_config_value( + &config_path, + "merge.default", + snapshot.sanitized_default_driver(), + )?; #[cfg(test)] MERGE_OVERLAY_COUNT.with(|count| count.set(count.get() + 1)); - Ok(Some(SealedMergeConfigOverride { + Ok(SealedMergeConfigOverride { owner: Arc::clone(&self.identity), - include_arg: format!("include.path={config_path}"), - _config_dir: config_dir, - })) + common_dir, + }) } - fn attach_merge_override( - &mut self, - neutralizer: Option, - ) -> io::Result<()> { + fn attach_merge_override(&mut self, isolated: SealedMergeConfigOverride) -> io::Result<()> { if self.merge_policy_installed { return Err(io::Error::new( io::ErrorKind::PermissionDenied, - "a second merge neutralizer is not permitted", + "a second isolated three-way config is not permitted", )); } let [apply] = self.filters.as_slice() else { @@ -244,28 +212,30 @@ impl<'git> GuardedGitConfig<'git> { "merge policy requires an apply filter snapshot", )); } - if let Some(neutralizer) = &neutralizer { - neutralizer.ensure_owner(&self.identity)?; - } - self.merge = neutralizer; + isolated.ensure_owner(&self.identity)?; + self.merge = Some(isolated); self.merge_policy_installed = true; Ok(()) } - fn write_merge_override_value(&self, config_path: &Path, key: &str) -> io::Result<()> { + fn write_sanitized_config_value( + &self, + config_path: &Path, + key: &str, + value: &str, + ) -> io::Result<()> { let mut command = self .sources .git .command_for_cwd(&self.sources.canonical_root)?; command - .args(&self.sources.base_config_args) .args(["config", "--file"]) .arg(config_path) - .args(["--add", key, ""]); + .args(["--add", key, value]); let output = self.sources.git.output(command)?; if !output.status.success() { return Err(io::Error::other(format!( - "failed to write Git merge-driver neutralization for {key:?} (status {}): {}", + "failed to write isolated Git config value {key:?} (status {}): {}", output.status, String::from_utf8_lossy(&output.stderr).trim() ))); diff --git a/codex-rs/git-utils/src/guarded_config/merge_overlay_tests.rs b/codex-rs/git-utils/src/guarded_config/merge_overlay_tests.rs index 069e4ff8c5..39fa26e970 100644 --- a/codex-rs/git-utils/src/guarded_config/merge_overlay_tests.rs +++ b/codex-rs/git-utils/src/guarded_config/merge_overlay_tests.rs @@ -35,10 +35,9 @@ fn sealed_merge_override_rejects_another_operation_identity() { let snapshot = first.read_merge_config_snapshot().expect("merge snapshot"); let neutralizer = first .build_merge_override(&snapshot) - .expect("build merge override") - .expect("physical merge override"); + .expect("build isolated merge config"); let error = second - .attach_merge_override(Some(neutralizer)) + .attach_merge_override(neutralizer) .expect_err("cross-operation override must refuse"); assert_eq!(error.kind(), io::ErrorKind::PermissionDenied); } diff --git a/codex-rs/git-utils/src/guarded_config_tests.rs b/codex-rs/git-utils/src/guarded_config_tests.rs index d849e35627..dac7b0b7f2 100644 --- a/codex-rs/git-utils/src/guarded_config_tests.rs +++ b/codex-rs/git-utils/src/guarded_config_tests.rs @@ -183,7 +183,7 @@ fn zero_driver_merge_policy_still_occupies_the_single_install_slot() { .install_three_way_merge_policy() .expect("zero-driver merge policy"); assert!(config.merge_policy_installed); - assert!(config.merge.is_none()); + assert!(config.merge.is_some()); let error = config .install_three_way_merge_policy() @@ -192,10 +192,10 @@ fn zero_driver_merge_policy_still_occupies_the_single_install_slot() { } #[test] -fn command_order_is_apply_filter_then_merge_then_git_add_filter() { +fn isolated_merge_config_is_not_attached_to_preparatory_commands() { if std::env::var_os("CODEX_GIT_UTILS_GUARDED_CONFIG_ENV_CHILD").is_none() { run_isolated_config_test( - "guarded_config::tests::command_order_is_apply_filter_then_merge_then_git_add_filter", + "guarded_config::tests::isolated_merge_config_is_not_attached_to_preparatory_commands", ); return; } @@ -226,26 +226,24 @@ fn command_order_is_apply_filter_then_merge_then_git_add_filter() { .expect("apply filter overlay") .include_arg .clone(); - let merge_include = config - .merge_include_arg() - .expect("merge overlay") - .to_string(); + let merge_config = config + .merge_common_config_path() + .expect("isolated merge config"); let git_add_include = config.filters[1] .neutralizer() .expect("Git-add filter overlay") .include_arg .clone(); - let overlay_paths = [&apply_include, &merge_include, &git_add_include] + let overlay_paths = [&apply_include, &git_add_include] .map(|include| PathBuf::from(include.strip_prefix("include.path=").expect("include path"))); let rendered = config .render_command_for_log(&["ls-files".to_string()]) .expect("render command"); let apply_offset = rendered.find(&apply_include).expect("apply include"); - let merge_offset = rendered.find(&merge_include).expect("merge include"); let git_add_offset = rendered.find(&git_add_include).expect("Git-add include"); - assert!(apply_offset < merge_offset && merge_offset < git_add_offset); - assert_eq!(rendered.matches(&merge_include).count(), 1); + assert!(apply_offset < git_add_offset); + assert!(!rendered.contains(&merge_config.display().to_string())); let output = config .ls_files_command() @@ -254,9 +252,11 @@ fn command_order_is_apply_filter_then_merge_then_git_add_filter() { .expect("execute bound command"); assert!(output.status.success()); assert!(overlay_paths.iter().all(|path| path.is_file())); + assert!(merge_config.is_file()); drop(config); assert!(overlay_paths.iter().all(|path| !path.exists())); + assert!(!merge_config.exists()); } #[test] diff --git a/codex-rs/git-utils/src/merge_driver_race_tests.rs b/codex-rs/git-utils/src/merge_driver_race_tests.rs index 53d45f5b6a..07a8e5a697 100644 --- a/codex-rs/git-utils/src/merge_driver_race_tests.rs +++ b/codex-rs/git-utils/src/merge_driver_race_tests.rs @@ -2,6 +2,7 @@ use super::*; use crate::apply::ApplyGitRequest; use crate::apply::apply_git_patch; use pretty_assertions::assert_eq; +use std::ffi::OsStr; use std::fs::OpenOptions; use std::io::Write; use std::path::Path; @@ -10,8 +11,8 @@ use std::thread; use std::time::Duration; use std::time::Instant; -const OLD_DRIVER_COMMAND: &str = "git config codex.oldmergeran true && false"; -const NEW_DRIVER_COMMAND: &str = "git config codex.newmergeran true && false"; +const OLD_DRIVER_COMMAND: &str = "git config --file .git/config codex.oldmergeran true && false"; +const NEW_DRIVER_COMMAND: &str = "git config --file .git/config codex.newmergeran true && false"; const TRACE_MUTATION_MARKER: &str = "CODEX_MERGE_DRIVER_TEST_MUTATION"; fn run(cwd: &Path, args: &[&str]) -> (i32, String, String) { @@ -109,6 +110,10 @@ fn index_tree(root: &Path) -> String { } fn run_isolated_test(test_name: &str) { + run_isolated_test_with_env(test_name, &[]); +} + +fn run_isolated_test_with_env(test_name: &str, extra_env: &[(&str, &OsStr)]) { let environment = tempfile::tempdir().expect("isolated Git environment"); let global_config = environment.path().join("global.gitconfig"); let system_config = environment.path().join("system.gitconfig"); @@ -119,7 +124,7 @@ fn run_isolated_test(test_name: &str) { let mut command = std::process::Command::new(std::env::current_exe().expect("test binary")); isolate_git_command_environment(&mut command); - let output = command + command .arg(test_name) .arg("--exact") .arg("--nocapture") @@ -127,9 +132,11 @@ fn run_isolated_test(test_name: &str) { .env("GIT_CONFIG_GLOBAL", &global_config) .env("GIT_CONFIG_SYSTEM", &system_config) .env("GIT_TRACE2_EVENT", &trace) - .env("RUST_TEST_THREADS", "1") - .output() - .expect("run isolated test process"); + .env("RUST_TEST_THREADS", "1"); + for (name, value) in extra_env { + command.env(name, value); + } + let output = command.output().expect("run isolated test process"); assert!( output.status.success(), "isolated test {test_name} failed:\nstdout:\n{}\nstderr:\n{}", @@ -189,6 +196,26 @@ fn append_driver_config(root: &Path, driver: &str, command: &str) { .expect("append replacement merge driver"); } +fn append_default_and_driver_config(root: &Path, driver: &str, command: &str) { + let mut config = OpenOptions::new() + .append(true) + .open(root.join(".git/config")) + .expect("open local config"); + writeln!( + config, + "\n[merge]\n\tdefault = {driver}\n[merge \"{driver}\"]\n\tdriver = {command}" + ) + .expect("append replacement default merge driver"); +} + +#[derive(Clone, Copy, Debug)] +enum DriverNamespaceRace { + EmptyKnownKey, + SelectedMissingKey, + BrandNewAttributeAndKey, + BrandNewDefaultAndKey, +} + #[test] fn clean_reverse_checks_worktree_before_staging_and_skips_merge_driver() { let repo = init_repo(); @@ -378,6 +405,164 @@ fn post_probe_attribute_and_same_driver_command_change_stays_neutralized() { assert!(!configured_marker_exists(root, "codex.newmergeran")); } +#[test] +fn isolated_three_way_config_blocks_driver_namespace_introduction_races() { + if std::env::var_os("CODEX_GIT_UTILS_MERGE_RACE_CHILD").is_none() { + run_isolated_test( + "merge_driver::race_tests::isolated_three_way_config_blocks_driver_namespace_introduction_races", + ); + return; + } + + for race in [ + DriverNamespaceRace::EmptyKnownKey, + DriverNamespaceRace::SelectedMissingKey, + DriverNamespaceRace::BrandNewAttributeAndKey, + DriverNamespaceRace::BrandNewDefaultAndKey, + ] { + let repo = init_repo(); + let root = repo.path(); + let attributes = match race { + DriverNamespaceRace::EmptyKnownKey | DriverNamespaceRace::SelectedMissingKey => { + "target.txt merge=demo\n" + } + DriverNamespaceRace::BrandNewAttributeAndKey + | DriverNamespaceRace::BrandNewDefaultAndKey => "# initially safe\n", + }; + let patch = build_conflicting_patch(root, attributes); + if matches!(race, DriverNamespaceRace::EmptyKnownKey) { + configure_driver(root, "demo", ""); + } + let trace = trace_path(); + std::fs::write(&trace, "").expect("clear fixture trace"); + let watcher_trace = trace.clone(); + let watcher_root = root.to_path_buf(); + let watcher = thread::spawn(move || { + let observed = wait_for_merge_attribute_probe(&watcher_trace); + if observed { + match race { + DriverNamespaceRace::EmptyKnownKey + | DriverNamespaceRace::SelectedMissingKey => { + append_driver_config(&watcher_root, "demo", NEW_DRIVER_COMMAND); + } + DriverNamespaceRace::BrandNewAttributeAndKey => { + append_driver_config(&watcher_root, "brandnew", NEW_DRIVER_COMMAND); + std::fs::write( + watcher_root.join(".gitattributes"), + "target.txt merge=brandnew\n", + ) + .expect("select brand-new merge driver"); + } + DriverNamespaceRace::BrandNewDefaultAndKey => { + append_default_and_driver_config( + &watcher_root, + "brandnew", + NEW_DRIVER_COMMAND, + ); + } + } + record_trace_mutation(&watcher_trace); + } + observed + }); + + let result = apply_git_patch(&request(root, patch, /*revert*/ false)) + .expect("run isolated three-way apply"); + assert!(watcher.join().expect("namespace watcher"), "{race:?}"); + + assert_mutation_happened_between_probe_and_three_way(&trace); + assert_ne!(result.exit_code, 0, "{race:?}"); + assert!(result.cmd_for_log.contains("GIT_COMMON_DIR=")); + assert!(!configured_marker_exists(root, "codex.oldmergeran")); + assert!(!configured_marker_exists(root, "codex.newmergeran")); + } +} + +#[test] +fn isolated_three_way_config_scrubs_command_config_channels() { + if std::env::var_os("CODEX_GIT_UTILS_MERGE_RACE_CHILD").is_none() { + run_isolated_test_with_env( + "merge_driver::race_tests::isolated_three_way_config_scrubs_command_config_channels", + &[ + ("GIT_CONFIG_COUNT", OsStr::new("1")), + ("GIT_CONFIG_KEY_0", OsStr::new("merge.env-selected.driver")), + ( + "GIT_CONFIG_VALUE_0", + OsStr::new("git config --file .git/config codex.envmergeran true && false"), + ), + ( + "CODEX_APPLY_GIT_CFG", + OsStr::new( + "merge.base-selected.driver=git config --file .git/config codex.basemergeran true && false", + ), + ), + ], + ); + return; + } + + for driver in ["env-selected", "base-selected"] { + let repo = init_repo(); + let root = repo.path(); + let patch = build_conflicting_patch(root, "# initially safe\n"); + let trace = trace_path(); + std::fs::write(&trace, "").expect("clear fixture trace"); + let watcher_trace = trace.clone(); + let attributes = root.join(".gitattributes"); + let watcher = thread::spawn(move || { + let observed = wait_for_merge_attribute_probe(&watcher_trace); + if observed { + std::fs::write(attributes, format!("target.txt merge={driver}\n")) + .expect("select command-config merge driver"); + record_trace_mutation(&watcher_trace); + } + observed + }); + + let result = apply_git_patch(&request(root, patch, /*revert*/ false)) + .expect("run isolated three-way apply"); + assert!(watcher.join().expect("config-channel watcher"), "{driver}"); + + assert_mutation_happened_between_probe_and_three_way(&trace); + assert_ne!(result.exit_code, 0, "{driver}"); + assert!(!configured_marker_exists(root, "codex.envmergeran")); + assert!(!configured_marker_exists(root, "codex.basemergeran")); + } +} + +#[test] +fn isolated_three_way_config_updates_the_linked_worktree_index() { + let repo = init_repo(); + let root = repo.path(); + std::fs::write(root.join("seed.txt"), "seed\n").expect("write seed"); + run_success(root, &["add", "seed.txt"]); + run_success(root, &["commit", "-m", "seed"]); + + let linked_parent = tempfile::tempdir().expect("linked worktree parent"); + let linked_root = linked_parent.path().join("linked"); + run_success( + root, + &[ + "worktree", + "add", + "-b", + "isolated-three-way-linked", + linked_root.to_str().expect("UTF-8 linked path"), + ], + ); + let patch = build_conflicting_patch(&linked_root, "# no custom merge driver\n"); + + let result = apply_git_patch(&request(&linked_root, patch, /*revert*/ false)) + .expect("run linked-worktree three-way apply"); + + assert_ne!(result.exit_code, 0); + assert!(result.cmd_for_log.contains("GIT_COMMON_DIR=")); + let unmerged = run_success(&linked_root, &["ls-files", "--unmerged", "target.txt"]); + assert!(unmerged.contains(" 1\ttarget.txt"), "{unmerged}"); + assert!(unmerged.contains(" 2\ttarget.txt"), "{unmerged}"); + assert!(unmerged.contains(" 3\ttarget.txt"), "{unmerged}"); +} + #[test] fn same_patch_attribute_activation_does_not_run_merge_driver() { let repo = init_repo(); diff --git a/codex-rs/git-utils/src/merge_driver_tests.rs b/codex-rs/git-utils/src/merge_driver_tests.rs index ba87579397..1ec4aad060 100644 --- a/codex-rs/git-utils/src/merge_driver_tests.rs +++ b/codex-rs/git-utils/src/merge_driver_tests.rs @@ -23,7 +23,7 @@ fn apply_allows_unused_global_merge_driver() { let system_config = config_dir.path().join("system.gitconfig"); std::fs::write( &global_config, - "[merge \"unused\"]\n\tdriver = git config codex.mergeran true && false\n", + "[merge \"unused\"]\n\tdriver = git config --file .git/config codex.mergeran true && false\n", ) .expect("write global config"); std::fs::write(&system_config, "").expect("write system config"); @@ -71,7 +71,7 @@ fn apply_rejects_global_merge_driver_before_three_way() { let system_config = config_dir.path().join("system.gitconfig"); std::fs::write( &global_config, - "[merge \"codex-test\"]\n\tdriver = git config codex.mergeran true && false\n", + "[merge \"codex-test\"]\n\tdriver = git config --file .git/config codex.mergeran true && false\n", ) .expect("write global config"); std::fs::write(&system_config, "").expect("write system config"); @@ -173,7 +173,7 @@ fn three_way_apply_allows_unrelated_local_merge_driver() { "git", "config", "merge.unused.driver", - "git config codex.mergeran true && false", + "git config --file .git/config codex.mergeran true && false", ], ); assert_eq!( @@ -206,6 +206,139 @@ fn three_way_apply_allows_unrelated_local_merge_driver() { assert_ne!(marker_code, 0, "unused merge driver must not run"); } +#[test] +fn isolated_three_way_config_preserves_builtin_union_default() { + let repo = init_repo(); + let root = repo.path(); + std::fs::write(root.join("file.txt"), "top\nbase\nbottom\n").expect("write base"); + let (add_code, _, add_err) = run(root, &["git", "add", "file.txt"]); + assert_eq!(add_code, 0, "add base: {add_err}"); + let (commit_code, _, commit_err) = run(root, &["git", "commit", "-m", "base"]); + assert_eq!(commit_code, 0, "commit base: {commit_err}"); + let (base_code, base, base_err) = run(root, &["git", "rev-parse", "HEAD"]); + assert_eq!(base_code, 0, "resolve base: {base_err}"); + + std::fs::write(root.join("file.txt"), "top\ntheirs\nbottom\n").expect("write theirs"); + let (add_code, _, add_err) = run(root, &["git", "add", "file.txt"]); + assert_eq!(add_code, 0, "add theirs: {add_err}"); + let (commit_code, _, commit_err) = run(root, &["git", "commit", "-m", "theirs"]); + assert_eq!(commit_code, 0, "commit theirs: {commit_err}"); + let (diff_code, diff, diff_err) = run( + root, + &[ + "git", + "diff", + "--full-index", + base.trim(), + "HEAD", + "--", + "file.txt", + ], + ); + assert_eq!(diff_code, 0, "create full-index patch: {diff_err}"); + + let (checkout_code, _, checkout_err) = + run(root, &["git", "checkout", "-b", "ours", base.trim()]); + assert_eq!(checkout_code, 0, "checkout base: {checkout_err}"); + std::fs::write(root.join("file.txt"), "top\nours\nbottom\n").expect("write ours"); + let (commit_code, _, commit_err) = run(root, &["git", "commit", "-am", "ours"]); + assert_eq!(commit_code, 0, "commit ours: {commit_err}"); + let (config_code, _, config_err) = run(root, &["git", "config", "merge.default", "union"]); + assert_eq!(config_code, 0, "configure union default: {config_err}"); + + let result = apply_git_patch(&ApplyGitRequest { + cwd: root.to_path_buf(), + diff, + revert: false, + preflight: false, + }) + .expect("apply with builtin union default"); + + assert_eq!(result.exit_code, 0, "{}", result.stderr); + assert!(result.cmd_for_log.contains("GIT_COMMON_DIR=")); + let contents = std::fs::read_to_string(root.join("file.txt")) + .expect("read union merge") + .replace("\r\n", "\n"); + assert!(contents.contains("ours\n"), "{contents}"); + assert!(contents.contains("theirs\n"), "{contents}"); + assert!(!contents.contains("<<<<<<<"), "{contents}"); +} + +#[test] +fn isolated_three_way_config_preserves_core_whitespace_error_classification() { + let repo = init_repo(); + let root = repo.path(); + let diff = build_clean_three_way_addition(root, "patched \n"); + assert_eq!( + run(root, &["git", "config", "apply.whitespace", "error"]).0, + 0 + ); + assert_eq!( + run( + root, + &["git", "config", "core.whitespace", "-trailing-space"] + ) + .0, + 0 + ); + configure_unused_marker_driver(root); + + let result = apply_git_patch(&ApplyGitRequest { + cwd: root.to_path_buf(), + diff, + revert: false, + preflight: false, + }) + .expect("run forced three-way apply with whitespace error policy"); + + assert_eq!(result.exit_code, 0, "{result:?}"); + assert!(result.cmd_for_log.contains("GIT_COMMON_DIR=")); + let contents = std::fs::read_to_string(root.join("file.txt")) + .expect("read whitespace result") + .replace("\r\n", "\n"); + assert_eq!(contents, "current\nbase\npatched \n"); + let marker = run(root, &["git", "config", "--get", "codex.mergeran"]); + assert_ne!(marker.0, 0, "unused merge driver ran"); +} + +#[test] +fn isolated_three_way_config_preserves_core_whitespace_fix_output() { + for (classifier, expected_line) in [ + ("-indent-with-non-tab", " patched\n"), + ("indent-with-non-tab", "\tpatched\n"), + ] { + let repo = init_repo(); + let root = repo.path(); + let diff = build_clean_three_way_addition(root, " patched\n"); + assert_eq!( + run(root, &["git", "config", "apply.whitespace", "fix"]).0, + 0 + ); + assert_eq!( + run(root, &["git", "config", "core.whitespace", classifier]).0, + 0 + ); + configure_unused_marker_driver(root); + + let result = apply_git_patch(&ApplyGitRequest { + cwd: root.to_path_buf(), + diff, + revert: false, + preflight: false, + }) + .expect("run forced three-way apply with whitespace fix policy"); + + assert_eq!(result.exit_code, 0, "{classifier}: {result:?}"); + assert!(result.cmd_for_log.contains("GIT_COMMON_DIR=")); + let contents = std::fs::read_to_string(root.join("file.txt")) + .expect("read fixed whitespace result") + .replace("\r\n", "\n"); + assert_eq!(contents, format!("current\nbase\n{expected_line}")); + let marker = run(root, &["git", "config", "--get", "codex.mergeran"]); + assert_ne!(marker.0, 0, "unused merge driver ran for {classifier}"); + } +} + #[test] fn apply_allows_clean_patch_with_selected_merge_driver() { let repo = init_repo(); @@ -223,7 +356,7 @@ fn apply_allows_clean_patch_with_selected_merge_driver() { "git", "config", "merge.codex-test.driver", - "git config codex.mergeran true && false", + "git config --file .git/config codex.mergeran true && false", ], ); assert_eq!(config_code, 0, "configure merge driver: {config_err}"); @@ -275,7 +408,7 @@ fn reverse_apply_allows_clean_patch_with_selected_merge_driver() { "git", "config", "merge.codex-test.driver", - "git config codex.mergeran true && false", + "git config --file .git/config codex.mergeran true && false", ], ); assert_eq!(config_code, 0, "configure merge driver: {config_err}"); @@ -336,7 +469,7 @@ fn preflight_does_not_probe_or_run_selected_merge_driver() { "git", "config", "merge.codex-test.driver", - "git config codex.mergeran true && false", + "git config --file .git/config codex.mergeran true && false", ], ); assert_eq!(config_code, 0, "configure merge driver: {config_err}"); @@ -397,7 +530,7 @@ fn merge_override_rejects_process_temp_directory_inside_worktree() { "git", "config", "merge.unused.driver", - "git config codex.mergeran true && false", + "git config --file .git/config codex.mergeran true && false", ], ); assert_eq!(config_code, 0, "configure merge driver: {config_err}"); @@ -642,6 +775,32 @@ fn build_three_way_fixture(root: &Path) -> String { patch.1 } +fn build_clean_three_way_addition(root: &Path, added_line: &str) -> String { + std::fs::write(root.join("file.txt"), "base\n").expect("write base"); + assert_eq!(run(root, &["git", "add", "file.txt"]).0, 0); + assert_eq!(run(root, &["git", "commit", "-m", "base"]).0, 0); + std::fs::write(root.join("file.txt"), format!("base\n{added_line}")).expect("write patch side"); + let patch = run(root, &["git", "diff", "--full-index", "--", "file.txt"]); + assert_eq!(patch.0, 0, "create patch: {}", patch.2); + assert_eq!(run(root, &["git", "checkout", "--", "file.txt"]).0, 0); + std::fs::write(root.join("file.txt"), "current\nbase\n").expect("write current side"); + assert_eq!(run(root, &["git", "add", "file.txt"]).0, 0); + patch.1 +} + +fn configure_unused_marker_driver(root: &Path) { + let configured = run( + root, + &[ + "git", + "config", + "merge.unused.driver", + "git config --file .git/config codex.mergeran true && false", + ], + ); + assert_eq!(configured.0, 0, "configure unused driver: {}", configured.2); +} + fn run(cwd: &Path, args: &[&str]) -> (i32, String, String) { let mut command = std::process::Command::new(args[0]); isolate_git_command_environment(&mut command); diff --git a/codex-rs/git-utils/src/repository_authority/authority.rs b/codex-rs/git-utils/src/repository_authority/authority.rs index cbdd194ce9..e0e1bc24cb 100644 --- a/codex-rs/git-utils/src/repository_authority/authority.rs +++ b/codex-rs/git-utils/src/repository_authority/authority.rs @@ -190,13 +190,22 @@ impl RepositoryAuthority { }) } - #[cfg(all(test, unix))] pub(crate) fn active_git_dir(&self) -> Option<&Path> { self.active_metadata .as_ref() .map(|metadata| metadata.git_dir.as_path()) } + pub(crate) fn active_common_dir(&self) -> Option<&Path> { + self.active_metadata + .as_ref() + .map(|metadata| metadata.common_dir.as_path()) + } + + pub(crate) fn active_worktree_root(&self) -> &Path { + &self.active_worktree_root + } + #[cfg(test)] pub(crate) fn contains_root(&self, root: &Path) -> bool { self.roots diff --git a/codex-rs/git-utils/src/reverse_apply_tests.rs b/codex-rs/git-utils/src/reverse_apply_tests.rs index c700a6b803..ce88e73db0 100644 --- a/codex-rs/git-utils/src/reverse_apply_tests.rs +++ b/codex-rs/git-utils/src/reverse_apply_tests.rs @@ -158,6 +158,8 @@ fn assert_refused_without_mutation(root: &Path, patch: &str) { let before_leaves = [ leaf_snapshot(root, "old.txt"), leaf_snapshot(root, "new.txt"), + leaf_snapshot(root, "file.txt"), + leaf_snapshot(root, "other.txt"), ]; let error = apply_git_patch(&request(root, patch)).expect_err("unsafe reverse must fail"); assert_eq!(error.kind(), io::ErrorKind::PermissionDenied, "{error}"); @@ -165,7 +167,9 @@ fn assert_refused_without_mutation(root: &Path, patch: &str) { assert_eq!( [ leaf_snapshot(root, "old.txt"), - leaf_snapshot(root, "new.txt") + leaf_snapshot(root, "new.txt"), + leaf_snapshot(root, "file.txt"), + leaf_snapshot(root, "other.txt") ], before_leaves ); @@ -195,6 +199,65 @@ fn reverse_delete_and_rename_preserve_divergent_staged_blobs() { } } +#[test] +fn reverse_three_way_refuses_staged_content_change_matching_the_worktree() { + let repo = init_repo(); + let root = repo.path(); + std::fs::write(root.join("file.txt"), b"top\nbase\nbottom\n").expect("base file"); + run_success(root, &["add", "file.txt"]); + run_success(root, &["commit", "-qm", "base"]); + let base = String::from_utf8(run_success(root, &["rev-parse", "HEAD"])).expect("base OID"); + std::fs::write(root.join("file.txt"), b"top\ntheirs\nbottom\n").expect("theirs file"); + run_success(root, &["add", "file.txt"]); + run_success(root, &["commit", "-qm", "theirs"]); + let patch = String::from_utf8(run_success( + root, + &[ + "diff", + "--full-index", + base.trim(), + "HEAD", + "--", + "file.txt", + ], + )) + .expect("patch"); + std::fs::write(root.join("file.txt"), b"top\nuser-staged\nbottom\n").expect("user staged file"); + run_success(root, &["add", "file.txt"]); + + assert_refused_without_mutation(root, &patch); +} + +#[test] +fn reverse_three_way_refuses_staged_non_candidate_before_staging_another_path() { + let repo = init_repo(); + let root = repo.path(); + for path in ["file.txt", "other.txt"] { + std::fs::write(root.join(path), b"top\nbase\nbottom\n").expect("base file"); + } + run_success(root, &["add", "."]); + run_success(root, &["commit", "-qm", "base"]); + let base = String::from_utf8(run_success(root, &["rev-parse", "HEAD"])).expect("base OID"); + for path in ["file.txt", "other.txt"] { + std::fs::write(root.join(path), b"top\ntheirs\nbottom\n").expect("theirs file"); + } + run_success(root, &["add", "."]); + run_success(root, &["commit", "-qm", "theirs"]); + let patch = String::from_utf8(run_success( + root, + &["diff", "--full-index", base.trim(), "HEAD"], + )) + .expect("patch"); + + std::fs::write(root.join("file.txt"), b"top\nuser-staged\nbottom\n") + .expect("staged non-candidate"); + run_success(root, &["add", "file.txt"]); + std::fs::write(root.join("other.txt"), b"top\nunstaged\nbottom\n") + .expect("unstaged staging candidate"); + + assert_refused_without_mutation(root, &patch); +} + #[test] fn reverse_rename_accepts_each_partially_staged_form() { for staged_endpoints in ["old", "new", "both"] { diff --git a/codex-rs/git-utils/src/reverse_staging.rs b/codex-rs/git-utils/src/reverse_staging.rs index c82d07eb87..198a00c2be 100644 --- a/codex-rs/git-utils/src/reverse_staging.rs +++ b/codex-rs/git-utils/src/reverse_staging.rs @@ -10,9 +10,16 @@ use crate::patch_paths::confine_patch_paths_guarded; use crate::patch_paths::leaf_is_traversable_directory; use crate::patch_paths::leaf_may_run_git_content_filter; +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub(crate) enum ReverseApplyMode { + Direct, + ThreeWay, +} + pub(crate) fn stage_effective_paths_for_reverse( config: &mut GuardedGitConfig<'_>, paths: &[String], + mode: ReverseApplyMode, ) -> io::Result<()> { let confined = confine_patch_paths_guarded(config, paths)?; let mut worktree_paths = BTreeMap::new(); @@ -82,6 +89,15 @@ pub(crate) fn stage_effective_paths_for_reverse( "refusing to stage reverse patch path {path:?} because it has intent-to-add index state" ))); } + if mode == ReverseApplyMode::ThreeWay + && let Some(path) = exact_paths + .iter() + .find(|path| cached_visible_status.contains_key(*path)) + { + return Err(reverse_staging_error(format!( + "refusing a three-way reverse patch because it would replace existing staged data for {path:?}" + ))); + } let worktree_changed_paths = read_changed_paths(config, &exact_paths)?; let mut staging_candidates = Vec::new(); From 653b79082cd54ca4b6867712c7f5a7dfcf1bfd19 Mon Sep 17 00:00:00 2001 From: Chris Bookholt Date: Fri, 3 Jul 2026 16:31:36 -0700 Subject: [PATCH 03/20] test(git): expose three-way apply failures --- .../chatgpt/tests/suite/apply_command_e2e.rs | 7 ++-- .../git-utils/src/merge_driver_race_tests.rs | 32 +++++++++---------- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/codex-rs/chatgpt/tests/suite/apply_command_e2e.rs b/codex-rs/chatgpt/tests/suite/apply_command_e2e.rs index c2d570528c..ed342d9aad 100644 --- a/codex-rs/chatgpt/tests/suite/apply_command_e2e.rs +++ b/codex-rs/chatgpt/tests/suite/apply_command_e2e.rs @@ -172,10 +172,7 @@ console.log(fib(10)); let apply_result = apply_diff_from_task(task_response, Some(repo_path.to_path_buf())).await; - assert!( - apply_result.is_err(), - "Expected apply to fail due to merge conflicts" - ); + let apply_error = apply_result.expect_err("Expected apply to fail due to merge conflicts"); let contents = std::fs::read_to_string(&fibonacci_path).expect("Failed to read fibonacci.js"); @@ -183,6 +180,6 @@ console.log(fib(10)); contents.contains("<<<<<<< HEAD") || contents.contains("=======") || contents.contains(">>>>>>> "), - "fibonacci.js should contain merge conflict markers, got: {contents}", + "fibonacci.js should contain merge conflict markers, got: {contents}\napply error: {apply_error:#}", ); } diff --git a/codex-rs/git-utils/src/merge_driver_race_tests.rs b/codex-rs/git-utils/src/merge_driver_race_tests.rs index 07a8e5a697..c2ee73c145 100644 --- a/codex-rs/git-utils/src/merge_driver_race_tests.rs +++ b/codex-rs/git-utils/src/merge_driver_race_tests.rs @@ -210,10 +210,10 @@ fn append_default_and_driver_config(root: &Path, driver: &str, command: &str) { #[derive(Clone, Copy, Debug)] enum DriverNamespaceRace { - EmptyKnownKey, - SelectedMissingKey, - BrandNewAttributeAndKey, - BrandNewDefaultAndKey, + KnownEmpty, + SelectedMissing, + BrandNewAttribute, + BrandNewDefault, } #[test] @@ -415,22 +415,23 @@ fn isolated_three_way_config_blocks_driver_namespace_introduction_races() { } for race in [ - DriverNamespaceRace::EmptyKnownKey, - DriverNamespaceRace::SelectedMissingKey, - DriverNamespaceRace::BrandNewAttributeAndKey, - DriverNamespaceRace::BrandNewDefaultAndKey, + DriverNamespaceRace::KnownEmpty, + DriverNamespaceRace::SelectedMissing, + DriverNamespaceRace::BrandNewAttribute, + DriverNamespaceRace::BrandNewDefault, ] { let repo = init_repo(); let root = repo.path(); let attributes = match race { - DriverNamespaceRace::EmptyKnownKey | DriverNamespaceRace::SelectedMissingKey => { + DriverNamespaceRace::KnownEmpty | DriverNamespaceRace::SelectedMissing => { "target.txt merge=demo\n" } - DriverNamespaceRace::BrandNewAttributeAndKey - | DriverNamespaceRace::BrandNewDefaultAndKey => "# initially safe\n", + DriverNamespaceRace::BrandNewAttribute | DriverNamespaceRace::BrandNewDefault => { + "# initially safe\n" + } }; let patch = build_conflicting_patch(root, attributes); - if matches!(race, DriverNamespaceRace::EmptyKnownKey) { + if matches!(race, DriverNamespaceRace::KnownEmpty) { configure_driver(root, "demo", ""); } let trace = trace_path(); @@ -441,11 +442,10 @@ fn isolated_three_way_config_blocks_driver_namespace_introduction_races() { let observed = wait_for_merge_attribute_probe(&watcher_trace); if observed { match race { - DriverNamespaceRace::EmptyKnownKey - | DriverNamespaceRace::SelectedMissingKey => { + DriverNamespaceRace::KnownEmpty | DriverNamespaceRace::SelectedMissing => { append_driver_config(&watcher_root, "demo", NEW_DRIVER_COMMAND); } - DriverNamespaceRace::BrandNewAttributeAndKey => { + DriverNamespaceRace::BrandNewAttribute => { append_driver_config(&watcher_root, "brandnew", NEW_DRIVER_COMMAND); std::fs::write( watcher_root.join(".gitattributes"), @@ -453,7 +453,7 @@ fn isolated_three_way_config_blocks_driver_namespace_introduction_races() { ) .expect("select brand-new merge driver"); } - DriverNamespaceRace::BrandNewDefaultAndKey => { + DriverNamespaceRace::BrandNewDefault => { append_default_and_driver_config( &watcher_root, "brandnew", From 7da7bcc8cd3505997245b45ffcc437de3c8907cc Mon Sep 17 00:00:00 2001 From: Chris Bookholt Date: Fri, 3 Jul 2026 17:19:26 -0700 Subject: [PATCH 04/20] fix(git): pass native paths to isolated apply --- codex-rs/git-utils/src/git_command.rs | 281 +++++++++++++++++-- codex-rs/git-utils/src/git_command_tests.rs | 149 ++++++++++ codex-rs/git-utils/src/merge_driver_tests.rs | 53 ++++ 3 files changed, 467 insertions(+), 16 deletions(-) diff --git a/codex-rs/git-utils/src/git_command.rs b/codex-rs/git-utils/src/git_command.rs index f04bfd7d2d..c67368e3e8 100644 --- a/codex-rs/git-utils/src/git_command.rs +++ b/codex-rs/git-utils/src/git_command.rs @@ -5,6 +5,9 @@ use std::path::PathBuf; use std::process::Command; use std::process::Stdio; +#[cfg(windows)] +use same_file::Handle; + use crate::errors::GitReadError; use crate::git_config_environment::GitConfigEnvironmentSnapshot; #[cfg(test)] @@ -247,43 +250,83 @@ impl GitRunner { isolate_git_command_environment(&mut command.inner); scrub_repository_and_config_environment(&mut command.inner); - let git_dir = self.authority.active_git_dir().ok_or_else(|| { + let canonical_git_dir = self.authority.active_git_dir().ok_or_else(|| { io::Error::new( io::ErrorKind::PermissionDenied, "active Git directory is unavailable for isolated three-way apply", ) })?; - let common_dir = self.authority.active_common_dir().ok_or_else(|| { + let canonical_common_dir = self.authority.active_common_dir().ok_or_else(|| { io::Error::new( io::ErrorKind::PermissionDenied, "active Git common directory is unavailable for isolated three-way apply", ) })?; - let worktree = self.authority.active_worktree_root(); + let canonical_worktree = self.authority.active_worktree_root(); + + // Repository authority retains canonical paths for identity and route + // validation. Git for Windows does not recognize the verbatim + // `\\?\` spellings returned by `std::fs::canonicalize` in these + // environment variables, so simplify only the child-facing spelling + // after proving that it names the same existing directory. + let git_dir = git_child_directory(canonical_git_dir, "active Git directory")?; + let common_dir = git_child_directory(canonical_common_dir, "active Git common directory")?; + let worktree = git_child_directory(canonical_worktree, "active Git worktree")?; + #[cfg(windows)] + let isolated_root_base = std::fs::canonicalize(isolated.root.path())?; + #[cfg(not(windows))] + let isolated_root_base = isolated.root.path().to_path_buf(); + let isolated_root = + git_child_directory(&isolated_root_base, "owned isolated Git common directory")?; + let index_file = git_dir.join("index"); + let object_directory = common_dir.join("objects"); + let system_config = isolated_root.join("system.gitconfig"); + let global_config = isolated_root.join("global.gitconfig"); + let home = isolated_root.join("home"); + let xdg_config_home = isolated_root.join("xdg"); command .inner - .env("GIT_DIR", git_dir) - .env("GIT_COMMON_DIR", isolated.root.path()) - .env("GIT_WORK_TREE", worktree) - .env("GIT_INDEX_FILE", git_dir.join("index")) - .env("GIT_OBJECT_DIRECTORY", common_dir.join("objects")) + .env("GIT_DIR", git_dir.as_path()) + .env("GIT_COMMON_DIR", isolated_root.as_path()) + .env("GIT_WORK_TREE", worktree.as_path()) + .env("GIT_INDEX_FILE", index_file) + .env("GIT_OBJECT_DIRECTORY", object_directory) .env("GIT_CONFIG_NOSYSTEM", "1") - .env("GIT_CONFIG_SYSTEM", isolated.system_config_path()) - .env("GIT_CONFIG_GLOBAL", isolated.global_config_path()) + .env("GIT_CONFIG_SYSTEM", system_config) + .env("GIT_CONFIG_GLOBAL", global_config) .env("GIT_CONFIG_COUNT", "0") .env("GIT_ATTR_NOSYSTEM", "1") .env("GIT_NO_REPLACE_OBJECTS", "1") - .env("HOME", isolated.home_path()) - .env("XDG_CONFIG_HOME", isolated.xdg_config_home()); + .env("HOME", &home) + .env("XDG_CONFIG_HOME", &xdg_config_home); #[cfg(windows)] command .inner - .env("APPDATA", isolated.home_path()) - .env("PROGRAMDATA", isolated.home_path()) - .env("USERPROFILE", isolated.home_path()); + .env("APPDATA", &home) + .env("PROGRAMDATA", &home) + .env("USERPROFILE", &home); command.inner.envs(crate::local_only_git_env()); harden_git_launch_environment(&mut command.inner, &self.safe_path); - command.inner.output() + self.output_after_isolated_child_validation( + &mut command.inner, + isolated, + [&git_dir, &common_dir, &worktree, &isolated_root], + ) + } + + fn output_after_isolated_child_validation( + &self, + command: &mut Command, + isolated: &IsolatedGitCommonDir, + child_directories: [&GitChildDirectory; 4], + ) -> io::Result { + self.revalidate_active_repository_metadata()?; + isolated.validate()?; + let _child_directory_validations = child_directories + .into_iter() + .map(GitChildDirectory::revalidate) + .collect::>>()?; + command.output() } fn revalidate_active_repository_metadata(&self) -> io::Result<()> { @@ -312,6 +355,212 @@ impl GitRunner { } } +struct GitChildDirectory { + child_path: PathBuf, + #[cfg(windows)] + canonical_path: PathBuf, + #[cfg(windows)] + canonical_identity: Handle, + #[cfg(windows)] + child_identity: Handle, + #[cfg(windows)] + description: &'static str, +} + +struct GitChildDirectoryValidation { + #[cfg(windows)] + _canonical_identity: Handle, + #[cfg(windows)] + _child_identity: Handle, +} + +impl GitChildDirectory { + fn as_path(&self) -> &Path { + &self.child_path + } + + fn join(&self, path: impl AsRef) -> PathBuf { + self.child_path.join(path) + } + + #[cfg(windows)] + fn new( + canonical_path: PathBuf, + child_path: PathBuf, + description: &'static str, + ) -> io::Result { + let canonical_metadata = std::fs::metadata(&canonical_path)?; + let child_metadata = std::fs::metadata(&child_path)?; + if !canonical_metadata.is_dir() || !child_metadata.is_dir() { + return Err(io::Error::new( + io::ErrorKind::PermissionDenied, + format!("{description} is not an existing directory"), + )); + } + let canonical_identity = Handle::from_path(&canonical_path)?; + let child_identity = Handle::from_path(&child_path)?; + if canonical_identity != child_identity { + return Err(io::Error::new( + io::ErrorKind::PermissionDenied, + format!("Git-compatible spelling changed the identity of {description}"), + )); + } + Ok(Self { + child_path, + canonical_path, + canonical_identity, + child_identity, + description, + }) + } + + #[cfg(windows)] + fn revalidate(&self) -> io::Result { + let canonical_metadata = std::fs::metadata(&self.canonical_path)?; + let child_metadata = std::fs::metadata(&self.child_path)?; + if !canonical_metadata.is_dir() || !child_metadata.is_dir() { + return Err(io::Error::new( + io::ErrorKind::PermissionDenied, + format!("{} is no longer an existing directory", self.description), + )); + } + let canonical_identity = Handle::from_path(&self.canonical_path)?; + let child_identity = Handle::from_path(&self.child_path)?; + if canonical_identity != self.canonical_identity + || child_identity != self.child_identity + || canonical_identity != child_identity + { + return Err(io::Error::new( + io::ErrorKind::PermissionDenied, + format!( + "Git-compatible spelling no longer identifies the original {}", + self.description + ), + )); + } + Ok(GitChildDirectoryValidation { + _canonical_identity: canonical_identity, + _child_identity: child_identity, + }) + } + + #[cfg(not(windows))] + fn revalidate(&self) -> io::Result { + Ok(GitChildDirectoryValidation {}) + } +} + +#[cfg(windows)] +fn git_child_directory(path: &Path, description: &'static str) -> io::Result { + let child_path = git_compatible_windows_path(path, description)?; + GitChildDirectory::new(path.to_path_buf(), child_path, description) +} + +#[cfg(not(windows))] +fn git_child_directory(path: &Path, _description: &'static str) -> io::Result { + Ok(GitChildDirectory { + child_path: path.to_path_buf(), + }) +} + +#[cfg(windows)] +fn git_compatible_windows_path(path: &Path, description: &'static str) -> io::Result { + use std::ffi::OsString; + use std::os::windows::ffi::OsStrExt; + use std::os::windows::ffi::OsStringExt; + + let units = path.as_os_str().encode_wide().collect::>(); + match classify_windows_git_path_units(&units) { + WindowsGitPathUnits::Unchanged => Ok(path.to_path_buf()), + WindowsGitPathUnits::Converted(simplified) => { + Ok(PathBuf::from(OsString::from_wide(&simplified))) + } + WindowsGitPathUnits::Rejected => Err(io::Error::new( + io::ErrorKind::PermissionDenied, + format!("refusing unsupported Windows namespace for {description}"), + )), + } +} + +#[cfg(any(windows, test))] +#[derive(Debug, Eq, PartialEq)] +enum WindowsGitPathUnits { + Unchanged, + Converted(Vec), + Rejected, +} + +#[cfg(any(windows, test))] +fn classify_windows_git_path_units(path: &[u16]) -> WindowsGitPathUnits { + const BACKSLASH: u16 = b'\\' as u16; + const VERBATIM_PREFIX: [u16; 4] = [BACKSLASH, BACKSLASH, b'?' as u16, BACKSLASH]; + const DEVICE_PREFIX: [u16; 4] = [BACKSLASH, BACKSLASH, b'.' as u16, BACKSLASH]; + const NT_PREFIX: [u16; 4] = [BACKSLASH, b'?' as u16, b'?' as u16, BACKSLASH]; + const FORWARD_VERBATIM_PREFIX: [u16; 4] = [b'/' as u16, b'/' as u16, b'?' as u16, b'/' as u16]; + const FORWARD_DEVICE_PREFIX: [u16; 4] = [b'/' as u16, b'/' as u16, b'.' as u16, b'/' as u16]; + + if path.contains(&0) { + return WindowsGitPathUnits::Rejected; + } + if path.starts_with(&DEVICE_PREFIX) + || path.starts_with(&NT_PREFIX) + || path.starts_with(&FORWARD_VERBATIM_PREFIX) + || path.starts_with(&FORWARD_DEVICE_PREFIX) + { + return WindowsGitPathUnits::Rejected; + } + let Some(path) = path.strip_prefix(&VERBATIM_PREFIX) else { + return WindowsGitPathUnits::Unchanged; + }; + + if path.len() >= 3 + && ascii_u16_is_alphabetic(path[0]) + && path[1] == b':' as u16 + && path[2] == BACKSLASH + { + return WindowsGitPathUnits::Converted(path.to_vec()); + } + + if path.len() < 4 + || !ascii_u16_eq_ignore_case(path[0], b'U') + || !ascii_u16_eq_ignore_case(path[1], b'N') + || !ascii_u16_eq_ignore_case(path[2], b'C') + || path[3] != BACKSLASH + { + return WindowsGitPathUnits::Rejected; + } + let unc_path = &path[4..]; + let Some(server_end) = unc_path.iter().position(|unit| *unit == BACKSLASH) else { + return WindowsGitPathUnits::Rejected; + }; + if server_end == 0 { + return WindowsGitPathUnits::Rejected; + } + let share = &unc_path[server_end + 1..]; + let share_end = share + .iter() + .position(|unit| *unit == BACKSLASH) + .unwrap_or(share.len()); + if share_end == 0 { + return WindowsGitPathUnits::Rejected; + } + + let mut simplified = Vec::with_capacity(unc_path.len() + 2); + simplified.extend([BACKSLASH, BACKSLASH]); + simplified.extend_from_slice(unc_path); + WindowsGitPathUnits::Converted(simplified) +} + +#[cfg(any(windows, test))] +fn ascii_u16_is_alphabetic(unit: u16) -> bool { + (b'A' as u16..=b'Z' as u16).contains(&unit) || (b'a' as u16..=b'z' as u16).contains(&unit) +} + +#[cfg(any(windows, test))] +fn ascii_u16_eq_ignore_case(unit: u16, ascii: u8) -> bool { + unit == ascii.to_ascii_lowercase() as u16 || unit == ascii.to_ascii_uppercase() as u16 +} + fn scrub_repository_and_config_environment(command: &mut Command) { let mut names = std::env::vars_os() .map(|(name, _)| name) diff --git a/codex-rs/git-utils/src/git_command_tests.rs b/codex-rs/git-utils/src/git_command_tests.rs index 3d191735b3..9c16771599 100644 --- a/codex-rs/git-utils/src/git_command_tests.rs +++ b/codex-rs/git-utils/src/git_command_tests.rs @@ -1356,6 +1356,155 @@ fn command_for_cwd_executes_from_a_canonical_windows_path() { ); } +#[test] +fn windows_verbatim_git_path_conversion_is_narrow_and_non_lossy() { + fn units(value: &str) -> Vec { + value.encode_utf16().collect() + } + + assert_eq!( + classify_windows_git_path_units(&units(r"\\?\C:\Users\alice\repo")), + WindowsGitPathUnits::Converted(units(r"C:\Users\alice\repo")) + ); + assert_eq!( + classify_windows_git_path_units(&units(r"\\?\UNC\server\share\repo")), + WindowsGitPathUnits::Converted(units(r"\\server\share\repo")) + ); + assert_eq!( + classify_windows_git_path_units(&units(r"\\?\unc\server\share")), + WindowsGitPathUnits::Converted(units(r"\\server\share")) + ); + + for unchanged in [r"C:\Users\alice\repo", r"\\server\share\repo"] { + assert_eq!( + classify_windows_git_path_units(&units(unchanged)), + WindowsGitPathUnits::Unchanged, + "unexpected conversion for {unchanged:?}" + ); + } + + for rejected in [ + r"\\.\C:\device-path", + r"\??\C:\nt-path", + r"//?/C:/forward-verbatim", + r"//./C:/forward-device", + r"\\?\C:relative", + r"\\?\C:/forward-tail", + r"\\?\GLOBALROOT\Device\HarddiskVolume1", + r"\\?\Volume{12345678-1234-1234-1234-123456789abc}\repo", + r"\\?\UNC\\share", + r"\\?\UNC\server\", + ] { + assert_eq!( + classify_windows_git_path_units(&units(rejected)), + WindowsGitPathUnits::Rejected, + "unexpected acceptance for {rejected:?}" + ); + } + + let mut unpaired_surrogate = units(r"\\?\C:\repo\"); + unpaired_surrogate.push(0xD800); + let mut expected = units(r"C:\repo\"); + expected.push(0xD800); + assert_eq!( + classify_windows_git_path_units(&unpaired_surrogate), + WindowsGitPathUnits::Converted(expected) + ); + + assert_eq!( + classify_windows_git_path_units(&units(r"\\?\C:\repo\😀")), + WindowsGitPathUnits::Converted(units(r"C:\repo\😀")) + ); + let mut unpaired_low_surrogate = units(r"\\?\C:\repo\"); + unpaired_low_surrogate.push(0xDC00); + let mut expected = units(r"C:\repo\"); + expected.push(0xDC00); + assert_eq!( + classify_windows_git_path_units(&unpaired_low_surrogate), + WindowsGitPathUnits::Converted(expected) + ); + + let mut nul = units(r"\\?\C:\repo\"); + nul.push(0); + assert_eq!( + classify_windows_git_path_units(&nul), + WindowsGitPathUnits::Rejected + ); +} + +#[cfg(windows)] +#[test] +fn git_child_directory_simplifies_canonical_path_without_changing_identity() { + use std::os::windows::ffi::OsStrExt; + + let directory = tempfile::tempdir().expect("fixture"); + let canonical = std::fs::canonicalize(directory.path()).expect("canonical fixture"); + let child = git_child_directory(&canonical, "test directory").expect("child spelling"); + assert_eq!( + same_file::Handle::from_path(&canonical).expect("canonical identity"), + same_file::Handle::from_path(child.as_path()).expect("child identity") + ); + child.revalidate().expect("revalidate child spelling"); + let child_units = child + .as_path() + .as_os_str() + .encode_wide() + .collect::>(); + assert!(!child_units.starts_with(&[b'\\' as u16, b'\\' as u16, b'?' as u16, b'\\' as u16,])); +} + +#[cfg(windows)] +#[test] +fn isolated_child_final_validation_rejects_retargeted_spelling_before_spawn() { + let fixture = tempfile::tempdir().expect("fixture"); + let repo = fixture.path().join("repo"); + let original = fixture.path().join("original"); + let attacker = fixture.path().join("attacker"); + let child_spelling = fixture.path().join("child-spelling"); + for directory in [&repo, &original, &attacker] { + std::fs::create_dir_all(directory).expect("create directory"); + } + run_git(&repo, &["init", "-q"]); + create_junction(&child_spelling, &original); + + let canonical = std::fs::canonicalize(&original).expect("canonical original"); + let child = GitChildDirectory::new( + canonical, + child_spelling.clone(), + "test Git child directory", + ) + .expect("initial child identity"); + let runner = GitRunner::for_cwd(&repo).expect("runner"); + let isolated = runner + .create_isolated_common_dir() + .expect("isolated common dir"); + let marker = fixture.path().join("child-ran.gitconfig"); + let mut command = runner.command_for_cwd(&repo).expect("marker command"); + command + .args(["config", "--file"]) + .arg(&marker) + .args(["codex.child-ran", "true"]); + + std::fs::remove_dir(&child_spelling).expect("remove original junction"); + create_junction(&child_spelling, &attacker); + + let error = runner + .output_after_isolated_child_validation( + &mut command.inner, + &isolated, + [&child, &child, &child, &child], + ) + .expect_err("retargeted child spelling must fail closed"); + assert_eq!(error.kind(), io::ErrorKind::PermissionDenied, "{error}"); + assert!( + error + .to_string() + .contains("no longer identifies the original"), + "{error}" + ); + assert!(!marker.exists(), "child command ran despite stale identity"); +} + #[test] fn gitdir_terminal_worktree_root_is_never_promoted_to_metadata() { let fixture = tempfile::tempdir().expect("fixture"); diff --git a/codex-rs/git-utils/src/merge_driver_tests.rs b/codex-rs/git-utils/src/merge_driver_tests.rs index 1ec4aad060..5ccdb48667 100644 --- a/codex-rs/git-utils/src/merge_driver_tests.rs +++ b/codex-rs/git-utils/src/merge_driver_tests.rs @@ -264,6 +264,59 @@ fn isolated_three_way_config_preserves_builtin_union_default() { assert!(!contents.contains("<<<<<<<"), "{contents}"); } +#[test] +fn isolated_three_way_add_add_conflict_preserves_markers_and_index_stages() { + let repo = init_repo(); + let root = repo.path(); + let target = root.join("conflict.txt"); + std::fs::write(&target, "existing\n").expect("write existing file"); + assert_eq!(run(root, &["git", "add", "conflict.txt"]).0, 0); + assert_eq!(run(root, &["git", "commit", "-m", "existing"]).0, 0); + + let incoming = root.join("incoming.txt"); + std::fs::write(&incoming, "incoming\n").expect("write incoming file"); + let (hash_code, object_id, hash_error) = + run(root, &["git", "hash-object", "-w", "incoming.txt"]); + assert_eq!(hash_code, 0, "hash incoming blob: {hash_error}"); + std::fs::remove_file(incoming).expect("remove incoming fixture"); + let object_id = object_id.trim(); + let null_object_id = "0".repeat(object_id.len()); + let patch = format!( + "diff --git a/conflict.txt b/conflict.txt\nnew file mode 100644\nindex {null_object_id}..{object_id}\n--- /dev/null\n+++ b/conflict.txt\n@@ -0,0 +1 @@\n+incoming\n" + ); + + let result = apply_git_patch(&ApplyGitRequest { + cwd: root.to_path_buf(), + diff: patch, + revert: false, + preflight: false, + }) + .expect("run isolated add/add three-way apply"); + + assert_ne!(result.exit_code, 0, "add/add apply unexpectedly succeeded"); + assert!( + result.cmd_for_log.contains("GIT_COMMON_DIR=") + && result.cmd_for_log.contains("--3way"), + "unexpected final command: {}", + result.cmd_for_log + ); + let contents = std::fs::read_to_string(&target).expect("read conflicted file"); + assert!( + contents.contains("<<<<<<<") + && contents.contains("=======") + && contents.contains(">>>>>>>"), + "three-way add/add apply did not leave conflict markers:\nresult: {result:#?}\ncontents:\n{contents}" + ); + let (stage_code, stages, stage_error) = + run(root, &["git", "ls-files", "--stage", "--", "conflict.txt"]); + assert_eq!(stage_code, 0, "read conflict stages: {stage_error}"); + let actual_stages = stages + .lines() + .filter_map(|line| line.split_ascii_whitespace().nth(2)) + .collect::>(); + assert_eq!(actual_stages, vec!["2", "3"], "{result:#?}"); +} + #[test] fn isolated_three_way_config_preserves_core_whitespace_error_classification() { let repo = init_repo(); From 92d1967d21c4d321d7b058a980b715785a9f9af2 Mon Sep 17 00:00:00 2001 From: Chris Bookholt Date: Fri, 3 Jul 2026 17:31:39 -0700 Subject: [PATCH 05/20] fix(git): bind staging to repository authority --- codex-rs/git-utils/src/exact_index_policy.rs | 23 +- codex-rs/git-utils/src/exact_staging.rs | 157 ++++-- codex-rs/git-utils/src/patch_paths.rs | 82 ++- codex-rs/git-utils/src/patch_paths_tests.rs | 487 ++++++++++++++++++ .../src/repository_authority/authority.rs | 49 +- 5 files changed, 739 insertions(+), 59 deletions(-) diff --git a/codex-rs/git-utils/src/exact_index_policy.rs b/codex-rs/git-utils/src/exact_index_policy.rs index e7c2b6947f..ae9f4abce7 100644 --- a/codex-rs/git-utils/src/exact_index_policy.rs +++ b/codex-rs/git-utils/src/exact_index_policy.rs @@ -14,6 +14,12 @@ pub(crate) enum ExactIndexPolicy { paths: Vec, content_filter_paths: Vec, }, + Flagged { + paths: Vec, + content_filter_paths: Vec, + skip_worktree: BTreeSet, + assume_unchanged: BTreeSet, + }, Refuse { stderr: String, }, @@ -154,18 +160,11 @@ pub(crate) fn resolve_exact_index_policy( }); } - let mut reasons = Vec::new(); - if !skipped.is_empty() { - reasons.push(format!("skip-worktree path(s): {}", quote_paths(&skipped))); - } - if !assumed_unchanged.is_empty() { - reasons.push(format!( - "assume-unchanged path(s): {}", - quote_paths(&assumed_unchanged) - )); - } - Ok(ExactIndexPolicy::Refuse { - stderr: format!("refusing to stage {}", reasons.join("; ")), + Ok(ExactIndexPolicy::Flagged { + paths, + content_filter_paths, + skip_worktree: skipped, + assume_unchanged: assumed_unchanged, }) } diff --git a/codex-rs/git-utils/src/exact_staging.rs b/codex-rs/git-utils/src/exact_staging.rs index 8ff94ae796..cd97a5b6b6 100644 --- a/codex-rs/git-utils/src/exact_staging.rs +++ b/codex-rs/git-utils/src/exact_staging.rs @@ -23,6 +23,11 @@ enum StagingProvenance { Standalone, } +enum SparseCheckoutPolicy { + Verified { excluded: BTreeSet }, + Refuse { result: StagePathsResult }, +} + /// Stage only the literal paths supplied by a caller that has already applied /// the operation-specific containment and filesystem policy. /// @@ -97,12 +102,23 @@ fn update_index_exact_paths_common( }); } - let (paths, content_filter_paths) = + let (paths, content_filter_paths, skip_worktree, assume_unchanged) = match resolve_exact_index_policy(config, paths, content_filter_paths)? { ExactIndexPolicy::Proceed { paths, content_filter_paths, - } => (paths, content_filter_paths), + } => ( + paths, + content_filter_paths, + BTreeSet::new(), + BTreeSet::new(), + ), + ExactIndexPolicy::Flagged { + paths, + content_filter_paths, + skip_worktree, + assume_unchanged, + } => (paths, content_filter_paths, skip_worktree, assume_unchanged), ExactIndexPolicy::Refuse { stderr } => { return Ok(StagePathsResult { exit_code: 1, @@ -114,31 +130,103 @@ fn update_index_exact_paths_common( config.ensure_apply_filter_path_subset(&content_filter_paths)?; } + if (matches!(provenance, StagingProvenance::Standalone) && !skip_worktree.is_empty()) + || !assume_unchanged.is_empty() + { + let mut reasons = Vec::new(); + if !skip_worktree.is_empty() { + reasons.push(format!( + "skip-worktree path(s): {}", + quote_paths(&skip_worktree) + )); + } + if !assume_unchanged.is_empty() { + reasons.push(format!( + "assume-unchanged path(s): {}", + quote_paths(&assume_unchanged) + )); + } + return Ok(StagePathsResult { + exit_code: 1, + stderr: format!("refusing to stage {}", reasons.join("; ")), + }); + } + let ignored = ignored_untracked_paths(config, &paths)?; - if !ignored.is_empty() { + let sparse_excluded = match sparse_checkout_policy(config, &paths)? { + SparseCheckoutPolicy::Verified { excluded } => excluded, + SparseCheckoutPolicy::Refuse { result } => return Ok(result), + }; + let non_sparse_skip_worktree = skip_worktree + .difference(&sparse_excluded) + .cloned() + .collect::>(); + if !non_sparse_skip_worktree.is_empty() { return Ok(StagePathsResult { exit_code: 1, stderr: format!( - "refusing to stage ignored untracked path(s): {}", - quote_paths(&ignored) + "refusing to stage skip-worktree path(s): {}", + quote_paths(&non_sparse_skip_worktree) ), }); } - if let Some(result) = sparse_checkout_policy(config, &paths)? { - return Ok(result); + let mut exclusions = ignored.clone(); + exclusions.extend(sparse_excluded.iter().cloned()); + let mut exclusion_reasons = Vec::new(); + if !ignored.is_empty() { + exclusion_reasons.push(format!( + "refusing to stage ignored untracked path(s): {}", + quote_paths(&ignored) + )); } + if !sparse_excluded.is_empty() { + exclusion_reasons.push(format!( + "refusing to stage path(s) outside the sparse-checkout definition: {}", + quote_paths(&sparse_excluded) + )); + } + let exclusion_stderr = exclusion_reasons.join("; "); + if !exclusions.is_empty() && matches!(provenance, StagingProvenance::Standalone) { + return Ok(StagePathsResult { + exit_code: 1, + stderr: exclusion_stderr, + }); + } + // Reverse pre-staging is intentionally best effort for ignored and sparse + // paths. Treat those expected omissions as successful after every fallible + // probe has completed, so a composed caller cannot report an error after + // the eligible subset has already changed the index. + let paths = paths + .into_iter() + .filter(|path| !exclusions.contains(path)) + .collect::>(); + let content_filter_paths = content_filter_paths + .into_iter() + .filter(|path| !exclusions.contains(path)) + .collect::>(); config.authorize_git_add_filter_paths(&content_filter_paths)?; + if paths.is_empty() { + return Ok(StagePathsResult { + exit_code: 0, + stderr: exclusion_stderr, + }); + } let mut command = config.update_index_literal_pathspecs_command()?; command .disable_optional_locks() .args(["--add", "--remove", "--"]) .args(&paths); let output = command.output()?; - Ok(StagePathsResult { - exit_code: output.status.code().unwrap_or(-1), - stderr: String::from_utf8_lossy(&output.stderr).into_owned(), - }) + let exit_code = output.status.code().unwrap_or(-1); + let mut stderr = String::from_utf8_lossy(&output.stderr).into_owned(); + if !exclusions.is_empty() { + if !stderr.is_empty() && !stderr.ends_with('\n') { + stderr.push('\n'); + } + stderr.push_str(&exclusion_stderr); + } + Ok(StagePathsResult { exit_code, stderr }) } fn ignored_untracked_paths( @@ -191,9 +279,11 @@ fn ignored_untracked_paths( fn sparse_checkout_policy( config: &GuardedGitConfig<'_>, paths: &[String], -) -> io::Result> { +) -> io::Result { if !config.read_bool("core.sparseCheckout")?.unwrap_or(false) { - return Ok(None); + return Ok(SparseCheckoutPolicy::Verified { + excluded: BTreeSet::new(), + }); } // Do not let an older Git fall back to a PATH-resolved @@ -205,11 +295,14 @@ fn sparse_checkout_policy( && std::str::from_utf8(&builtins.stdout) .is_ok_and(|output| output.lines().any(|command| command == "sparse-checkout")); if !sparse_checkout_is_builtin { - return Ok(Some(StagePathsResult { - exit_code: builtins.status.code().unwrap_or(-1).max(1), - stderr: "unable to verify sparse-checkout staging policy with a built-in Git command" - .to_string(), - })); + return Ok(SparseCheckoutPolicy::Refuse { + result: StagePathsResult { + exit_code: builtins.status.code().unwrap_or(-1).max(1), + stderr: + "unable to verify sparse-checkout staging policy with a built-in Git command" + .to_string(), + }, + }); } let input = nul_path_input(paths)?; @@ -220,13 +313,15 @@ fn sparse_checkout_policy( .stdin(Stdio::from(input)); let output = command.output()?; if !output.status.success() { - return Ok(Some(StagePathsResult { - exit_code: output.status.code().unwrap_or(-1), - stderr: format!( - "unable to verify sparse-checkout staging policy: {}", - String::from_utf8_lossy(&output.stderr).trim() - ), - })); + return Ok(SparseCheckoutPolicy::Refuse { + result: StagePathsResult { + exit_code: output.status.code().unwrap_or(-1), + stderr: format!( + "unable to verify sparse-checkout staging policy: {}", + String::from_utf8_lossy(&output.stderr).trim() + ), + }, + }); } let included = parse_reported_paths(&output.stdout, paths, "sparse-checkout")?; let excluded = paths @@ -234,17 +329,7 @@ fn sparse_checkout_policy( .filter(|path| !included.contains(path.as_str())) .cloned() .collect::>(); - if excluded.is_empty() { - Ok(None) - } else { - Ok(Some(StagePathsResult { - exit_code: 1, - stderr: format!( - "refusing to stage path(s) outside the sparse-checkout definition: {}", - quote_paths(&excluded) - ), - })) - } + Ok(SparseCheckoutPolicy::Verified { excluded }) } fn nul_path_input(paths: &[String]) -> io::Result { diff --git a/codex-rs/git-utils/src/patch_paths.rs b/codex-rs/git-utils/src/patch_paths.rs index 4c719ffde6..468fa6f1b5 100644 --- a/codex-rs/git-utils/src/patch_paths.rs +++ b/codex-rs/git-utils/src/patch_paths.rs @@ -1,6 +1,7 @@ //! Effective patch-path discovery and safe staging guards. use std::io; +use std::path::Component; use std::path::Path; use std::path::PathBuf; @@ -11,6 +12,36 @@ use crate::git_command::GitRunner; use crate::git_config::path_is_within; use crate::guarded_config::GuardedGitConfig; +#[cfg(test)] +enum StagePathsHookPoint { + BindRunner = 0, + ResolvePhysicalCwd = 1, + PreSpawn = 2, +} + +#[cfg(test)] +std::thread_local! { + #[allow(clippy::type_complexity)] + static STAGE_PATHS_HOOKS: std::cell::RefCell<[Option>; 3]> = + std::cell::RefCell::new([None, None, None]); +} + +#[cfg(test)] +fn set_stage_paths_hook(point: StagePathsHookPoint, hook: impl FnOnce() + 'static) { + STAGE_PATHS_HOOKS.with(|slots| { + let prior = slots.borrow_mut()[point as usize].replace(Box::new(hook)); + assert!(prior.is_none()); + }); +} + +#[cfg(test)] +fn run_stage_paths_hook(point: StagePathsHookPoint) { + let hook = STAGE_PATHS_HOOKS.with(|slots| slots.borrow_mut()[point as usize].take()); + if let Some(hook) = hook { + hook(); + } +} + /// Extract effective patch paths through a bound operation configuration. pub(crate) fn extract_effective_paths_from_patch_guarded( config: &GuardedGitConfig<'_>, @@ -52,6 +83,8 @@ fn git_apply_numstat_paths_guarded( revert: bool, ) -> io::Result> { let mut command = config.apply_command()?; + #[cfg(test)] + run_stage_paths_hook(StagePathsHookPoint::PreSpawn); command.args(["--numstat", "-z"]); if revert { command.arg("-R"); @@ -240,9 +273,52 @@ fn invalid_windows_patch_component(component: &str) -> bool { /// Stage only the files that actually exist on disk for the given diff. pub fn stage_paths(git_root: &Path, diff: &str) -> io::Result<()> { - let git = GitRunner::for_cwd_io(git_root)?; - git.ensure_repository_root_route(git_root)?; - let canonical_root = std::fs::canonicalize(git_root)?; + let immutable_traversal = !git_root.is_absolute() + && git_root.components().next().is_some() + && git_root + .components() + .all(|component| matches!(component, Component::CurDir | Component::ParentDir)); + let (guarded_root, bound_git) = if git_root.is_absolute() { + (git_root.to_path_buf(), None) + } else if immutable_traversal { + let git = GitRunner::for_cwd_io(Path::new("."))?; + #[cfg(test)] + run_stage_paths_hook(StagePathsHookPoint::BindRunner); + let resolve_immutable_traversal = || -> io::Result { + let mut root = std::fs::canonicalize(Path::new("."))?; + for component in git_root.components() { + match component { + Component::CurDir => {} + Component::ParentDir => { + root.pop(); + } + _ => unreachable!("non-traversal component"), + } + } + Ok(root) + }; + let initial_root = resolve_immutable_traversal()?; + git.ensure_active_worktree_root(&initial_root)?; + #[cfg(test)] + run_stage_paths_hook(StagePathsHookPoint::ResolvePhysicalCwd); + let live_root = resolve_immutable_traversal()?; + git.ensure_active_worktree_root(&live_root)?; + (live_root, Some(git)) + } else { + (std::env::current_dir()?.join(git_root), None) + }; + if !guarded_root.is_absolute() { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + "repository root route could not be anchored to an absolute path", + )); + } + let git = match bound_git { + Some(git) => git, + None => GitRunner::for_cwd_io(&guarded_root)?, + }; + git.ensure_repository_root_route(&guarded_root)?; + let canonical_root = std::fs::canonicalize(&guarded_root)?; let mut config = GuardedGitConfig::authorize(&git, &canonical_root, Vec::new())?; let (tmpdir, patch_path) = write_temp_patch(diff)?; let paths = diff --git a/codex-rs/git-utils/src/patch_paths_tests.rs b/codex-rs/git-utils/src/patch_paths_tests.rs index f0485ad8ca..25697efc24 100644 --- a/codex-rs/git-utils/src/patch_paths_tests.rs +++ b/codex-rs/git-utils/src/patch_paths_tests.rs @@ -234,6 +234,318 @@ fn standalone_stage_paths_rejects_a_route_crossing_a_related_worktree() { ); } +#[cfg(any(unix, windows))] +fn configure_and_commit_seed(root: &Path) { + assert_eq!(run(root, &["git", "config", "user.email", "a@b.c"]).0, 0); + assert_eq!(run(root, &["git", "config", "user.name", "Codex"]).0, 0); + commit_seed(root); +} + +#[cfg(any(unix, windows))] +fn linked_route_fixture() -> (tempfile::TempDir, PathBuf, PathBuf, PathBuf) { + let fixture = tempfile::tempdir().expect("fixture"); + let common = fixture.path().join("repository.git"); + let primary = fixture.path().join("primary"); + let linked = fixture.path().join("linked"); + let nested = primary.join("nested"); + let c = common.to_string_lossy().into_owned(); + let p = primary.to_string_lossy().into_owned(); + assert_eq!(run(fixture.path(), &["git", "init", "--bare", &c]).0, 0); + let primary_add = run( + fixture.path(), + &["git", "--git-dir", &c, "worktree", "add", "--orphan", &p], + ); + assert_eq!(primary_add.0, 0, "add primary: {primary_add:?}"); + configure_and_commit_seed(&primary); + let primary_head = run(&primary, &["git", "rev-parse", "HEAD"]); + assert_eq!(primary_head.0, 0, "resolve primary HEAD: {primary_head:?}"); + let primary_head = primary_head.1.trim().to_string(); + let l = linked.to_string_lossy().into_owned(); + let linked_add = run( + fixture.path(), + &[ + "git", + "--git-dir", + &c, + "worktree", + "add", + "--detach", + &l, + &primary_head, + ], + ); + assert_eq!(linked_add.0, 0, "add linked: {linked_add:?}"); + std::fs::create_dir_all(&nested).expect("nested primary directory"); + std::fs::write(linked.join("routed.txt"), "routed\n").expect("write linked file"); + (fixture, primary, linked, nested) +} + +#[cfg(any(unix, windows))] +#[test] +fn standalone_stage_paths_rejects_relative_related_worktree_routes_from_nested_cwd() { + const TEST_NAME: &str = "patch_paths::tests::standalone_stage_paths_rejects_relative_related_worktree_routes_from_nested_cwd"; + const CWD_ALIAS_ENV: &str = "CODEX_GIT_UTILS_RELATIVE_ROUTE_CWD_ALIAS"; + const INDEPENDENT_ENV: &str = "CODEX_GIT_UTILS_RELATIVE_ROUTE_INDEPENDENT"; + const PRIMARY_ENV: &str = "CODEX_GIT_UTILS_RELATIVE_ROUTE_PRIMARY"; + const NESTED_ENV: &str = "CODEX_GIT_UTILS_RELATIVE_ROUTE_NESTED"; + + if std::env::var_os(NESTED_ENV).is_none() { + let (fixture, primary, linked, nested) = linked_route_fixture(); + let deeper = nested.join("deeper"); + let independent = primary.join("independent"); + std::fs::create_dir_all(nested.join("routes")).expect("nested primary directories"); + std::fs::create_dir_all(&deeper).expect("deeper primary directory"); + std::fs::create_dir_all(&independent).expect("independent repository directory"); + assert_eq!(run(&independent, &["git", "init", "-q"]).0, 0); + configure_and_commit_seed(&independent); + let linked_child = linked.join("child"); + std::fs::create_dir_all(&linked_child).expect("linked child directory"); + for path in [ + "absolute-parent-control.txt", + "deep-curdir-control.txt", + "deep-parent-control.txt", + "parent-control.txt", + "physical-cwd-control.txt", + "root-curdir-control.txt", + ] { + std::fs::write(primary.join(path), "control\n").expect("write control file"); + } + let _direct_route = create_dir_alias(&linked, &nested.join("linked-route")); + let _nested_route = create_dir_alias(&linked, &nested.join("routes/linked-route")); + let _direct_parent_route = + create_dir_alias(&linked_child, &nested.join("linked-child-route")); + let _nested_parent_route = + create_dir_alias(&linked_child, &nested.join("routes/linked-child-route")); + let cwd_alias = fixture.path().join("nested-cwd-route"); + let _cwd_alias = create_dir_alias(&nested, &cwd_alias); + + run_isolated_test( + TEST_NAME, + &[ + (CWD_ALIAS_ENV, cwd_alias.as_os_str()), + (INDEPENDENT_ENV, independent.as_os_str()), + (PRIMARY_ENV, primary.as_os_str()), + (NESTED_ENV, nested.as_os_str()), + ], + ); + assert!( + run(&linked, &["git", "diff", "--cached", "--name-only"]) + .1 + .is_empty(), + "relative related-worktree routes must not stage the linked index" + ); + assert_eq!( + run(&primary, &["git", "diff", "--cached", "--name-only"]).1, + "absolute-parent-control.txt\ndeep-curdir-control.txt\ndeep-parent-control.txt\nparent-control.txt\nphysical-cwd-control.txt\nroot-curdir-control.txt\n" + ); + assert!( + run(&independent, &["git", "diff", "--cached", "--name-only"]) + .1 + .is_empty(), + "parent traversal must not mutate the nested independent index" + ); + return; + } + + let primary = PathBuf::from(std::env::var_os(PRIMARY_ENV).expect("primary route")); + let nested = PathBuf::from(std::env::var_os(NESTED_ENV).expect("nested route")); + let independent = + PathBuf::from(std::env::var_os(INDEPENDENT_ENV).expect("independent repository")); + std::env::set_current_dir(&nested).expect("enter nested primary directory"); + let primary_before = git_index_bytes(&primary); + let linked = primary.parent().expect("fixture").join("linked"); + let linked_before = git_index_bytes(&linked); + for route in [ + ".", + "linked-route", + "routes/linked-route", + "linked-child-route/..", + "routes/linked-child-route/./..", + ] { + let error = stage_paths(Path::new(route), &new_file_diff("routed.txt")) + .expect_err("unsafe relative route must reject"); + assert_eq!(error.kind(), io::ErrorKind::PermissionDenied, "{route}"); + assert_eq!(git_index_bytes(&primary), primary_before, "{route}"); + assert_eq!(git_index_bytes(&linked), linked_before, "{route}"); + } + + stage_paths(Path::new(".."), &new_file_diff("parent-control.txt")) + .expect("parent-only route from nested cwd selects the active root"); + + std::env::set_current_dir(nested.join("deeper")).expect("enter deeper primary directory"); + stage_paths( + Path::new("../.."), + &new_file_diff("deep-parent-control.txt"), + ) + .expect("deeper parent-only route selects the active root"); + stage_paths( + Path::new("./.././.."), + &new_file_diff("deep-curdir-control.txt"), + ) + .expect("deeper parent and current-directory route selects the active root"); + + let cwd_alias = PathBuf::from(std::env::var_os(CWD_ALIAS_ENV).expect("cwd alias")); + std::env::set_current_dir(cwd_alias).expect("enter aliased nested cwd"); + stage_paths(Path::new(".."), &new_file_diff("physical-cwd-control.txt")) + .expect("parent traversal is based on the captured physical cwd"); + + std::env::set_current_dir(&primary).expect("enter primary worktree root"); + stage_paths(Path::new("."), &new_file_diff("root-curdir-control.txt")) + .expect("ordinary relative active-worktree route remains supported"); + + std::env::set_current_dir(&independent).expect("enter nested independent repository"); + let primary_before = git_index_bytes(&primary); + let independent_before = git_index_bytes(&independent); + let error = stage_paths(Path::new(".."), &new_file_diff("parent-control.txt")) + .expect_err("parent traversal must not escape a nested independent repository"); + assert_eq!(error.kind(), io::ErrorKind::PermissionDenied); + assert_eq!(git_index_bytes(&primary), primary_before); + assert_eq!(git_index_bytes(&independent), independent_before); + stage_paths(&primary, &new_file_diff("absolute-parent-control.txt")) + .expect("explicit outer route remains supported"); + assert_eq!(git_index_bytes(&independent), independent_before); +} + +#[cfg(unix)] +#[test] +fn standalone_stage_paths_rejects_actual_cwd_reparented_into_linked_worktree() { + const TEST_NAME: &str = "patch_paths::tests::standalone_stage_paths_rejects_actual_cwd_reparented_into_linked_worktree"; + const MODE_ENV: &str = "CODEX_GIT_UTILS_REPARENTED_CWD_MODE"; + const PRIMARY_ENV: &str = "CODEX_GIT_UTILS_REPARENTED_CWD_PRIMARY"; + const NESTED_ENV: &str = "CODEX_GIT_UTILS_REPARENTED_CWD_NESTED"; + + if std::env::var_os(MODE_ENV).is_none() { + for mode in ["pathname", "aba", "reparent", "combined", "prespawn"] { + let (_fixture, primary, linked, nested) = linked_route_fixture(); + if mode == "pathname" { + std::fs::create_dir_all(linked.join("child")).expect("linked child directory"); + } + + run_isolated_test( + TEST_NAME, + &[ + (MODE_ENV, OsStr::new(mode)), + (PRIMARY_ENV, primary.as_os_str()), + (NESTED_ENV, nested.as_os_str()), + ], + ); + } + return; + } + + let mode = std::env::var(MODE_ENV).expect("reparent mode"); + let primary = PathBuf::from(std::env::var_os(PRIMARY_ENV).expect("primary route")); + let nested = PathBuf::from(std::env::var_os(NESTED_ENV).expect("nested route")); + let linked = primary.parent().expect("fixture").join("linked"); + let moved_primary = primary.parent().expect("fixture").join("primary-moved"); + std::env::set_current_dir(&nested).expect("enter nested primary directory"); + let index_path = |root: &Path| { + let output = run(root, &["git", "rev-parse", "--git-path", "index"]); + assert_eq!(output.0, 0, "resolve index path: {output:?}"); + let path = PathBuf::from(output.1.trim()); + let path = if path.is_absolute() { + path + } else { + root.join(path) + }; + std::fs::canonicalize(path).expect("canonical index path") + }; + let primary_index = index_path(&primary); + let linked_index = index_path(&linked); + let primary_before = std::fs::read(&primary_index).expect("read primary index"); + let linked_before = std::fs::read(&linked_index).expect("read linked index"); + let replacement = std::rc::Rc::new(std::cell::RefCell::new(None)); + if matches!(mode.as_str(), "combined" | "prespawn") { + let replacement_for_hook = std::rc::Rc::clone(&replacement); + let primary_for_hook = primary.clone(); + let linked_for_hook = linked; + let moved_primary_for_hook = moved_primary; + let primary_admin = PathBuf::from( + run(&primary, &["git", "rev-parse", "--absolute-git-dir"]) + .1 + .trim(), + ); + let hook = move || { + std::fs::rename(&primary_for_hook, &moved_primary_for_hook) + .expect("rename initial repository root"); + std::fs::write( + primary_admin.join("gitdir"), + format!("{}\n", moved_primary_for_hook.join(".git").display()), + ) + .expect("update moved primary worktree backlink"); + std::fs::rename( + moved_primary_for_hook.join("nested"), + linked_for_hook.join("child"), + ) + .expect("reparent actual cwd into linked worktree"); + replacement_for_hook + .borrow_mut() + .replace(create_dir_alias(&linked_for_hook, &primary_for_hook)); + }; + set_stage_paths_hook( + if mode == "combined" { + StagePathsHookPoint::BindRunner + } else { + StagePathsHookPoint::PreSpawn + }, + hook, + ); + } else if mode == "aba" { + let primary_for_hook = primary; + let linked_for_hook = linked; + let moved_primary_for_hook = moved_primary; + set_stage_paths_hook(StagePathsHookPoint::BindRunner, move || { + std::fs::rename(&primary_for_hook, &moved_primary_for_hook).expect("rename root"); + drop(create_dir_alias(&linked_for_hook, &primary_for_hook)); + std::fs::rename(&moved_primary_for_hook, &primary_for_hook).expect("restore root"); + }); + } else if mode == "reparent" { + let nested_for_hook = nested; + let linked_child_for_hook = linked.join("child"); + set_stage_paths_hook(StagePathsHookPoint::ResolvePhysicalCwd, move || { + std::fs::rename(&nested_for_hook, &linked_child_for_hook) + .expect("reparent actual cwd into linked worktree"); + }); + } else { + let replacement_for_hook = std::rc::Rc::clone(&replacement); + let nested_for_hook = nested; + let moved_nested_for_hook = primary.join("nested-moved"); + let linked_child_for_hook = linked.join("child"); + set_stage_paths_hook(StagePathsHookPoint::ResolvePhysicalCwd, move || { + std::fs::rename(&nested_for_hook, &moved_nested_for_hook) + .expect("rename actual cwd directory"); + replacement_for_hook + .borrow_mut() + .replace(create_dir_alias(&linked_child_for_hook, &nested_for_hook)); + }); + } + + let result = stage_paths(Path::new(".."), &new_file_diff("routed.txt")); + assert_eq!( + std::fs::read(primary_index).expect("reread primary index"), + primary_before + ); + assert_eq!( + std::fs::read(linked_index).expect("reread linked index"), + linked_before + ); + if matches!(mode.as_str(), "pathname" | "aba") { + result.expect("a restored or renamed pathname must remain bound to the origin repository"); + } else { + let error = result.expect_err("reparented cwd must fail closed"); + assert_eq!(error.kind(), io::ErrorKind::PermissionDenied); + if matches!(mode.as_str(), "combined" | "prespawn") { + let expected = if mode == "combined" { + "does not match runner repository" + } else { + "changed before Git launch" + }; + assert!(error.to_string().contains(expected), "{mode}: {error}"); + } + } + drop(replacement.borrow_mut().take()); +} + #[test] fn effective_paths_cover_supported_patch_headers() { let cases = [ @@ -715,6 +1027,181 @@ fn stage_effective_paths( stage_effective_paths_standalone(&mut config, paths) } +#[test] +fn composed_staging_preserves_eligible_path_when_ignored_peer_is_refused() { + let repo = init_repo(); + let root = repo.path(); + std::fs::write(root.join(".gitignore"), "ignored.txt\n").expect("write ignore rule"); + assert_eq!(run(root, &["git", "add", ".gitignore"]).0, 0); + assert_eq!(run(root, &["git", "commit", "-m", "ignore rule"]).0, 0); + std::fs::write(root.join("ignored.txt"), "ignored\n").expect("write ignored file"); + std::fs::write(root.join("ordinary.txt"), "ordinary\n").expect("write ordinary file"); + let paths = vec!["ignored.txt".to_string(), "ordinary.txt".to_string()]; + + let git = GitRunner::for_cwd_io(root).expect("trusted Git"); + let canonical_root = std::fs::canonicalize(root).expect("canonical root"); + let mut config = + GuardedGitConfig::authorize(&git, &canonical_root, Vec::new()).expect("authorization"); + config + .authorize_filter_paths(&paths) + .expect("apply filter snapshot"); + let result = update_index_exact_paths_from_apply(&mut config, &paths, &paths) + .expect("ignored peer does not suppress eligible pre-staging"); + + assert_eq!(result.exit_code, 0, "{result:?}"); + assert!(result.stderr.contains("ignored.txt"), "{result:?}"); + assert_eq!( + run(root, &["git", "diff", "--cached", "--name-only"]).1, + "ordinary.txt\n" + ); + assert!(index_entry(root, "ignored.txt").is_empty()); +} + +#[test] +fn composed_staging_preserves_eligible_sparse_path_and_skips_excluded_peer() { + let repo = init_repo(); + let root = repo.path(); + std::fs::create_dir_all(root.join("inside")).expect("create included directory"); + std::fs::create_dir_all(root.join("outside")).expect("create excluded directory"); + std::fs::write(root.join("inside/base.txt"), "inside\n").expect("write included base"); + std::fs::write(root.join("outside/base.txt"), "outside\n").expect("write excluded base"); + assert_eq!(run(root, &["git", "add", "."]).0, 0); + assert_eq!(run(root, &["git", "commit", "-m", "sparse base"]).0, 0); + assert_eq!( + run(root, &["git", "sparse-checkout", "init", "--cone"]).0, + 0 + ); + assert_eq!(run(root, &["git", "sparse-checkout", "set", "inside"]).0, 0); + std::fs::write(root.join("inside/eligible.txt"), "eligible\n").expect("write eligible path"); + std::fs::create_dir_all(root.join("outside")).expect("recreate excluded directory"); + std::fs::write(root.join("outside/base.txt"), "changed outside\n") + .expect("modify sparse-excluded tracked path"); + let paths = vec![ + "inside/eligible.txt".to_string(), + "outside/base.txt".to_string(), + ]; + + let git = GitRunner::for_cwd_io(root).expect("trusted Git"); + let canonical_root = std::fs::canonicalize(root).expect("canonical root"); + let mut config = + GuardedGitConfig::authorize(&git, &canonical_root, Vec::new()).expect("authorization"); + config + .authorize_filter_paths(&paths) + .expect("apply filter snapshot"); + stage_effective_paths_from_apply(&mut config, &paths) + .expect("sparse-excluded peer does not suppress eligible pre-staging"); + + assert_eq!( + run(root, &["git", "diff", "--cached", "--name-only"]).1, + "inside/eligible.txt\n" + ); + assert!( + run(root, &["git", "diff", "--cached", "--", "outside/base.txt"]) + .1 + .is_empty(), + "sparse-excluded tracked path must retain its prior index entry" + ); +} + +#[test] +fn composed_staging_non_sparse_skip_worktree_refusal_remains_atomic() { + let repo = init_repo(); + let root = repo.path(); + std::fs::write(root.join("eligible.txt"), "base eligible\n").expect("write eligible base"); + std::fs::write(root.join("skipped.txt"), "base skipped\n").expect("write skipped base"); + assert_eq!( + run(root, &["git", "add", "eligible.txt", "skipped.txt"]).0, + 0 + ); + assert_eq!(run(root, &["git", "commit", "-m", "base"]).0, 0); + assert_eq!( + run( + root, + &[ + "git", + "update-index", + "--skip-worktree", + "--", + "skipped.txt" + ] + ) + .0, + 0 + ); + std::fs::write(root.join("eligible.txt"), "changed eligible\n").expect("modify eligible path"); + std::fs::write(root.join("skipped.txt"), "changed skipped\n").expect("modify skipped path"); + let paths = vec!["eligible.txt".to_string(), "skipped.txt".to_string()]; + let before = git_index_bytes(root); + + let git = GitRunner::for_cwd_io(root).expect("trusted Git"); + let canonical_root = std::fs::canonicalize(root).expect("canonical root"); + let mut config = + GuardedGitConfig::authorize(&git, &canonical_root, Vec::new()).expect("authorization"); + config + .authorize_filter_paths(&paths) + .expect("apply filter snapshot"); + let result = update_index_exact_paths_from_apply(&mut config, &paths, &paths) + .expect("policy refusal is a staging result"); + + assert_ne!(result.exit_code, 0); + assert!(result.stderr.contains("skip-worktree"), "{result:?}"); + assert_eq!(git_index_bytes(root), before); +} + +#[test] +fn composed_staging_late_filter_refusal_precedes_eligible_index_mutation() { + let repo = init_repo(); + let root = repo.path(); + std::fs::write(root.join(".gitignore"), "ignored.txt\n").expect("write ignore rule"); + std::fs::write(root.join(".gitattributes"), "ordinary.txt filter=fresh\n") + .expect("write attributes"); + std::fs::write(root.join("ordinary.txt"), "base\n").expect("write ordinary base"); + assert_eq!( + run( + root, + &["git", "add", ".gitignore", ".gitattributes", "ordinary.txt"] + ) + .0, + 0 + ); + assert_eq!(run(root, &["git", "commit", "-m", "base"]).0, 0); + std::fs::write(root.join("ordinary.txt"), "changed\n").expect("modify ordinary path"); + std::fs::write(root.join("ignored.txt"), "ignored\n").expect("write ignored path"); + let paths = vec!["ignored.txt".to_string(), "ordinary.txt".to_string()]; + + let git = GitRunner::for_cwd_io(root).expect("trusted Git"); + let canonical_root = std::fs::canonicalize(root).expect("canonical root"); + let mut config = + GuardedGitConfig::authorize(&git, &canonical_root, Vec::new()).expect("authorization"); + config + .authorize_filter_paths(&paths) + .expect("initial apply filter snapshot"); + assert_eq!( + run( + root, + &[ + "git", + "config", + "filter.fresh.clean", + "git config codex.stage-filter-ran true && git hash-object --stdin", + ], + ) + .0, + 0 + ); + let before = git_index_bytes(root); + + let error = stage_effective_paths_from_apply(&mut config, &paths) + .expect_err("fresh selected filter must refuse before staging eligible paths"); + assert_eq!(error.kind(), io::ErrorKind::Unsupported); + assert_eq!(git_index_bytes(root), before); + assert_ne!( + run(root, &["git", "config", "--get", "codex.stage-filter-ran"]).0, + 0, + "refused filter helper must not execute" + ); +} + #[test] fn standalone_stage_paths_uses_one_source_authorization_and_one_filter_read() { if std::env::var_os("CODEX_GIT_UTILS_PATH_ENV_CHILD").is_none() { diff --git a/codex-rs/git-utils/src/repository_authority/authority.rs b/codex-rs/git-utils/src/repository_authority/authority.rs index cbdd194ce9..db55a32a9e 100644 --- a/codex-rs/git-utils/src/repository_authority/authority.rs +++ b/codex-rs/git-utils/src/repository_authority/authority.rs @@ -2,6 +2,8 @@ use std::io; use std::path::Path; use std::path::PathBuf; +use same_file::Handle; + use crate::errors::GitReadError; use crate::git_config::path_is_within; use crate::path_authority::RepositoryRouteBoundaries; @@ -29,6 +31,7 @@ mod policy; #[derive(Debug)] pub(crate) struct RepositoryAuthority { active_worktree_root: PathBuf, + active_worktree_identity: Option, roots: Vec, worktree_roots: Vec, common_dirs: Vec, @@ -57,8 +60,11 @@ impl RepositoryAuthority { let worktree_root = crate::get_git_repo_root(&canonical_cwd) .and_then(|root| std::fs::canonicalize(root).ok()) .unwrap_or_else(|| canonical_cwd.clone()); + let active_worktree_identity = Handle::from_path(&worktree_root) + .map_err(|error| invalid_metadata(&worktree_root, error))?; let mut authority = Self { active_worktree_root: worktree_root.clone(), + active_worktree_identity: Some(active_worktree_identity), roots: Vec::new(), worktree_roots: Vec::new(), common_dirs: Vec::new(), @@ -116,6 +122,7 @@ impl RepositoryAuthority { } pub(crate) fn revalidate_active_repository_metadata(&self) -> io::Result<()> { + self.revalidate_active_worktree_identity()?; let Some(expected) = &self.active_metadata else { return Ok(()); }; @@ -150,13 +157,17 @@ impl RepositoryAuthority { pub(crate) fn ensure_active_worktree_root(&self, root: &Path) -> io::Result<()> { let canonical_root = std::fs::canonicalize(root)?; - let same_root = canonical_root == self.active_worktree_root - || same_file::is_same_file(&canonical_root, &self.active_worktree_root)?; + let same_root = if let Some(expected) = &self.active_worktree_identity { + Handle::from_path(&canonical_root)? == *expected + } else { + canonical_root == self.active_worktree_root + || same_file::is_same_file(&canonical_root, &self.active_worktree_root)? + }; if !same_root { return Err(io::Error::new( io::ErrorKind::PermissionDenied, format!( - "guarded Git repository root {} does not match runner repository {}", + "guarded Git repository identity {} does not match runner repository {}", canonical_root.display(), self.active_worktree_root.display() ), @@ -165,6 +176,25 @@ impl RepositoryAuthority { Ok(()) } + fn revalidate_active_worktree_identity(&self) -> io::Result<()> { + let Some(expected) = &self.active_worktree_identity else { + return Ok(()); + }; + let actual = Handle::from_path(&self.active_worktree_root).map_err(|error| { + io::Error::new( + io::ErrorKind::PermissionDenied, + format!("guarded Git repository identity could not be revalidated: {error}"), + ) + })?; + if &actual != expected { + return Err(io::Error::new( + io::ErrorKind::PermissionDenied, + "guarded Git repository identity changed before Git launch", + )); + } + Ok(()) + } + #[cfg(test)] pub(crate) fn from_test_locations( roots: Vec, @@ -173,12 +203,15 @@ impl RepositoryAuthority { ) -> Result { let route_boundaries = repository_route_boundaries(&worktree_roots, &common_dirs) .map_err(|error| invalid_metadata(Path::new(""), error))?; + let active_worktree_root = worktree_roots + .first() + .or_else(|| roots.first()) + .cloned() + .unwrap_or_default(); + let active_worktree_identity = Handle::from_path(&active_worktree_root).ok(); Ok(Self { - active_worktree_root: worktree_roots - .first() - .or_else(|| roots.first()) - .cloned() - .unwrap_or_default(), + active_worktree_root, + active_worktree_identity, roots, worktree_roots, common_dirs, From 809d6129a38d35b7ffa5b14a3b16110ded7049f3 Mon Sep 17 00:00:00 2001 From: Chris Bookholt Date: Fri, 3 Jul 2026 17:52:35 -0700 Subject: [PATCH 06/20] test(git): allow platform-specific staging hook --- codex-rs/git-utils/src/patch_paths.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/codex-rs/git-utils/src/patch_paths.rs b/codex-rs/git-utils/src/patch_paths.rs index 468fa6f1b5..f5fc309dc1 100644 --- a/codex-rs/git-utils/src/patch_paths.rs +++ b/codex-rs/git-utils/src/patch_paths.rs @@ -27,6 +27,7 @@ std::thread_local! { } #[cfg(test)] +#[cfg_attr(not(unix), allow(dead_code))] fn set_stage_paths_hook(point: StagePathsHookPoint, hook: impl FnOnce() + 'static) { STAGE_PATHS_HOOKS.with(|slots| { let prior = slots.borrow_mut()[point as usize].replace(Box::new(hook)); From 0b46244061505f2d3443135b7c3a9369b1103fc1 Mon Sep 17 00:00:00 2001 From: Chris Bookholt Date: Fri, 3 Jul 2026 18:22:22 -0700 Subject: [PATCH 07/20] fix(git): satisfy child-layer lint --- codex-rs/git-utils/src/exact_staging.rs | 2 ++ codex-rs/git-utils/src/git_command.rs | 6 +++--- codex-rs/git-utils/src/patch_paths.rs | 1 + 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/codex-rs/git-utils/src/exact_staging.rs b/codex-rs/git-utils/src/exact_staging.rs index 3585b6ba77..bc18171555 100644 --- a/codex-rs/git-utils/src/exact_staging.rs +++ b/codex-rs/git-utils/src/exact_staging.rs @@ -18,6 +18,7 @@ pub(crate) struct StagePathsResult { } #[derive(Clone, Copy)] +#[cfg_attr(not(test), allow(dead_code))] enum StagingMode { ComposedApply, ReverseApply, @@ -42,6 +43,7 @@ enum SparseCheckoutPolicy { /// bytes read for an already-confined index pathname. It cannot make this /// exact command recurse into, or create index entries for, descendant paths; /// the filter neutralizer also remains in force for the entire command. +#[cfg_attr(not(test), allow(dead_code))] pub(crate) fn update_index_exact_paths_from_apply( config: &mut GuardedGitConfig<'_>, paths: &[String], diff --git a/codex-rs/git-utils/src/git_command.rs b/codex-rs/git-utils/src/git_command.rs index c67368e3e8..ea247b2154 100644 --- a/codex-rs/git-utils/src/git_command.rs +++ b/codex-rs/git-utils/src/git_command.rs @@ -522,9 +522,9 @@ fn classify_windows_git_path_units(path: &[u16]) -> WindowsGitPathUnits { } if path.len() < 4 - || !ascii_u16_eq_ignore_case(path[0], b'U') - || !ascii_u16_eq_ignore_case(path[1], b'N') - || !ascii_u16_eq_ignore_case(path[2], b'C') + || !ascii_u16_eq_ignore_case(path[0], /*ascii*/ b'U') + || !ascii_u16_eq_ignore_case(path[1], /*ascii*/ b'N') + || !ascii_u16_eq_ignore_case(path[2], /*ascii*/ b'C') || path[3] != BACKSLASH { return WindowsGitPathUnits::Rejected; diff --git a/codex-rs/git-utils/src/patch_paths.rs b/codex-rs/git-utils/src/patch_paths.rs index c348b8be02..fd71bf6f82 100644 --- a/codex-rs/git-utils/src/patch_paths.rs +++ b/codex-rs/git-utils/src/patch_paths.rs @@ -350,6 +350,7 @@ pub fn stage_paths(git_root: &Path, diff: &str) -> io::Result<()> { stage_effective_paths_standalone(&mut config, &paths) } +#[cfg_attr(not(test), allow(dead_code))] pub(crate) fn stage_effective_paths_from_apply( config: &mut GuardedGitConfig<'_>, paths: &[String], From 21ca43303bd71d79082d8122e9d37978b00fc03a Mon Sep 17 00:00:00 2001 From: Chris Bookholt Date: Fri, 3 Jul 2026 18:29:53 -0700 Subject: [PATCH 08/20] fix(git): bind configuration environment --- codex-rs/git-utils/src/errors.rs | 6 +- codex-rs/git-utils/src/git_command.rs | 9 ++ codex-rs/git-utils/src/git_command_tests.rs | 95 ++++++++++++ .../git-utils/src/git_config_environment.rs | 136 ++++++++++++++++++ .../src/git_config_environment_tests.rs | 126 ++++++++++++++++ codex-rs/git-utils/src/lib.rs | 1 + 6 files changed, 372 insertions(+), 1 deletion(-) create mode 100644 codex-rs/git-utils/src/git_config_environment.rs create mode 100644 codex-rs/git-utils/src/git_config_environment_tests.rs diff --git a/codex-rs/git-utils/src/errors.rs b/codex-rs/git-utils/src/errors.rs index 574cfc311a..04274135ec 100644 --- a/codex-rs/git-utils/src/errors.rs +++ b/codex-rs/git-utils/src/errors.rs @@ -50,6 +50,8 @@ pub(crate) enum GitReadError { InvalidRepositoryMetadata { path: PathBuf, reason: String }, #[error("{path:?} is not a Git repository")] NotRepository { path: PathBuf }, + #[error("invalid Git configuration environment: {reason}")] + InvalidConfigEnvironment { reason: String }, } impl GitReadError { @@ -59,7 +61,9 @@ impl GitReadError { Self::UnprovenPrimaryAuthority { .. } | Self::UnsafeRepositoryMetadata { .. } => { std::io::ErrorKind::PermissionDenied } - Self::InvalidRepositoryMetadata { .. } => std::io::ErrorKind::InvalidData, + Self::InvalidRepositoryMetadata { .. } | Self::InvalidConfigEnvironment { .. } => { + std::io::ErrorKind::InvalidData + } } } diff --git a/codex-rs/git-utils/src/git_command.rs b/codex-rs/git-utils/src/git_command.rs index 924ed32385..3cac863d21 100644 --- a/codex-rs/git-utils/src/git_command.rs +++ b/codex-rs/git-utils/src/git_command.rs @@ -5,6 +5,7 @@ use std::path::PathBuf; use std::process::Command; use crate::errors::GitReadError; +use crate::git_config_environment::GitConfigEnvironmentSnapshot; #[cfg(test)] use crate::git_executable::git_executable_name; use crate::git_executable::harden_git_launch_environment; @@ -30,6 +31,7 @@ pub(crate) struct GitRunner { argv0: PathBuf, safe_path: std::ffi::OsString, authority: RepositoryAuthority, + config_environment: GitConfigEnvironmentSnapshot, } /// A Git command that can only be spawned through [`GitRunner::output`], @@ -84,6 +86,7 @@ impl GitRunner { command.current_dir(parent); } harden_git_launch_environment(&mut command, &self.safe_path); + self.config_environment.apply_to(&mut command); GitCommand { inner: command } } @@ -117,12 +120,18 @@ impl GitRunner { ) -> Result { authority.ensure_primary_authority()?; let selected = select_git_executable(&authority, search_path)?; + let config_environment = GitConfigEnvironmentSnapshot::capture().map_err(|error| { + GitReadError::InvalidConfigEnvironment { + reason: error.to_string(), + } + })?; Ok(Self { executable: selected.executable, #[cfg(any(unix, test))] argv0: selected.argv0, safe_path: selected.safe_path, authority, + config_environment, }) } } diff --git a/codex-rs/git-utils/src/git_command_tests.rs b/codex-rs/git-utils/src/git_command_tests.rs index 2679f744fc..868ad6c097 100644 --- a/codex-rs/git-utils/src/git_command_tests.rs +++ b/codex-rs/git-utils/src/git_command_tests.rs @@ -52,6 +52,95 @@ fn run_git_stdout(cwd: &Path, args: &[&str]) -> String { .to_string() } +#[test] +fn runner_binds_config_environment_across_ambient_mutation() { + const CHILD: &str = "CODEX_GIT_CONFIG_BINDING_CHILD"; + if std::env::var_os(CHILD).is_none() { + let fixture = tempfile::tempdir().expect("fixture"); + let repo = fixture.path().join("repo"); + let safe_home = fixture.path().join("safe-home"); + let malicious_home = repo.join("malicious-home"); + std::fs::create_dir_all(&repo).expect("repo"); + std::fs::create_dir_all(&safe_home).expect("safe home"); + std::fs::create_dir_all(&malicious_home).expect("malicious home"); + run_git(&repo, &["init", "-q"]); + std::fs::write( + safe_home.join(".gitconfig"), + "[codex]\n\tbound = safe-home\n", + ) + .expect("safe home config"); + let malicious_global = repo.join("malicious.gitconfig"); + std::fs::write(&malicious_global, "[codex]\n\tbound = malicious\n") + .expect("malicious global config"); + std::fs::write( + malicious_home.join(".gitconfig"), + "[codex]\n\tbound = malicious-home\n", + ) + .expect("malicious home config"); + + let mut command = Command::new(std::env::current_exe().expect("test executable")); + isolate_git_command_environment(&mut command); + let output = command + .arg("git_command::tests::runner_binds_config_environment_across_ambient_mutation") + .arg("--exact") + .arg("--nocapture") + .env(CHILD, "1") + .env("RUST_TEST_THREADS", "1") + .env("CODEX_GIT_CONFIG_BINDING_ROOT", &repo) + .env("CODEX_GIT_CONFIG_BINDING_MALICIOUS", &malicious_global) + .env("CODEX_GIT_CONFIG_BINDING_MALICIOUS_HOME", &malicious_home) + .env("HOME", &safe_home) + .env("GIT_CONFIG_NOSYSTEM", "1") + .env("GIT_CONFIG_COUNT", "1") + .env("GIT_CONFIG_KEY_0", "codex.count") + .env("GIT_CONFIG_VALUE_0", "safe-count") + .env_remove("GIT_CONFIG_GLOBAL") + .env_remove("GIT_CONFIG_SYSTEM") + .env_remove("GIT_CONFIG_PARAMETERS") + .output() + .expect("isolated config binding test"); + assert!( + output.status.success(), + "isolated config binding test failed:\nstdout:\n{}\nstderr:\n{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); + return; + } + + let root = PathBuf::from(std::env::var_os("CODEX_GIT_CONFIG_BINDING_ROOT").expect("root")); + let malicious = + std::env::var_os("CODEX_GIT_CONFIG_BINDING_MALICIOUS").expect("malicious config"); + let malicious_home = + std::env::var_os("CODEX_GIT_CONFIG_BINDING_MALICIOUS_HOME").expect("malicious home"); + let runner = GitRunner::for_cwd_io(&root).expect("runner with captured config environment"); + + // SAFETY: the parent starts an isolated test process that runs only this + // exact test with one harness thread. No other application thread reads or + // writes these variables, and the process exits after the assertions. + unsafe { + std::env::set_var("GIT_CONFIG_GLOBAL", malicious); + std::env::set_var("HOME", malicious_home); + std::env::set_var("GIT_CONFIG_VALUE_0", "malicious-count"); + std::env::set_var( + "GIT_CONFIG_PARAMETERS", + "'codex.count'='malicious-parameter'", + ); + } + + for (key, expected) in [("codex.bound", "safe-home"), ("codex.count", "safe-count")] { + let mut command = runner.command_for_cwd(&root).expect("bound command"); + command.args(["config", "--get", key]); + let output = runner.output(command).expect("bound config read"); + assert!( + output.status.success(), + "{key}: {}", + String::from_utf8_lossy(&output.stderr) + ); + assert_eq!(String::from_utf8_lossy(&output.stdout).trim(), expected); + } +} + fn commit_all(cwd: &Path, message: &str) { run_git( cwd, @@ -396,6 +485,12 @@ fn git_read_error_io_kind_table_is_exhaustive() { }, io::ErrorKind::InvalidData, ), + ( + GitReadError::InvalidConfigEnvironment { + reason: "malformed GIT_CONFIG_COUNT".to_string(), + }, + io::ErrorKind::InvalidData, + ), ] { assert_eq!(error.io_kind(), expected, "{error}"); assert_eq!(error.into_io_error().kind(), expected); diff --git a/codex-rs/git-utils/src/git_config_environment.rs b/codex-rs/git-utils/src/git_config_environment.rs new file mode 100644 index 0000000000..43d80a3afd --- /dev/null +++ b/codex-rs/git-utils/src/git_config_environment.rs @@ -0,0 +1,136 @@ +use std::collections::BTreeMap; +use std::ffi::OsStr; +use std::ffi::OsString; +use std::io; +use std::process::Command; + +const FIXED_CONFIG_ENVIRONMENT_KEYS: &[&str] = &[ + "GIT_CONFIG_GLOBAL", + "GIT_CONFIG_SYSTEM", + "GIT_CONFIG_NOSYSTEM", + "GIT_CONFIG_COUNT", + "GIT_CONFIG_PARAMETERS", + "HOME", + "XDG_CONFIG_HOME", + #[cfg(windows)] + "APPDATA", + #[cfg(windows)] + "PROGRAMDATA", + #[cfg(windows)] + "USERPROFILE", + #[cfg(windows)] + "HOMEDRIVE", + #[cfg(windows)] + "HOMEPATH", +]; + +// Git config command entries are an untrusted process input. Keep capture +// bounded rather than allocating attacker-selected amounts of memory before +// Git gets a chance to reject an unreasonable count. +const MAX_CONFIG_ENVIRONMENT_ENTRIES: usize = 1024; + +/// Exact config-relevant process environment bound to one +/// [`GitRunner`](crate::git_command::GitRunner). +/// +/// Every ordinary Git child receives these captured values (including explicit +/// removals for variables that were absent). Commands that intentionally +/// isolate a config source may override them after command construction. +pub(crate) struct GitConfigEnvironmentSnapshot { + entries: Box<[(OsString, Option)]>, +} + +impl std::fmt::Debug for GitConfigEnvironmentSnapshot { + fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + formatter + .debug_struct("GitConfigEnvironmentSnapshot") + .field("captured_entries", &self.entries.len()) + .finish() + } +} + +impl GitConfigEnvironmentSnapshot { + pub(crate) fn capture() -> io::Result { + let environment = std::env::vars_os().collect::>(); + Self::capture_from(|name| captured_environment_value(&environment, name)) + } + + fn capture_from(mut value_for: impl FnMut(&OsStr) -> Option) -> io::Result { + let mut entries = FIXED_CONFIG_ENVIRONMENT_KEYS + .iter() + .map(|name| (OsString::from(name), value_for(OsStr::new(name)))) + .collect::>(); + + if let Some(raw_count) = value_for(OsStr::new("GIT_CONFIG_COUNT")) { + let count = raw_count + .to_str() + .ok_or_else(|| invalid_environment("non-UTF-8 GIT_CONFIG_COUNT"))? + .parse::() + .map_err(|_| invalid_environment("invalid GIT_CONFIG_COUNT"))?; + if count > MAX_CONFIG_ENVIRONMENT_ENTRIES { + return Err(invalid_environment( + "GIT_CONFIG_COUNT exceeds the supported safety bound", + )); + } + for index in 0..count { + for prefix in ["GIT_CONFIG_KEY_", "GIT_CONFIG_VALUE_"] { + let name = OsString::from(format!("{prefix}{index}")); + let value = value_for(&name); + entries.push((name, value)); + } + } + } + + Ok(Self { + entries: entries.into_boxed_slice(), + }) + } + + pub(crate) fn apply_to(&self, command: &mut Command) { + for (name, value) in &self.entries { + match value { + Some(value) => { + command.env(name, value); + } + None => { + command.env_remove(name); + } + } + } + } + + pub(crate) fn value(&self, name: &str) -> Option<&OsStr> { + self.entries + .iter() + .find_map(|(candidate, value)| (candidate == name).then_some(value.as_deref())) + .flatten() + } +} + +#[cfg(not(windows))] +fn captured_environment_value( + environment: &BTreeMap, + name: &OsStr, +) -> Option { + environment.get(name).cloned() +} + +#[cfg(windows)] +fn captured_environment_value( + environment: &BTreeMap, + name: &OsStr, +) -> Option { + environment.iter().find_map(|(candidate, value)| { + candidate + .to_string_lossy() + .eq_ignore_ascii_case(&name.to_string_lossy()) + .then(|| value.clone()) + }) +} + +fn invalid_environment(message: &str) -> io::Error { + io::Error::new(io::ErrorKind::InvalidData, message) +} + +#[cfg(test)] +#[path = "git_config_environment_tests.rs"] +mod tests; diff --git a/codex-rs/git-utils/src/git_config_environment_tests.rs b/codex-rs/git-utils/src/git_config_environment_tests.rs new file mode 100644 index 0000000000..3cdec9a790 --- /dev/null +++ b/codex-rs/git-utils/src/git_config_environment_tests.rs @@ -0,0 +1,126 @@ +use super::*; +use pretty_assertions::assert_eq; + +#[test] +fn snapshot_binds_present_empty_and_absent_fixed_and_indexed_values() { + let values = BTreeMap::from([ + (OsString::from("GIT_CONFIG_GLOBAL"), OsString::from("")), + ( + OsString::from("GIT_CONFIG_PARAMETERS"), + OsString::from("'safe.parameter'='present'"), + ), + (OsString::from("GIT_CONFIG_NOSYSTEM"), OsString::from("1")), + (OsString::from("HOME"), OsString::from("/safe/home")), + ( + OsString::from("XDG_CONFIG_HOME"), + OsString::from("/safe/xdg"), + ), + (OsString::from("GIT_CONFIG_COUNT"), OsString::from("2")), + ( + OsString::from("GIT_CONFIG_KEY_0"), + OsString::from("safe.one"), + ), + ( + OsString::from("GIT_CONFIG_VALUE_0"), + OsString::from("present"), + ), + ( + OsString::from("GIT_CONFIG_KEY_1"), + OsString::from("safe.two"), + ), + ]); + let snapshot = GitConfigEnvironmentSnapshot::capture_from(|name| values.get(name).cloned()) + .expect("capture environment"); + + assert_eq!(snapshot.value("GIT_CONFIG_GLOBAL"), Some(OsStr::new(""))); + assert_eq!(snapshot.value("HOME"), Some(OsStr::new("/safe/home"))); + assert_eq!(snapshot.value("GIT_CONFIG_SYSTEM"), None); + assert_eq!(snapshot.value("GIT_CONFIG_NOSYSTEM"), Some(OsStr::new("1"))); + assert_eq!( + snapshot.value("GIT_CONFIG_PARAMETERS"), + Some(OsStr::new("'safe.parameter'='present'")) + ); + assert_eq!( + snapshot.value("GIT_CONFIG_KEY_0"), + Some(OsStr::new("safe.one")) + ); + assert_eq!(snapshot.value("GIT_CONFIG_VALUE_1"), None); + + let mut command = Command::new("git"); + command + .env("GIT_CONFIG_SYSTEM", "/later/system") + .env("GIT_CONFIG_VALUE_1", "later"); + snapshot.apply_to(&mut command); + let child_environment = command + .get_envs() + .map(|(name, value)| (name.to_owned(), value.map(OsStr::to_owned))) + .collect::>(); + assert_eq!( + child_environment.get(OsStr::new("GIT_CONFIG_SYSTEM")), + Some(&None) + ); + assert_eq!( + child_environment.get(OsStr::new("GIT_CONFIG_VALUE_1")), + Some(&None) + ); + assert_eq!( + child_environment.get(OsStr::new("HOME")), + Some(&Some(OsString::from("/safe/home"))) + ); +} + +#[test] +fn snapshot_rejects_malformed_or_unbounded_command_config_count() { + for count in [OsString::from("invalid"), OsString::from("1025")] { + let error = GitConfigEnvironmentSnapshot::capture_from(|name| { + (name == "GIT_CONFIG_COUNT").then(|| count.clone()) + }) + .expect_err("reject count"); + assert_eq!(error.kind(), io::ErrorKind::InvalidData); + } +} + +#[cfg(unix)] +#[test] +fn snapshot_preserves_non_utf8_config_path_bytes() { + use std::os::unix::ffi::OsStringExt; + + let raw = OsString::from_vec(vec![b'/', b't', b'm', b'p', b'/', 0xff]); + let snapshot = GitConfigEnvironmentSnapshot::capture_from(|name| { + (name == "GIT_CONFIG_GLOBAL").then(|| raw.clone()) + }) + .expect("capture non-UTF-8 path"); + assert_eq!(snapshot.value("GIT_CONFIG_GLOBAL"), Some(raw.as_os_str())); +} + +#[cfg(windows)] +#[test] +fn snapshot_matches_mixed_case_windows_environment_names() { + let environment = BTreeMap::from([ + (OsString::from("git_config_count"), OsString::from("1")), + ( + OsString::from("Git_Config_Key_0"), + OsString::from("safe.key"), + ), + ( + OsString::from("gIt_CoNfIg_VaLuE_0"), + OsString::from("safe-value"), + ), + (OsString::from("Home"), OsString::from(r"C:\safe-home")), + ]); + let snapshot = GitConfigEnvironmentSnapshot::capture_from(|name| { + captured_environment_value(&environment, name) + }) + .expect("capture mixed-case Windows environment"); + + assert_eq!(snapshot.value("GIT_CONFIG_COUNT"), Some(OsStr::new("1"))); + assert_eq!( + snapshot.value("GIT_CONFIG_KEY_0"), + Some(OsStr::new("safe.key")) + ); + assert_eq!( + snapshot.value("GIT_CONFIG_VALUE_0"), + Some(OsStr::new("safe-value")) + ); + assert_eq!(snapshot.value("HOME"), Some(OsStr::new(r"C:\safe-home"))); +} diff --git a/codex-rs/git-utils/src/lib.rs b/codex-rs/git-utils/src/lib.rs index 9309328e0b..e4dca00166 100644 --- a/codex-rs/git-utils/src/lib.rs +++ b/codex-rs/git-utils/src/lib.rs @@ -6,6 +6,7 @@ mod errors; mod fsmonitor; mod git_command; mod git_config; +mod git_config_environment; mod git_executable; mod info; mod local_only; From 3e1688f2cb8eff2efabb92f7e17ea46909cb7bf1 Mon Sep 17 00:00:00 2001 From: Chris Bookholt Date: Fri, 3 Jul 2026 18:32:25 -0700 Subject: [PATCH 09/20] fix(git): authorize primary config sources --- codex-rs/git-utils/src/apply.rs | 14 +- codex-rs/git-utils/src/git_command.rs | 18 + codex-rs/git-utils/src/git_command_tests.rs | 109 ++++ codex-rs/git-utils/src/git_config.rs | 15 +- codex-rs/git-utils/src/git_config_sources.rs | 59 ++ .../src/git_config_sources/path_safety.rs | 38 ++ .../src/git_config_sources/primary_sources.rs | 205 ++++++ .../git-utils/src/git_config_sources_tests.rs | 611 ++++++++++++++++++ codex-rs/git-utils/src/git_config_tests.rs | 35 + codex-rs/git-utils/src/lib.rs | 1 + codex-rs/git-utils/src/patch_paths.rs | 56 +- codex-rs/git-utils/src/patch_paths_tests.rs | 20 +- codex-rs/git-utils/src/path_authority.rs | 2 +- .../repository_authority/authority/policy.rs | 33 + 14 files changed, 1190 insertions(+), 26 deletions(-) create mode 100644 codex-rs/git-utils/src/git_config_sources.rs create mode 100644 codex-rs/git-utils/src/git_config_sources/path_safety.rs create mode 100644 codex-rs/git-utils/src/git_config_sources/primary_sources.rs create mode 100644 codex-rs/git-utils/src/git_config_sources_tests.rs diff --git a/codex-rs/git-utils/src/apply.rs b/codex-rs/git-utils/src/apply.rs index 2b9d76e959..5ba64abde2 100644 --- a/codex-rs/git-utils/src/apply.rs +++ b/codex-rs/git-utils/src/apply.rs @@ -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_primary_config_sources; use crate::patch_paths::extract_effective_paths_from_patch; use crate::patch_paths::stage_effective_paths; use crate::safe_git::DISABLED_HOOKS_PATH; @@ -47,17 +48,24 @@ pub struct ApplyGitResult { pub fn apply_git_patch(req: &ApplyGitRequest) -> io::Result { let git = GitRunner::for_cwd_io(&req.cwd)?; let mut cfg_parts = configured_git_config_parts(); + ensure_no_worktree_primary_config_sources(&git, &req.cwd)?; + 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_primary_config_sources(&git, &git_root)?; + } // 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_paths = extract_effective_paths_from_patch(&git, &patch_path, req.revert)?; + let patch_paths = + extract_effective_paths_from_patch(&git, &git_root, &patch_path, req.revert, &cfg_parts)?; + cfg_parts.extend(safe_git_config_parts()); if req.revert && !req.preflight { // Stage WT paths first to avoid index mismatch on revert. - stage_effective_paths(&git, &git_root, &patch_paths)?; + stage_effective_paths(&git, &git_root, &patch_paths, &cfg_parts)?; } // Build git args @@ -66,8 +74,6 @@ pub fn apply_git_patch(req: &ApplyGitRequest) -> io::Result { args.push("-R".into()); } - cfg_parts.extend(safe_git_config_parts()); - args.push(patch_path.to_string_lossy().to_string()); // Optional preflight: dry-run only; do not modify working tree diff --git a/codex-rs/git-utils/src/git_command.rs b/codex-rs/git-utils/src/git_command.rs index 3cac863d21..9d3c6af66e 100644 --- a/codex-rs/git-utils/src/git_command.rs +++ b/codex-rs/git-utils/src/git_command.rs @@ -59,6 +59,11 @@ impl GitCommand { self.inner.env(key, value); self } + + pub(crate) fn env_remove(&mut self, key: impl AsRef) -> &mut Self { + self.inner.env_remove(key); + self + } } impl GitRunner { @@ -102,6 +107,19 @@ impl GitRunner { Ok(command) } + pub(crate) fn ensure_config_source_is_not_worktree_controlled( + &self, + path: &Path, + description: &str, + ) -> io::Result<()> { + self.authority + .ensure_config_source_is_not_worktree_controlled(path, description) + } + + pub(crate) fn config_environment_value(&self, name: &str) -> Option<&OsStr> { + self.config_environment.value(name) + } + pub(crate) fn output(&self, mut command: GitCommand) -> io::Result { self.revalidate_active_repository_metadata()?; isolate_git_command_environment(&mut command.inner); diff --git a/codex-rs/git-utils/src/git_command_tests.rs b/codex-rs/git-utils/src/git_command_tests.rs index 868ad6c097..3d191735b3 100644 --- a/codex-rs/git-utils/src/git_command_tests.rs +++ b/codex-rs/git-utils/src/git_command_tests.rs @@ -242,6 +242,16 @@ fn locations_for_root(root: &Path) -> RepositoryAuthority { .expect("test repository authority") } +fn config_authority_for_root(root: &Path) -> RepositoryAuthority { + let mut roots = vec![root.to_path_buf()]; + let canonical = std::fs::canonicalize(root).expect("canonical root"); + if !roots.contains(&canonical) { + roots.push(canonical); + } + RepositoryAuthority::from_test_locations(roots.clone(), roots, vec![root.join(".git")]) + .expect("config repository authority") +} + fn raw_parent_traversal(root: &Path, sibling: &str) -> PathBuf { let separator = std::path::MAIN_SEPARATOR.to_string(); let mut path = root.as_os_str().to_os_string(); @@ -381,6 +391,105 @@ fn git_metadata_marker_parser_preserves_leading_path_space() { assert!(parse_git_marker_path(b"gitdir:/missing-space\n", b"gitdir: ").is_err()); } +#[test] +fn config_source_authority_requires_an_absolute_raw_path() { + let fixture = tempdir_for_native_git(); + let root = fixture.path().join("repo"); + std::fs::create_dir_all(&root).expect("create repository"); + run_git(&root, &["init", "-q"]); + let authority = config_authority_for_root(&root); + let path = Path::new("relative/config"); + + let error = authority + .ensure_config_source_is_not_worktree_controlled(path, "test config") + .expect_err("relative config path"); + + assert_eq!(error.kind(), io::ErrorKind::PermissionDenied, "{error}"); + assert!(error.to_string().contains("test config"), "{error}"); + assert!(error.to_string().contains("relative/config"), "{error}"); +} + +#[test] +fn config_source_authority_preserves_and_rejects_a_worktree_crossing_spelling() { + let fixture = tempdir_for_native_git(); + let root = fixture.path().join("repo"); + let safe = fixture.path().join("safe"); + std::fs::create_dir_all(root.join("nested")).expect("create repository descendant"); + std::fs::create_dir_all(&safe).expect("create safe directory"); + run_git(&root, &["init", "-q"]); + let authority = config_authority_for_root(&root); + let raw = root + .join("nested") + .join("..") + .join("..") + .join("safe/config"); + + let error = authority + .ensure_config_source_is_not_worktree_controlled(&raw, "crossing config") + .expect_err("worktree-crossing config path"); + + assert_eq!(error.kind(), io::ErrorKind::PermissionDenied, "{error}"); + assert!(error.to_string().contains("crossing config"), "{error}"); + assert!( + error.to_string().contains(&raw.display().to_string()), + "{error}" + ); +} + +#[test] +fn config_source_authority_allows_protected_metadata_and_unrelated_external_paths() { + let fixture = tempdir_for_native_git(); + let root = fixture.path().join("repo"); + let external = fixture.path().join("external/config"); + std::fs::create_dir_all(&root).expect("create repository"); + std::fs::create_dir_all(external.parent().expect("external parent")) + .expect("create external directory"); + run_git(&root, &["init", "-q"]); + let authority = config_authority_for_root(&root); + + authority + .ensure_config_source_is_not_worktree_controlled( + &root.join(".git/config"), + "protected metadata config", + ) + .expect("protected metadata config"); + let canonical_config = + std::fs::canonicalize(root.join(".git/config")).expect("canonical metadata config"); + authority + .ensure_config_source_is_not_worktree_controlled( + &canonical_config, + "canonical protected metadata config", + ) + .expect("canonical protected metadata config"); + authority + .ensure_config_source_is_not_worktree_controlled(&external, "external config") + .expect("unrelated external config"); +} + +#[test] +fn config_source_authority_rejects_an_unregistered_related_repository_root() { + let fixture = tempdir_for_native_git(); + let root = fixture.path().join("repo"); + let alias = fixture.path().join("unregistered-related-root"); + std::fs::create_dir_all(&root).expect("create repository"); + std::fs::create_dir_all(&alias).expect("create related root"); + run_git(&root, &["init", "-q"]); + std::fs::write( + alias.join(".git"), + format!("gitdir: {}\n", root.join(".git").display()), + ) + .expect("write related metadata marker"); + let authority = config_authority_for_root(&root); + let path = alias.join("config"); + + let error = authority + .ensure_config_source_is_not_worktree_controlled(&path, "related config") + .expect_err("unregistered related repository config"); + + assert_eq!(error.kind(), io::ErrorKind::PermissionDenied, "{error}"); + assert!(error.to_string().contains("related config"), "{error}"); +} + fn selected_git(locations: &RepositoryAuthority, directories: &[&Path]) -> PathBuf { let search_path = std::env::join_paths(directories).expect("PATH"); select_git_executable(locations, &search_path) diff --git a/codex-rs/git-utils/src/git_config.rs b/codex-rs/git-utils/src/git_config.rs index 92496132a1..737d3c433b 100644 --- a/codex-rs/git-utils/src/git_config.rs +++ b/codex-rs/git-utils/src/git_config.rs @@ -2,6 +2,19 @@ use std::path::Component; use std::path::Path; pub(crate) fn parse_git_boolean(value: &[u8]) -> Option { + parse_git_boolean_with_minimum(value, i128::from(i32::MIN)) +} + +/// Parse the Git boolean grammar while excluding numeric `INT_MIN`. +/// +/// Older supported Git releases reject that one signed endpoint after unit +/// expansion, so environment gates that must succeed across versions use this +/// conservative variant. The accepted spellings otherwise share one parser. +pub(crate) fn parse_git_boolean_symmetric_i32(value: &[u8]) -> Option { + parse_git_boolean_with_minimum(value, -i128::from(i32::MAX)) +} + +fn parse_git_boolean_with_minimum(value: &[u8], minimum: i128) -> Option { if value.eq_ignore_ascii_case(b"true") || value.eq_ignore_ascii_case(b"yes") || value.eq_ignore_ascii_case(b"on") @@ -61,7 +74,7 @@ pub(crate) fn parse_git_boolean(value: &[u8]) -> Option { let magnitude = i128::from_str_radix(digits, base).ok()?; let signed = if negative { -magnitude } else { magnitude }; let value = signed.checked_mul(factor)?; - (i128::from(i32::MIN)..=i128::from(i32::MAX)) + (minimum..=i128::from(i32::MAX)) .contains(&value) .then_some(value != 0) } diff --git a/codex-rs/git-utils/src/git_config_sources.rs b/codex-rs/git-utils/src/git_config_sources.rs new file mode 100644 index 0000000000..b539ef266c --- /dev/null +++ b/codex-rs/git-utils/src/git_config_sources.rs @@ -0,0 +1,59 @@ +use std::io; +use std::path::Path; + +use crate::git_command::GitRunner; + +mod path_safety; +mod primary_sources; + +use path_safety::normalize_absolute_path; +use path_safety::resolve_literal_path; +use primary_sources::is_disabled_primary_config_path; +use primary_sources::legacy_primary_config_source_candidates; +use primary_sources::selected_git_home_config_candidates; +use primary_sources::selected_git_prefix_system_candidate; + +/// Reject primary Git configuration paths selected from worktree-controlled +/// routes before any ordinary Git command can open them. +/// +/// This layer intentionally does not authorize includes. The complete include +/// graph is added by the next stack layer before this is promoted to a full +/// config-source capability. +pub(crate) fn ensure_no_worktree_primary_config_sources( + git: &GitRunner, + git_root: &Path, +) -> io::Result<()> { + let git_root = normalize_absolute_path(std::fs::canonicalize(git_root)?)?; + + for (description, candidate) in legacy_primary_config_source_candidates(git)? { + if !is_disabled_primary_config_path(&candidate) { + reject_source(git, &git_root, &candidate, description)?; + } + } + for candidate in selected_git_home_config_candidates(git, &git_root)? { + reject_source(git, &git_root, &candidate, "selected Git HOME config")?; + } + if let Some(candidate) = selected_git_prefix_system_candidate(git, &git_root)? { + reject_source( + git, + &git_root, + &candidate, + "selected Git prefix system config", + )?; + } + Ok(()) +} + +fn reject_source( + git: &GitRunner, + cwd: &Path, + candidate: &Path, + description: &str, +) -> io::Result<()> { + let absolute = resolve_literal_path(candidate, cwd); + git.ensure_config_source_is_not_worktree_controlled(&absolute, description) +} + +#[cfg(test)] +#[path = "git_config_sources_tests.rs"] +mod tests; diff --git a/codex-rs/git-utils/src/git_config_sources/path_safety.rs b/codex-rs/git-utils/src/git_config_sources/path_safety.rs new file mode 100644 index 0000000000..a2d86fd062 --- /dev/null +++ b/codex-rs/git-utils/src/git_config_sources/path_safety.rs @@ -0,0 +1,38 @@ +use std::io; +use std::path::Path; +use std::path::PathBuf; + +use codex_utils_absolute_path::AbsolutePathBuf; + +pub(super) const CONFIG_PATH_KEY: &str = "codex.config-source.path"; + +#[cfg(unix)] +pub(super) fn git_var_path_from_bytes(path: &[u8]) -> io::Result { + use std::os::unix::ffi::OsStringExt; + + Ok(PathBuf::from(std::ffi::OsString::from_vec(path.to_vec()))) +} + +#[cfg(not(unix))] +pub(super) fn git_var_path_from_bytes(path: &[u8]) -> io::Result { + Ok(PathBuf::from(std::str::from_utf8(path).map_err(|_| { + invalid_config_source("non-UTF-8 Git config source path") + })?)) +} + +pub(super) fn resolve_literal_path(path: impl AsRef, base: &Path) -> PathBuf { + let path = path.as_ref(); + if path.is_absolute() { + path.to_path_buf() + } else { + base.join(path) + } +} + +pub(super) fn normalize_absolute_path(path: impl AsRef) -> io::Result { + Ok(AbsolutePathBuf::from_absolute_path(path)?.into_path_buf()) +} + +pub(super) fn invalid_config_source(message: &str) -> io::Error { + io::Error::new(io::ErrorKind::InvalidData, message) +} diff --git a/codex-rs/git-utils/src/git_config_sources/primary_sources.rs b/codex-rs/git-utils/src/git_config_sources/primary_sources.rs new file mode 100644 index 0000000000..1c5e835935 --- /dev/null +++ b/codex-rs/git-utils/src/git_config_sources/primary_sources.rs @@ -0,0 +1,205 @@ +use std::ffi::OsStr; +use std::io; +use std::path::Path; +use std::path::PathBuf; + +use super::path_safety::CONFIG_PATH_KEY; +use super::path_safety::git_var_path_from_bytes; +use super::path_safety::invalid_config_source; +use crate::git_command::GitRunner; +use crate::git_config::parse_git_boolean_symmetric_i32; + +pub(super) fn selected_git_home_config_candidates( + git: &GitRunner, + cwd: &Path, +) -> io::Result> { + if git.config_environment_value("GIT_CONFIG_GLOBAL").is_some() { + return Ok(Vec::new()); + } + #[cfg(not(windows))] + if git.config_environment_value("HOME").is_none() { + return Ok(Vec::new()); + } + let dot_gitconfig = selected_git_path_candidate(git, cwd, "~/.gitconfig")?; + let mut candidates = vec![dot_gitconfig.clone()]; + if git + .config_environment_value("XDG_CONFIG_HOME") + .is_none_or(OsStr::is_empty) + { + let home = dot_gitconfig + .parent() + .ok_or_else(|| invalid_config_source("selected Git HOME has no parent"))?; + candidates.push(home.join(".config/git/config")); + } + Ok(candidates) +} + +pub(super) fn selected_git_prefix_system_candidate( + git: &GitRunner, + cwd: &Path, +) -> io::Result> { + if git_env_bool(git, "GIT_CONFIG_NOSYSTEM")? + || git.config_environment_value("GIT_CONFIG_SYSTEM").is_some() + { + return Ok(None); + } + selected_git_path_candidate(git, cwd, "%(prefix)/etc/gitconfig").map(Some) +} + +fn selected_git_path_candidate(git: &GitRunner, cwd: &Path, raw: &str) -> io::Result { + let tempdir = tempfile::tempdir()?; + let nonexistent_git_dir = tempdir.path().join("nonexistent-git-dir"); + let disabled_config = if cfg!(windows) { "NUL" } else { "/dev/null" }; + let mut command = git.command_for_cwd(cwd)?; + command + .env("GIT_CONFIG_GLOBAL", disabled_config) + .env("GIT_CONFIG_NOSYSTEM", "1") + .env("GIT_CONFIG_COUNT", "0") + .env_remove("GIT_CONFIG_PARAMETERS") + .arg("--git-dir") + .arg(&nonexistent_git_dir) + .arg("-c") + .arg(format!("{CONFIG_PATH_KEY}={raw}")) + .args([ + "config", + "--null", + "--no-includes", + "--path", + "--get", + CONFIG_PATH_KEY, + ]); + let output = git.output(command)?; + if !output.status.success() { + return Err(io::Error::other(format!( + "isolated selected Git path expansion failed with status {}: {}", + output.status, + String::from_utf8_lossy(&output.stderr).trim() + ))); + } + let value = output + .stdout + .strip_suffix(&[0]) + .ok_or_else(|| invalid_config_source("unterminated selected Git path"))?; + if value.is_empty() || value.contains(&0) { + return Err(invalid_config_source("ambiguous selected Git path")); + } + git_var_path_from_bytes(value) +} + +pub(super) fn legacy_primary_config_source_candidates( + git: &GitRunner, +) -> io::Result> { + let mut candidates = Vec::new(); + match git.config_environment_value("GIT_CONFIG_GLOBAL") { + Some(path) if !path.is_empty() => { + candidates.push(("GIT_CONFIG_GLOBAL", PathBuf::from(path))); + } + Some(_) => {} + None => { + let homes = git_home_directories(git); + match git.config_environment_value("XDG_CONFIG_HOME") { + Some(xdg) if !xdg.is_empty() => candidates.push(( + "XDG_CONFIG_HOME Git config", + PathBuf::from(xdg).join("git/config"), + )), + _ => { + #[cfg(windows)] + if let Some(app_data) = git.config_environment_value("APPDATA") + && !app_data.is_empty() + { + candidates.push(( + "APPDATA Git config", + PathBuf::from(app_data).join("Git/config"), + )); + } + for home in &homes { + candidates.push(( + "HOME XDG Git config", + home_config_path(home, ".config/git/config"), + )); + } + } + } + for home in &homes { + candidates.push(("HOME Git config", home_config_path(home, ".gitconfig"))); + } + } + } + if !git_env_bool(git, "GIT_CONFIG_NOSYSTEM")? { + match git.config_environment_value("GIT_CONFIG_SYSTEM") { + Some(path) if !path.is_empty() => { + candidates.push(("GIT_CONFIG_SYSTEM", PathBuf::from(path))); + } + Some(_) => {} + None => { + #[cfg(windows)] + if let Some(program_data) = git.config_environment_value("PROGRAMDATA") + && !program_data.is_empty() + { + candidates.push(( + "PROGRAMDATA Git config", + PathBuf::from(program_data).join("Git/config"), + )); + } + } + } + } + Ok(candidates) +} + +fn git_home_directories(git: &GitRunner) -> Vec { + if let Some(home) = git.config_environment_value("HOME") { + return vec![home.to_owned()]; + } + #[cfg(windows)] + { + let mut homes = Vec::new(); + if let (Some(drive), Some(path)) = ( + git.config_environment_value("HOMEDRIVE"), + git.config_environment_value("HOMEPATH"), + ) { + let mut home = drive.to_owned(); + home.push(path); + homes.push(home); + } + if let Some(profile) = git.config_environment_value("USERPROFILE") + && !homes.iter().any(|home| home == profile) + { + homes.push(profile.to_owned()); + } + homes + } + #[cfg(not(windows))] + Vec::new() +} + +fn home_config_path(home: &std::ffi::OsStr, suffix: &str) -> PathBuf { + if home.is_empty() { + PathBuf::from(std::path::MAIN_SEPARATOR.to_string()).join(suffix) + } else { + PathBuf::from(home).join(suffix) + } +} + +fn git_env_bool(git: &GitRunner, name: &str) -> io::Result { + let Some(value) = git.config_environment_value(name) else { + return Ok(false); + }; + let value = value + .to_str() + .ok_or_else(|| invalid_config_source("non-UTF-8 Git boolean environment value"))?; + parse_git_boolean_symmetric_i32(value.as_bytes()) + .ok_or_else(|| invalid_config_source("invalid Git boolean environment value")) +} + +#[cfg(windows)] +pub(super) fn is_disabled_primary_config_path(path: &Path) -> bool { + path.as_os_str() + .to_str() + .is_some_and(|path| path.eq_ignore_ascii_case("NUL")) +} + +#[cfg(not(windows))] +pub(super) fn is_disabled_primary_config_path(_path: &Path) -> bool { + false +} diff --git a/codex-rs/git-utils/src/git_config_sources_tests.rs b/codex-rs/git-utils/src/git_config_sources_tests.rs new file mode 100644 index 0000000000..222e9a614f --- /dev/null +++ b/codex-rs/git-utils/src/git_config_sources_tests.rs @@ -0,0 +1,611 @@ +use super::*; +use pretty_assertions::assert_eq; +use std::ffi::OsStr; +use std::path::PathBuf; + +fn run(cwd: &Path, args: &[&str]) -> (i32, String, String) { + let mut command = std::process::Command::new("git"); + crate::safe_git::isolate_git_command_environment(&mut command); + let output = command + .args(args) + .current_dir(cwd) + .output() + .expect("run Git"); + ( + output.status.code().unwrap_or(-1), + String::from_utf8_lossy(&output.stdout).into_owned(), + String::from_utf8_lossy(&output.stderr).into_owned(), + ) +} + +fn run_success(cwd: &Path, args: &[&str]) -> String { + let (code, stdout, stderr) = run(cwd, args); + assert_eq!(code, 0, "git {args:?}: {stderr}"); + stdout.trim().to_string() +} + +fn init_repo() -> tempfile::TempDir { + let repo = tempfile::tempdir().expect("tempdir"); + init_repo_at(repo.path()); + repo +} + +fn init_repo_at(root: &Path) { + std::fs::create_dir_all(root).expect("create repository directory"); + run_success(root, &["init"]); + run_success(root, &["config", "user.email", "codex@example.com"]); + run_success(root, &["config", "user.name", "Codex"]); + std::fs::write(root.join("file.txt"), "orig\n").expect("write file"); + run_success(root, &["add", "file.txt"]); + run_success(root, &["commit", "-m", "seed"]); +} + +#[cfg(windows)] +fn create_junction(path: &Path, target: &Path) { + // Bazel's GNU Windows runner can surface temporary paths with `/` + // separators. `mklink` treats those separators as option prefixes, so + // pass native separators to the cmd.exe built-in. + let path = path.as_os_str().to_string_lossy().replace('/', "\\"); + let target = target.as_os_str().to_string_lossy().replace('/', "\\"); + let output = std::process::Command::new("cmd.exe") + .args(["/D", "/C", "mklink", "/J"]) + .arg(path) + .arg(target) + .output() + .expect("create junction"); + assert!( + output.status.success(), + "mklink failed: stdout={} stderr={}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); +} + +fn guard(root: &Path) -> io::Result<()> { + let git = GitRunner::for_cwd_io(root)?; + ensure_no_worktree_primary_config_sources(&git, root) +} + +fn assert_worktree_rejection(error: io::Error) { + assert_eq!(error.kind(), io::ErrorKind::PermissionDenied, "{error}"); + assert!(error.to_string().contains("worktree-controlled"), "{error}"); +} + +fn run_isolated_source_test(test_name: &str, env: &[(&str, &OsStr)], removed: &[&str]) { + let mut command = std::process::Command::new(std::env::current_exe().expect("test binary")); + crate::safe_git::isolate_git_command_environment(&mut command); + command + .arg(test_name) + .arg("--exact") + .arg("--nocapture") + .env("CODEX_GIT_CONFIG_SOURCE_CHILD", "1") + .env("RUST_TEST_THREADS", "1"); + for (name, value) in env { + command.env(name, value); + } + for name in removed { + command.env_remove(name); + } + let output = command.output().expect("run isolated source test"); + assert!( + output.status.success(), + "isolated test {test_name} failed:\nstdout:\n{}\nstderr:\n{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); +} + +#[test] +fn rejects_empty_primary_global_and_system_sources_in_the_worktree() { + const TEST_NAME: &str = "git_config_sources::tests::rejects_empty_primary_global_and_system_sources_in_the_worktree"; + if std::env::var_os("CODEX_GIT_CONFIG_SOURCE_CHILD").is_none() { + let repo = init_repo(); + let root = repo.path(); + let unsafe_config = root.join("empty-primary.gitconfig"); + std::fs::write(&unsafe_config, "").expect("write empty primary config"); + let safe = tempfile::NamedTempFile::new().expect("safe external config"); + run_isolated_source_test( + TEST_NAME, + &[ + ("CODEX_GIT_CONFIG_SOURCE_ROOT", root.as_os_str()), + ("GIT_CONFIG_GLOBAL", unsafe_config.as_os_str()), + ("GIT_CONFIG_SYSTEM", safe.path().as_os_str()), + ], + &[], + ); + run_isolated_source_test( + TEST_NAME, + &[ + ("CODEX_GIT_CONFIG_SOURCE_ROOT", root.as_os_str()), + ("GIT_CONFIG_GLOBAL", safe.path().as_os_str()), + ("GIT_CONFIG_SYSTEM", unsafe_config.as_os_str()), + ], + &[], + ); + return; + } + + let root = PathBuf::from( + std::env::var_os("CODEX_GIT_CONFIG_SOURCE_ROOT").expect("fixture repository root"), + ); + assert_worktree_rejection(guard(&root).expect_err("unsafe primary config source")); +} + +#[test] +fn rejects_missing_home_and_xdg_sources_in_the_worktree() { + const TEST_NAME: &str = + "git_config_sources::tests::rejects_missing_home_and_xdg_sources_in_the_worktree"; + if std::env::var_os("CODEX_GIT_CONFIG_SOURCE_CHILD").is_none() { + let repo = init_repo(); + let root = repo.path(); + let xdg = root.join("xdg"); + std::fs::create_dir(&xdg).expect("create XDG root"); + let safe_system = tempfile::NamedTempFile::new().expect("safe system config"); + run_isolated_source_test( + TEST_NAME, + &[ + ("CODEX_GIT_CONFIG_SOURCE_ROOT", root.as_os_str()), + ("HOME", root.as_os_str()), + ("XDG_CONFIG_HOME", xdg.as_os_str()), + ("GIT_CONFIG_SYSTEM", safe_system.path().as_os_str()), + ], + &["GIT_CONFIG_GLOBAL"], + ); + return; + } + let root = PathBuf::from( + std::env::var_os("CODEX_GIT_CONFIG_SOURCE_ROOT").expect("fixture repository root"), + ); + assert_worktree_rejection(guard(&root).expect_err("unsafe HOME/XDG source")); +} + +#[test] +fn allows_explicitly_disabled_global_and_system_sources() { + const TEST_NAME: &str = + "git_config_sources::tests::allows_explicitly_disabled_global_and_system_sources"; + if std::env::var_os("CODEX_GIT_CONFIG_SOURCE_CHILD").is_none() { + let repo = init_repo(); + run_isolated_source_test( + TEST_NAME, + &[ + ("CODEX_GIT_CONFIG_SOURCE_ROOT", repo.path().as_os_str()), + ("GIT_CONFIG_GLOBAL", OsStr::new("")), + ("GIT_CONFIG_SYSTEM", OsStr::new("")), + ], + &[], + ); + return; + } + let root = PathBuf::from( + std::env::var_os("CODEX_GIT_CONFIG_SOURCE_ROOT").expect("fixture repository root"), + ); + guard(&root).expect("disabled primary config sources"); +} + +#[cfg(unix)] +#[test] +fn allows_unset_home_when_git_has_no_global_source() { + const TEST_NAME: &str = + "git_config_sources::tests::allows_unset_home_when_git_has_no_global_source"; + if std::env::var_os("CODEX_GIT_CONFIG_SOURCE_CHILD").is_none() { + let repo = init_repo(); + run_isolated_source_test( + TEST_NAME, + &[ + ("CODEX_GIT_CONFIG_SOURCE_ROOT", repo.path().as_os_str()), + ("GIT_CONFIG_NOSYSTEM", OsStr::new("1")), + ], + &[ + "HOME", + "XDG_CONFIG_HOME", + "GIT_CONFIG_GLOBAL", + "GIT_CONFIG_SYSTEM", + ], + ); + return; + } + let root = PathBuf::from( + std::env::var_os("CODEX_GIT_CONFIG_SOURCE_ROOT").expect("fixture repository root"), + ); + guard(&root).expect("Git without HOME or global config"); +} + +#[cfg(unix)] +#[test] +fn rejects_newline_bearing_raw_global_source_before_git_normalizes_it() { + const TEST_NAME: &str = "git_config_sources::tests::rejects_newline_bearing_raw_global_source_before_git_normalizes_it"; + if std::env::var_os("CODEX_GIT_CONFIG_SOURCE_CHILD").is_none() { + let parent = tempfile::tempdir().expect("fixture parent"); + let root = parent.path().join("repo"); + init_repo_at(&root); + let newline_dir = parent.path().join("external\n"); + std::fs::create_dir(&newline_dir).expect("create newline path component"); + let unsafe_config = root.join("global.gitconfig"); + std::fs::write(&unsafe_config, "").expect("write unsafe global config"); + let raw = newline_dir.join("../repo/global.gitconfig"); + run_isolated_source_test( + TEST_NAME, + &[ + ("CODEX_GIT_CONFIG_SOURCE_ROOT", root.as_os_str()), + ("GIT_CONFIG_GLOBAL", raw.as_os_str()), + ("GIT_CONFIG_NOSYSTEM", OsStr::new("1")), + ], + &["GIT_CONFIG_SYSTEM"], + ); + return; + } + + let root = PathBuf::from( + std::env::var_os("CODEX_GIT_CONFIG_SOURCE_ROOT").expect("fixture repository root"), + ); + assert_worktree_rejection(guard(&root).expect_err("newline raw global source")); +} + +#[cfg(unix)] +#[test] +fn ignores_non_utf8_values_for_unrelated_global_keys() { + const TEST_NAME: &str = + "git_config_sources::tests::ignores_non_utf8_values_for_unrelated_global_keys"; + if std::env::var_os("CODEX_GIT_CONFIG_SOURCE_CHILD").is_none() { + let repo = init_repo(); + let global = tempfile::NamedTempFile::new().expect("external global config"); + std::fs::write(global.path(), b"[user]\nname = \xff\n") + .expect("write non-UTF-8 unrelated config value"); + run_isolated_source_test( + TEST_NAME, + &[ + ("CODEX_GIT_CONFIG_SOURCE_ROOT", repo.path().as_os_str()), + ("GIT_CONFIG_GLOBAL", global.path().as_os_str()), + ("GIT_CONFIG_NOSYSTEM", OsStr::new("1")), + ], + &["GIT_CONFIG_SYSTEM"], + ); + return; + } + + let root = PathBuf::from( + std::env::var_os("CODEX_GIT_CONFIG_SOURCE_ROOT").expect("fixture repository root"), + ); + guard(&root).expect("unrelated non-UTF-8 global value"); +} + +#[cfg(unix)] +#[test] +fn rejects_worktree_fifo_primary_source_without_opening_it() { + const TEST_NAME: &str = + "git_config_sources::tests::rejects_worktree_fifo_primary_source_without_opening_it"; + if std::env::var_os("CODEX_GIT_CONFIG_SOURCE_CHILD").is_none() { + let repo = init_repo(); + let fifo = repo.path().join("global.fifo"); + let status = std::process::Command::new("mkfifo") + .arg(&fifo) + .status() + .expect("run mkfifo"); + assert!(status.success(), "mkfifo failed: {status}"); + run_isolated_source_test( + TEST_NAME, + &[ + ("CODEX_GIT_CONFIG_SOURCE_ROOT", repo.path().as_os_str()), + ("GIT_CONFIG_GLOBAL", fifo.as_os_str()), + ("GIT_CONFIG_NOSYSTEM", OsStr::new("1")), + ], + &["GIT_CONFIG_SYSTEM"], + ); + return; + } + + let root = PathBuf::from( + std::env::var_os("CODEX_GIT_CONFIG_SOURCE_ROOT").expect("fixture repository root"), + ); + assert_worktree_rejection(guard(&root).expect_err("worktree FIFO config source")); +} + +#[test] +fn git_config_nosystem_uses_the_shared_parser_with_a_cross_version_numeric_subset() { + const TEST_NAME: &str = "git_config_sources::tests::git_config_nosystem_uses_the_shared_parser_with_a_cross_version_numeric_subset"; + if std::env::var_os("CODEX_GIT_CONFIG_SOURCE_CHILD").is_none() { + let repo = init_repo(); + let unsafe_system = repo.path().join("system.gitconfig"); + std::fs::write(&unsafe_system, "").expect("write unsafe system config"); + for (value, expected) in [ + ("1", "ignored"), + ("2", "ignored"), + ("-1", "ignored"), + ("01", "ignored"), + ("+1", "ignored"), + ("0x1", "ignored"), + ("010", "ignored"), + ("1k", "ignored"), + ("-1g", "ignored"), + (" 1", "ignored"), + ("2147483647", "ignored"), + ("-2147483647", "ignored"), + ("true", "ignored"), + ("yes", "ignored"), + ("on", "ignored"), + ("", "rejected"), + ("0", "rejected"), + ("-0", "rejected"), + ("false", "rejected"), + ("no", "rejected"), + ("off", "rejected"), + ("not-a-bool", "invalid"), + ("08", "invalid"), + ("2147483648", "invalid"), + ("-2147483648", "invalid"), + ("-0x80000000", "invalid"), + ("-020000000000", "invalid"), + ("-2097152k", "invalid"), + ("-2048m", "invalid"), + ("-2g", "invalid"), + (" -2G", "invalid"), + ("-2147483649", "invalid"), + ("2g", "invalid"), + ] { + run_isolated_source_test( + TEST_NAME, + &[ + ("CODEX_GIT_CONFIG_SOURCE_ROOT", repo.path().as_os_str()), + ("CODEX_GIT_CONFIG_SOURCE_EXPECTED", OsStr::new(expected)), + ("GIT_CONFIG_GLOBAL", OsStr::new("")), + ("GIT_CONFIG_SYSTEM", unsafe_system.as_os_str()), + ("GIT_CONFIG_NOSYSTEM", OsStr::new(value)), + ], + &[], + ); + } + return; + } + + let root = PathBuf::from( + std::env::var_os("CODEX_GIT_CONFIG_SOURCE_ROOT").expect("fixture repository root"), + ); + match std::env::var("CODEX_GIT_CONFIG_SOURCE_EXPECTED") + .expect("expected child outcome") + .as_str() + { + "ignored" => guard(&root).expect("system source disabled"), + "rejected" => assert_worktree_rejection(guard(&root).expect_err("system source enabled")), + "invalid" => assert_eq!( + guard(&root).expect_err("invalid NOSYSTEM value").kind(), + io::ErrorKind::InvalidData + ), + expected => panic!("unexpected child outcome {expected}"), + } +} + +#[cfg(windows)] +#[test] +fn rejects_both_windows_home_candidates_when_home_is_absent() { + const TEST_NAME: &str = + "git_config_sources::tests::rejects_both_windows_home_candidates_when_home_is_absent"; + if std::env::var_os("CODEX_GIT_CONFIG_SOURCE_CHILD").is_none() { + let repo = init_repo(); + let external_profile = tempfile::tempdir().expect("external USERPROFILE"); + for home in [repo.path().to_path_buf(), repo.path().join("future-home")] { + let home = home.to_str().expect("UTF-8 Windows test path"); + assert!(home.len() >= 3 && home.as_bytes()[1] == b':', "{home}"); + run_isolated_source_test( + TEST_NAME, + &[ + ("CODEX_GIT_CONFIG_SOURCE_ROOT", repo.path().as_os_str()), + ("GIT_CONFIG_NOSYSTEM", OsStr::new("1")), + ("HOMEDRIVE", OsStr::new(&home[..2])), + ("HOMEPATH", OsStr::new(&home[2..])), + ("USERPROFILE", external_profile.path().as_os_str()), + ], + &[ + "HOME", + "XDG_CONFIG_HOME", + "GIT_CONFIG_GLOBAL", + "GIT_CONFIG_SYSTEM", + ], + ); + } + let external_home = external_profile + .path() + .to_str() + .expect("UTF-8 external Windows home"); + assert!( + external_home.len() >= 3 && external_home.as_bytes()[1] == b':', + "{external_home}" + ); + for profile in [ + repo.path().to_path_buf(), + repo.path().join("future-profile"), + ] { + run_isolated_source_test( + TEST_NAME, + &[ + ("CODEX_GIT_CONFIG_SOURCE_ROOT", repo.path().as_os_str()), + ("GIT_CONFIG_NOSYSTEM", OsStr::new("1")), + ("HOMEDRIVE", OsStr::new(&external_home[..2])), + ("HOMEPATH", OsStr::new(&external_home[2..])), + ("USERPROFILE", profile.as_os_str()), + ], + &[ + "HOME", + "XDG_CONFIG_HOME", + "GIT_CONFIG_GLOBAL", + "GIT_CONFIG_SYSTEM", + ], + ); + } + return; + } + + let root = PathBuf::from( + std::env::var_os("CODEX_GIT_CONFIG_SOURCE_ROOT").expect("fixture repository root"), + ); + assert_worktree_rejection(guard(&root).expect_err("Windows synthesized HOME source")); +} + +#[cfg(windows)] +#[test] +fn allows_exact_windows_nul_for_both_primary_config_channels() { + const TEST_NAME: &str = + "git_config_sources::tests::allows_exact_windows_nul_for_both_primary_config_channels"; + if std::env::var_os("CODEX_GIT_CONFIG_SOURCE_CHILD").is_none() { + let repo = init_repo(); + run_isolated_source_test( + TEST_NAME, + &[ + ("CODEX_GIT_CONFIG_SOURCE_ROOT", repo.path().as_os_str()), + ("GIT_CONFIG_GLOBAL", OsStr::new("NUL")), + ("GIT_CONFIG_SYSTEM", OsStr::new("NUL")), + ], + &["GIT_CONFIG_NOSYSTEM"], + ); + return; + } + let root = PathBuf::from( + std::env::var_os("CODEX_GIT_CONFIG_SOURCE_ROOT").expect("fixture repository root"), + ); + guard(&root).expect("exact NUL disables primary config channels"); +} + +#[cfg(windows)] +#[test] +fn windows_appdata_global_config_matches_native_git_selection_and_precedence() { + const TEST_NAME: &str = "git_config_sources::tests::windows_appdata_global_config_matches_native_git_selection_and_precedence"; + if std::env::var_os("CODEX_GIT_CONFIG_SOURCE_CHILD").is_none() { + for xdg in [None, Some(OsStr::new(""))] { + for existing in [true, false] { + let repo = init_repo(); + let appdata = repo.path().join("appdata"); + if existing { + let config = appdata.join("Git/config"); + std::fs::create_dir_all(config.parent().expect("APPDATA config parent")) + .expect("create APPDATA config parent"); + std::fs::write(&config, "[codex]\nappdata-marker = selected\n") + .expect("write APPDATA config"); + } + let external_home = tempfile::tempdir().expect("external HOME"); + let mut env = vec![ + ("CODEX_GIT_CONFIG_SOURCE_ROOT", repo.path().as_os_str()), + ( + "CODEX_GIT_CONFIG_SOURCE_EXPECTED", + OsStr::new(if existing { + "reject-existing-appdata" + } else { + "reject-missing-appdata" + }), + ), + ("GIT_CONFIG_NOSYSTEM", OsStr::new("1")), + ("HOME", external_home.path().as_os_str()), + ("APPDATA", appdata.as_os_str()), + ]; + if let Some(xdg) = xdg { + env.push(("XDG_CONFIG_HOME", xdg)); + } + let mut removed = vec![ + "GIT_CONFIG_GLOBAL", + "GIT_CONFIG_SYSTEM", + "HOMEDRIVE", + "HOMEPATH", + "USERPROFILE", + ]; + if xdg.is_none() { + removed.push("XDG_CONFIG_HOME"); + } + run_isolated_source_test(TEST_NAME, &env, &removed); + } + } + + let repo = init_repo(); + let appdata = repo.path().join("appdata"); + let appdata_config = appdata.join("Git/config"); + std::fs::create_dir_all(appdata_config.parent().expect("APPDATA config parent")) + .expect("create APPDATA config parent"); + std::fs::write(&appdata_config, "[codex]\nappdata-marker = appdata\n") + .expect("write APPDATA config"); + let external_home = tempfile::tempdir().expect("external HOME"); + let external_xdg = tempfile::tempdir().expect("external XDG_CONFIG_HOME"); + let xdg_config = external_xdg.path().join("git/config"); + std::fs::create_dir_all(xdg_config.parent().expect("XDG config parent")) + .expect("create XDG config parent"); + std::fs::write(&xdg_config, "[codex]\nappdata-marker = xdg\n").expect("write XDG config"); + run_isolated_source_test( + TEST_NAME, + &[ + ("CODEX_GIT_CONFIG_SOURCE_ROOT", repo.path().as_os_str()), + ("CODEX_GIT_CONFIG_SOURCE_EXPECTED", OsStr::new("allow-xdg")), + ("GIT_CONFIG_NOSYSTEM", OsStr::new("1")), + ("HOME", external_home.path().as_os_str()), + ("APPDATA", appdata.as_os_str()), + ("XDG_CONFIG_HOME", external_xdg.path().as_os_str()), + ], + &[ + "GIT_CONFIG_GLOBAL", + "GIT_CONFIG_SYSTEM", + "HOMEDRIVE", + "HOMEPATH", + "USERPROFILE", + ], + ); + + let external_global = tempfile::NamedTempFile::new().expect("external global config"); + std::fs::write( + external_global.path(), + "[codex]\nappdata-marker = explicit\n", + ) + .expect("write explicit global config"); + run_isolated_source_test( + TEST_NAME, + &[ + ("CODEX_GIT_CONFIG_SOURCE_ROOT", repo.path().as_os_str()), + ( + "CODEX_GIT_CONFIG_SOURCE_EXPECTED", + OsStr::new("allow-explicit-global"), + ), + ("GIT_CONFIG_NOSYSTEM", OsStr::new("1")), + ("HOME", repo.path().as_os_str()), + ("APPDATA", appdata.as_os_str()), + ("XDG_CONFIG_HOME", repo.path().as_os_str()), + ("GIT_CONFIG_GLOBAL", external_global.path().as_os_str()), + ], + &["GIT_CONFIG_SYSTEM", "HOMEDRIVE", "HOMEPATH", "USERPROFILE"], + ); + return; + } + + let root = PathBuf::from( + std::env::var_os("CODEX_GIT_CONFIG_SOURCE_ROOT").expect("fixture repository root"), + ); + let expected = + std::env::var("CODEX_GIT_CONFIG_SOURCE_EXPECTED").expect("expected child outcome"); + if expected != "reject-missing-appdata" { + let output = std::process::Command::new("git") + .args(["config", "--global", "--get", "codex.appdata-marker"]) + .current_dir(&root) + .output() + .expect("run native Git global config query"); + assert!( + output.status.success(), + "native Git global query failed: {}", + String::from_utf8_lossy(&output.stderr) + ); + let actual = String::from_utf8_lossy(&output.stdout); + let native_expected = match expected.as_str() { + "reject-existing-appdata" => "selected", + "allow-xdg" => "xdg", + "allow-explicit-global" => "explicit", + expected => panic!("unexpected child outcome {expected}"), + }; + assert_eq!( + actual.trim(), + native_expected, + "native Git global selection" + ); + } + match expected.as_str() { + "reject-existing-appdata" | "reject-missing-appdata" => { + assert_worktree_rejection(guard(&root).expect_err("worktree APPDATA config")); + } + "allow-xdg" => guard(&root).expect("nonempty XDG suppresses APPDATA"), + "allow-explicit-global" => { + guard(&root).expect("explicit global config suppresses HOME, XDG, and APPDATA") + } + expected => panic!("unexpected child outcome {expected}"), + } +} diff --git a/codex-rs/git-utils/src/git_config_tests.rs b/codex-rs/git-utils/src/git_config_tests.rs index 825d9a597f..8f49871c03 100644 --- a/codex-rs/git-utils/src/git_config_tests.rs +++ b/codex-rs/git-utils/src/git_config_tests.rs @@ -64,6 +64,41 @@ fn git_boolean_rejects_values_native_git_rejects() { } } +#[test] +fn symmetric_git_boolean_parser_excludes_only_int_min_spellings() { + for value in [ + b"-2147483648".as_slice(), + b"-0x80000000".as_slice(), + b"-020000000000".as_slice(), + b"-2097152k".as_slice(), + b"-2048m".as_slice(), + b"-2g".as_slice(), + b" -2G".as_slice(), + ] { + assert_eq!(parse_git_boolean(value), Some(true), "value {value:?}"); + assert_eq!( + parse_git_boolean_symmetric_i32(value), + None, + "value {value:?}" + ); + } + + for value in [ + b"0x1".as_slice(), + b"010".as_slice(), + b"1k".as_slice(), + b"-1g".as_slice(), + b" 1".as_slice(), + b"-2147483647".as_slice(), + ] { + assert_eq!( + parse_git_boolean_symmetric_i32(value), + parse_git_boolean(value), + "value {value:?}" + ); + } +} + #[test] fn path_containment_uses_component_boundaries() { let root = Path::new("/repo/root"); diff --git a/codex-rs/git-utils/src/lib.rs b/codex-rs/git-utils/src/lib.rs index e4dca00166..b52bf1ef19 100644 --- a/codex-rs/git-utils/src/lib.rs +++ b/codex-rs/git-utils/src/lib.rs @@ -7,6 +7,7 @@ mod fsmonitor; mod git_command; mod git_config; mod git_config_environment; +mod git_config_sources; mod git_executable; mod info; mod local_only; diff --git a/codex-rs/git-utils/src/patch_paths.rs b/codex-rs/git-utils/src/patch_paths.rs index 168d9b448f..378a245859 100644 --- a/codex-rs/git-utils/src/patch_paths.rs +++ b/codex-rs/git-utils/src/patch_paths.rs @@ -9,16 +9,23 @@ use crate::apply::safe_git_config_parts; use crate::apply::write_temp_patch; use crate::git_command::GitRunner; use crate::git_config::path_is_within; +use crate::git_config_sources::ensure_no_worktree_primary_config_sources; +/// Extract paths with Git from a cwd whose config sources have already been +/// authorized for `git_config_args`. pub(crate) fn extract_effective_paths_from_patch( git: &GitRunner, + authorized_cwd: &Path, patch_path: &Path, revert: bool, + git_config_args: &[String], ) -> io::Result> { - let forward_paths = git_apply_numstat_paths(git, patch_path, revert)?; + let forward_paths = + git_apply_numstat_paths(git, authorized_cwd, patch_path, revert, git_config_args)?; // `git apply --numstat` reports only the destination of a rename. Parse the // opposite orientation too so both endpoints are included in the result. - let reverse_paths = git_apply_numstat_paths(git, patch_path, !revert)?; + let reverse_paths = + git_apply_numstat_paths(git, authorized_cwd, patch_path, !revert, git_config_args)?; if forward_paths.len() != reverse_paths.len() { return Err(io::Error::new( io::ErrorKind::InvalidData, @@ -44,27 +51,38 @@ pub(crate) fn extract_effective_paths_from_patch( /// Security-sensitive callers must use the fallible internal extractor so an /// invalid or ambiguous patch is rejected instead of becoming an empty list. pub fn extract_paths_from_patch(diff_text: &str) -> Vec { + let Ok(cwd) = std::env::current_dir() else { + return Vec::new(); + }; + extract_paths_from_patch_from_cwd(diff_text, &cwd) +} + +fn extract_paths_from_patch_from_cwd(diff_text: &str, cwd: &Path) -> Vec { let Ok((tmpdir, patch_path)) = write_temp_patch(diff_text) else { return Vec::new(); }; - let paths = std::env::current_dir() - .ok() - .and_then(|cwd| GitRunner::for_cwd(&cwd).ok()) - .and_then(|git| { - extract_effective_paths_from_patch(&git, &patch_path, /*revert*/ false).ok() - }) - .unwrap_or_default(); + let paths = (|| -> io::Result> { + let git = GitRunner::for_cwd_io(cwd)?; + let git_root = crate::get_git_repo_root(cwd) + .ok_or_else(|| io::Error::other("not a Git repository"))?; + let git_root = std::fs::canonicalize(git_root)?; + ensure_no_worktree_primary_config_sources(&git, &git_root)?; + extract_effective_paths_from_patch(&git, &git_root, &patch_path, /*revert*/ false, &[]) + })() + .unwrap_or_default(); drop(tmpdir); paths } fn git_apply_numstat_paths( git: &GitRunner, + authorized_cwd: &Path, patch_path: &Path, revert: bool, + git_config_args: &[String], ) -> io::Result> { - let command_cwd = patch_path.parent().unwrap_or_else(|| Path::new(".")); - let mut cmd = git.command_for_cwd(command_cwd)?; + let mut cmd = git.command_for_cwd(authorized_cwd)?; + cmd.args(git_config_args); cmd.args(["apply", "--numstat", "-z"]); if revert { cmd.arg("-R"); @@ -226,16 +244,25 @@ fn invalid_windows_patch_component(component: &str) -> bool { /// Stage only the files that actually exist on disk for the given diff. pub fn stage_paths(git_root: &Path, diff: &str) -> io::Result<()> { let git = GitRunner::for_cwd_io(git_root)?; + let git_config_args = safe_git_config_parts(); + ensure_no_worktree_primary_config_sources(&git, git_root)?; let (tmpdir, patch_path) = write_temp_patch(diff)?; - let paths = extract_effective_paths_from_patch(&git, &patch_path, /*revert*/ true)?; + let paths = extract_effective_paths_from_patch( + &git, + git_root, + &patch_path, + /*revert*/ true, + &git_config_args, + )?; let _guard = tmpdir; - stage_effective_paths(&git, git_root, &paths) + stage_effective_paths(&git, git_root, &paths, &git_config_args) } pub(crate) fn stage_effective_paths( git: &GitRunner, git_root: &Path, paths: &[String], + git_config_args: &[String], ) -> io::Result<()> { let confined = confine_patch_paths(git, git_root, paths)?; let mut existing = Vec::new(); @@ -259,8 +286,7 @@ pub(crate) fn stage_effective_paths( "--".to_string(), ]; args.extend(existing); - let config_parts = safe_git_config_parts(); - let (_code, _, _) = run_git(git, git_root, &config_parts, &args)?; + let (_code, _, _) = run_git(git, git_root, git_config_args, &args)?; // We do not hard fail staging; best-effort is OK. Return Ok even on non-zero. Ok(()) } diff --git a/codex-rs/git-utils/src/patch_paths_tests.rs b/codex-rs/git-utils/src/patch_paths_tests.rs index e805c2d75b..4d6643a97a 100644 --- a/codex-rs/git-utils/src/patch_paths_tests.rs +++ b/codex-rs/git-utils/src/patch_paths_tests.rs @@ -62,13 +62,23 @@ fn read_file_normalized(path: &Path) -> String { fn effective_paths(diff: &str, revert: bool) -> io::Result> { let (tmpdir, patch_path) = write_temp_patch(diff)?; - let cwd = std::env::current_dir()?; - let git = GitRunner::for_cwd_io(&cwd)?; - let paths = extract_effective_paths_from_patch(&git, &patch_path, revert)?; + let repo = init_repo(); + let cwd = repo.path(); + let git = GitRunner::for_cwd_io(cwd)?; + let git_root = + crate::get_git_repo_root(cwd).ok_or_else(|| io::Error::other("not a Git repository"))?; + let git_root = std::fs::canonicalize(git_root)?; + ensure_no_worktree_primary_config_sources(&git, &git_root)?; + let paths = extract_effective_paths_from_patch(&git, &git_root, &patch_path, revert, &[])?; drop(tmpdir); Ok(paths) } +fn best_effort_paths(diff: &str) -> Vec { + let repo = init_repo(); + extract_paths_from_patch_from_cwd(diff, repo.path()) +} + #[test] fn effective_paths_cover_supported_patch_headers() { let cases = [ @@ -127,7 +137,7 @@ fn effective_paths_cover_supported_patch_headers() { "{name}, revert={revert}" ); } - assert_eq!(extract_paths_from_patch(diff), expected, "{name}"); + assert_eq!(best_effort_paths(diff), expected, "{name}"); } let nul_rename_paths = parse_numstat_paths(b"0\t0\t\0old name.txt\0new name.txt\0") @@ -150,7 +160,7 @@ fn effective_paths_follow_git_for_mismatched_headers() { effective_paths(mismatch, /*revert*/ true).unwrap(), expected ); - assert_eq!(extract_paths_from_patch(mismatch), expected); + assert_eq!(best_effort_paths(mismatch), expected); } #[test] diff --git a/codex-rs/git-utils/src/path_authority.rs b/codex-rs/git-utils/src/path_authority.rs index b567df0368..1e2157c378 100644 --- a/codex-rs/git-utils/src/path_authority.rs +++ b/codex-rs/git-utils/src/path_authority.rs @@ -88,7 +88,7 @@ pub(crate) fn repository_route_boundaries( let canonical_root = std::fs::canonicalize(root)?; let canonical_marker = std::fs::canonicalize(&marker)?; if canonical_marker == canonical_root.join(".git") { - push_unique_location(&mut metadata_dirs, canonical_marker)?; + push_unique_location(&mut metadata_dirs, marker)?; } } for common in common_dirs { diff --git a/codex-rs/git-utils/src/repository_authority/authority/policy.rs b/codex-rs/git-utils/src/repository_authority/authority/policy.rs index e77afde247..e5f2379460 100644 --- a/codex-rs/git-utils/src/repository_authority/authority/policy.rs +++ b/codex-rs/git-utils/src/repository_authority/authority/policy.rs @@ -33,6 +33,29 @@ impl RepositoryAuthority { Ok(canonical) } + pub(crate) fn ensure_config_source_is_not_worktree_controlled( + &self, + path: &Path, + description: &str, + ) -> io::Result<()> { + if !path.is_absolute() { + return Err(worktree_controlled_config_source(path, description)); + } + let inspection = self.route_boundaries.inspect_route(path)?; + if inspection.crosses_worktree { + return Err(worktree_controlled_config_source(path, description)); + } + for observed in inspection.observed_paths { + if self.route_boundaries.contains_known_boundary(&observed)? { + continue; + } + if self.has_related_repository_ancestor(&observed)? { + return Err(worktree_controlled_config_source(path, description)); + } + } + Ok(()) + } + pub(super) fn path_is_untrusted_for_executable_result(&self, path: &Path) -> io::Result { let inspection = self.route_boundaries.inspect_route(path)?; if inspection.touches_worktree || inspection.crosses_metadata { @@ -145,3 +168,13 @@ impl RepositoryAuthority { Ok(()) } } + +fn worktree_controlled_config_source(path: &Path, description: &str) -> io::Error { + io::Error::new( + io::ErrorKind::PermissionDenied, + format!( + "refusing to use worktree-controlled {description}: {}", + path.display() + ), + ) +} From 9c2e1342a4a24fca84165c2c8f96cb1ac7391a99 Mon Sep 17 00:00:00 2001 From: Chris Bookholt Date: Fri, 3 Jul 2026 18:34:27 -0700 Subject: [PATCH 10/20] fix(git): authorize included config sources --- codex-rs/git-utils/src/apply.rs | 6 +- codex-rs/git-utils/src/git_command_tests.rs | 40 + codex-rs/git-utils/src/git_config.rs | 236 +++++ codex-rs/git-utils/src/git_config_sources.rs | 95 +- .../src/git_config_sources/include_graph.rs | 120 +++ .../src/git_config_sources/path_safety.rs | 18 + .../src/git_config_sources/primary_sources.rs | 72 ++ .../git-utils/src/git_config_sources_tests.rs | 993 +++++++++++++++++- codex-rs/git-utils/src/git_config_tests.rs | 56 + codex-rs/git-utils/src/patch_paths.rs | 6 +- codex-rs/git-utils/src/patch_paths_tests.rs | 2 +- codex-rs/git-utils/src/path_authority.rs | 2 +- .../src/repository_authority/authority.rs | 39 +- 13 files changed, 1663 insertions(+), 22 deletions(-) create mode 100644 codex-rs/git-utils/src/git_config_sources/include_graph.rs diff --git a/codex-rs/git-utils/src/apply.rs b/codex-rs/git-utils/src/apply.rs index 5ba64abde2..d14d0b2d4c 100644 --- a/codex-rs/git-utils/src/apply.rs +++ b/codex-rs/git-utils/src/apply.rs @@ -13,7 +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_primary_config_sources; +use crate::git_config_sources::ensure_no_worktree_config_sources; use crate::patch_paths::extract_effective_paths_from_patch; use crate::patch_paths::stage_effective_paths; use crate::safe_git::DISABLED_HOOKS_PATH; @@ -48,11 +48,11 @@ pub struct ApplyGitResult { pub fn apply_git_patch(req: &ApplyGitRequest) -> io::Result { let git = GitRunner::for_cwd_io(&req.cwd)?; let mut cfg_parts = configured_git_config_parts(); - ensure_no_worktree_primary_config_sources(&git, &req.cwd)?; + 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_primary_config_sources(&git, &git_root)?; + ensure_no_worktree_config_sources(&git, &git_root, &cfg_parts)?; } // Write unified diff into a temporary file diff --git a/codex-rs/git-utils/src/git_command_tests.rs b/codex-rs/git-utils/src/git_command_tests.rs index 3d191735b3..8f161e12be 100644 --- a/codex-rs/git-utils/src/git_command_tests.rs +++ b/codex-rs/git-utils/src/git_command_tests.rs @@ -1514,6 +1514,46 @@ fn active_commondir_route_is_revalidated_before_every_child() { ); } +#[cfg(any(unix, windows))] +#[test] +fn active_worktree_identity_is_revalidated_before_every_child() { + let fixture = tempfile::tempdir().expect("fixture"); + let root = fixture.path().join("repo"); + let moved_root = fixture.path().join("repo-moved"); + std::fs::create_dir_all(&root).expect("create repository"); + run_git(&root, &["init", "-q"]); + let runner = GitRunner::for_cwd(&root).expect("runner for original repository"); + let sentinel = fixture.path().join("git-child-ran"); + let mut command = runner + .command_for_cwd(&root) + .expect("command for original repository"); + command + .args(["config", "--file"]) + .arg(&sentinel) + .args(["probe.value", "ran"]); + + std::fs::rename(&root, &moved_root).expect("move original repository"); + std::fs::create_dir_all(&root).expect("create replacement repository"); + run_git(&root, &["init", "-q"]); + assert!(moved_root.join(".git").is_dir()); + assert!(root.join(".git").is_dir()); + + let error = runner + .output(command) + .expect_err("same-path repository replacement must block Git child"); + assert_eq!(error.kind(), io::ErrorKind::PermissionDenied, "{error}"); + assert!( + error + .to_string() + .contains("guarded Git repository identity changed before Git launch"), + "{error}" + ); + assert!( + !sentinel.exists(), + "Git child executed after same-path repository replacement" + ); +} + #[cfg(unix)] #[test] fn direct_external_separate_git_dir_absolute_and_relative_routes_execute_git() { diff --git a/codex-rs/git-utils/src/git_config.rs b/codex-rs/git-utils/src/git_config.rs index 737d3c433b..dddab6d652 100644 --- a/codex-rs/git-utils/src/git_config.rs +++ b/codex-rs/git-utils/src/git_config.rs @@ -1,6 +1,9 @@ +use std::io; use std::path::Component; use std::path::Path; +use crate::git_command::GitRunner; + pub(crate) fn parse_git_boolean(value: &[u8]) -> Option { parse_git_boolean_with_minimum(value, i128::from(i32::MIN)) } @@ -79,6 +82,193 @@ fn parse_git_boolean_with_minimum(value: &[u8], minimum: i128) -> Option { .then_some(value != 0) } +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub(crate) enum GitConfigScope { + Unknown, + System, + Global, + Local, + Worktree, + Command, +} + +#[derive(Clone, Debug, Eq, PartialEq)] +pub(crate) struct GitConfigEntry { + pub(crate) scope: GitConfigScope, + pub(crate) origin: GitConfigOrigin, + pub(crate) key: String, + pub(crate) value: String, +} + +#[derive(Clone, Debug, Eq, PartialEq)] +pub(crate) enum GitConfigOrigin { + CommandLine, + File(std::path::PathBuf), +} + +pub(crate) fn parse_config_entries(output: &[u8]) -> io::Result> { + if output.is_empty() { + return Ok(Vec::new()); + } + let Some(body) = output.strip_suffix(&[0]) else { + return Err(invalid_config_output("unterminated Git config output")); + }; + let fields = body.split(|byte| *byte == 0).collect::>(); + if fields.len() % 3 != 0 { + return Err(invalid_config_output("incomplete Git config record")); + } + + let mut entries = Vec::new(); + for record in fields.chunks_exact(3) { + let scope = parse_scope(record[0])?; + let origin = parse_config_origin(record[1])?; + let entry = parse_utf8_field(record[2], "config key/value")?; + let Some((key, value)) = entry.split_once('\n') else { + return Err(invalid_config_output( + "Git config record has no key/value separator", + )); + }; + if key.is_empty() { + return Err(invalid_config_output("empty Git config key")); + } + let entry = GitConfigEntry { + scope, + origin, + key: key.to_string(), + value: value.to_string(), + }; + entries.push(entry); + } + Ok(entries) +} + +/// Parse the `--show-origin` form used by Git versions that predate +/// `config --show-scope`. The record order still reflects effective config +/// precedence, so duplicate include directives remain ordered. Scope is +/// retained as best-effort metadata only; command-line entries remain +/// distinguishable. +pub(crate) fn parse_config_entries_with_origins(output: &[u8]) -> io::Result> { + if output.is_empty() { + return Ok(Vec::new()); + } + let Some(body) = output.strip_suffix(&[0]) else { + return Err(invalid_config_output("unterminated Git config output")); + }; + let fields = body.split(|byte| *byte == 0).collect::>(); + if fields.len() % 2 != 0 { + return Err(invalid_config_output("incomplete Git config record")); + } + + let mut entries = Vec::new(); + for record in fields.chunks_exact(2) { + let origin = parse_config_origin(record[0])?; + let entry = parse_utf8_field(record[1], "config key/value")?; + let Some((key, value)) = entry.split_once('\n') else { + return Err(invalid_config_output( + "Git config record has no key/value separator", + )); + }; + if key.is_empty() { + return Err(invalid_config_output("empty Git config key")); + } + let scope = if origin == GitConfigOrigin::CommandLine { + GitConfigScope::Command + } else { + // Older Git does not expose the scope. Selection depends on the + // emitted precedence order, not on this informational label. + GitConfigScope::Local + }; + entries.push(GitConfigEntry { + scope, + origin, + key: key.to_string(), + value: value.to_string(), + }); + } + Ok(entries) +} + +pub(crate) fn read_config_entries_without_includes( + git: &GitRunner, + cwd: &Path, + git_config_args: &[String], + pattern: &str, + probe: &str, + config_file: Option<&Path>, +) -> io::Result> { + read_config_entries_with_fallback(git, cwd, git_config_args, pattern, probe, config_file) +} + +fn read_config_entries_with_fallback( + git: &GitRunner, + cwd: &Path, + git_config_args: &[String], + pattern: &str, + probe: &str, + config_file: Option<&Path>, +) -> io::Result> { + let scoped = run_effective_config_query( + git, + cwd, + git_config_args, + pattern, + /*show_scope*/ true, + config_file, + )?; + if scoped + .status + .code() + .is_some_and(|code| code == 0 || code == 1) + { + return parse_config_entries(&scoped.stdout); + } + + let legacy = run_effective_config_query( + git, + cwd, + git_config_args, + pattern, + /*show_scope*/ false, + config_file, + )?; + if !legacy + .status + .code() + .is_some_and(|code| code == 0 || code == 1) + { + return Err(io::Error::other(format!( + "git {probe} config probe failed with status {}: {}", + legacy.status, + String::from_utf8_lossy(&legacy.stderr).trim() + ))); + } + parse_config_entries_with_origins(&legacy.stdout) +} + +fn run_effective_config_query( + git: &GitRunner, + cwd: &Path, + git_config_args: &[String], + pattern: &str, + show_scope: bool, + config_file: Option<&Path>, +) -> io::Result { + let mut command = git.command_for_cwd(cwd)?; + command + .env("GIT_OPTIONAL_LOCKS", "0") + .args(git_config_args) + .arg("config"); + if let Some(config_file) = config_file { + command.arg("--file").arg(config_file); + } + command.arg("--null"); + if show_scope { + command.arg("--show-scope"); + } + command.args(["--show-origin", "--no-includes", "--get-regexp", pattern]); + git.output(command) +} + pub(crate) fn path_is_within(path: &Path, root: &Path) -> bool { let mut path_components = path.components(); for root_component in root.components() { @@ -104,6 +294,52 @@ fn components_equal(left: Component<'_>, right: Component<'_>) -> bool { left == right } +fn parse_scope(scope: &[u8]) -> io::Result { + match scope { + b"unknown" => Ok(GitConfigScope::Unknown), + b"system" => Ok(GitConfigScope::System), + b"global" => Ok(GitConfigScope::Global), + b"local" => Ok(GitConfigScope::Local), + b"worktree" => Ok(GitConfigScope::Worktree), + b"command" => Ok(GitConfigScope::Command), + _ => Err(invalid_config_output("unknown Git config scope")), + } +} + +fn parse_config_origin(origin: &[u8]) -> io::Result { + if origin == b"command line:" { + return Ok(GitConfigOrigin::CommandLine); + } + let path = origin + .strip_prefix(b"file:") + .ok_or_else(|| invalid_config_output("unsupported Git config origin"))?; + if path.is_empty() || path.contains(&0) { + return Err(invalid_config_output("empty Git config origin")); + } + #[cfg(unix)] + { + use std::os::unix::ffi::OsStringExt; + + Ok(GitConfigOrigin::File(std::path::PathBuf::from( + std::ffi::OsString::from_vec(path.to_vec()), + ))) + } + #[cfg(not(unix))] + { + Ok(GitConfigOrigin::File(std::path::PathBuf::from( + parse_utf8_field(path, "Git config origin path")?, + ))) + } +} + +fn parse_utf8_field<'a>(field: &'a [u8], name: &str) -> io::Result<&'a str> { + std::str::from_utf8(field).map_err(|_| invalid_config_output(&format!("non-UTF-8 {name}"))) +} + +fn invalid_config_output(message: &str) -> io::Error { + io::Error::new(io::ErrorKind::InvalidData, message) +} + #[cfg(test)] #[path = "git_config_tests.rs"] mod tests; diff --git a/codex-rs/git-utils/src/git_config_sources.rs b/codex-rs/git-utils/src/git_config_sources.rs index b539ef266c..bcfec7bdf1 100644 --- a/codex-rs/git-utils/src/git_config_sources.rs +++ b/codex-rs/git-utils/src/git_config_sources.rs @@ -1,30 +1,41 @@ +use std::collections::BTreeSet; use std::io; use std::path::Path; +#[cfg(test)] +use std::path::PathBuf; use crate::git_command::GitRunner; +use crate::git_config::read_config_entries_without_includes; +mod include_graph; mod path_safety; mod primary_sources; +use include_graph::validate_include_entries; use path_safety::normalize_absolute_path; use path_safety::resolve_literal_path; +use primary_sources::default_system_config_source_candidates; use primary_sources::is_disabled_primary_config_path; use primary_sources::legacy_primary_config_source_candidates; use primary_sources::selected_git_home_config_candidates; use primary_sources::selected_git_prefix_system_candidate; -/// Reject primary Git configuration paths selected from worktree-controlled -/// routes before any ordinary Git command can open them. -/// -/// This layer intentionally does not authorize includes. The complete include -/// graph is added by the next stack layer before this is promoted to a full -/// config-source capability. -pub(crate) fn ensure_no_worktree_primary_config_sources( +const INCLUDE_CONFIG_PATTERN: &str = r"^include(\.path|if\..*\.path)$"; +const MAX_CONFIG_INCLUDE_DEPTH: usize = 10; +const MAX_CONFIG_INCLUDE_FILES: usize = 1024; + +/// Reject configuration that an untrusted worktree writer can change between +/// a policy probe and the Git command it guards. +pub(crate) fn ensure_no_worktree_config_sources( git: &GitRunner, git_root: &Path, + git_config_args: &[String], ) -> io::Result<()> { let git_root = normalize_absolute_path(std::fs::canonicalize(git_root)?)?; + // Environment, HOME, and XDG paths are attacker-controlled byte strings. + // Classify their exact OsString spelling before `git var` can open or + // newline-delimit one of them. for (description, candidate) in legacy_primary_config_source_candidates(git)? { if !is_disabled_primary_config_path(&candidate) { reject_source(git, &git_root, &candidate, description)?; @@ -41,6 +52,65 @@ pub(crate) fn ensure_no_worktree_primary_config_sources( "selected Git prefix system config", )?; } + let entries = read_config_entries_without_includes( + git, + &git_root, + git_config_args, + INCLUDE_CONFIG_PATTERN, + "include", + /*config_file*/ None, + )?; + let mut pending = Vec::new(); + validate_include_entries(git, &git_root, entries, /*depth*/ 1, &mut pending)?; + let mut visited = BTreeSet::new(); + while let Some((config_path, depth)) = pending.pop() { + if depth > MAX_CONFIG_INCLUDE_DEPTH { + return Err(path_safety::invalid_config_source( + "Git config include depth exceeded", + )); + } + match std::fs::canonicalize(&config_path) { + Ok(_) => {} + Err(error) + if matches!( + error.kind(), + io::ErrorKind::NotFound | io::ErrorKind::NotADirectory + ) => + { + continue; + } + Err(error) => return Err(error), + } + // The same file reached through two spellings can resolve a relative + // child include differently. Deduplicate only the exact source + // spelling, not its canonical target. + if !visited.insert(config_path.clone()) { + continue; + } + if visited.len() > MAX_CONFIG_INCLUDE_FILES { + return Err(path_safety::invalid_config_source( + "too many Git config include files", + )); + } + let entries = read_config_entries_without_includes( + git, + &git_root, + &[], + INCLUDE_CONFIG_PATTERN, + "include", + Some(&config_path), + )?; + validate_include_entries(git, &git_root, entries, depth + 1, &mut pending)?; + } + // `git var` loads repository config before reporting modern default-system + // paths. Delay that supplemental probe until the complete no-includes + // source graph above has been classified, so it cannot be tricked into + // opening an untrusted include or FIFO first. + for (description, candidate) in default_system_config_source_candidates(git, &git_root)? { + if !is_disabled_primary_config_path(&candidate) { + reject_source(git, &git_root, &candidate, description)?; + } + } Ok(()) } @@ -54,6 +124,17 @@ fn reject_source( git.ensure_config_source_is_not_worktree_controlled(&absolute, description) } +#[cfg(all(test, not(windows)))] +use crate::git_config::GitConfigEntry; +#[cfg(all(test, not(windows)))] +use codex_utils_absolute_path::AbsolutePathBuf; +#[cfg(test)] +use include_graph::expand_git_config_path; +#[cfg(all(test, not(windows)))] +use include_graph::resolve_include_path; +#[cfg(test)] +use path_safety::windows_config_path_is_ambiguous; + #[cfg(test)] #[path = "git_config_sources_tests.rs"] mod tests; diff --git a/codex-rs/git-utils/src/git_config_sources/include_graph.rs b/codex-rs/git-utils/src/git_config_sources/include_graph.rs new file mode 100644 index 0000000000..da267505f5 --- /dev/null +++ b/codex-rs/git-utils/src/git_config_sources/include_graph.rs @@ -0,0 +1,120 @@ +use std::io; +use std::path::Path; +use std::path::PathBuf; + +use super::path_safety::CONFIG_PATH_KEY; +use super::path_safety::invalid_config_source; +use super::path_safety::reject_raw_ambiguous_windows_config_path; +use super::path_safety::resolve_literal_path; +use super::reject_source; +use crate::git_command::GitRunner; +use crate::git_config::GitConfigEntry; +use crate::git_config::GitConfigOrigin; + +pub(super) fn validate_include_entries( + git: &GitRunner, + git_root: &Path, + entries: Vec, + depth: usize, + pending: &mut Vec<(PathBuf, usize)>, +) -> io::Result<()> { + for entry in entries { + if !is_include_path(&entry.key) { + return Err(invalid_config_source("unexpected Git config include key")); + } + if let Some(origin) = config_file_origin(&entry, git_root)? { + reject_source(git, git_root, &origin, "Git config origin")?; + } + let include = resolve_include_path(git, git_root, &entry)?; + reject_source(git, git_root, &include, "Git config include")?; + pending.push((include, depth)); + } + Ok(()) +} + +fn config_file_origin(entry: &GitConfigEntry, cwd: &Path) -> io::Result> { + match &entry.origin { + GitConfigOrigin::CommandLine => Ok(None), + GitConfigOrigin::File(path) => Ok(Some(resolve_literal_path(path, cwd))), + } +} + +fn is_include_path(key: &str) -> bool { + key == "include.path" || key.starts_with("includeif.") && key.ends_with(".path") +} + +pub(super) fn resolve_include_path( + git: &GitRunner, + cwd: &Path, + entry: &GitConfigEntry, +) -> io::Result { + let raw = entry.value.as_str(); + if raw.is_empty() { + return Err(invalid_config_source("empty Git config include path")); + } + // Unlike generic `git config --path`, include.path treats `:(...)` as + // literal path text. Bypass the generic path expander for those spellings + // so the validated path exactly matches Git's include loader. + let expanded = if raw.starts_with(":(") { + PathBuf::from(raw) + } else { + expand_git_config_path(git, cwd, raw)? + }; + reject_raw_ambiguous_windows_config_path( + expanded + .to_str() + .ok_or_else(|| invalid_config_source("non-UTF-8 Git include path"))?, + )?; + let base = match config_file_origin(entry, cwd)? { + Some(origin) => origin + .parent() + .ok_or_else(|| invalid_config_source("Git config origin has no parent"))? + .to_path_buf(), + None if expanded.is_absolute() => cwd.to_path_buf(), + None => { + return Err(invalid_config_source( + "relative Git config include has no file origin", + )); + } + }; + Ok(resolve_literal_path(expanded, &base)) +} + +pub(super) fn expand_git_config_path( + git: &GitRunner, + cwd: &Path, + raw: &str, +) -> io::Result { + let mut command = git.command_for_cwd(cwd)?; + command + .arg("-c") + .arg(format!("{CONFIG_PATH_KEY}={raw}")) + .args([ + "config", + "--null", + "--no-includes", + "--path", + "--get", + CONFIG_PATH_KEY, + ]); + let output = git.output(command)?; + if !output.status.success() { + return Err(io::Error::other(format!( + "git include path expansion failed with status {}: {}", + output.status, + String::from_utf8_lossy(&output.stderr).trim() + ))); + } + let value = output + .stdout + .strip_suffix(&[0]) + .ok_or_else(|| invalid_config_source("unterminated Git include path expansion"))?; + if value.is_empty() || value.contains(&0) { + return Err(invalid_config_source( + "ambiguous Git include path expansion", + )); + } + let value = std::str::from_utf8(value) + .map_err(|_| invalid_config_source("non-UTF-8 Git include path expansion"))?; + Ok(PathBuf::from(value)) +} diff --git a/codex-rs/git-utils/src/git_config_sources/path_safety.rs b/codex-rs/git-utils/src/git_config_sources/path_safety.rs index a2d86fd062..42509affdf 100644 --- a/codex-rs/git-utils/src/git_config_sources/path_safety.rs +++ b/codex-rs/git-utils/src/git_config_sources/path_safety.rs @@ -33,6 +33,24 @@ pub(super) fn normalize_absolute_path(path: impl AsRef) -> io::Result io::Result<()> { + if windows_config_path_is_ambiguous(path) { + return Err(invalid_config_source("ambiguous Windows Git config path")); + } + Ok(()) +} + +#[cfg(not(windows))] +pub(super) fn reject_raw_ambiguous_windows_config_path(_path: &str) -> io::Result<()> { + Ok(()) +} + +#[cfg(any(windows, test))] +pub(super) fn windows_config_path_is_ambiguous(path: &str) -> bool { + crate::path_authority::windows_path_is_ambiguous(path) +} + pub(super) fn invalid_config_source(message: &str) -> io::Error { io::Error::new(io::ErrorKind::InvalidData, message) } diff --git a/codex-rs/git-utils/src/git_config_sources/primary_sources.rs b/codex-rs/git-utils/src/git_config_sources/primary_sources.rs index 1c5e835935..4ee758ce87 100644 --- a/codex-rs/git-utils/src/git_config_sources/primary_sources.rs +++ b/codex-rs/git-utils/src/git_config_sources/primary_sources.rs @@ -9,6 +9,30 @@ use super::path_safety::invalid_config_source; use crate::git_command::GitRunner; use crate::git_config::parse_git_boolean_symmetric_i32; +pub(super) fn default_system_config_source_candidates( + git: &GitRunner, + cwd: &Path, +) -> io::Result> { + if git_env_bool(git, "GIT_CONFIG_NOSYSTEM")? + || git.config_environment_value("GIT_CONFIG_SYSTEM").is_some() + { + return Ok(Vec::new()); + } + // `GIT_CONFIG_SYSTEM` was added to `git var` in Git 2.42. The PSEC-4394 + // boundary treats the selected Git installation and its non-environment + // compile-time system config as host-owned trusted inputs. For older Git, + // the exact custom ETC_GITCONFIG path is therefore a documented residual; + // the derivable prefix/ProgramData paths are still checked separately and + // the no-includes graph validates every directive the system file exposes. + let Some(paths) = git_var_config_paths(git, cwd, "GIT_CONFIG_SYSTEM")? else { + return Ok(Vec::new()); + }; + Ok(paths + .into_iter() + .map(|path| ("GIT_CONFIG_SYSTEM", path)) + .collect()) +} + pub(super) fn selected_git_home_config_candidates( git: &GitRunner, cwd: &Path, @@ -86,6 +110,54 @@ fn selected_git_path_candidate(git: &GitRunner, cwd: &Path, raw: &str) -> io::Re git_var_path_from_bytes(value) } +fn git_var_config_paths( + git: &GitRunner, + cwd: &Path, + variable: &str, +) -> io::Result>> { + let mut command = git.command_for_cwd(cwd)?; + command + .env("GIT_OPTIONAL_LOCKS", "0") + .args(["var", variable]); + let output = git.output(command)?; + parse_git_var_config_paths_result( + output.status.code(), + &output.stdout, + &output.stderr, + variable, + ) +} + +pub(super) fn parse_git_var_config_paths_result( + status_code: Option, + stdout: &[u8], + stderr: &[u8], + variable: &str, +) -> io::Result>> { + match status_code { + Some(0) => parse_git_var_paths(stdout).map(Some), + Some(1) if stdout.is_empty() && stderr.is_empty() => Ok(Some(Vec::new())), + // These variables were added in Git 2.42. Older Git reports usage + // status 129; fall back to its documented environment/home sources. + Some(129) => Ok(None), + _ => Err(io::Error::other(format!( + "git {variable} source probe failed with status {status_code:?}: {}", + String::from_utf8_lossy(stderr).trim() + ))), + } +} + +fn parse_git_var_paths(output: &[u8]) -> io::Result> { + output + .split(|byte| *byte == b'\n') + .filter(|path| !path.is_empty()) + .map(|path| { + let path = path.strip_suffix(b"\r").unwrap_or(path); + git_var_path_from_bytes(path) + }) + .collect() +} + pub(super) fn legacy_primary_config_source_candidates( git: &GitRunner, ) -> io::Result> { diff --git a/codex-rs/git-utils/src/git_config_sources_tests.rs b/codex-rs/git-utils/src/git_config_sources_tests.rs index 222e9a614f..e44696f883 100644 --- a/codex-rs/git-utils/src/git_config_sources_tests.rs +++ b/codex-rs/git-utils/src/git_config_sources_tests.rs @@ -1,7 +1,6 @@ use super::*; use pretty_assertions::assert_eq; use std::ffi::OsStr; -use std::path::PathBuf; fn run(cwd: &Path, args: &[&str]) -> (i32, String, String) { let mut command = std::process::Command::new("git"); @@ -63,7 +62,11 @@ fn create_junction(path: &Path, target: &Path) { fn guard(root: &Path) -> io::Result<()> { let git = GitRunner::for_cwd_io(root)?; - ensure_no_worktree_primary_config_sources(&git, root) + ensure_no_worktree_config_sources(&git, root, &[]) +} + +fn add_include(root: &Path, key: &str, value: &str) { + run_success(root, &["config", "--add", key, value]); } fn assert_worktree_rejection(error: io::Error) { @@ -95,6 +98,45 @@ fn run_isolated_source_test(test_name: &str, env: &[(&str, &OsStr)], removed: &[ ); } +#[test] +fn allows_protected_metadata_and_external_config_sources() { + let repo = init_repo(); + let root = repo.path(); + guard(root).expect("ordinary metadata config"); + + std::fs::write( + root.join(".git/protected.gitconfig"), + "[user]\nname = protected\n", + ) + .expect("write protected config"); + add_include(root, "include.path", "protected.gitconfig"); + guard(root).expect("protected metadata include"); + std::fs::create_dir(root.join(".git/subdir")).expect("create metadata subdirectory"); + add_include(root, "include.path", "subdir/../protected.gitconfig"); + guard(root).expect("metadata-local parent traversal"); + + let external = tempfile::tempdir().expect("external config directory"); + let external_config = external.path().join("safe.gitconfig"); + std::fs::write(&external_config, "[user]\nemail = safe@example.com\n") + .expect("write external config"); + add_include( + root, + "include.path", + external_config.to_str().expect("UTF-8 external path"), + ); + guard(root).expect("external include"); + add_include( + root, + "include.path", + external + .path() + .join("missing.gitconfig") + .to_str() + .expect("UTF-8 missing external path"), + ); + guard(root).expect("missing external include"); +} + #[test] fn rejects_empty_primary_global_and_system_sources_in_the_worktree() { const TEST_NAME: &str = "git_config_sources::tests::rejects_empty_primary_global_and_system_sources_in_the_worktree"; @@ -210,6 +252,20 @@ fn allows_unset_home_when_git_has_no_global_source() { guard(&root).expect("Git without HOME or global config"); } +#[test] +fn supports_pre_242_git_without_system_var_enumeration() { + assert_eq!( + primary_sources::parse_git_var_config_paths_result( + Some(129), + b"", + b"usage: git var (-l | )\n", + "GIT_CONFIG_SYSTEM", + ) + .expect("pre-2.42 fallback"), + None + ); +} + #[cfg(unix)] #[test] fn rejects_newline_bearing_raw_global_source_before_git_normalizes_it() { @@ -269,6 +325,51 @@ fn ignores_non_utf8_values_for_unrelated_global_keys() { guard(&root).expect("unrelated non-UTF-8 global value"); } +#[cfg(all(unix, not(target_os = "macos")))] +#[test] +fn preserves_non_utf8_unix_repository_and_config_origin_paths() { + use std::os::unix::ffi::OsStringExt; + + const TEST_NAME: &str = + "git_config_sources::tests::preserves_non_utf8_unix_repository_and_config_origin_paths"; + if std::env::var_os("CODEX_GIT_CONFIG_SOURCE_CHILD").is_none() { + let parent = tempfile::tempdir().expect("non-UTF-8 fixture parent"); + let root = parent + .path() + .join(std::ffi::OsString::from_vec(b"repo-\xff".to_vec())); + init_repo_at(&root); + let safe = tempfile::NamedTempFile::new().expect("safe included config"); + add_include( + &root, + "include.path", + safe.path().to_str().expect("UTF-8 safe include"), + ); + let global = parent + .path() + .join(std::ffi::OsString::from_vec(b"global-\xff".to_vec())); + std::fs::write( + &global, + format!("[include]\npath = {}\n", safe.path().display()), + ) + .expect("write non-UTF-8 global path"); + run_isolated_source_test( + TEST_NAME, + &[ + ("CODEX_GIT_CONFIG_SOURCE_ROOT", root.as_os_str()), + ("GIT_CONFIG_GLOBAL", global.as_os_str()), + ("GIT_CONFIG_NOSYSTEM", OsStr::new("1")), + ], + &["GIT_CONFIG_SYSTEM"], + ); + return; + } + + let root = PathBuf::from( + std::env::var_os("CODEX_GIT_CONFIG_SOURCE_ROOT").expect("fixture repository root"), + ); + guard(&root).expect("non-UTF-8 repository and config origins"); +} + #[cfg(unix)] #[test] fn rejects_worktree_fifo_primary_source_without_opening_it() { @@ -609,3 +710,891 @@ fn windows_appdata_global_config_matches_native_git_selection_and_precedence() { expected => panic!("unexpected child outcome {expected}"), } } + +#[test] +fn rejects_empty_nonempty_absolute_missing_and_conditional_worktree_includes() { + for (name, body, include_key, include_value) in [ + ( + "empty", + "", + "include.path".to_string(), + "../driver-config".to_string(), + ), + ( + "nonempty", + "[user]\nname = worktree\n", + "include.path".to_string(), + "../driver-config".to_string(), + ), + ( + "missing", + "", + "include.path".to_string(), + "../future.gitconfig".to_string(), + ), + ( + "inactive includeIf", + "", + "includeIf.gitdir:/definitely/not/this/repository/**.path".to_string(), + "../driver-config".to_string(), + ), + ( + "case-insensitive gitdir includeIf", + "", + "includeIf.gitdir/i:/definitely/not/this/repository/**.path".to_string(), + "../driver-config".to_string(), + ), + ( + "onbranch includeIf", + "", + "includeIf.onbranch:definitely-never/**.path".to_string(), + "../driver-config".to_string(), + ), + ( + "hasconfig includeIf", + "", + "includeIf.hasconfig:remote.*.url:https://never.example/**.path".to_string(), + "../driver-config".to_string(), + ), + ] { + let repo = init_repo(); + let root = repo.path(); + if name != "missing" { + std::fs::write(root.join("driver-config"), body).expect("write config fixture"); + } + add_include(root, &include_key, &include_value); + assert_worktree_rejection(guard(root).expect_err(name)); + } + + let repo = init_repo(); + let root = repo.path(); + let config = root.join("absolute.gitconfig"); + std::fs::write(&config, "").expect("write absolute config"); + add_include( + root, + "include.path", + config.to_str().expect("UTF-8 config path"), + ); + assert_worktree_rejection(guard(root).expect_err("absolute include")); + + let repo = init_repo(); + let root = repo.path(); + std::fs::write(root.join("driver-config"), "").expect("write conditional config"); + let git_dir = run_success(root, &["rev-parse", "--absolute-git-dir"]).replace('\\', "/"); + add_include( + root, + &format!("includeIf.gitdir:{git_dir}/.path"), + "../driver-config", + ); + assert_worktree_rejection(guard(root).expect_err("active includeIf")); +} + +#[test] +fn command_scoped_include_paths_follow_the_same_boundary() { + let repo = init_repo(); + let root = repo.path(); + let git = GitRunner::for_cwd_io(root).expect("Git runner"); + let unsafe_config = root.join("command.gitconfig"); + std::fs::write(&unsafe_config, "").expect("write worktree config"); + let unsafe_args = vec![ + "-c".to_string(), + format!("include.path={}", unsafe_config.display()), + ]; + assert_worktree_rejection( + ensure_no_worktree_config_sources(&git, root, &unsafe_args) + .expect_err("command-scoped worktree include"), + ); + + let external = tempfile::NamedTempFile::new().expect("safe external config"); + let safe_args = vec![ + "-c".to_string(), + format!("include.path={}", external.path().display()), + ]; + ensure_no_worktree_config_sources(&git, root, &safe_args) + .expect("command-scoped external include"); + + let relative_args = vec![ + "-c".to_string(), + "include.path=relative.gitconfig".to_string(), + ]; + assert!( + ensure_no_worktree_config_sources(&git, root, &relative_args).is_err(), + "relative command includes must fail closed" + ); +} + +#[test] +fn rejects_every_duplicate_and_nested_include_target() { + for unsafe_first in [true, false] { + let repo = init_repo(); + let root = repo.path(); + std::fs::write(root.join("driver-config"), "").expect("write unsafe config"); + let external = tempfile::NamedTempFile::new().expect("safe external config"); + let safe = external.path().to_str().expect("UTF-8 safe path"); + let values = if unsafe_first { + ["../driver-config", safe] + } else { + [safe, "../driver-config"] + }; + for value in values { + add_include(root, "include.path", value); + } + assert_worktree_rejection(guard(root).expect_err("duplicate include")); + } + + let repo = init_repo(); + let root = repo.path(); + std::fs::write(root.join("driver-config"), "").expect("write nested target"); + let external = tempfile::tempdir().expect("external config directory"); + let outer = external.path().join("outer.gitconfig"); + let nested = root.join("driver-config"); + run_success( + root, + &[ + "config", + "--file", + outer.to_str().expect("UTF-8 outer path"), + "include.path", + nested.to_str().expect("UTF-8 nested path"), + ], + ); + add_include( + root, + "include.path", + outer.to_str().expect("UTF-8 outer path"), + ); + assert_worktree_rejection(guard(root).expect_err("nested include")); +} + +#[cfg(unix)] +#[test] +fn rejects_symlink_aliases_across_the_worktree_and_metadata_boundaries() { + use std::os::unix::fs::symlink; + + let repo = init_repo(); + let root = repo.path(); + let external = tempfile::tempdir().expect("external directory"); + std::fs::write(external.path().join("safe.gitconfig"), "").expect("write safe config"); + symlink(external.path(), root.join("inside-link")).expect("inside to outside link"); + add_include(root, "include.path", "../inside-link/safe.gitconfig"); + assert_worktree_rejection(guard(root).expect_err("worktree symlink alias")); + + let repo = init_repo(); + let root = repo.path(); + std::fs::write(root.join("driver-config"), "").expect("write worktree target"); + let external = tempfile::tempdir().expect("external directory"); + symlink(root, external.path().join("outside-link")).expect("outside to inside link"); + add_include( + root, + "include.path", + external + .path() + .join("outside-link/driver-config") + .to_str() + .expect("UTF-8 alias path"), + ); + assert_worktree_rejection(guard(root).expect_err("external symlink alias")); + + let repo = init_repo(); + let root = repo.path(); + std::fs::write(root.join("driver-config"), "").expect("write escaped target"); + symlink(root, root.join(".git/config-link")).expect("metadata escape link"); + add_include(root, "include.path", "config-link/driver-config"); + assert_worktree_rejection(guard(root).expect_err("metadata symlink escape")); + + let repo = init_repo(); + let root = repo.path(); + let external = tempfile::tempdir().expect("external directory"); + symlink( + root.join("future-config"), + external.path().join("dangling-link"), + ) + .expect("dangling link into worktree"); + add_include( + root, + "include.path", + external + .path() + .join("dangling-link/config") + .to_str() + .expect("UTF-8 dangling alias"), + ); + assert_worktree_rejection(guard(root).expect_err("dangling symlink alias")); + + let repo = init_repo(); + let root = repo.path(); + let entry = tempfile::tempdir().expect("external entry directory"); + let exit = tempfile::tempdir().expect("external exit directory"); + std::fs::write(exit.path().join("safe.gitconfig"), "").expect("write exit config"); + symlink(root, entry.path().join("through-worktree")).expect("link into worktree"); + symlink(exit.path(), root.join("back-out")).expect("worktree-controlled exit link"); + add_include( + root, + "include.path", + entry + .path() + .join("through-worktree/back-out/safe.gitconfig") + .to_str() + .expect("UTF-8 nested alias"), + ); + assert_worktree_rejection(guard(root).expect_err("nested symlink traversal")); + + let repo = init_repo(); + let root = repo.path(); + let entry = tempfile::tempdir().expect("external entry directory"); + let exit = tempfile::tempdir().expect("external exit directory"); + std::fs::write(exit.path().join("safe.gitconfig"), "").expect("write chained config"); + symlink(exit.path(), root.join("workspace-link")).expect("workspace link out"); + symlink( + root.join("workspace-link"), + entry.path().join("direct-chain"), + ) + .expect("external link to workspace link"); + add_include( + root, + "include.path", + entry + .path() + .join("direct-chain/safe.gitconfig") + .to_str() + .expect("UTF-8 direct chain"), + ); + assert_worktree_rejection(guard(root).expect_err("direct symlink chain")); + + let repo = init_repo(); + let root = repo.path(); + std::fs::create_dir(root.join("subdir")).expect("create symlink target"); + std::fs::write(root.join("evil.gitconfig"), "[user]\nname = worktree\n") + .expect("write worktree config"); + std::fs::write( + root.join(".git/evil.gitconfig"), + "[user]\nname = metadata\n", + ) + .expect("write metadata decoy"); + symlink(root.join("subdir"), root.join(".git/link")).expect("metadata link to worktree"); + add_include(root, "include.path", "link/../evil.gitconfig"); + assert_worktree_rejection(guard(root).expect_err("symlink parent traversal")); + + let repo = init_repo(); + let root = repo.path(); + std::fs::create_dir(root.join("pivot")).expect("create worktree pivot"); + std::fs::write(root.join(".git/protected.gitconfig"), "") + .expect("write protected metadata config"); + add_include( + root, + "include.path", + root.join("pivot/../.git/protected.gitconfig") + .to_str() + .expect("UTF-8 pivot path"), + ); + assert_worktree_rejection(guard(root).expect_err("raw path pivots through worktree")); +} + +#[cfg(unix)] +#[test] +fn rejects_external_symlink_target_with_hidden_worktree_pivot_to_protected_config() { + use std::os::unix::fs::symlink; + + let repo = init_repo(); + let root = std::fs::canonicalize(repo.path()).expect("canonical repository"); + let external = tempfile::tempdir().expect("external alias directory"); + let protected = root.join(".git/protected.gitconfig"); + std::fs::write(&protected, "[codex]\n\tpivot = protected\n").expect("write protected config"); + std::fs::create_dir(root.join("pivot")).expect("create ordinary worktree pivot"); + let entry = external.path().join("entry"); + symlink(root.join("pivot/../.git/protected.gitconfig"), &entry) + .expect("external symlink target with raw worktree pivot"); + add_include( + &root, + "include.path", + entry.to_str().expect("UTF-8 entry path"), + ); + assert_eq!( + run_success(&root, &["config", "--includes", "--get", "codex.pivot"]), + "protected" + ); + assert_worktree_rejection( + guard(&root).expect_err("hidden pivot inside external symlink target"), + ); +} + +#[cfg(target_os = "macos")] +#[test] +fn rejects_apfs_alias_symlink_target_with_hidden_worktree_pivot() { + use std::os::unix::fs::symlink; + + let temp = std::fs::canonicalize(std::env::temp_dir()).expect("canonical temp directory"); + let parent = tempfile::tempdir_in(temp).expect("Data-volume fixture"); + let root = parent.path().join("repo"); + init_repo_at(&root); + let root = std::fs::canonicalize(root).expect("canonical repository"); + let data_alias = PathBuf::from("/System/Volumes/Data") + .join(root.strip_prefix("/").expect("absolute repository path")); + if std::fs::metadata(&data_alias).is_err() { + eprintln!("APFS Data alias unavailable; skipping native pivot assertion"); + return; + } + let protected = root.join(".git/protected.gitconfig"); + std::fs::write(&protected, "[codex]\n\tpivot = protected\n").expect("write protected config"); + std::fs::create_dir(root.join("pivot")).expect("create ordinary worktree pivot"); + let external = tempfile::tempdir().expect("external alias directory"); + let entry = external.path().join("entry"); + symlink(data_alias.join("pivot/../.git/protected.gitconfig"), &entry) + .expect("APFS alias target with raw worktree pivot"); + add_include( + &root, + "include.path", + entry.to_str().expect("UTF-8 entry path"), + ); + assert_eq!( + run_success(&root, &["config", "--includes", "--get", "codex.pivot"]), + "protected" + ); + assert_worktree_rejection(guard(&root).expect_err("APFS hidden pivot inside symlink target")); +} + +#[cfg(unix)] +#[test] +fn rejects_unregistered_same_common_worktree_used_only_as_alias_intermediate() { + use std::os::unix::fs::symlink; + + let parent = tempfile::tempdir().expect("fixture"); + let main = parent.path().join("main"); + let linked = parent.path().join("linked"); + let stale = parent.path().join("stale-unregistered"); + let external = parent.path().join("external"); + let safe = external.join("safe.gitconfig"); + let entry = external.join("entry"); + init_repo_at(&main); + run_success( + &main, + &[ + "worktree", + "add", + "-b", + "same-common-active", + linked.to_str().expect("UTF-8 linked path"), + ], + ); + std::fs::create_dir_all(&stale).expect("create stale worktree"); + std::fs::write( + stale.join(".git"), + format!("gitdir: {}\n", main.join(".git").display()), + ) + .expect("write stale same-common marker"); + std::fs::create_dir_all(&external).expect("create external directory"); + std::fs::write(&safe, "[codex]\n\tsameCommon = protected\n") + .expect("write external safe config"); + symlink(&safe, stale.join("switch")).expect("stale worktree controlled switch"); + symlink(stale.join("switch"), &entry).expect("external entry through stale worktree"); + add_include( + &linked, + "include.path", + entry.to_str().expect("UTF-8 entry path"), + ); + assert_eq!( + run_success( + &linked, + &["config", "--includes", "--get", "codex.sameCommon"] + ), + "protected" + ); + assert!( + guard(&linked).is_err(), + "unregistered same-common route intermediate must fail closed" + ); +} + +#[cfg(windows)] +#[test] +fn rejects_windows_junction_aliases_across_worktree_and_metadata_boundaries() { + let repo = init_repo(); + let root = repo.path(); + std::fs::write(root.join("driver-config"), "").expect("write worktree config"); + let external = tempfile::tempdir().expect("external directory"); + create_junction(&external.path().join("into-worktree"), root); + add_include( + root, + "include.path", + external + .path() + .join("into-worktree/driver-config") + .to_str() + .expect("UTF-8 junction path"), + ); + assert_worktree_rejection(guard(root).expect_err("external junction into worktree")); + + let repo = init_repo(); + let root = repo.path(); + let external = tempfile::tempdir().expect("external directory"); + std::fs::write(external.path().join("safe.gitconfig"), "").expect("write external config"); + create_junction(&root.join("out-of-worktree"), external.path()); + add_include(root, "include.path", "../out-of-worktree/safe.gitconfig"); + assert_worktree_rejection(guard(root).expect_err("worktree junction to external")); + + let repo = init_repo(); + let root = repo.path(); + std::fs::write(root.join("driver-config"), "").expect("write worktree config"); + create_junction(&root.join(".git/metadata-escape"), root); + add_include(root, "include.path", "metadata-escape/driver-config"); + assert_worktree_rejection(guard(root).expect_err("metadata junction escape")); + + let repo = init_repo(); + let root = repo.path(); + let entry = tempfile::tempdir().expect("external entry directory"); + let exit = tempfile::tempdir().expect("external exit directory"); + std::fs::write(exit.path().join("safe.gitconfig"), "").expect("write external config"); + create_junction(&entry.path().join("through-worktree"), root); + create_junction(&root.join("back-out"), exit.path()); + add_include( + root, + "include.path", + entry + .path() + .join("through-worktree/back-out/safe.gitconfig") + .to_str() + .expect("UTF-8 multi-hop junction path"), + ); + assert_worktree_rejection(guard(root).expect_err("multi-hop junction traversal")); + + let repo = init_repo(); + let root = repo.path(); + let external = tempfile::tempdir().expect("external directory"); + create_junction(&external.path().join("into-worktree"), root); + add_include( + root, + "include.path", + external + .path() + .join("into-worktree/future.gitconfig") + .to_str() + .expect("UTF-8 missing junction target"), + ); + assert_worktree_rejection(guard(root).expect_err("junction missing suffix")); +} + +#[test] +fn linked_worktree_metadata_is_allowed_but_linked_worktree_config_is_rejected() { + let parent = tempfile::tempdir().expect("worktree parent"); + let main = parent.path().join("main"); + let linked = parent.path().join("linked"); + init_repo_at(&main); + run_success( + &main, + &[ + "worktree", + "add", + "-b", + "config-guard-linked", + linked.to_str().expect("UTF-8 linked path"), + ], + ); + + let git_dir = PathBuf::from(run_success(&linked, &["rev-parse", "--absolute-git-dir"])); + let protected = git_dir.join("protected.gitconfig"); + std::fs::write(&protected, "[user]\nname = linked\n").expect("write linked metadata config"); + add_include( + &linked, + "include.path", + protected.to_str().expect("UTF-8 protected path"), + ); + guard(&linked).expect("linked metadata include"); + run_success(&linked, &["config", "--unset-all", "include.path"]); + + std::fs::write(main.join("driver-config"), "").expect("write main-worktree config"); + add_include( + &linked, + "include.path", + main.join("driver-config") + .to_str() + .expect("UTF-8 main-worktree config"), + ); + assert_worktree_rejection(guard(&linked).expect_err("main worktree include")); + run_success(&linked, &["config", "--unset-all", "include.path"]); + add_include( + &linked, + "include.path", + main.join("future.gitconfig") + .to_str() + .expect("UTF-8 missing main-worktree config"), + ); + assert_worktree_rejection(guard(&linked).expect_err("missing main worktree include")); + run_success(&linked, &["config", "--unset-all", "include.path"]); + + let unsafe_config = linked.join("driver-config"); + std::fs::write(&unsafe_config, "").expect("write linked worktree config"); + add_include( + &linked, + "include.path", + unsafe_config.to_str().expect("UTF-8 unsafe path"), + ); + assert_worktree_rejection(guard(&linked).expect_err("linked worktree include")); +} + +#[test] +fn enclosing_repository_sibling_worktrees_are_always_untrusted() { + let parent = tempfile::tempdir().expect("fixture parent"); + let outer = parent.path().join("outer"); + let sibling = parent.path().join("sibling"); + init_repo_at(&outer); + run_success( + &outer, + &[ + "worktree", + "add", + "-b", + "config-guard-sibling", + sibling.to_str().expect("UTF-8 sibling worktree"), + ], + ); + let nested = outer.join("nested"); + init_repo_at(&nested); + + std::fs::write(sibling.join("driver-config"), "").expect("write sibling config"); + add_include( + &nested, + "include.path", + sibling + .join("driver-config") + .to_str() + .expect("UTF-8 sibling config"), + ); + assert_worktree_rejection(guard(&nested).expect_err("enclosing repo sibling config")); + + run_success(&nested, &["config", "--unset-all", "include.path"]); + std::fs::remove_dir_all(&sibling).expect("remove sibling but retain registry entry"); + add_include( + &nested, + "include.path", + sibling + .join("future.gitconfig") + .to_str() + .expect("UTF-8 missing sibling config"), + ); + assert_worktree_rejection(guard(&nested).expect_err("missing enclosing repo sibling config")); + + let nested_primary = outer.join("inner-primary"); + let inner_linked = parent.path().join("inner-linked"); + init_repo_at(&nested_primary); + run_success( + &nested_primary, + &[ + "worktree", + "add", + "-b", + "inner-linked-from-outer", + inner_linked.to_str().expect("UTF-8 inner linked worktree"), + ], + ); + for candidate in [ + outer.join("outer.txt"), + outer.join("future-outer.gitconfig"), + ] { + if candidate.file_name() == Some(OsStr::new("outer.txt")) { + std::fs::write(&candidate, "").expect("write outer config source"); + } + add_include( + &inner_linked, + "include.path", + candidate.to_str().expect("UTF-8 outer config source"), + ); + assert_worktree_rejection( + guard(&inner_linked).expect_err("enclosing primary config source"), + ); + run_success(&inner_linked, &["config", "--unset-all", "include.path"]); + } + add_include( + &inner_linked, + "include.path", + sibling + .join("future-from-inner.gitconfig") + .to_str() + .expect("UTF-8 enclosing sibling source"), + ); + assert_worktree_rejection( + guard(&inner_linked).expect_err("enclosing sibling from linked inner worktree"), + ); + run_success(&inner_linked, &["config", "--unset-all", "include.path"]); +} + +#[cfg(unix)] +#[test] +fn preserves_symlinked_caller_ancestry_and_each_include_spelling() { + use std::os::unix::fs::symlink; + + let parent = tempfile::tempdir().expect("fixture parent"); + let outer = parent.path().join("outer"); + let nested = parent.path().join("external-nested"); + init_repo_at(&outer); + init_repo_at(&nested); + std::fs::write(outer.join("driver-config"), "").expect("write outer config"); + symlink(&nested, outer.join("nested-link")).expect("link nested cwd through outer repo"); + add_include( + &nested, + "include.path", + outer + .join("driver-config") + .to_str() + .expect("UTF-8 outer config"), + ); + let git = GitRunner::for_cwd_io(&outer.join("nested-link")) + .expect("Git runner from symlinked nested cwd"); + assert_worktree_rejection( + ensure_no_worktree_config_sources(&git, &nested, &[]) + .expect_err("logical outer ancestry config"), + ); + + let repo = init_repo(); + let root = repo.path(); + std::fs::write(root.join("driver-config"), "").expect("write unsafe child config"); + let external = tempfile::tempdir().expect("external include tree"); + let shared = external.path().join("shared.gitconfig"); + std::fs::write(&shared, "[include]\npath = child.gitconfig\n") + .expect("write shared parent config"); + let unsafe_dir = external.path().join("unsafe-spelling"); + let safe_dir = external.path().join("safe-spelling"); + std::fs::create_dir(&unsafe_dir).expect("create unsafe spelling directory"); + std::fs::create_dir(&safe_dir).expect("create safe spelling directory"); + symlink(&shared, unsafe_dir.join("parent.gitconfig")) + .expect("unsafe spelling of shared parent"); + symlink(&shared, safe_dir.join("parent.gitconfig")).expect("safe spelling of shared parent"); + symlink( + root.join("driver-config"), + unsafe_dir.join("child.gitconfig"), + ) + .expect("unsafe relative child"); + std::fs::write(safe_dir.join("child.gitconfig"), "").expect("safe relative child"); + add_include( + root, + "include.path", + unsafe_dir + .join("parent.gitconfig") + .to_str() + .expect("UTF-8 unsafe spelling"), + ); + add_include( + root, + "include.path", + safe_dir + .join("parent.gitconfig") + .to_str() + .expect("UTF-8 safe spelling"), + ); + assert_worktree_rejection(guard(root).expect_err("unsafe relative alias child")); + + let parent = tempfile::tempdir().expect("linked-main dangling fixture"); + let main = parent.path().join("main"); + let linked = parent.path().join("linked"); + init_repo_at(&main); + run_success( + &main, + &[ + "worktree", + "add", + "-b", + "config-guard-dangling-main", + linked.to_str().expect("UTF-8 linked worktree"), + ], + ); + let external = tempfile::tempdir().expect("external dangling alias directory"); + symlink( + main.join("future.gitconfig"), + external.path().join("dangling-main-config"), + ) + .expect("dangling alias into main worktree"); + add_include( + &linked, + "include.path", + external + .path() + .join("dangling-main-config") + .to_str() + .expect("UTF-8 dangling main alias"), + ); + assert_worktree_rejection(guard(&linked).expect_err("dangling alias into main worktree")); +} + +#[cfg(target_os = "macos")] +#[test] +fn rejects_apfs_data_firmlink_aliases_for_existing_and_missing_worktree_configs() { + let data_root = Path::new("/System/Volumes/Data"); + if !data_root.is_dir() { + return; + } + let home = PathBuf::from(std::env::var_os("HOME").expect("HOME")); + let temp_parent = home.join(".cache/codex-git-utils-tests"); + std::fs::create_dir_all(&temp_parent).expect("create test temp parent"); + + for exists in [true, false] { + let repo = tempfile::Builder::new() + .prefix("firmlink-config-") + .tempdir_in(&temp_parent) + .expect("home tempdir"); + let root = repo.path(); + init_repo_at(root); + if exists { + std::fs::write(root.join("driver-config"), "").expect("write aliased config"); + } + let alias = data_root + .join(root.strip_prefix("/").expect("absolute repository path")) + .join("driver-config"); + add_include( + root, + "include.path", + alias.to_str().expect("UTF-8 firmlink alias"), + ); + assert_worktree_rejection(guard(root).expect_err("APFS firmlink alias")); + } + + let family = tempfile::Builder::new() + .prefix("firmlink-linked-family-") + .tempdir_in(&temp_parent) + .expect("home linked-family tempdir"); + let main = family.path().join("main"); + let linked = family.path().join("linked"); + init_repo_at(&main); + run_success( + &main, + &[ + "worktree", + "add", + "-b", + "firmlink-linked-main", + linked.to_str().expect("UTF-8 linked path"), + ], + ); + for exists in [true, false] { + let name = if exists { + "driver-config" + } else { + "future.gitconfig" + }; + if exists { + std::fs::write(main.join(name), "").expect("write main config"); + } + let alias = data_root + .join(main.strip_prefix("/").expect("absolute main path")) + .join(name); + add_include( + &linked, + "include.path", + alias.to_str().expect("UTF-8 main firmlink alias"), + ); + assert_worktree_rejection(guard(&linked).expect_err("main firmlink alias")); + run_success(&linked, &["config", "--unset-all", "include.path"]); + } + std::fs::write(main.join(".git/protected.gitconfig"), "") + .expect("write protected main metadata config"); + let protected_alias = data_root + .join(main.strip_prefix("/").expect("absolute main path")) + .join(".git/protected.gitconfig"); + add_include( + &linked, + "include.path", + protected_alias + .to_str() + .expect("UTF-8 protected metadata alias"), + ); + guard(&linked).expect("protected metadata firmlink alias"); +} + +#[test] +fn include_path_expansion_supports_git_prefix() { + let repo = init_repo(); + let root = repo.path(); + let git = GitRunner::for_cwd_io(root).expect("Git runner"); + let prefix = + expand_git_config_path(&git, root, "%(prefix)/etc/gitconfig").expect("expand Git prefix"); + assert!(prefix.is_absolute()); +} + +#[cfg(not(windows))] +#[test] +fn include_path_expansion_preserves_colon_parentheses_as_literal_text() { + let repo = init_repo(); + let root = repo.path(); + let git = GitRunner::for_cwd_io(root).expect("Git runner"); + let optional = GitConfigEntry { + scope: crate::git_config::GitConfigScope::Local, + origin: crate::git_config::GitConfigOrigin::File(".git/config".into()), + key: "include.path".to_string(), + value: ":(optional)../future.gitconfig".to_string(), + }; + let optional_path = resolve_include_path(&git, root, &optional).expect("resolve optional path"); + assert_eq!( + AbsolutePathBuf::resolve_path_against_base(optional_path, root).as_path(), + root.join(".git/:(optional)../future.gitconfig") + ); + + let unknown = GitConfigEntry { + value: ":(unknown)../future.gitconfig".to_string(), + ..optional + }; + let unknown_path = + resolve_include_path(&git, root, &unknown).expect("resolve literal unknown path"); + assert_eq!( + AbsolutePathBuf::resolve_path_against_base(unknown_path, root).as_path(), + root.join(".git/:(unknown)../future.gitconfig") + ); + + for spelling in [":(optional)", ":(unknown)"] { + let external = tempfile::tempdir().expect("external literal config directory"); + let literal_dir = external.path().join(spelling); + std::fs::create_dir(&literal_dir).expect("create literal include directory"); + std::fs::write(literal_dir.join("child"), "[probe]\nvalue = literal\n") + .expect("write literal include child"); + let parent = external.path().join("parent.gitconfig"); + std::fs::write(&parent, format!("[include]\npath = {spelling}/child\n")) + .expect("write literal parent config"); + let value = run_success( + root, + &[ + "config", + "--file", + parent.to_str().expect("UTF-8 parent path"), + "--includes", + "--get", + "probe.value", + ], + ); + assert_eq!(value, "literal"); + add_include( + root, + "include.path", + parent.to_str().expect("UTF-8 parent path"), + ); + guard(root).expect("literal include path is external"); + run_success(root, &["config", "--unset-all", "include.path"]); + } +} + +#[test] +fn windows_path_validator_rejects_namespaces_streams_and_alias_components() { + for path in [ + r"\??\C:\repo\config", + r"\\?\GLOBALROOT\Device\config", + r"\\.\pipe\config", + r"C:\repo\config:stream", + r"C:relative\config", + r":(optional)..\future.gitconfig", + r":(unknown)..\future.gitconfig", + r"C:\repo\NUL.gitconfig", + r"C:\repo\COM¹.gitconfig", + r"C:\repo\LPT³.gitconfig", + r"C:\repo\trailing.\config", + ] { + assert!(windows_config_path_is_ambiguous(path), "{path:?}"); + } + for path in [ + r"C:\external\config", + r"\\server\share\config", + r"\\?\C:\repo\.git\config", + r"\\.\C:\repo\.git\config", + r"\\?\UNC\server\share\config", + r"\\.\UNC\server\share\config", + r"..\driver-config", + r".\driver-config", + ] { + assert!(!windows_config_path_is_ambiguous(path), "{path:?}"); + } +} diff --git a/codex-rs/git-utils/src/git_config_tests.rs b/codex-rs/git-utils/src/git_config_tests.rs index 8f49871c03..b8c167edbd 100644 --- a/codex-rs/git-utils/src/git_config_tests.rs +++ b/codex-rs/git-utils/src/git_config_tests.rs @@ -99,6 +99,62 @@ fn symmetric_git_boolean_parser_excludes_only_int_min_spellings() { } } +#[test] +fn entry_parsers_preserve_duplicate_include_directives_in_order() { + let scoped = b"local\0file:.git/config\0include.path\n../unsafe.gitconfig\0\ +local\0file:.git/config\0include.path\n/absolute/external.gitconfig\0"; + let legacy = b"file:.git/config\0include.path\n../unsafe.gitconfig\0\ +file:.git/config\0include.path\n/absolute/external.gitconfig\0"; + let expected_scoped = vec![ + GitConfigEntry { + scope: GitConfigScope::Local, + origin: GitConfigOrigin::File(".git/config".into()), + key: "include.path".to_string(), + value: "../unsafe.gitconfig".to_string(), + }, + GitConfigEntry { + scope: GitConfigScope::Local, + origin: GitConfigOrigin::File(".git/config".into()), + key: "include.path".to_string(), + value: "/absolute/external.gitconfig".to_string(), + }, + ]; + assert_eq!( + parse_config_entries(scoped).expect("scoped entries"), + expected_scoped + ); + assert_eq!( + parse_config_entries_with_origins(legacy).expect("legacy entries"), + expected_scoped + ); +} + +#[cfg(unix)] +#[test] +fn config_origin_parser_preserves_non_utf8_unix_path_bytes() { + use std::os::unix::ffi::OsStrExt; + + let entries = parse_config_entries(b"local\0file:/tmp/config-\xff\0include.path\n/tmp/safe\0") + .expect("non-UTF-8 config origin"); + let GitConfigOrigin::File(path) = &entries[0].origin else { + panic!("expected file origin"); + }; + assert_eq!(path.as_os_str().as_bytes(), b"/tmp/config-\xff"); +} + +#[test] +fn rejects_malformed_config_records() { + for output in [ + b"global\0file:/tmp/config\0key\nvalue".as_slice(), + b"global\0file:/tmp/config\0key\0".as_slice(), + b"mystery\0file:/tmp/config\0key\nvalue\0".as_slice(), + b"global\0\0key\nvalue\0".as_slice(), + b"global\0file:/tmp/config\0\nvalue\0".as_slice(), + ] { + assert!(parse_config_entries(output).is_err(), "{output:?}"); + } +} + #[test] fn path_containment_uses_component_boundaries() { let root = Path::new("/repo/root"); diff --git a/codex-rs/git-utils/src/patch_paths.rs b/codex-rs/git-utils/src/patch_paths.rs index 378a245859..b42defd914 100644 --- a/codex-rs/git-utils/src/patch_paths.rs +++ b/codex-rs/git-utils/src/patch_paths.rs @@ -9,7 +9,7 @@ use crate::apply::safe_git_config_parts; use crate::apply::write_temp_patch; use crate::git_command::GitRunner; use crate::git_config::path_is_within; -use crate::git_config_sources::ensure_no_worktree_primary_config_sources; +use crate::git_config_sources::ensure_no_worktree_config_sources; /// Extract paths with Git from a cwd whose config sources have already been /// authorized for `git_config_args`. @@ -66,7 +66,7 @@ fn extract_paths_from_patch_from_cwd(diff_text: &str, cwd: &Path) -> Vec let git_root = crate::get_git_repo_root(cwd) .ok_or_else(|| io::Error::other("not a Git repository"))?; let git_root = std::fs::canonicalize(git_root)?; - ensure_no_worktree_primary_config_sources(&git, &git_root)?; + ensure_no_worktree_config_sources(&git, &git_root, &[])?; extract_effective_paths_from_patch(&git, &git_root, &patch_path, /*revert*/ false, &[]) })() .unwrap_or_default(); @@ -245,7 +245,7 @@ fn invalid_windows_patch_component(component: &str) -> bool { pub fn stage_paths(git_root: &Path, diff: &str) -> io::Result<()> { let git = GitRunner::for_cwd_io(git_root)?; let git_config_args = safe_git_config_parts(); - ensure_no_worktree_primary_config_sources(&git, git_root)?; + ensure_no_worktree_config_sources(&git, git_root, &git_config_args)?; let (tmpdir, patch_path) = write_temp_patch(diff)?; let paths = extract_effective_paths_from_patch( &git, diff --git a/codex-rs/git-utils/src/patch_paths_tests.rs b/codex-rs/git-utils/src/patch_paths_tests.rs index 4d6643a97a..8f72e3841e 100644 --- a/codex-rs/git-utils/src/patch_paths_tests.rs +++ b/codex-rs/git-utils/src/patch_paths_tests.rs @@ -68,7 +68,7 @@ fn effective_paths(diff: &str, revert: bool) -> io::Result> { let git_root = crate::get_git_repo_root(cwd).ok_or_else(|| io::Error::other("not a Git repository"))?; let git_root = std::fs::canonicalize(git_root)?; - ensure_no_worktree_primary_config_sources(&git, &git_root)?; + ensure_no_worktree_config_sources(&git, &git_root, &[])?; let paths = extract_effective_paths_from_patch(&git, &git_root, &patch_path, revert, &[])?; drop(tmpdir); Ok(paths) diff --git a/codex-rs/git-utils/src/path_authority.rs b/codex-rs/git-utils/src/path_authority.rs index 1e2157c378..c27f9e698c 100644 --- a/codex-rs/git-utils/src/path_authority.rs +++ b/codex-rs/git-utils/src/path_authority.rs @@ -18,7 +18,7 @@ use route_walker::invalid_data; use route_walker::observe_route; #[cfg(any(windows, test))] pub(crate) use windows_path::windows_authority_path_is_ambiguous; -#[cfg(windows)] +#[cfg(any(windows, test))] pub(crate) use windows_path::windows_path_is_ambiguous; #[derive(Clone, Copy, Eq, PartialEq)] diff --git a/codex-rs/git-utils/src/repository_authority/authority.rs b/codex-rs/git-utils/src/repository_authority/authority.rs index 05b792d357..3d014f810c 100644 --- a/codex-rs/git-utils/src/repository_authority/authority.rs +++ b/codex-rs/git-utils/src/repository_authority/authority.rs @@ -2,6 +2,8 @@ use std::io; use std::path::Path; use std::path::PathBuf; +use same_file::Handle; + use crate::errors::GitReadError; use crate::git_config::path_is_within; use crate::path_authority::RepositoryRouteBoundaries; @@ -29,6 +31,7 @@ mod policy; #[derive(Debug)] pub(crate) struct RepositoryAuthority { active_worktree_root: PathBuf, + active_worktree_identity: Option, roots: Vec, worktree_roots: Vec, common_dirs: Vec, @@ -57,8 +60,11 @@ impl RepositoryAuthority { let worktree_root = crate::get_git_repo_root(&canonical_cwd) .and_then(|root| std::fs::canonicalize(root).ok()) .unwrap_or_else(|| canonical_cwd.clone()); + let active_worktree_identity = Handle::from_path(&worktree_root) + .map_err(|error| invalid_metadata(&worktree_root, error))?; let mut authority = Self { active_worktree_root: worktree_root.clone(), + active_worktree_identity: Some(active_worktree_identity), roots: Vec::new(), worktree_roots: Vec::new(), common_dirs: Vec::new(), @@ -116,6 +122,7 @@ impl RepositoryAuthority { } pub(crate) fn revalidate_active_repository_metadata(&self) -> io::Result<()> { + self.revalidate_active_worktree_identity()?; let Some(expected) = &self.active_metadata else { return Ok(()); }; @@ -148,6 +155,25 @@ impl RepositoryAuthority { }) } + fn revalidate_active_worktree_identity(&self) -> io::Result<()> { + let Some(expected) = &self.active_worktree_identity else { + return Ok(()); + }; + let actual = Handle::from_path(&self.active_worktree_root).map_err(|error| { + io::Error::new( + io::ErrorKind::PermissionDenied, + format!("guarded Git repository identity could not be revalidated: {error}"), + ) + })?; + if &actual != expected { + return Err(io::Error::new( + io::ErrorKind::PermissionDenied, + "guarded Git repository identity changed before Git launch", + )); + } + Ok(()) + } + #[cfg(test)] pub(crate) fn from_test_locations( roots: Vec, @@ -156,12 +182,15 @@ impl RepositoryAuthority { ) -> Result { let route_boundaries = repository_route_boundaries(&worktree_roots, &common_dirs) .map_err(|error| invalid_metadata(Path::new(""), error))?; + let active_worktree_root = worktree_roots + .first() + .or_else(|| roots.first()) + .cloned() + .unwrap_or_default(); + let active_worktree_identity = Handle::from_path(&active_worktree_root).ok(); Ok(Self { - active_worktree_root: worktree_roots - .first() - .or_else(|| roots.first()) - .cloned() - .unwrap_or_default(), + active_worktree_root, + active_worktree_identity, roots, worktree_roots, common_dirs, From 463eed4400c3986e5ffce30ffd151f0e7cc4e9fc Mon Sep 17 00:00:00 2001 From: Chris Bookholt Date: Fri, 3 Jul 2026 18:36:13 -0700 Subject: [PATCH 11/20] fix(git): bind guarded patch operations --- codex-rs/git-utils/src/apply.rs | 190 +++++++-------- codex-rs/git-utils/src/git_command.rs | 4 + codex-rs/git-utils/src/guarded_config.rs | 228 ++++++++++++++++++ .../git-utils/src/guarded_config_tests.rs | 56 +++++ codex-rs/git-utils/src/lib.rs | 1 + codex-rs/git-utils/src/patch_paths.rs | 174 ++++++------- codex-rs/git-utils/src/patch_paths_tests.rs | 4 +- .../src/repository_authority/authority.rs | 21 ++ 8 files changed, 499 insertions(+), 179 deletions(-) create mode 100644 codex-rs/git-utils/src/guarded_config.rs create mode 100644 codex-rs/git-utils/src/guarded_config_tests.rs diff --git a/codex-rs/git-utils/src/apply.rs b/codex-rs/git-utils/src/apply.rs index d14d0b2d4c..d3a0787674 100644 --- a/codex-rs/git-utils/src/apply.rs +++ b/codex-rs/git-utils/src/apply.rs @@ -10,13 +10,11 @@ use std::io; use std::path::Path; 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::patch_paths::extract_effective_paths_from_patch; -use crate::patch_paths::stage_effective_paths; -use crate::safe_git::DISABLED_HOOKS_PATH; +use crate::guarded_config::GuardedGitConfig; +use crate::patch_paths::extract_effective_paths_from_patch_guarded; +use crate::patch_paths::stage_effective_paths_guarded; #[cfg(test)] use crate::safe_git::isolate_git_command_environment; @@ -46,26 +44,26 @@ pub struct ApplyGitResult { /// When [`ApplyGitRequest::preflight`] is `true`, this behaves like `git apply --check` and /// leaves the working tree untouched while still parsing the command output for diagnostics. pub fn apply_git_patch(req: &ApplyGitRequest) -> io::Result { + let cfg_parts = configured_git_config_parts(); + // Construct from the caller's exact route before canonicalization so + // repository authority retains lexical enclosing worktrees. 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)?; - } + let expected_root = crate::get_git_repo_root(&requested_cwd) + .ok_or_else(|| io::Error::other("not a Git repository")) + .and_then(std::fs::canonicalize)?; + let config = GuardedGitConfig::authorize(&git, &expected_root, cfg_parts)?; + resolve_git_root(&config, &requested_cwd)?; // 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_paths = - extract_effective_paths_from_patch(&git, &git_root, &patch_path, req.revert, &cfg_parts)?; - cfg_parts.extend(safe_git_config_parts()); + let patch_paths = extract_effective_paths_from_patch_guarded(&config, &patch_path, req.revert)?; if req.revert && !req.preflight { // Stage WT paths first to avoid index mismatch on revert. - stage_effective_paths(&git, &git_root, &patch_paths, &cfg_parts)?; + stage_effective_paths_guarded(&config, &patch_paths)?; } // Build git args @@ -83,8 +81,8 @@ pub fn apply_git_patch(req: &ApplyGitRequest) -> io::Result { check_args.push("-R".to_string()); } check_args.push(patch_path.to_string_lossy().to_string()); - let rendered = render_command_for_log(&git_root, &cfg_parts, &check_args); - let (c_code, c_out, c_err) = run_git(&git, &git_root, &cfg_parts, &check_args)?; + let rendered = config.render_command_for_log(&check_args)?; + let (c_code, c_out, c_err) = run_guarded_apply(&config, &check_args)?; let (mut applied_paths, mut skipped_paths, mut conflicted_paths) = parse_git_apply_output(&c_out, &c_err); applied_paths.sort(); @@ -104,8 +102,8 @@ pub fn apply_git_patch(req: &ApplyGitRequest) -> io::Result { }); } - let cmd_for_log = render_command_for_log(&git_root, &cfg_parts, &args); - let (code, stdout, stderr) = run_git(&git, &git_root, &cfg_parts, &args)?; + let cmd_for_log = config.render_command_for_log(&args)?; + let (code, stdout, stderr) = run_guarded_apply(&config, &args)?; let (mut applied_paths, mut skipped_paths, mut conflicted_paths) = parse_git_apply_output(&stdout, &stderr); @@ -127,18 +125,10 @@ pub fn apply_git_patch(req: &ApplyGitRequest) -> io::Result { }) } -fn resolve_git_root( - git: &GitRunner, - cwd: &Path, - git_config_args: &[String], -) -> io::Result { - let requested_cwd = std::fs::canonicalize(cwd)?; - let mut command = git.command_for_cwd(&requested_cwd)?; - command - .args(git_config_args) - .arg("rev-parse") - .arg("--show-toplevel"); - let out = git.output(command)?; +fn resolve_git_root(config: &GuardedGitConfig<'_>, requested_cwd: &Path) -> io::Result { + let mut command = config.rev_parse_command()?; + command.arg("--show-toplevel"); + let out = command.output()?; let code = out.status.code().unwrap_or(-1); if code != 0 { return Err(io::Error::other(format!( @@ -149,18 +139,7 @@ fn resolve_git_root( } let reported_root = PathBuf::from(String::from_utf8_lossy(&out.stdout).trim()); let root = std::fs::canonicalize(&reported_root)?; - let expected_root = crate::get_git_repo_root(&requested_cwd) - .ok_or_else(|| { - io::Error::new( - io::ErrorKind::PermissionDenied, - format!( - "refusing to apply a patch because Git resolved worktree {} without a .git marker above requested cwd {}", - root.display(), - requested_cwd.display() - ), - ) - }) - .and_then(std::fs::canonicalize)?; + let expected_root = config.canonical_root(); if root != expected_root { return Err(io::Error::new( io::ErrorKind::PermissionDenied, @@ -197,60 +176,30 @@ pub(crate) fn write_temp_patch(diff: &str) -> io::Result<(tempfile::TempDir, Pat Ok((dir, path)) } -pub(crate) fn run_git( - git: &GitRunner, - cwd: &Path, - git_cfg: &[String], +fn run_guarded_apply( + config: &GuardedGitConfig<'_>, args: &[String], ) -> io::Result<(i32, String, String)> { - let mut cmd = git.command_for_cwd(cwd)?; - for p in git_cfg { - cmd.arg(p); + let Some((subcommand, args)) = args.split_first() else { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + "missing guarded Git subcommand", + )); + }; + if subcommand != "apply" { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + "unexpected guarded Git subcommand", + )); } - for a in args { - cmd.arg(a); - } - let out = git.output(cmd)?; - let code = out.status.code().unwrap_or(-1); - let stdout = String::from_utf8_lossy(&out.stdout).into_owned(); - let stderr = String::from_utf8_lossy(&out.stderr).into_owned(); - Ok((code, stdout, stderr)) -} - -pub(crate) fn safe_git_config_parts() -> Vec { - vec![ - "-c".to_string(), - format!("core.hooksPath={DISABLED_HOOKS_PATH}"), - "-c".to_string(), - FsmonitorOverride::Disabled.git_config_arg().to_string(), - ] -} - -fn quote_shell(s: &str) -> String { - let simple = s - .chars() - .all(|c| c.is_ascii_alphanumeric() || "-_.:/@%+".contains(c)); - if simple { - s.to_string() - } else { - format!("'{}'", s.replace('\'', "'\\''")) - } -} - -fn render_command_for_log(cwd: &Path, git_cfg: &[String], args: &[String]) -> String { - let mut parts: Vec = Vec::new(); - parts.push("git".to_string()); - for a in git_cfg { - parts.push(quote_shell(a)); - } - for a in args { - parts.push(quote_shell(a)); - } - format!( - "(cd {} && {})", - quote_shell(&cwd.display().to_string()), - parts.join(" ") - ) + let mut command = config.apply_command()?; + command.args(args); + let output = command.output()?; + Ok(( + output.status.code().unwrap_or(-1), + String::from_utf8_lossy(&output.stdout).into_owned(), + String::from_utf8_lossy(&output.stderr).into_owned(), + )) } #[cfg(all(test, unix))] @@ -260,6 +209,8 @@ mod transport_tests; #[cfg(test)] mod tests { use super::*; + use crate::guarded_config::config_source_authorization_count; + use crate::guarded_config::reset_config_source_authorization_count; use std::ffi::OsStr; use std::path::Path; use std::sync::Mutex; @@ -353,7 +304,9 @@ mod tests { revert: false, preflight: false, }; + reset_config_source_authorization_count(); let r = apply_git_patch(&req).expect("run apply"); + assert_eq!(config_source_authorization_count(), 1); assert_eq!(r.exit_code, 0, "exit code 0"); // File exists now assert!(root.join("hello.txt").exists()); @@ -482,6 +435,41 @@ mod tests { assert!(!marker.exists(), "enclosing Git shim must not run"); } + #[cfg(unix)] + #[test] + fn apply_uses_physical_repository_for_symlinked_nested_cwd() { + let _g = env_lock().lock().unwrap(); + let fixture = tempfile::tempdir().expect("fixture"); + let outer = fixture.path().join("outer"); + let target = fixture.path().join("target"); + let nested = target.join("nested"); + std::fs::create_dir_all(&outer).expect("outer"); + std::fs::create_dir_all(&nested).expect("nested"); + let (outer_init, _, outer_error) = run(&outer, &["git", "init", "-q"]); + assert_eq!(outer_init, 0, "outer init: {outer_error}"); + let (target_init, _, target_error) = run(&target, &["git", "init", "-q"]); + assert_eq!(target_init, 0, "target init: {target_error}"); + let lexical_cwd = outer.join("linked-nested"); + std::os::unix::fs::symlink(&nested, &lexical_cwd).expect("nested cwd symlink"); + + reset_config_source_authorization_count(); + let result = apply_git_patch(&ApplyGitRequest { + cwd: lexical_cwd, + diff: "diff --git a/physical.txt b/physical.txt\nnew file mode 100644\n--- /dev/null\n+++ b/physical.txt\n@@ -0,0 +1 @@\n+physical\n".to_string(), + revert: false, + preflight: false, + }) + .expect("apply through symlinked nested cwd"); + + assert_eq!(result.exit_code, 0); + assert_eq!(config_source_authorization_count(), 1); + assert_eq!( + read_file_normalized(&target.join("physical.txt")), + "physical\n" + ); + assert!(!outer.join("physical.txt").exists()); + } + #[test] fn apply_modify_conflict() { let _g = env_lock().lock().unwrap(); @@ -540,7 +528,9 @@ mod tests { revert: false, preflight: false, }; + reset_config_source_authorization_count(); let res_apply = apply_git_patch(&apply_req).expect("apply ok"); + assert_eq!(config_source_authorization_count(), 1); assert_eq!(res_apply.exit_code, 0, "forward apply succeeded"); let after_apply = read_file_normalized(&root.join("file.txt")); assert_eq!(after_apply, "ORIG\n"); @@ -552,7 +542,9 @@ mod tests { revert: true, preflight: false, }; + reset_config_source_authorization_count(); let res_revert = apply_git_patch(&revert_req).expect("revert ok"); + assert_eq!(config_source_authorization_count(), 1); assert_eq!(res_revert.exit_code, 0, "revert apply succeeded"); let after_revert = read_file_normalized(&root.join("file.txt")); assert_eq!(after_revert, "orig\n"); @@ -589,7 +581,9 @@ mod tests { revert: true, preflight: true, }; + reset_config_source_authorization_count(); let res_preflight = apply_git_patch(&preflight_req).expect("preflight ok"); + assert_eq!(config_source_authorization_count(), 1); assert_eq!(res_preflight.exit_code, 0, "revert preflight succeeded"); let (_code_after, staged_after, _stderr_after) = run(root, &["git", "diff", "--cached", "--name-only"]); @@ -619,7 +613,9 @@ diff --git a/ghost.txt b/ghost.txt\n--- a/ghost.txt\n+++ b/ghost.txt\n@@ -1,1 +1 revert: false, preflight: true, }; + reset_config_source_authorization_count(); let r1 = apply_git_patch(&req1).expect("preflight apply"); + assert_eq!(config_source_authorization_count(), 1); assert_ne!(r1.exit_code, 0, "preflight reports failure"); assert!( !root.join("ok.txt").exists(), @@ -637,7 +633,9 @@ diff --git a/ghost.txt b/ghost.txt\n--- a/ghost.txt\n+++ b/ghost.txt\n@@ -1,1 +1 revert: false, preflight: false, }; + reset_config_source_authorization_count(); let r2 = apply_git_patch(&req2).expect("direct apply"); + assert_eq!(config_source_authorization_count(), 1); assert_ne!(r2.exit_code, 0, "apply is expected to fail overall"); assert!( !r2.cmd_for_log.contains("--check"), @@ -664,8 +662,10 @@ diff --git a/ghost.txt b/ghost.txt\n--- a/ghost.txt\n+++ b/ghost.txt\n@@ -1,1 +1 assert_eq!(config_code, 0, "configure core.worktree: {config_err}"); let git = GitRunner::for_cwd_io(&attacker).expect("trusted Git"); + let guarded = GuardedGitConfig::authorize(&git, &attacker, Vec::new()) + .expect("authorize attacker repository config"); let error = - resolve_git_root(&git, &attacker, &[]).expect_err("reject redirected worktree"); + resolve_git_root(&guarded, &attacker).expect_err("reject redirected worktree"); assert_eq!(error.kind(), io::ErrorKind::PermissionDenied); assert!(error.to_string().contains("instead of expected worktree")); } diff --git a/codex-rs/git-utils/src/git_command.rs b/codex-rs/git-utils/src/git_command.rs index 9d3c6af66e..0cba0b465c 100644 --- a/codex-rs/git-utils/src/git_command.rs +++ b/codex-rs/git-utils/src/git_command.rs @@ -116,6 +116,10 @@ impl GitRunner { .ensure_config_source_is_not_worktree_controlled(path, description) } + pub(crate) fn ensure_active_worktree_root(&self, root: &Path) -> io::Result<()> { + self.authority.ensure_active_worktree_root(root) + } + pub(crate) fn config_environment_value(&self, name: &str) -> Option<&OsStr> { self.config_environment.value(name) } diff --git a/codex-rs/git-utils/src/guarded_config.rs b/codex-rs/git-utils/src/guarded_config.rs new file mode 100644 index 0000000000..4f9c008c2a --- /dev/null +++ b/codex-rs/git-utils/src/guarded_config.rs @@ -0,0 +1,228 @@ +use std::ffi::OsStr; +use std::io; +use std::path::Path; +use std::path::PathBuf; + +use crate::FsmonitorOverride; +use crate::git_command::GitCommand; +use crate::git_command::GitRunner; +use crate::git_config_sources::ensure_no_worktree_config_sources; +use crate::safe_git::DISABLED_HOOKS_PATH; + +/// Proof that one exact Git config invocation has no worktree-controlled +/// source routes for one runner and canonical repository root. +/// +/// The capability deliberately owns the ordered base arguments and cannot be +/// cloned or rebound to another runner, root, or command environment. +pub(crate) struct ValidatedConfigSources<'git> { + git: &'git GitRunner, + canonical_root: PathBuf, + base_config_args: Box<[String]>, +} + +impl<'git> ValidatedConfigSources<'git> { + fn authorize( + git: &'git GitRunner, + canonical_root: &Path, + base_config_args: Vec, + ) -> io::Result { + #[cfg(test)] + CONFIG_SOURCE_AUTHORIZATION_COUNT.with(|count| count.set(count.get() + 1)); + + validate_base_config_args(&base_config_args)?; + let canonical_root = std::fs::canonicalize(canonical_root)?; + git.ensure_active_worktree_root(&canonical_root)?; + ensure_no_worktree_config_sources(git, &canonical_root, &base_config_args)?; + Ok(Self { + git, + canonical_root, + base_config_args: base_config_args.into_boxed_slice(), + }) + } +} + +fn validate_base_config_args(args: &[String]) -> io::Result<()> { + let mut pairs = args.chunks_exact(2); + for pair in &mut pairs { + let Some((key, _value)) = pair[1].split_once('=') else { + return Err(invalid_base_config_args()); + }; + if pair[0] != "-c" || key.is_empty() { + return Err(invalid_base_config_args()); + } + } + if !pairs.remainder().is_empty() { + return Err(invalid_base_config_args()); + } + Ok(()) +} + +fn invalid_base_config_args() -> io::Error { + io::Error::new( + io::ErrorKind::InvalidInput, + "guarded Git base config must contain only ordered -c key=value pairs", + ) +} + +/// Operation-owned Git configuration capability. +/// +/// All operation children are rooted at the authorized repository, inherit +/// the exact frozen base invocation, and receive fixed library safety scalars. +pub(crate) struct GuardedGitConfig<'git> { + sources: ValidatedConfigSources<'git>, +} + +#[derive(Clone, Copy)] +enum BoundSubcommand { + AddLiteralPathspecs, + Apply, + RevParse, +} + +impl BoundSubcommand { + fn as_str(self) -> &'static str { + match self { + Self::AddLiteralPathspecs => "add", + Self::Apply => "apply", + Self::RevParse => "rev-parse", + } + } + + fn append_to(self, command: &mut GitCommand) { + if matches!(self, Self::AddLiteralPathspecs) { + command.arg("--literal-pathspecs"); + } + command.arg(self.as_str()); + } +} + +/// A command whose runner, root, config invocation, and fixed subcommand are +/// inseparably bound to one operation capability. +pub(crate) struct GuardedGitCommand<'operation, 'git> { + operation: &'operation GuardedGitConfig<'git>, + inner: GitCommand, +} + +impl GuardedGitCommand<'_, '_> { + pub(crate) fn arg(&mut self, arg: impl AsRef) -> &mut Self { + self.inner.arg(arg); + self + } + + pub(crate) fn args(&mut self, args: I) -> &mut Self + where + I: IntoIterator, + S: AsRef, + { + self.inner.args(args); + self + } + + pub(crate) fn output(self) -> io::Result { + self.operation.sources.git.output(self.inner) + } +} + +impl<'git> GuardedGitConfig<'git> { + pub(crate) fn authorize( + git: &'git GitRunner, + canonical_root: &Path, + base_config_args: Vec, + ) -> io::Result { + Ok(Self { + sources: ValidatedConfigSources::authorize(git, canonical_root, base_config_args)?, + }) + } + + pub(crate) fn canonical_root(&self) -> &Path { + &self.sources.canonical_root + } + + pub(crate) fn apply_command(&self) -> io::Result> { + self.guarded_command(BoundSubcommand::Apply) + } + + pub(crate) fn literal_add_command(&self) -> io::Result> { + self.guarded_command(BoundSubcommand::AddLiteralPathspecs) + } + + pub(crate) fn rev_parse_command(&self) -> io::Result> { + self.guarded_command(BoundSubcommand::RevParse) + } + + fn guarded_command( + &self, + subcommand: BoundSubcommand, + ) -> io::Result> { + let mut command = self + .sources + .git + .command_for_cwd(&self.sources.canonical_root)?; + command.args(&self.sources.base_config_args); + append_safe_scalar_overrides(&mut command); + subcommand.append_to(&mut command); + Ok(GuardedGitCommand { + operation: self, + inner: command, + }) + } + + pub(crate) fn render_command_for_log(&self, args: &[String]) -> io::Result { + let mut parts = vec!["git".to_string()]; + parts.extend(self.sources.base_config_args.iter().cloned()); + parts.extend(safe_scalar_override_args()); + parts.extend(args.iter().cloned()); + Ok(format!( + "(cd {} && {})", + quote_shell(&self.sources.canonical_root.display().to_string()), + parts + .into_iter() + .map(|part| quote_shell(&part)) + .collect::>() + .join(" ") + )) + } +} + +fn append_safe_scalar_overrides(command: &mut GitCommand) { + command.args(safe_scalar_override_args()); +} + +fn safe_scalar_override_args() -> [String; 4] { + [ + "-c".to_string(), + format!("core.hooksPath={DISABLED_HOOKS_PATH}"), + "-c".to_string(), + FsmonitorOverride::Disabled.git_config_arg().to_string(), + ] +} + +fn quote_shell(value: &str) -> String { + let simple = value + .chars() + .all(|character| character.is_ascii_alphanumeric() || "-_.:/@%+".contains(character)); + if simple { + value.to_string() + } else { + format!("'{}'", value.replace('\'', "'\\''")) + } +} + +#[cfg(test)] +thread_local! { + static CONFIG_SOURCE_AUTHORIZATION_COUNT: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +#[cfg(test)] +pub(crate) fn reset_config_source_authorization_count() { + CONFIG_SOURCE_AUTHORIZATION_COUNT.with(|count| count.set(0)); +} + +#[cfg(test)] +pub(crate) fn config_source_authorization_count() -> usize { + CONFIG_SOURCE_AUTHORIZATION_COUNT.with(std::cell::Cell::get) +} + +#[cfg(test)] +#[path = "guarded_config_tests.rs"] +mod tests; diff --git a/codex-rs/git-utils/src/guarded_config_tests.rs b/codex-rs/git-utils/src/guarded_config_tests.rs new file mode 100644 index 0000000000..7d0eaed76b --- /dev/null +++ b/codex-rs/git-utils/src/guarded_config_tests.rs @@ -0,0 +1,56 @@ +use super::*; +use crate::safe_git::isolate_git_command_environment; +use pretty_assertions::assert_eq; + +fn run_git(cwd: &Path, args: &[&str]) { + let mut command = std::process::Command::new("git"); + isolate_git_command_environment(&mut command); + let output = command.current_dir(cwd).args(args).output().expect("Git"); + assert!( + output.status.success(), + "git {args:?}: {}", + String::from_utf8_lossy(&output.stderr) + ); +} + +#[test] +fn authorization_rejects_untyped_global_config_arguments() { + let repo = tempfile::tempdir().expect("repo"); + run_git(repo.path(), &["init", "-q"]); + let git = GitRunner::for_cwd_io(repo.path()).expect("runner"); + + for args in [ + vec!["--config-env=include.path=UNSNAPSHOTTED".to_string()], + vec!["--git-dir=elsewhere".to_string()], + vec!["-c".to_string(), "missing-value".to_string()], + vec!["-c".to_string()], + ] { + let error = match GuardedGitConfig::authorize(&git, repo.path(), args) { + Ok(_) => panic!("accepted untyped base config arguments"), + Err(error) => error, + }; + assert_eq!(error.kind(), io::ErrorKind::InvalidInput); + } +} + +#[test] +fn authorization_rejects_another_runner_repository_and_nested_or_prefix_roots() { + let fixture = tempfile::tempdir().expect("fixture"); + let outer = fixture.path().join("repo"); + let nested = outer.join("nested"); + let prefix = fixture.path().join("repo-evil"); + std::fs::create_dir_all(&nested).expect("nested"); + std::fs::create_dir_all(&prefix).expect("prefix"); + run_git(&outer, &["init", "-q"]); + run_git(&nested, &["init", "-q"]); + run_git(&prefix, &["init", "-q"]); + let git = GitRunner::for_cwd_io(&outer).expect("outer runner"); + + for wrong_root in [&nested, &prefix] { + let error = match GuardedGitConfig::authorize(&git, wrong_root, Vec::new()) { + Ok(_) => panic!("accepted another repository root"), + Err(error) => error, + }; + assert_eq!(error.kind(), io::ErrorKind::PermissionDenied); + } +} diff --git a/codex-rs/git-utils/src/lib.rs b/codex-rs/git-utils/src/lib.rs index b52bf1ef19..0377521aef 100644 --- a/codex-rs/git-utils/src/lib.rs +++ b/codex-rs/git-utils/src/lib.rs @@ -9,6 +9,7 @@ mod git_config; mod git_config_environment; mod git_config_sources; mod git_executable; +mod guarded_config; mod info; mod local_only; mod operations; diff --git a/codex-rs/git-utils/src/patch_paths.rs b/codex-rs/git-utils/src/patch_paths.rs index b42defd914..f09e027c75 100644 --- a/codex-rs/git-utils/src/patch_paths.rs +++ b/codex-rs/git-utils/src/patch_paths.rs @@ -4,28 +4,26 @@ use std::io; use std::path::Path; use std::path::PathBuf; -use crate::apply::run_git; -use crate::apply::safe_git_config_parts; use crate::apply::write_temp_patch; use crate::git_command::GitRunner; use crate::git_config::path_is_within; -use crate::git_config_sources::ensure_no_worktree_config_sources; +use crate::guarded_config::GuardedGitConfig; -/// Extract paths with Git from a cwd whose config sources have already been -/// authorized for `git_config_args`. -pub(crate) fn extract_effective_paths_from_patch( - git: &GitRunner, - authorized_cwd: &Path, +/// Extract effective patch paths through a bound operation configuration. +pub(crate) fn extract_effective_paths_from_patch_guarded( + config: &GuardedGitConfig<'_>, patch_path: &Path, revert: bool, - git_config_args: &[String], ) -> io::Result> { - let forward_paths = - git_apply_numstat_paths(git, authorized_cwd, patch_path, revert, git_config_args)?; - // `git apply --numstat` reports only the destination of a rename. Parse the - // opposite orientation too so both endpoints are included in the result. - let reverse_paths = - git_apply_numstat_paths(git, authorized_cwd, patch_path, !revert, git_config_args)?; + let forward_paths = git_apply_numstat_paths_guarded(config, patch_path, revert)?; + let reverse_paths = git_apply_numstat_paths_guarded(config, patch_path, !revert)?; + normalize_effective_patch_paths(forward_paths, reverse_paths) +} + +fn normalize_effective_patch_paths( + forward_paths: Vec, + reverse_paths: Vec, +) -> io::Result> { if forward_paths.len() != reverse_paths.len() { return Err(io::Error::new( io::ErrorKind::InvalidData, @@ -46,6 +44,30 @@ pub(crate) fn extract_effective_paths_from_patch( .collect() } +fn git_apply_numstat_paths_guarded( + config: &GuardedGitConfig<'_>, + patch_path: &Path, + revert: bool, +) -> io::Result> { + let mut command = config.apply_command()?; + command.args(["--numstat", "-z"]); + if revert { + command.arg("-R"); + } + command.arg("--").arg(patch_path); + let output = command.output()?; + if !output.status.success() { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!( + "failed to parse patch paths: {}", + String::from_utf8_lossy(&output.stderr).trim() + ), + )); + } + parse_numstat_paths(&output.stdout) +} + /// Best-effort extraction of the paths Git would apply. /// /// Security-sensitive callers must use the fallible internal extractor so an @@ -66,42 +88,14 @@ fn extract_paths_from_patch_from_cwd(diff_text: &str, cwd: &Path) -> Vec let git_root = crate::get_git_repo_root(cwd) .ok_or_else(|| io::Error::other("not a Git repository"))?; let git_root = std::fs::canonicalize(git_root)?; - ensure_no_worktree_config_sources(&git, &git_root, &[])?; - extract_effective_paths_from_patch(&git, &git_root, &patch_path, /*revert*/ false, &[]) + let config = GuardedGitConfig::authorize(&git, &git_root, Vec::new())?; + extract_effective_paths_from_patch_guarded(&config, &patch_path, /*revert*/ false) })() .unwrap_or_default(); drop(tmpdir); paths } -fn git_apply_numstat_paths( - git: &GitRunner, - authorized_cwd: &Path, - patch_path: &Path, - revert: bool, - git_config_args: &[String], -) -> io::Result> { - let mut cmd = git.command_for_cwd(authorized_cwd)?; - cmd.args(git_config_args); - cmd.args(["apply", "--numstat", "-z"]); - if revert { - cmd.arg("-R"); - } - cmd.arg("--").arg(patch_path); - let out = git.output(cmd)?; - if !out.status.success() { - return Err(io::Error::new( - io::ErrorKind::InvalidInput, - format!( - "failed to parse patch paths: {}", - String::from_utf8_lossy(&out.stderr).trim() - ), - )); - } - - parse_numstat_paths(&out.stdout) -} - fn parse_numstat_paths(output: &[u8]) -> io::Result> { if !output.is_empty() && !output.ends_with(&[0]) { return Err(io::Error::new( @@ -244,30 +238,28 @@ fn invalid_windows_patch_component(component: &str) -> bool { /// Stage only the files that actually exist on disk for the given diff. pub fn stage_paths(git_root: &Path, diff: &str) -> io::Result<()> { let git = GitRunner::for_cwd_io(git_root)?; - let git_config_args = safe_git_config_parts(); - ensure_no_worktree_config_sources(&git, git_root, &git_config_args)?; + let requested_cwd = std::fs::canonicalize(git_root)?; + let canonical_root = crate::get_git_repo_root(&requested_cwd) + .ok_or_else(|| io::Error::other("not a Git repository")) + .and_then(std::fs::canonicalize)?; + let config = GuardedGitConfig::authorize(&git, &canonical_root, Vec::new())?; let (tmpdir, patch_path) = write_temp_patch(diff)?; - let paths = extract_effective_paths_from_patch( - &git, - git_root, - &patch_path, - /*revert*/ true, - &git_config_args, - )?; + let paths = + extract_effective_paths_from_patch_guarded(&config, &patch_path, /*revert*/ true)?; let _guard = tmpdir; - stage_effective_paths(&git, git_root, &paths, &git_config_args) + stage_effective_paths_guarded(&config, &paths) } -pub(crate) fn stage_effective_paths( - git: &GitRunner, - git_root: &Path, +/// Preserve the existing reverse staging behavior while constructing every +/// Git child from the operation-owned configuration capability. +pub(crate) fn stage_effective_paths_guarded( + config: &GuardedGitConfig<'_>, paths: &[String], - git_config_args: &[String], ) -> io::Result<()> { - let confined = confine_patch_paths(git, git_root, paths)?; + let confined = confine_patch_paths_guarded(config, paths)?; let mut existing = Vec::new(); for path in confined.into_exact_leaves()? { - let joined = git_root.join(&path); + let joined = config.canonical_root().join(&path); if let Ok(metadata) = std::fs::symlink_metadata(&joined) { if leaf_is_traversable_directory(metadata.file_type()) { return Err(containment_error( @@ -280,14 +272,12 @@ pub(crate) fn stage_effective_paths( if existing.is_empty() { return Ok(()); } - let mut args = vec![ - "--literal-pathspecs".to_string(), - "add".to_string(), - "--".to_string(), - ]; + let mut args = vec!["--".to_string()]; args.extend(existing); - let (_code, _, _) = run_git(git, git_root, git_config_args, &args)?; - // We do not hard fail staging; best-effort is OK. Return Ok even on non-zero. + let mut command = config.literal_add_command()?; + command.args(&args); + let _output = command.output()?; + // Preserve the existing best-effort staging contract on non-zero status. Ok(()) } @@ -375,10 +365,29 @@ impl ConfinedPatchPaths { } } +#[cfg(test)] pub(crate) fn confine_patch_paths( git: &GitRunner, git_root: &Path, paths: &[String], +) -> io::Result { + let config = GuardedGitConfig::authorize(git, git_root, Vec::new())?; + confine_patch_paths_guarded(&config, paths) +} + +fn confine_patch_paths_guarded( + config: &GuardedGitConfig<'_>, + paths: &[String], +) -> io::Result { + let canonical_root = std::fs::canonicalize(config.canonical_root())?; + let metadata_dirs = canonical_git_metadata_dirs_guarded(config)?; + confine_patch_paths_with_metadata(&canonical_root, paths, &metadata_dirs) +} + +fn confine_patch_paths_with_metadata( + canonical_root: &Path, + paths: &[String], + metadata_dirs: &[PathBuf], ) -> io::Result { if paths.is_empty() { return Ok(ConfinedPatchPaths { @@ -401,8 +410,6 @@ pub(crate) fn confine_patch_paths( }); } - let canonical_root = std::fs::canonicalize(git_root)?; - let metadata_dirs = canonical_git_metadata_dirs(git, &canonical_root)?; let mut entries = Vec::with_capacity(paths.len()); let mut prefix_cache = std::collections::BTreeMap::new(); @@ -416,9 +423,9 @@ pub(crate) fn confine_patch_paths( ); let (existing_len, mut projected) = longest_existing_strict_prefix( - &canonical_root, + canonical_root, &components, - &metadata_dirs, + metadata_dirs, &mut prefix_cache, )?; projected.extend( @@ -513,22 +520,25 @@ fn containment_error(message: &'static str) -> io::Error { io::Error::new(io::ErrorKind::PermissionDenied, message) } -fn canonical_git_metadata_dirs(git: &GitRunner, git_root: &Path) -> io::Result> { - let config_parts = safe_git_config_parts(); +fn canonical_git_metadata_dirs_guarded(config: &GuardedGitConfig<'_>) -> io::Result> { let queries = [ - vec!["rev-parse".to_string(), "--absolute-git-dir".to_string()], - vec!["rev-parse".to_string(), "--git-common-dir".to_string()], + ["rev-parse", "--absolute-git-dir"], + ["rev-parse", "--git-common-dir"], ]; let mut metadata_dirs = std::collections::BTreeSet::new(); for args in queries { - let (code, stdout, stderr) = run_git(git, git_root, &config_parts, &args)?; - if code != 0 { + let mut command = config.rev_parse_command()?; + command.args(&args[1..]); + let output = command.output()?; + if !output.status.success() { return Err(io::Error::other(format!( - "failed to resolve Git repository metadata (exit {code}): {}", - stderr.trim() + "failed to resolve Git repository metadata (status {}): {}", + output.status, + String::from_utf8_lossy(&output.stderr).trim() ))); } - let path = stdout.trim_end_matches(['\r', '\n']); + let path = String::from_utf8_lossy(&output.stdout); + let path = path.trim_end_matches(['\r', '\n']); if path.is_empty() { return Err(io::Error::new( io::ErrorKind::InvalidData, @@ -539,7 +549,7 @@ fn canonical_git_metadata_dirs(git: &GitRunner, git_root: &Path) -> io::Result io::Result> { let git_root = crate::get_git_repo_root(cwd).ok_or_else(|| io::Error::other("not a Git repository"))?; let git_root = std::fs::canonicalize(git_root)?; - ensure_no_worktree_config_sources(&git, &git_root, &[])?; - let paths = extract_effective_paths_from_patch(&git, &git_root, &patch_path, revert, &[])?; + let config = GuardedGitConfig::authorize(&git, &git_root, Vec::new())?; + let paths = extract_effective_paths_from_patch_guarded(&config, &patch_path, revert)?; drop(tmpdir); Ok(paths) } diff --git a/codex-rs/git-utils/src/repository_authority/authority.rs b/codex-rs/git-utils/src/repository_authority/authority.rs index 3d014f810c..db55a32a9e 100644 --- a/codex-rs/git-utils/src/repository_authority/authority.rs +++ b/codex-rs/git-utils/src/repository_authority/authority.rs @@ -155,6 +155,27 @@ impl RepositoryAuthority { }) } + pub(crate) fn ensure_active_worktree_root(&self, root: &Path) -> io::Result<()> { + let canonical_root = std::fs::canonicalize(root)?; + let same_root = if let Some(expected) = &self.active_worktree_identity { + Handle::from_path(&canonical_root)? == *expected + } else { + canonical_root == self.active_worktree_root + || same_file::is_same_file(&canonical_root, &self.active_worktree_root)? + }; + if !same_root { + return Err(io::Error::new( + io::ErrorKind::PermissionDenied, + format!( + "guarded Git repository identity {} does not match runner repository {}", + canonical_root.display(), + self.active_worktree_root.display() + ), + )); + } + Ok(()) + } + fn revalidate_active_worktree_identity(&self) -> io::Result<()> { let Some(expected) = &self.active_worktree_identity else { return Ok(()); From f6b6a4951f6247b9eaabd2af28615ad082dc245e Mon Sep 17 00:00:00 2001 From: Chris Bookholt Date: Fri, 3 Jul 2026 18:55:35 -0700 Subject: [PATCH 12/20] fix(git): allow staged config accessor --- codex-rs/git-utils/src/git_config_environment.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/codex-rs/git-utils/src/git_config_environment.rs b/codex-rs/git-utils/src/git_config_environment.rs index 43d80a3afd..f386cfda39 100644 --- a/codex-rs/git-utils/src/git_config_environment.rs +++ b/codex-rs/git-utils/src/git_config_environment.rs @@ -98,6 +98,7 @@ impl GitConfigEnvironmentSnapshot { } } + #[cfg_attr(not(test), allow(dead_code))] pub(crate) fn value(&self, name: &str) -> Option<&OsStr> { self.entries .iter() From 409f1f78e7bdb39a5b3c09b06bbe5e435136a4df Mon Sep 17 00:00:00 2001 From: Chris Bookholt Date: Fri, 3 Jul 2026 19:08:26 -0700 Subject: [PATCH 13/20] test(git): allow staged Windows helper --- codex-rs/git-utils/src/git_config_sources_tests.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/codex-rs/git-utils/src/git_config_sources_tests.rs b/codex-rs/git-utils/src/git_config_sources_tests.rs index 222e9a614f..5a0569d424 100644 --- a/codex-rs/git-utils/src/git_config_sources_tests.rs +++ b/codex-rs/git-utils/src/git_config_sources_tests.rs @@ -41,6 +41,7 @@ fn init_repo_at(root: &Path) { } #[cfg(windows)] +#[allow(dead_code)] fn create_junction(path: &Path, target: &Path) { // Bazel's GNU Windows runner can surface temporary paths with `/` // separators. `mklink` treats those separators as option prefixes, so From 380d6ebd62bda95a3d9edda512fb62bfc81ed32c Mon Sep 17 00:00:00 2001 From: Chris Bookholt Date: Fri, 3 Jul 2026 19:53:49 -0700 Subject: [PATCH 14/20] test(git): respect Windows repository handle pinning --- codex-rs/git-utils/src/git_command_tests.rs | 24 ++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/codex-rs/git-utils/src/git_command_tests.rs b/codex-rs/git-utils/src/git_command_tests.rs index 8f161e12be..8113d2478e 100644 --- a/codex-rs/git-utils/src/git_command_tests.rs +++ b/codex-rs/git-utils/src/git_command_tests.rs @@ -1514,7 +1514,7 @@ fn active_commondir_route_is_revalidated_before_every_child() { ); } -#[cfg(any(unix, windows))] +#[cfg(unix)] #[test] fn active_worktree_identity_is_revalidated_before_every_child() { let fixture = tempfile::tempdir().expect("fixture"); @@ -1554,6 +1554,28 @@ fn active_worktree_identity_is_revalidated_before_every_child() { ); } +#[cfg(windows)] +#[test] +fn active_worktree_identity_pins_repository_for_runner_lifetime() { + let fixture = tempfile::tempdir().expect("fixture"); + let root = fixture.path().join("repo"); + let moved_root = fixture.path().join("repo-moved"); + std::fs::create_dir_all(&root).expect("create repository"); + run_git(&root, &["init", "-q"]); + let runner = GitRunner::for_cwd(&root).expect("runner for original repository"); + + let error = std::fs::rename(&root, &moved_root) + .expect_err("live repository identity handle must block replacement"); + assert_eq!(error.kind(), io::ErrorKind::PermissionDenied, "{error}"); + assert!(root.join(".git").is_dir()); + assert!(!moved_root.exists()); + + drop(runner); + std::fs::rename(&root, &moved_root).expect("released identity handle must allow move"); + assert!(!root.exists()); + assert!(moved_root.join(".git").is_dir()); +} + #[cfg(unix)] #[test] fn direct_external_separate_git_dir_absolute_and_relative_routes_execute_git() { From cc35a0c8847b7f2ce90f15ab443dbc1e6097f58f Mon Sep 17 00:00:00 2001 From: Chris Bookholt Date: Fri, 3 Jul 2026 20:10:31 -0700 Subject: [PATCH 15/20] fix(git): accept empty config count --- .../git-utils/src/git_config_environment.rs | 13 +++++-- .../src/git_config_environment_tests.rs | 38 +++++++++++++++++++ 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/codex-rs/git-utils/src/git_config_environment.rs b/codex-rs/git-utils/src/git_config_environment.rs index f386cfda39..1cd718c13e 100644 --- a/codex-rs/git-utils/src/git_config_environment.rs +++ b/codex-rs/git-utils/src/git_config_environment.rs @@ -61,11 +61,16 @@ impl GitConfigEnvironmentSnapshot { .collect::>(); if let Some(raw_count) = value_for(OsStr::new("GIT_CONFIG_COUNT")) { - let count = raw_count + let raw_count = raw_count .to_str() - .ok_or_else(|| invalid_environment("non-UTF-8 GIT_CONFIG_COUNT"))? - .parse::() - .map_err(|_| invalid_environment("invalid GIT_CONFIG_COUNT"))?; + .ok_or_else(|| invalid_environment("non-UTF-8 GIT_CONFIG_COUNT"))?; + let count = if raw_count.is_empty() { + 0 + } else { + raw_count + .parse::() + .map_err(|_| invalid_environment("invalid GIT_CONFIG_COUNT"))? + }; if count > MAX_CONFIG_ENVIRONMENT_ENTRIES { return Err(invalid_environment( "GIT_CONFIG_COUNT exceeds the supported safety bound", diff --git a/codex-rs/git-utils/src/git_config_environment_tests.rs b/codex-rs/git-utils/src/git_config_environment_tests.rs index 3cdec9a790..54e4cd5766 100644 --- a/codex-rs/git-utils/src/git_config_environment_tests.rs +++ b/codex-rs/git-utils/src/git_config_environment_tests.rs @@ -40,10 +40,15 @@ fn snapshot_binds_present_empty_and_absent_fixed_and_indexed_values() { snapshot.value("GIT_CONFIG_PARAMETERS"), Some(OsStr::new("'safe.parameter'='present'")) ); + assert_eq!(snapshot.value("GIT_CONFIG_COUNT"), Some(OsStr::new("2"))); assert_eq!( snapshot.value("GIT_CONFIG_KEY_0"), Some(OsStr::new("safe.one")) ); + assert_eq!( + snapshot.value("GIT_CONFIG_VALUE_0"), + Some(OsStr::new("present")) + ); assert_eq!(snapshot.value("GIT_CONFIG_VALUE_1"), None); let mut command = Command::new("git"); @@ -80,6 +85,39 @@ fn snapshot_rejects_malformed_or_unbounded_command_config_count() { } } +#[test] +fn snapshot_treats_empty_command_config_count_as_zero() { + let values = BTreeMap::from([ + (OsString::from("GIT_CONFIG_COUNT"), OsString::from("")), + ( + OsString::from("GIT_CONFIG_KEY_0"), + OsString::from("untrusted.key"), + ), + ( + OsString::from("GIT_CONFIG_VALUE_0"), + OsString::from("untrusted-value"), + ), + ]); + let snapshot = GitConfigEnvironmentSnapshot::capture_from(|name| values.get(name).cloned()) + .expect("capture empty count"); + + assert_eq!(snapshot.value("GIT_CONFIG_COUNT"), Some(OsStr::new(""))); + assert_eq!(snapshot.value("GIT_CONFIG_KEY_0"), None); + assert_eq!(snapshot.value("GIT_CONFIG_VALUE_0"), None); + + let mut command = Command::new("git"); + command.env("GIT_CONFIG_COUNT", "1"); + snapshot.apply_to(&mut command); + let child_environment = command + .get_envs() + .map(|(name, value)| (name.to_owned(), value.map(OsStr::to_owned))) + .collect::>(); + assert_eq!( + child_environment.get(OsStr::new("GIT_CONFIG_COUNT")), + Some(&Some(OsString::from(""))) + ); +} + #[cfg(unix)] #[test] fn snapshot_preserves_non_utf8_config_path_bytes() { From f6adc9fbcc19569b047fb425d509081508f882e6 Mon Sep 17 00:00:00 2001 From: Chris Bookholt Date: Fri, 3 Jul 2026 20:32:11 -0700 Subject: [PATCH 16/20] fix(git): preserve patch parsing compatibility --- codex-rs/git-utils/src/apply.rs | 76 +++++++++++++++++++-- codex-rs/git-utils/src/patch_paths.rs | 23 +++++-- codex-rs/git-utils/src/patch_paths_tests.rs | 4 +- 3 files changed, 90 insertions(+), 13 deletions(-) diff --git a/codex-rs/git-utils/src/apply.rs b/codex-rs/git-utils/src/apply.rs index 5ba64abde2..8526a62682 100644 --- a/codex-rs/git-utils/src/apply.rs +++ b/codex-rs/git-utils/src/apply.rs @@ -48,12 +48,12 @@ pub struct ApplyGitResult { pub fn apply_git_patch(req: &ApplyGitRequest) -> io::Result { let git = GitRunner::for_cwd_io(&req.cwd)?; let mut cfg_parts = configured_git_config_parts(); - ensure_no_worktree_primary_config_sources(&git, &req.cwd)?; 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_primary_config_sources(&git, &git_root)?; - } + let expected_root = crate::get_git_repo_root(&requested_cwd) + .ok_or_else(|| io::Error::other("not a Git repository")) + .and_then(std::fs::canonicalize)?; + ensure_no_worktree_primary_config_sources(&git, &expected_root)?; + let git_root = resolve_git_root(&git, &expected_root, &cfg_parts)?; // Write unified diff into a temporary file let (tmpdir, patch_path) = write_temp_patch(&req.diff)?; @@ -359,6 +359,72 @@ mod tests { assert!(root.join("hello.txt").exists()); } + #[test] + fn apply_resolves_relative_primary_config_from_repository_root() { + let _g = env_lock().lock().unwrap(); + if std::env::var_os("CODEX_GIT_UTILS_APPLY_ENV_CHILD").is_none() { + run_isolated_test( + "apply::tests::apply_resolves_relative_primary_config_from_repository_root", + &[("GIT_CONFIG_GLOBAL", OsStr::new("../external/config"))], + ); + return; + } + + let fixture = tempfile::tempdir().expect("fixture"); + let root = fixture.path().join("repo"); + let nested_cwd = root.join("nested"); + let external_config = fixture.path().join("external/config"); + let mismatched_nested_config = root.join("external/config"); + std::fs::create_dir_all(&nested_cwd).expect("nested cwd"); + std::fs::create_dir_all(external_config.parent().expect("config parent")) + .expect("external config directory"); + std::fs::create_dir_all( + mismatched_nested_config + .parent() + .expect("mismatched config parent"), + ) + .expect("mismatched config directory"); + std::fs::write(&external_config, "[codex]\n\tprobe = loaded\n").expect("external config"); + std::fs::write(&mismatched_nested_config, "[invalid\n") + .expect("mismatched nested-cwd config"); + let (init_code, _, init_err) = run(&root, &["git", "init"]); + assert_eq!(init_code, 0, "init repository: {init_err}"); + + let result = apply_git_patch(&ApplyGitRequest { + cwd: nested_cwd, + diff: "diff --git a/hello.txt b/hello.txt\nnew file mode 100644\n--- /dev/null\n+++ b/hello.txt\n@@ -0,0 +1 @@\n+hello\n".to_string(), + revert: false, + preflight: false, + }) + .expect("apply with root-relative primary config"); + assert_eq!(result.exit_code, 0, "apply result: {result:?}"); + assert_eq!(read_file_normalized(&root.join("hello.txt")), "hello\n"); + } + + #[test] + fn numstat_path_discovery_does_not_preempt_apply_whitespace_result() { + let _g = env_lock().lock().unwrap(); + let repo = init_repo(); + let root = repo.path(); + let (config_code, _, config_err) = + run(root, &["git", "config", "apply.whitespace", "error"]); + assert_eq!(config_code, 0, "configure whitespace policy: {config_err}"); + + let result = apply_git_patch(&ApplyGitRequest { + cwd: root.to_path_buf(), + diff: "diff --git a/trailing.txt b/trailing.txt\nnew file mode 100644\n--- /dev/null\n+++ b/trailing.txt\n@@ -0,0 +1 @@\n+trailing \n".to_string(), + revert: false, + preflight: false, + }) + .expect("path discovery must leave apply failure in the structured result"); + assert_ne!(result.exit_code, 0); + assert!( + result.stderr.contains("trailing whitespace"), + "apply result: {result:?}" + ); + assert!(!root.join("trailing.txt").exists()); + } + #[test] fn apply_uses_cwd_repo_despite_inherited_repository_selectors() { let _g = env_lock().lock().unwrap(); diff --git a/codex-rs/git-utils/src/patch_paths.rs b/codex-rs/git-utils/src/patch_paths.rs index 378a245859..782c24e08d 100644 --- a/codex-rs/git-utils/src/patch_paths.rs +++ b/codex-rs/git-utils/src/patch_paths.rs @@ -11,6 +11,10 @@ use crate::git_command::GitRunner; use crate::git_config::path_is_within; use crate::git_config_sources::ensure_no_worktree_primary_config_sources; +// Path discovery must not turn a repository's whitespace policy into an +// API-level parser failure before the real apply can report its status. +const APPLY_NUMSTAT_ARGS: [&str; 3] = ["--numstat", "--whitespace=nowarn", "-z"]; + /// Extract paths with Git from a cwd whose config sources have already been /// authorized for `git_config_args`. pub(crate) fn extract_effective_paths_from_patch( @@ -63,11 +67,18 @@ fn extract_paths_from_patch_from_cwd(diff_text: &str, cwd: &Path) -> Vec }; let paths = (|| -> io::Result> { let git = GitRunner::for_cwd_io(cwd)?; - let git_root = crate::get_git_repo_root(cwd) - .ok_or_else(|| io::Error::other("not a Git repository"))?; - let git_root = std::fs::canonicalize(git_root)?; - ensure_no_worktree_primary_config_sources(&git, &git_root)?; - extract_effective_paths_from_patch(&git, &git_root, &patch_path, /*revert*/ false, &[]) + let authorized_cwd = crate::get_git_repo_root(cwd) + .map(std::fs::canonicalize) + .transpose()? + .unwrap_or(std::fs::canonicalize(cwd)?); + ensure_no_worktree_primary_config_sources(&git, &authorized_cwd)?; + extract_effective_paths_from_patch( + &git, + &authorized_cwd, + &patch_path, + /*revert*/ false, + &[], + ) })() .unwrap_or_default(); drop(tmpdir); @@ -83,7 +94,7 @@ fn git_apply_numstat_paths( ) -> io::Result> { let mut cmd = git.command_for_cwd(authorized_cwd)?; cmd.args(git_config_args); - cmd.args(["apply", "--numstat", "-z"]); + cmd.arg("apply").args(APPLY_NUMSTAT_ARGS); if revert { cmd.arg("-R"); } diff --git a/codex-rs/git-utils/src/patch_paths_tests.rs b/codex-rs/git-utils/src/patch_paths_tests.rs index 4d6643a97a..bf2396b22a 100644 --- a/codex-rs/git-utils/src/patch_paths_tests.rs +++ b/codex-rs/git-utils/src/patch_paths_tests.rs @@ -75,8 +75,8 @@ fn effective_paths(diff: &str, revert: bool) -> io::Result> { } fn best_effort_paths(diff: &str) -> Vec { - let repo = init_repo(); - extract_paths_from_patch_from_cwd(diff, repo.path()) + let cwd = tempfile::tempdir().expect("non-repository cwd"); + extract_paths_from_patch_from_cwd(diff, cwd.path()) } #[test] From c56bd60054e19335ae0b7442278cc84e00f98971 Mon Sep 17 00:00:00 2001 From: Chris Bookholt Date: Mon, 6 Jul 2026 15:27:36 -0700 Subject: [PATCH 17/20] fix(git): reject process-relative procfs config --- codex-rs/Cargo.lock | 1 + codex-rs/git-utils/Cargo.toml | 3 + codex-rs/git-utils/src/apply.rs | 44 +++++++++ codex-rs/git-utils/src/git_command_tests.rs | 33 +++++++ .../git-utils/src/git_config_sources_tests.rs | 97 ++++++++++++++++++- codex-rs/git-utils/src/patch_paths_tests.rs | 25 +++++ codex-rs/git-utils/src/path_authority.rs | 8 ++ .../src/path_authority/route_walker.rs | 76 +++++++++++++++ .../src/path_authority/route_walker_tests.rs | 62 ++++++++++++ .../repository_authority/authority/policy.rs | 16 +++ 10 files changed, 364 insertions(+), 1 deletion(-) diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index 3ba4ea8a25..aa27d32491 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -3137,6 +3137,7 @@ dependencies = [ "once_cell", "pretty_assertions", "regex", + "rustix 1.1.4", "same-file", "schemars 0.8.22", "serde", diff --git a/codex-rs/git-utils/Cargo.toml b/codex-rs/git-utils/Cargo.toml index 9f76e14424..9f35494902 100644 --- a/codex-rs/git-utils/Cargo.toml +++ b/codex-rs/git-utils/Cargo.toml @@ -33,6 +33,9 @@ ts-rs = { workspace = true, features = [ ] } walkdir = { workspace = true } +[target.'cfg(target_os = "linux")'.dependencies] +rustix = { version = "1.1.4", features = ["fs"] } + [dev-dependencies] pretty_assertions = { workspace = true } diff --git a/codex-rs/git-utils/src/apply.rs b/codex-rs/git-utils/src/apply.rs index 8526a62682..5d389ee3aa 100644 --- a/codex-rs/git-utils/src/apply.rs +++ b/codex-rs/git-utils/src/apply.rs @@ -401,6 +401,50 @@ mod tests { assert_eq!(read_file_normalized(&root.join("hello.txt")), "hello\n"); } + #[cfg(target_os = "linux")] + #[test] + fn apply_refuses_process_relative_primary_config_before_mutating_the_repository() { + const TEST_NAME: &str = "apply::tests::apply_refuses_process_relative_primary_config_before_mutating_the_repository"; + if std::env::var_os("CODEX_GIT_UTILS_APPLY_ENV_CHILD").is_none() { + run_isolated_test( + TEST_NAME, + &[ + ( + "GIT_CONFIG_GLOBAL", + OsStr::new("/proc/self/cwd/codex-process-relative.gitconfig"), + ), + ("GIT_CONFIG_NOSYSTEM", OsStr::new("1")), + ], + ); + return; + } + + let repo = init_repo(); + let root = repo.path(); + std::fs::write( + root.join("codex-process-relative.gitconfig"), + "[filter \"unsafe\"]\nclean = false\n", + ) + .expect("worktree config"); + let before_index = run(root, &["git", "ls-files", "--stage", "-z"]).1; + let request = ApplyGitRequest { + cwd: root.to_path_buf(), + diff: "diff --git a/hello.txt b/hello.txt\nnew file mode 100644\n--- /dev/null\n+++ b/hello.txt\n@@ -0,0 +1 @@\n+hello\n".to_string(), + revert: false, + preflight: false, + }; + + let error = apply_git_patch(&request).expect_err("process-relative primary config"); + + assert_eq!(error.kind(), io::ErrorKind::PermissionDenied, "{error}"); + assert!(error.to_string().contains("process-relative"), "{error}"); + assert!(!root.join("hello.txt").exists()); + assert_eq!( + run(root, &["git", "ls-files", "--stage", "-z"]).1, + before_index + ); + } + #[test] fn numstat_path_discovery_does_not_preempt_apply_whitespace_result() { let _g = env_lock().lock().unwrap(); diff --git a/codex-rs/git-utils/src/git_command_tests.rs b/codex-rs/git-utils/src/git_command_tests.rs index 3d191735b3..8d78a002d3 100644 --- a/codex-rs/git-utils/src/git_command_tests.rs +++ b/codex-rs/git-utils/src/git_command_tests.rs @@ -436,6 +436,39 @@ fn config_source_authority_preserves_and_rejects_a_worktree_crossing_spelling() ); } +#[cfg(target_os = "linux")] +#[test] +fn config_source_authority_rejects_direct_and_aliased_procfs_symlinks() { + use std::os::unix::fs::symlink; + + let fixture = tempdir_for_native_git(); + let root = fixture.path().join("repo"); + let alias = fixture.path().join("config-alias"); + std::fs::create_dir_all(&root).expect("create repository"); + run_git(&root, &["init", "-q"]); + symlink("/proc/self/cwd", &alias).expect("create procfs alias"); + let authority = config_authority_for_root(&root); + + for path in [ + PathBuf::from("/proc/self/cwd/config"), + PathBuf::from("/proc/thread-self/cwd/config"), + PathBuf::from("/proc/self/fd/1048575/config"), + PathBuf::from("/proc/4294967295/cwd/config"), + alias.join("config"), + ] { + let error = authority + .ensure_config_source_is_not_worktree_controlled(&path, "test config") + .expect_err("process-relative config source"); + assert_eq!(error.kind(), io::ErrorKind::PermissionDenied, "{error}"); + assert!(error.to_string().contains("process-relative"), "{error}"); + assert!(error.to_string().contains("test config"), "{error}"); + assert!( + error.to_string().contains(&path.display().to_string()), + "{error}" + ); + } +} + #[test] fn config_source_authority_allows_protected_metadata_and_unrelated_external_paths() { let fixture = tempdir_for_native_git(); diff --git a/codex-rs/git-utils/src/git_config_sources_tests.rs b/codex-rs/git-utils/src/git_config_sources_tests.rs index 5a0569d424..2cd7d91ed8 100644 --- a/codex-rs/git-utils/src/git_config_sources_tests.rs +++ b/codex-rs/git-utils/src/git_config_sources_tests.rs @@ -73,6 +73,16 @@ fn assert_worktree_rejection(error: io::Error) { } fn run_isolated_source_test(test_name: &str, env: &[(&str, &OsStr)], removed: &[&str]) { + let cwd = std::env::current_dir().expect("current directory"); + run_isolated_source_test_from(test_name, env, removed, &cwd); +} + +fn run_isolated_source_test_from( + test_name: &str, + env: &[(&str, &OsStr)], + removed: &[&str], + cwd: &Path, +) { let mut command = std::process::Command::new(std::env::current_exe().expect("test binary")); crate::safe_git::isolate_git_command_environment(&mut command); command @@ -80,7 +90,8 @@ fn run_isolated_source_test(test_name: &str, env: &[(&str, &OsStr)], removed: &[ .arg("--exact") .arg("--nocapture") .env("CODEX_GIT_CONFIG_SOURCE_CHILD", "1") - .env("RUST_TEST_THREADS", "1"); + .env("RUST_TEST_THREADS", "1") + .current_dir(cwd); for (name, value) in env { command.env(name, value); } @@ -96,6 +107,90 @@ fn run_isolated_source_test(test_name: &str, env: &[(&str, &OsStr)], removed: &[ ); } +#[cfg(target_os = "linux")] +#[test] +fn rejects_process_relative_primary_config_sources_before_git_changes_cwd() { + use std::os::unix::fs::symlink; + + const TEST_NAME: &str = "git_config_sources::tests::rejects_process_relative_primary_config_sources_before_git_changes_cwd"; + if std::env::var_os("CODEX_GIT_CONFIG_SOURCE_CHILD").is_none() { + let fixture = tempfile::tempdir().expect("fixture"); + let root = fixture.path().join("repo"); + let external = fixture.path().join("external"); + let alias = fixture.path().join("config-alias"); + let chained_alias = fixture.path().join("chained-alias"); + init_repo_at(&root); + std::fs::create_dir(&external).expect("external directory"); + std::fs::write( + external.join("decoy.gitconfig"), + "[safe]\nvalue = external\n", + ) + .expect("external decoy"); + std::fs::write( + external.join(".gitconfig"), + "[safe]\nvalue = external-home\n", + ) + .expect("external HOME config"); + for leaf in ["decoy.gitconfig", "missing-parent.gitconfig"] { + std::fs::write(root.join(leaf), "[unsafe]\nhelper = worktree\n") + .expect("worktree config"); + } + symlink("/proc/self/cwd", &alias).expect("procfs alias"); + symlink("config-alias", &chained_alias).expect("chained procfs alias"); + + let candidates = [ + PathBuf::from("/proc/self/cwd/decoy.gitconfig"), + PathBuf::from("/proc/thread-self/cwd/decoy.gitconfig"), + PathBuf::from("/proc/self/cwd/missing-parent.gitconfig"), + alias.join("decoy.gitconfig"), + chained_alias.join("decoy.gitconfig"), + ]; + for candidate in &candidates { + run_isolated_source_test_from( + TEST_NAME, + &[ + ("CODEX_GIT_CONFIG_SOURCE_ROOT", root.as_os_str()), + ("GIT_CONFIG_GLOBAL", candidate.as_os_str()), + ("GIT_CONFIG_NOSYSTEM", OsStr::new("1")), + ], + &["GIT_CONFIG_SYSTEM"], + &external, + ); + } + run_isolated_source_test_from( + TEST_NAME, + &[ + ("CODEX_GIT_CONFIG_SOURCE_ROOT", root.as_os_str()), + ("GIT_CONFIG_GLOBAL", OsStr::new("")), + ( + "GIT_CONFIG_SYSTEM", + OsStr::new("/proc/self/cwd/decoy.gitconfig"), + ), + ], + &["GIT_CONFIG_NOSYSTEM"], + &external, + ); + run_isolated_source_test_from( + TEST_NAME, + &[ + ("CODEX_GIT_CONFIG_SOURCE_ROOT", root.as_os_str()), + ("HOME", OsStr::new("/proc/self/cwd")), + ("GIT_CONFIG_NOSYSTEM", OsStr::new("1")), + ], + &["GIT_CONFIG_GLOBAL", "GIT_CONFIG_SYSTEM", "XDG_CONFIG_HOME"], + &external, + ); + return; + } + + let root = PathBuf::from( + std::env::var_os("CODEX_GIT_CONFIG_SOURCE_ROOT").expect("fixture repository root"), + ); + let error = guard(&root).expect_err("process-relative primary config source"); + assert_eq!(error.kind(), io::ErrorKind::PermissionDenied, "{error}"); + assert!(error.to_string().contains("process-relative"), "{error}"); +} + #[test] fn rejects_empty_primary_global_and_system_sources_in_the_worktree() { const TEST_NAME: &str = "git_config_sources::tests::rejects_empty_primary_global_and_system_sources_in_the_worktree"; diff --git a/codex-rs/git-utils/src/patch_paths_tests.rs b/codex-rs/git-utils/src/patch_paths_tests.rs index bf2396b22a..66b2319f78 100644 --- a/codex-rs/git-utils/src/patch_paths_tests.rs +++ b/codex-rs/git-utils/src/patch_paths_tests.rs @@ -148,6 +148,31 @@ fn effective_paths_cover_supported_patch_headers() { ); } +#[cfg(target_os = "linux")] +#[test] +fn best_effort_parser_returns_empty_for_process_relative_primary_config() { + const TEST_NAME: &str = + "patch_paths::tests::best_effort_parser_returns_empty_for_process_relative_primary_config"; + if std::env::var_os("CODEX_GIT_UTILS_PATH_ENV_CHILD").is_none() { + run_isolated_test( + TEST_NAME, + &[ + ( + "GIT_CONFIG_GLOBAL", + OsStr::new("/proc/self/cwd/codex-process-relative.gitconfig"), + ), + ("GIT_CONFIG_NOSYSTEM", OsStr::new("1")), + ], + ); + return; + } + + assert_eq!( + extract_paths_from_patch(&new_file_diff("safe.txt")), + Vec::::new() + ); +} + #[test] fn effective_paths_follow_git_for_mismatched_headers() { let mismatch = "diff --git a/safe.txt b/safe.txt\n--- a/nested/file.txt\n+++ b/nested/file.txt\n@@ -1 +1 @@\n-old\n+new\n"; diff --git a/codex-rs/git-utils/src/path_authority.rs b/codex-rs/git-utils/src/path_authority.rs index 1e2157c378..aa843838d8 100644 --- a/codex-rs/git-utils/src/path_authority.rs +++ b/codex-rs/git-utils/src/path_authority.rs @@ -16,6 +16,7 @@ use route_walker::RouteObservationSnapshot; #[cfg(windows)] use route_walker::invalid_data; use route_walker::observe_route; +use route_walker::route_contains_process_relative_procfs_path; #[cfg(any(windows, test))] pub(crate) use windows_path::windows_authority_path_is_ambiguous; #[cfg(windows)] @@ -64,6 +65,13 @@ impl RepositoryRouteBoundaries { &self.metadata_identities, )? != CandidateBoundary::Outside) } + + pub(crate) fn route_contains_process_relative_procfs_path( + &self, + route: &Path, + ) -> io::Result { + route_contains_process_relative_procfs_path(route) + } } pub(crate) fn repository_route_boundaries( diff --git a/codex-rs/git-utils/src/path_authority/route_walker.rs b/codex-rs/git-utils/src/path_authority/route_walker.rs index 12f14df273..b1d883f06e 100644 --- a/codex-rs/git-utils/src/path_authority/route_walker.rs +++ b/codex-rs/git-utils/src/path_authority/route_walker.rs @@ -107,6 +107,82 @@ fn symlink_route_hops(path: &Path) -> io::Result> { Ok(hops) } +/// Detect Linux procfs routes whose target can differ in a later Git process. +/// +/// Existing procfs-hosted symlinks are process-dependent, and missing procfs +/// descendants can materialize when the Git child's PID or descriptors exist. +pub(super) fn route_contains_process_relative_procfs_path(path: &Path) -> io::Result { + #[cfg(target_os = "linux")] + { + let hops = symlink_route_hops(path)?; + for hop in &hops { + if symlink_is_on_procfs(&hop.entry)? { + return Ok(true); + } + } + if missing_descendant_is_below_procfs(path)? { + return Ok(true); + } + for hop in hops { + if missing_descendant_is_below_procfs(&hop.target)? + || missing_descendant_is_below_procfs(&hop.projected)? + { + return Ok(true); + } + } + } + #[cfg(not(target_os = "linux"))] + let _ = path; + Ok(false) +} + +#[cfg(target_os = "linux")] +fn symlink_is_on_procfs(path: &Path) -> io::Result { + filesystem_is_procfs( + path, + rustix::fs::OFlags::PATH | rustix::fs::OFlags::NOFOLLOW | rustix::fs::OFlags::CLOEXEC, + ) +} + +#[cfg(target_os = "linux")] +fn missing_descendant_is_below_procfs(path: &Path) -> io::Result { + match std::fs::symlink_metadata(path) { + Ok(_) => return Ok(false), + Err(error) if is_missing_path_error(&error) => {} + Err(error) => return Err(error), + } + for ancestor in path.ancestors().skip(1) { + match std::fs::symlink_metadata(ancestor) { + Ok(_) => match filesystem_is_procfs( + ancestor, + rustix::fs::OFlags::PATH | rustix::fs::OFlags::CLOEXEC, + ) { + Ok(is_procfs) => return Ok(is_procfs), + Err(error) if is_missing_path_error(&error) => {} + Err(error) => return Err(error), + }, + Err(error) if is_missing_path_error(&error) => {} + Err(error) => return Err(error), + } + } + Err(invalid_data("authority path has no existing ancestor")) +} + +#[cfg(target_os = "linux")] +fn filesystem_is_procfs(path: &Path, flags: rustix::fs::OFlags) -> io::Result { + let fd = rustix::fs::open(path, flags, rustix::fs::Mode::empty()).map_err(io::Error::from)?; + let filesystem = rustix::fs::fstatfs(&fd).map_err(io::Error::from)?; + Ok(filesystem.f_type == rustix::fs::PROC_SUPER_MAGIC) +} + +#[cfg(target_os = "linux")] +fn is_missing_path_error(error: &io::Error) -> bool { + matches!( + error.kind(), + io::ErrorKind::NotFound | io::ErrorKind::NotADirectory + ) +} + fn collect_symlink_route_hops( path: &Path, depth: usize, diff --git a/codex-rs/git-utils/src/path_authority/route_walker_tests.rs b/codex-rs/git-utils/src/path_authority/route_walker_tests.rs index 1b7604d93a..6ff3e68b18 100644 --- a/codex-rs/git-utils/src/path_authority/route_walker_tests.rs +++ b/codex-rs/git-utils/src/path_authority/route_walker_tests.rs @@ -1,6 +1,12 @@ use pretty_assertions::assert_eq; +#[cfg(target_os = "linux")] +use std::path::Path; +#[cfg(target_os = "linux")] +use std::path::PathBuf; use super::read_link_if_symlink; +#[cfg(target_os = "linux")] +use super::route_contains_process_relative_procfs_path; #[test] fn ordinary_paths_are_not_treated_as_failed_symlink_reads() { @@ -16,3 +22,59 @@ fn ordinary_paths_are_not_treated_as_failed_symlink_reads() { ); assert_eq!(read_link_if_symlink(&file).expect("ordinary file"), None); } + +#[cfg(target_os = "linux")] +#[test] +fn process_relative_procfs_detection_follows_aliases_without_rejecting_ordinary_paths() { + use std::os::unix::fs::symlink; + + let fixture = tempfile::tempdir().expect("fixture"); + let external = fixture.path().join("external"); + let ordinary_alias = fixture.path().join("ordinary-alias"); + let procfs_alias = fixture.path().join("procfs-alias"); + let procfs_root_alias = fixture.path().join("procfs-root-alias"); + let chained_alias = fixture.path().join("chained-alias"); + let literal = fixture.path().join("proc/self/cwd/config"); + std::fs::create_dir_all(&external).expect("external directory"); + std::fs::create_dir_all(literal.parent().expect("literal parent")) + .expect("literal directories"); + std::fs::write(&literal, "[safe]\n").expect("literal config"); + symlink(&external, &ordinary_alias).expect("ordinary alias"); + symlink("/proc/self/cwd", &procfs_alias).expect("procfs alias"); + symlink("/proc", &procfs_root_alias).expect("procfs root alias"); + symlink("procfs-alias", &chained_alias).expect("chained alias"); + + let mut procfs_paths = vec![ + PathBuf::from("/proc/self/cwd/missing-config"), + PathBuf::from("/proc/thread-self/cwd/missing-config"), + PathBuf::from("/proc/self/fd/1048575/missing-config"), + PathBuf::from("/proc/self/exe"), + PathBuf::from(format!("/proc/{}/cwd/missing-config", std::process::id())), + PathBuf::from("/proc/4294967295/cwd/missing-config"), + procfs_alias.join("missing-config"), + procfs_root_alias.join("4294967295/cwd/missing-config"), + chained_alias.join("missing-config"), + ]; + if Path::new("/dev/fd").exists() { + procfs_paths.push(PathBuf::from("/dev/fd/1048575/missing-config")); + } + for path in procfs_paths { + assert!( + route_contains_process_relative_procfs_path(&path).expect("inspect procfs route"), + "expected process-relative procfs route in {}", + path.display() + ); + } + + for path in [ + PathBuf::from("/proc/version"), + ordinary_alias.join("missing-config"), + literal, + ] { + assert!( + !route_contains_process_relative_procfs_path(&path).expect("inspect ordinary route"), + "unexpected process-relative procfs route in {}", + path.display() + ); + } +} diff --git a/codex-rs/git-utils/src/repository_authority/authority/policy.rs b/codex-rs/git-utils/src/repository_authority/authority/policy.rs index e5f2379460..b8c43f18f7 100644 --- a/codex-rs/git-utils/src/repository_authority/authority/policy.rs +++ b/codex-rs/git-utils/src/repository_authority/authority/policy.rs @@ -41,6 +41,12 @@ impl RepositoryAuthority { if !path.is_absolute() { return Err(worktree_controlled_config_source(path, description)); } + if self + .route_boundaries + .route_contains_process_relative_procfs_path(path)? + { + return Err(process_relative_config_source(path, description)); + } let inspection = self.route_boundaries.inspect_route(path)?; if inspection.crosses_worktree { return Err(worktree_controlled_config_source(path, description)); @@ -178,3 +184,13 @@ fn worktree_controlled_config_source(path: &Path, description: &str) -> io::Erro ), ) } + +fn process_relative_config_source(path: &Path, description: &str) -> io::Error { + io::Error::new( + io::ErrorKind::PermissionDenied, + format!( + "refusing to use process-relative {description}: {}", + path.display() + ), + ) +} From a69db7b619bf380327fc7369dd2b0fc98590314d Mon Sep 17 00:00:00 2001 From: Chris Bookholt Date: Mon, 6 Jul 2026 15:34:53 -0700 Subject: [PATCH 18/20] fix(deps): update crossbeam-epoch --- codex-rs/Cargo.lock | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index aa27d32491..726fec4823 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -4822,9 +4822,9 @@ dependencies = [ [[package]] name = "crossbeam-epoch" -version = "0.9.18" +version = "0.9.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" +checksum = "2d6914041f254d6e9176c01941b21115dcfb7089e55135a35411081bd106ef3f" dependencies = [ "crossbeam-utils", ] From 2642f7aad49e3d5c878f7cb8f5b6d2bb530ec5c0 Mon Sep 17 00:00:00 2001 From: Chris Bookholt Date: Mon, 6 Jul 2026 15:38:36 -0700 Subject: [PATCH 19/20] fix(deps): refresh Bazel crate lock --- MODULE.bazel.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MODULE.bazel.lock b/MODULE.bazel.lock index d572f68f21..3356b66c14 100644 --- a/MODULE.bazel.lock +++ b/MODULE.bazel.lock @@ -804,7 +804,7 @@ "critical-section_1.2.0": "{\"dependencies\":[],\"features\":{\"restore-state-bool\":[],\"restore-state-none\":[],\"restore-state-u16\":[],\"restore-state-u32\":[],\"restore-state-u64\":[],\"restore-state-u8\":[],\"restore-state-usize\":[],\"std\":[\"restore-state-bool\"]}}", "crossbeam-channel_0.5.15": "{\"dependencies\":[{\"default_features\":false,\"name\":\"crossbeam-utils\",\"req\":\"^0.8.18\"},{\"kind\":\"dev\",\"name\":\"num_cpus\",\"req\":\"^1.13.0\"},{\"kind\":\"dev\",\"name\":\"rand\",\"req\":\"^0.8\"},{\"kind\":\"dev\",\"name\":\"signal-hook\",\"req\":\"^0.3\"}],\"features\":{\"default\":[\"std\"],\"std\":[\"crossbeam-utils/std\"]}}", "crossbeam-deque_0.8.6": "{\"dependencies\":[{\"default_features\":false,\"name\":\"crossbeam-epoch\",\"req\":\"^0.9.17\"},{\"default_features\":false,\"name\":\"crossbeam-utils\",\"req\":\"^0.8.18\"},{\"kind\":\"dev\",\"name\":\"rand\",\"req\":\"^0.8\"}],\"features\":{\"default\":[\"std\"],\"std\":[\"crossbeam-epoch/std\",\"crossbeam-utils/std\"]}}", - "crossbeam-epoch_0.9.18": "{\"dependencies\":[{\"default_features\":false,\"name\":\"crossbeam-utils\",\"req\":\"^0.8.18\"},{\"name\":\"loom-crate\",\"optional\":true,\"package\":\"loom\",\"req\":\"^0.7.1\",\"target\":\"cfg(crossbeam_loom)\"},{\"kind\":\"dev\",\"name\":\"rand\",\"req\":\"^0.8\"}],\"features\":{\"alloc\":[],\"default\":[\"std\"],\"loom\":[\"loom-crate\",\"crossbeam-utils/loom\"],\"nightly\":[\"crossbeam-utils/nightly\"],\"std\":[\"alloc\",\"crossbeam-utils/std\"]}}", + "crossbeam-epoch_0.9.20": "{\"dependencies\":[{\"default_features\":false,\"name\":\"crossbeam-utils\",\"req\":\"^0.8.18\"},{\"name\":\"loom-crate\",\"optional\":true,\"package\":\"loom\",\"req\":\"^0.7.1\",\"target\":\"cfg(crossbeam_loom)\"},{\"kind\":\"dev\",\"name\":\"rand\",\"req\":\"^0.8\"}],\"features\":{\"alloc\":[],\"default\":[\"std\"],\"loom\":[\"loom-crate\",\"crossbeam-utils/loom\"],\"nightly\":[\"crossbeam-utils/nightly\"],\"std\":[\"alloc\",\"crossbeam-utils/std\"]}}", "crossbeam-queue_0.3.12": "{\"dependencies\":[{\"default_features\":false,\"name\":\"crossbeam-utils\",\"req\":\"^0.8.18\"},{\"kind\":\"dev\",\"name\":\"rand\",\"req\":\"^0.8\"}],\"features\":{\"alloc\":[],\"default\":[\"std\"],\"nightly\":[\"crossbeam-utils/nightly\"],\"std\":[\"alloc\",\"crossbeam-utils/std\"]}}", "crossbeam-utils_0.8.21": "{\"dependencies\":[{\"name\":\"loom\",\"optional\":true,\"req\":\"^0.7.1\",\"target\":\"cfg(crossbeam_loom)\"},{\"kind\":\"dev\",\"name\":\"rand\",\"req\":\"^0.8\"}],\"features\":{\"default\":[\"std\"],\"nightly\":[],\"std\":[]}}", "crossterm_winapi_0.9.1": "{\"dependencies\":[{\"features\":[\"winbase\",\"consoleapi\",\"processenv\",\"handleapi\",\"synchapi\",\"impl-default\"],\"name\":\"winapi\",\"req\":\"^0.3.8\",\"target\":\"cfg(windows)\"}],\"features\":{}}", From 26ce6873cd02fd381bdce026046c262a9fd79eba Mon Sep 17 00:00:00 2001 From: Chris Bookholt Date: Mon, 6 Jul 2026 16:13:28 -0700 Subject: [PATCH 20/20] test(git): assert Windows sharing violation --- codex-rs/git-utils/src/git_command_tests.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/codex-rs/git-utils/src/git_command_tests.rs b/codex-rs/git-utils/src/git_command_tests.rs index ab563f94f9..b0a77aac26 100644 --- a/codex-rs/git-utils/src/git_command_tests.rs +++ b/codex-rs/git-utils/src/git_command_tests.rs @@ -1599,7 +1599,12 @@ fn active_worktree_identity_pins_repository_for_runner_lifetime() { let error = std::fs::rename(&root, &moved_root) .expect_err("live repository identity handle must block replacement"); - assert_eq!(error.kind(), io::ErrorKind::PermissionDenied, "{error}"); + const WINDOWS_ERROR_SHARING_VIOLATION: i32 = 32; + assert_eq!( + error.raw_os_error(), + Some(WINDOWS_ERROR_SHARING_VIOLATION), + "{error}" + ); assert!(root.join(".git").is_dir()); assert!(!moved_root.exists());