Validate linked worktrees before inheriting project trust (#39616)

## Why

A checkout could point its `.git` file at a trusted repository's worktree
directory without proving that the repository had registered that checkout.
This could cause project configuration from an unrelated checkout to be treated
as trusted.

## What changed

- Verify the linked worktree's `gitdir` backlink, `commondir`, registered
  checkout, and main checkout ownership before resolving the main repository's
  trust key.
- Reject missing, oversized, symlinked, mismatched, or swapped Git metadata.
- Preserve valid linked worktrees that use path aliases, separate Git
  directories, or non-UTF-8 POSIX paths.

## Testing

Add resolver and config-loading coverage for forged worktrees, metadata races,
case-sensitive paths, moved worktrees, and host MCP startup from project config.

GitOrigin-RevId: 6052a7d10ad2d613436f20175c356abdef8c758e
This commit is contained in:
Jeremy Rose authored and copyberry committed 2026-08-20 06:29:32 +00:00
1 parent 250b5ea2bf
commit bc3545b805
12 files changed
+859 -80

No files matched your search

@@ -103,7 +103,13 @@ async fn write_linked_worktree_pointer(
worktree_root.join(".git"),
format!("gitdir: {}\n", worktree_git_dir.display()),
)
.await
.await?;
tokio::fs::write(
worktree_git_dir.join("gitdir"),
format!("{}\n", worktree_root.join(".git").display()),
)
.await?;
tokio::fs::write(worktree_git_dir.join("commondir"), "../..\n").await
}
async fn write_project_hook_config(
@@ -2835,6 +2841,59 @@ async fn linked_worktree_project_layers_keep_worktree_config_but_use_root_repo_h
Ok(())
}
#[tokio::test]
async fn forged_linked_worktree_does_not_inherit_repo_trust() -> std::io::Result<()> {
let tmp = tempdir()?;
let trusted_root = tmp.path().join("trusted");
let attacker_root = tmp.path().join("attacker");
tokio::fs::create_dir_all(trusted_root.join(".git")).await?;
tokio::fs::create_dir_all(attacker_root.join(".codex")).await?;
tokio::fs::write(
attacker_root.join(".git"),
format!(
"gitdir: {}\n",
trusted_root.join(".git/worktrees/missing").display()
),
)
.await?;
tokio::fs::write(
attacker_root.join(".codex").join(CONFIG_TOML_FILE),
r#"foo = "attacker"
"#,
)
.await?;
let codex_home = tmp.path().join("home");
tokio::fs::create_dir_all(&codex_home).await?;
make_config_for_test(
&codex_home,
&trusted_root,
TrustLevel::Trusted,
/*project_root_markers*/ None,
)
.await?;
let layers = load_config_layers_state(
LOCAL_FS.as_ref(),
&codex_home,
Some(AbsolutePathBuf::from_absolute_path(&attacker_root)?),
&[] as &[(String, TomlValue)],
LoaderOverrides::default(),
&codex_config::NoopThreadConfigLoader,
)
.await?;
let project_layers = layers
.all_layers_high_to_low()
.filter(|layer| matches!(layer.name, ConfigLayerSource::Project { .. }))
.collect::<Vec<_>>();
assert_eq!(project_layers.len(), 1);
assert!(project_layers[0].disabled_reason.is_some());
assert_eq!(layers.effective_config().get("foo"), None);
Ok(())
}
#[tokio::test]
async fn malformed_untrusted_linked_worktree_does_not_read_root_hooks() -> std::io::Result<()> {
let tmp = tempdir()?;