Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(tools): neutralise core.hooksPath and commit.gpgSign at execution time\n\nTwo findings from review:\n\n- TOCTOU between the config inspection and the real git invocation: they\n are separate processes, so a core.hooksPath set in the gap (a second\n concurrent writer, or a worktree-scoped write) would be invisible to\n the inspection but still reach the real command, and commit/checkout\n both run hooks. core.hooksPath is now neutralised with -c at the point\n hardened_git actually runs, independent of what the inspection saw.\n Verified there is in fact a portable "nowhere" value for it (the same\n null path already used for GIT_CONFIG_GLOBAL) - a previous comment\n claiming otherwise was wrong.\n- commit.gpgsign is on the allowlist as an ordinary boolean, but left\n un-neutralised a repository could force every commit through this\n tool to be signed with whatever key the host has configured, without\n the operator asking for that. Overridden the same way core.editor\n already is.\n\nBoth are added as explicit -c args in hardened_git rather than the\nstatic NEUTRALISED_CONFIG list, since hooksPath's safe value is\nplatform-dependent (reuses NULL_CONFIG_PATH).\n\nAdds two tests that call hardened_git directly, skipping the allowlist\ninspection entirely, to prove the execution-time override holds on its\nown rather than depending on the inspection having caught anything.\n

rsd-darshan9fd488dff980
diff --git a/src/openhuman/tools/impl/filesystem/git_operations.rs b/src/openhuman/tools/impl/filesystem/git_operations.rs
index 7fb904deac..b81b1e147d 100644
--- a/src/openhuman/tools/impl/filesystem/git_operations.rs
+++ b/src/openhuman/tools/impl/filesystem/git_operations.rs
@@ -522,22 +522,31 @@ const ALLOWED_REPO_CONFIG: &[&str] = &[
/// Command-valued keys cleared on the command line as a second layer.
///
/// Command-line `-c` outranks every config file, so this genuinely
-/// neutralises these keys even when a repository sets them. It is a denylist
-/// and therefore cannot be the guarantee — [`ALLOWED_REPO_CONFIG`] is — but it
-/// costs nothing and it means a key that slips past the allowlist check still
-/// does not run. `core.hooksPath` is deliberately absent: there is no portable
-/// value that means "nowhere", and none of `status` / `log` / `diff` /
-/// `branch` run hooks anyway. An unrecognised `core.hookspath` is refused by
-/// [`ALLOWED_REPO_CONFIG`], which is the layer that is supposed to catch it.
+/// neutralises these keys even when a repository sets them — and it does so
+/// at the moment `git` actually runs, not at the moment
+/// [`first_disallowed_repo_config_key`] happened to inspect the config a
+/// moment earlier. That distinction matters: the inspection and the real
+/// command are two separate `git` invocations, so a key set in the gap
+/// between them (a second concurrent writer, or a worktree-scoped write) is
+/// invisible to the inspection but still reaches here — where it is
+/// neutralised regardless of timing. It is a denylist and therefore cannot be
+/// the *only* guarantee — [`ALLOWED_REPO_CONFIG`] is the one that fails
+/// closed on a key nobody anticipated — but this layer is what actually holds
+/// under a race, not the inspection step.
///
/// `credential.helper` is command-valued — a value beginning `!` is run as a
/// shell command — so it belongs nowhere near [`ALLOWED_REPO_CONFIG`] despite
-/// reading like a mere preference; it is refused there.
+/// reading like a mere preference; it is refused there instead, since none of
+/// this tool's operations reach a remote and so have no legitimate use for it
+/// to preserve.
///
/// A `git lfs install` clone is refused, deliberately: `filter.lfs.clean`,
/// `.smudge` and `.process` each name a program, so an LFS working copy
/// cannot be read by this tool. That is the fail-closed answer and it is the
/// intended one. Only `filter.<driver>.required`, a boolean, is allowed.
+///
+/// `core.hooksPath` and `commit.gpgSign` are handled separately, in
+/// [`hardened_git`] — see there for why.
const NEUTRALISED_CONFIG: &[&str] = &[
"core.fsmonitor=",
"core.sshCommand=",
@@ -583,13 +592,43 @@ fn suppress_ambient_git_config(cmd: &mut tokio::process::Command) -> &mut tokio:
///
/// Layers [`suppress_ambient_git_config`] under the [`NEUTRALISED_CONFIG`]
/// `-c` overrides, which outrank every config file the repository itself
-/// could set.
+/// could set — including one set in the gap between
+/// [`first_disallowed_repo_config_key`]'s inspection and this invocation.
+///
+/// Two more `-c` overrides are added here rather than in the static
+/// [`NEUTRALISED_CONFIG`] list, because their safe value is not a fixed
+/// literal:
+///
+/// - `core.hooksPath=` pointed at [`NULL_CONFIG_PATH`]. `commit` and
+/// `checkout` are the two operations this tool exposes that run hooks
+/// (`pre-commit`/`commit-msg`/`post-commit`, `post-checkout`), and a
+/// repository-writable `core.hooksPath` naming a directory with an
+/// executable `pre-commit` in it is exactly the shape of the worktree-scoped
+/// bypass this hardening exists to close — verified directly: pointing
+/// `core.hooksPath` at a directory containing a `pre-commit` that touches a
+/// marker file, then running with `-c core.hooksPath=<null path>`, leaves
+/// the marker untouched. A previous version of this comment claimed there
+/// was no portable value that meant "nowhere"; there is — the same one
+/// [`suppress_ambient_git_config`] already uses for `GIT_CONFIG_GLOBAL`,
+/// since a location with nothing at it is exactly what git needs it to be.
+/// - `commit.gpgSign=false`. `commit.gpgsign` is on [`ALLOWED_REPO_CONFIG`] as
+/// an ordinary boolean, but a repository could still set it to force every
+/// commit through this tool to be GPG-signed — with whatever real signing
+/// key the *host* happens to have configured, silently attributing a
+/// cryptographic signature to a commit the operator did not ask this tool to
+/// sign. Overriding it here removes that decision from the repository
+/// entirely, the same way `core.editor=false` removes commit message
+/// editing from it in [`NEUTRALISED_CONFIG`] above.
fn hardened_git(dir: &Path) -> tokio::process::Command {
let mut cmd = tokio::process::Command::new("git");
suppress_ambient_git_config(&mut cmd).current_dir(dir);
for kv in NEUTRALISED_CONFIG {
cmd.arg("-c").arg(kv);
}
+ cmd.arg("-c")
+ .arg(format!("core.hooksPath={NULL_CONFIG_PATH}"))
+ .arg("-c")
+ .arg("commit.gpgSign=false");
cmd
}
diff --git a/src/openhuman/tools/impl/filesystem/git_operations_tests.rs b/src/openhuman/tools/impl/filesystem/git_operations_tests.rs
index 97f4f2229c..b88e381e02 100644
--- a/src/openhuman/tools/impl/filesystem/git_operations_tests.rs
+++ b/src/openhuman/tools/impl/filesystem/git_operations_tests.rs
@@ -733,6 +733,108 @@ async fn a_hookspath_hidden_in_worktree_scoped_config_is_still_refused() {
);
}
+/// The allowlist inspection and the real command are two separate `git`
+/// invocations, so a config change landing in the gap between them would be
+/// invisible to the first and still reach the second. This test calls
+/// `hardened_git` directly — skipping `first_disallowed_repo_config_key`
+/// entirely, standing in for that gap — to prove the second invocation does
+/// not depend on the first having caught anything: `core.hooksPath` is
+/// neutralised at the point of execution regardless of what any inspection
+/// saw or missed.
+#[cfg(unix)]
+#[tokio::test]
+async fn hardened_git_neutralises_hookspath_even_if_the_allowlist_check_never_ran() {
+ let tmp = TempDir::new().unwrap();
+ init_git_repo(tmp.path());
+ set_config(tmp.path(), "user.email", "[email protected]");
+ set_config(tmp.path(), "user.name", "Test");
+
+ let hooks_dir = tmp.path().join("evil-hooks");
+ std::fs::create_dir(&hooks_dir).unwrap();
+ let marker = tmp.path().join("HOOK_RAN");
+ std::fs::write(
+ hooks_dir.join("pre-commit"),
+ format!("#!/bin/sh\ntouch {:?}\nexit 0\n", marker.to_string_lossy()),
+ )
+ .unwrap();
+ {
+ use std::os::unix::fs::PermissionsExt;
+ std::fs::set_permissions(
+ hooks_dir.join("pre-commit"),
+ std::fs::Permissions::from_mode(0o755),
+ )
+ .unwrap();
+ }
+ set_config(tmp.path(), "core.hooksPath", &hooks_dir.to_string_lossy());
+
+ std::fs::write(tmp.path().join("f.txt"), "hi").unwrap();
+ let staged = hermetic(
+ std::process::Command::new("git")
+ .args(["add", "f.txt"])
+ .current_dir(tmp.path()),
+ )
+ .status()
+ .unwrap()
+ .success();
+ assert!(staged, "failed to stage the test file");
+
+ let output = super::hardened_git(tmp.path())
+ .args(["commit", "-m", "msg"])
+ .output()
+ .await
+ .unwrap();
+
+ assert!(
+ output.status.success(),
+ "commit should succeed: {}",
+ String::from_utf8_lossy(&output.stderr)
+ );
+ assert!(
+ !marker.exists(),
+ "hardened_git ran the repository-configured pre-commit hook — the \
+ override that is supposed to hold even without the allowlist check \
+ did not"
+ );
+}
+
+/// `commit.gpgsign` is on `ALLOWED_REPO_CONFIG` as an ordinary boolean, but
+/// left un-neutralised it would let a repository force every commit through
+/// this tool to be signed with whatever key the host happens to have
+/// configured. If the override in `hardened_git` were missing or wrong, this
+/// commit would fail (no usable signing key in a test environment) rather
+/// than succeed.
+#[tokio::test]
+async fn hardened_git_neutralises_forced_commit_signing() {
+ let tmp = TempDir::new().unwrap();
+ init_git_repo(tmp.path());
+ set_config(tmp.path(), "user.email", "[email protected]");
+ set_config(tmp.path(), "user.name", "Test");
+ set_config(tmp.path(), "commit.gpgsign", "true");
+
+ std::fs::write(tmp.path().join("f.txt"), "hi").unwrap();
+ let staged = hermetic(
+ std::process::Command::new("git")
+ .args(["add", "f.txt"])
+ .current_dir(tmp.path()),
+ )
+ .status()
+ .unwrap()
+ .success();
+ assert!(staged, "failed to stage the test file");
+
+ let output = super::hardened_git(tmp.path())
+ .args(["commit", "-m", "msg"])
+ .output()
+ .await
+ .unwrap();
+
+ assert!(
+ output.status.success(),
+ "commit.gpgsign=true must not be honoured, got: {}",
+ String::from_utf8_lossy(&output.stderr)
+ );
+}
+
/// The config-inspection step must fail closed: if `git config --list
/// --local` cannot be read, that is not the same as "nothing to distrust",
/// and running the real command anyway would skip the check entirely.