GITv

Run SSH git with the same clean environment as every other git

Browse files

serve_git cleared the environment by hand and so left out the settings the core helper adds: receive-pack and upload-pack over SSH still read /etc/gitconfig, unlike every other git GITv runs. They now share the core's setup, and the GIT_PROTOCOL check that SSH and HTTPS each had their own copy of lives in core too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Author
Um9i vulgarmessiah@gmail.com
Date
22 Nov 2026, 22:08
Commit
4788d2fc41a7
Parent
26d14b5

3 files changed +24−9

@@ -58,7 +58,10 @@ pub fn git_base() -> Command {
command
}
fn configure(command: &mut Command) {
/// Give any git program (`git`, or `git-receive-pack` run directly) the same clean environment
/// as [`git_base`]: no inherited variables, no user or system config, stdin closed until the
/// caller says otherwise.
pub fn configure(command: &mut Command) {
command
.env_clear()
.env("PATH", "/usr/bin:/bin")
@@ -288,6 +291,12 @@ fn run_git<S: AsRef<OsStr>>(
}
}
/// A client's requested git protocol (`version=2`, say), when it is well formed, to pass on as
/// `GIT_PROTOCOL`, as sshd and `git http-backend` do. Anything else is ignored.
pub fn git_protocol(value: &str) -> Option<&str> {
(value.len() <= 64 && value.bytes().all(|b| b.is_ascii_alphanumeric() || b"=:.".contains(&b))).then_some(value)
}
/// Make a rename or new file in `path`'s directory durable: until the directory itself is
/// synced, a crash can leave the old name, or none, in place of what was just published.
pub fn sync_directory_of(path: &Path) -> std::io::Result<()> {
@@ -493,6 +502,15 @@ mod tests {
fs::remove_dir_all(&dir.path).unwrap();
}
#[test]
fn only_well_formed_protocol_requests_pass() {
assert_eq!(git_protocol("version=2"), Some("version=2"));
assert_eq!(git_protocol("version=2:value=1"), Some("version=2:value=1"));
for bad in ["version=2\n", "a b", "x;rm", &"v".repeat(65)] {
assert_eq!(git_protocol(bad), None, "{bad:?}");
}
}
#[test]
fn capped_output_stops_early_and_says_so() {
let (dir, oid) = repo_with_blob("capped", 1 << 20);
@@ -32,9 +32,7 @@ pub async fn refs(
/// The protocol the client asked for in its `Git-Protocol` header (`version=2`, say), when it
/// is well formed, to hand to upload-pack as `GIT_PROTOCOL`, as `git http-backend` does.
fn protocol(headers: &HeaderMap) -> Option<String> {
let value = headers.get("git-protocol")?.to_str().ok()?;
(value.len() <= 64 && value.bytes().all(|b| b.is_ascii_alphanumeric() || b"=:.".contains(&b)))
.then(|| value.to_string())
gitv_core::git_protocol(headers.get("git-protocol")?.to_str().ok()?).map(str::to_string)
}
pub async fn pack(Path(path): Path<(String, String)>, client: Client, request: Request) -> Response {
@@ -651,13 +651,12 @@ fn serve_git(owner: &str, program: &str, path: &str) -> Result<u8, String> {
};
// Run inside the opened directory, so the path can't be swapped for a symlink meanwhile.
let mut command = repo.command(program);
command.arg(".").env_clear().env("PATH", "/usr/bin:/bin").env("HOME", "/var/empty");
// The same clean environment as every other git GITv runs, system config included.
gitv_core::configure(&mut command);
command.arg(".");
// Pass the client's protocol request through (when sshd accepts it), so clients can use the
// faster protocol v2.
if let Ok(protocol) = std::env::var("GIT_PROTOCOL")
&& protocol.len() <= 64
&& protocol.bytes().all(|b| b.is_ascii_alphanumeric() || b"=:.".contains(&b))
{
if let Some(protocol) = std::env::var("GIT_PROTOCOL").ok().as_deref().and_then(gitv_core::git_protocol) {
command.env("GIT_PROTOCOL", protocol);
}
let pushing = program == "git-receive-pack";