Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.
Rust ai-agentsai-assistantai-botautomationbotbots-teamchatbotclaude-duetgrokgrok-botgrok-bot-alternativeharnessllmmulti-agentopenai-dotsopenbotpersonal-agentrustself-hostedselfhosted-ai
Dev floor: head_branch reads .git through the shared safe reader (0.7.7) (#444)
Oleksandr Ivanchenko07c342582d1a
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex ff6f4c88..de146685 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).### Fixed- **A checkpoint's git commit only comes from the task's own git files.** When a mission takes a checkpoint, kwant notes the commit the task's folder is on. It now never follows a symlink inside `.git`, ignores a `ref:` that points outside it, skips FIFOs and oversized files, and keeps the value only when it's a real 40- or 64-character commit id. Before, a bot that could write its task folder could make a checkpoint show the first 64 characters of another file kwant can read. (#443)+- **Dev mode reads the checked-out branch safely too.** The production safeguard reads which branch a bot's folder is on. A planted FIFO could stall that check, and a symlink, a hard link or a `.git` file could point it at another file. It now reads `.git` the same careful way checkpoints do, and only follows a worktree's pointer into a real `.git/worktrees/` folder. Checkpoints also now skip a hard-linked git file. (#444)## [Mac 0.7.6] - 2026-11-08diff --git a/src/server/dev/floor.rs b/src/server/dev/floor.rsindex 2fc1fdb5..6fae0ce7 100644--- a/src/server/dev/floor.rs+++ b/src/server/dev/floor.rs@@ -7,8 +7,9 @@use crate::db::Database;use crate::mcp::prod_floor::{self, DevCtx, Floor};use crate::mcp::ToolApprovalRequest;+use crate::server::gitfile::{self, plain_ref};use serde_json::{json, Value};-use std::path::Path;+use std::path::{Component, Path, PathBuf};/// The Dev mode context of a bot's space, or `None` when Dev mode is off there (no floor)./// With Dev mode off in every space it reads nothing.@@ -61,29 +62,82 @@ pub fn prod_names<'a>(marked: &[String], servers: impl Iterator<Item = (&'a str,/// The branch checked out in `cwd` (walking up to its `.git`), from `HEAD` alone: no git process./// `None` for a detached head or no repo.+///+/// `cwd` can be a bot's folder, so its `.git` is untrusted: every file is read through+/// [`gitfile::read`] (no symlink, FIFO, hard link or big file, so this never blocks), a `.git`+/// that is a symlink is no repo, a worktree's `gitdir:` is followed only as [`worktree_head`]+/// allows, and the branch must be a plain ref.pub fn head_branch(cwd: &Path) -> Option<String> {let mut dir = Some(cwd);for _ in 0..12 {let d = dir?;- let git = d.join(".git");- let head_file = if git.is_dir() {- git.join("HEAD")- } else if git.is_file() {- // A worktree: `.git` says `gitdir: <path>`- let text = std::fs::read_to_string(&git).ok()?;- let gd = text.trim().strip_prefix("gitdir:")?.trim().to_string();- let gd = if Path::new(&gd).is_absolute() { Path::new(&gd).to_path_buf() } else { d.join(gd) };- gd.join("HEAD")- } else {- dir = d.parent();- continue;+ let head = match std::fs::symlink_metadata(d.join(".git")) {+ Err(_) => {+ dir = d.parent();+ continue;+ }+ Ok(m) if m.is_dir() => gitfile::read(&d.join(".git"), "HEAD", gitfile::REF_MAX)?,+ Ok(m) if m.is_file() => worktree_head(d)?,+ Ok(_) => return None,};- let head = std::fs::read_to_string(head_file).ok()?;- return head.trim().strip_prefix("ref: refs/heads/").map(str::to_string);+ let r = head.trim().strip_prefix("ref: ")?.trim();+ return r.strip_prefix("refs/heads/").filter(|_| plain_ref(r)).map(str::to_string);}None}+/// `HEAD` of the worktree whose `.git` file is in `d`. The file must say+/// `gitdir: <repo>/.git/worktrees/<name>` (relative to `d` unless absolute). `.` and `..` are+/// resolved on the text by [`lexical`], so only a path that was checked is opened, and every+/// folder on it that `d`'s own path doesn't already name (always `.git`, `worktrees` and `<name>`)+/// must be a real folder, not a symlink. The kernel and git take `link/..` to the parent of the+/// link's *target*, not to the folder the text names, so a `..` that would climb over a symlink+/// (in the gitdir or in `d` itself) makes this `None` rather than read a different `HEAD` than+/// git would (F-3). A common sibling worktree still reads. Folder races between the checks and+/// the open remain, as in `discover::read_file`; the 0.7.8 openat walk closes them.+fn worktree_head(d: &Path) -> Option<String> {+ let text = gitfile::read(d, ".git", gitfile::REF_MAX)?;+ let gd = text.trim().strip_prefix("gitdir:")?.trim();+ let here = lexical(d)?;+ let target = lexical(&d.join(gd))?;+ let parts: Vec<Component> = target.components().collect();+ let [.., Component::Normal(g), Component::Normal(w), Component::Normal(_)] = parts[..] else {+ return None;+ };+ if g.to_str() != Some(".git") || w.to_str() != Some("worktrees") {+ return None;+ }+ let shared = parts.iter().zip(here.components()).take_while(|(a, b)| **a == *b).count().min(parts.len() - 3);+ let mut at: PathBuf = parts[..shared].iter().collect();+ for c in &parts[shared..] {+ at.push(c);+ if !std::fs::symlink_metadata(&at).ok()?.file_type().is_dir() {+ return None;+ }+ }+ gitfile::read(&target, "HEAD", gitfile::REF_MAX)+}++/// `p` with `.` dropped and each `..` taken off the text, but only where the kernel would land in+/// the same folder: `a/..` is `.` only when `a` is a real folder, not a symlink (`link/..` is the+/// link target's parent). A `..` after a symlink, or past the start, is `None`.+fn lexical(p: &Path) -> Option<PathBuf> {+ let mut out = PathBuf::new();+ for c in p.components() {+ match c {+ Component::CurDir => {}+ Component::ParentDir => {+ let real_dir = !out.as_os_str().is_empty() && std::fs::symlink_metadata(&out).is_ok_and(|m| m.file_type().is_dir());+ if !real_dir || !out.pop() {+ return None;+ }+ }+ c => out.push(c),+ }+ }+ Some(out)+}+/// The card's `prod_floor` object: `{kind, why, blast, needs_backup, space_id, backup}`.pub async fn payload(db: &Database, space_id: &str, floor: &Floor, ctx: &DevCtx) -> Value {let mut v = serde_json::to_value(floor).unwrap_or_else(|_| json!({}));@@ -161,6 +215,193 @@ mod tests {assert_eq!(head_branch(Path::new("/nonexistent/kwnt-dev")), None);}+ /// A repo with `.git/HEAD` on `lin-dev-core` in a temp folder.+ fn head_repo() -> (tempfile::TempDir, PathBuf) {+ let d = tempfile::tempdir().unwrap();+ let root = d.path().join("repo");+ std::fs::create_dir_all(root.join(".git")).unwrap();+ std::fs::write(root.join(".git/HEAD"), "ref: refs/heads/lin-dev-core\n").unwrap();+ (d, root)+ }++ /// `head_branch` on another thread, failing (not hanging) after 5 s.+ fn head_within_5s(cwd: &Path) -> Option<String> {+ let (tx, rx) = std::sync::mpsc::channel();+ let cwd = cwd.to_path_buf();+ std::thread::spawn(move || tx.send(head_branch(&cwd)).unwrap());+ rx.recv_timeout(std::time::Duration::from_secs(5)).expect("head_branch blocked")+ }++ #[cfg(unix)]+ fn mkfifo(p: &Path) {+ let c = std::ffi::CString::new(p.as_os_str().as_encoded_bytes()).unwrap();+ assert_eq!(unsafe { libc::mkfifo(c.as_ptr(), 0o644) }, 0);+ }++ /// F-1: a planted FIFO as HEAD, as a worktree's `.git` file or as its HEAD never blocks the+ /// approval check.+ #[cfg(unix)]+ #[test]+ fn head_branch_does_not_block_on_a_fifo() {+ let (d, root) = head_repo();+ std::fs::remove_file(root.join(".git/HEAD")).unwrap();+ mkfifo(&root.join(".git/HEAD"));+ assert_eq!(head_within_5s(&root), None, "HEAD FIFO");+ let wt = d.path().join("wt");+ std::fs::create_dir_all(&wt).unwrap();+ mkfifo(&wt.join(".git"));+ assert_eq!(head_within_5s(&wt), None, ".git FIFO");+ std::fs::remove_file(wt.join(".git")).unwrap();+ std::fs::create_dir_all(root.join(".git/worktrees/wt")).unwrap();+ std::fs::write(wt.join(".git"), format!("gitdir: {}\n", root.join(".git/worktrees/wt").display())).unwrap();+ mkfifo(&root.join(".git/worktrees/wt/HEAD"));+ assert_eq!(head_within_5s(&wt), None, "worktree HEAD FIFO");+ }++ /// F-2: a HEAD that is another name for some other file isn't read.+ #[cfg(unix)]+ #[test]+ fn head_branch_never_reads_a_hard_link() {+ let (d, root) = head_repo();+ let host = d.path().join("host-file");+ std::fs::write(&host, "ref: refs/heads/LEAKED-FROM-OUTSIDE\n").unwrap();+ std::fs::remove_file(root.join(".git/HEAD")).unwrap();+ std::fs::hard_link(&host, root.join(".git/HEAD")).unwrap();+ assert_eq!(head_within_5s(&root), None);+ }++ #[cfg(unix)]+ #[test]+ fn head_branch_never_follows_a_symlink() {+ use std::os::unix::fs::symlink;+ let (d, root) = head_repo();+ let host = d.path().join("host-file");+ std::fs::write(&host, "ref: refs/heads/LEAKED-FROM-OUTSIDE\n").unwrap();+ std::fs::remove_file(root.join(".git/HEAD")).unwrap();+ symlink(&host, root.join(".git/HEAD")).unwrap();+ assert_eq!(head_branch(&root), None, "HEAD symlink");+ // `.git` as a symlink to another repo, or to a `gitdir:` file+ let (_d2, other) = head_repo();+ let linked = d.path().join("linked");+ std::fs::create_dir_all(&linked).unwrap();+ symlink(other.join(".git"), linked.join(".git")).unwrap();+ assert_eq!(head_branch(&linked), None, ".git symlink to a folder");+ assert_eq!(head_branch(&linked.join("sub")), None, "and not from below it either");+ std::fs::remove_file(linked.join(".git")).unwrap();+ std::fs::write(d.path().join("gitfile"), format!("gitdir: {}\n", other.join(".git").display())).unwrap();+ symlink(d.path().join("gitfile"), linked.join(".git")).unwrap();+ assert_eq!(head_branch(&linked), None, ".git symlink to a file");+ // A worktree whose gitdir goes through a planted folder link+ let wt = d.path().join("wt");+ std::fs::create_dir_all(other.join(".git/worktrees/wt")).unwrap();+ std::fs::write(other.join(".git/worktrees/wt/HEAD"), "ref: refs/heads/LEAKED-FROM-OUTSIDE\n").unwrap();+ std::fs::create_dir_all(&wt).unwrap();+ symlink(other.parent().unwrap(), wt.join("hop")).unwrap();+ std::fs::write(wt.join(".git"), "gitdir: hop/repo/.git/worktrees/wt\n").unwrap();+ assert_eq!(head_branch(&wt), None, "gitdir through a symlinked folder");+ }++ /// F-3, M1: `link/..` in the gitdir is the link target's parent to the kernel and git, not+ /// the folder the text names. A decoy on the text path says `feature`, the real gitdir `main`.+ #[cfg(unix)]+ #[test]+ fn head_branch_refuses_dotdot_after_a_symlink_in_the_gitdir() {+ use std::os::unix::fs::symlink;+ let d = tempfile::tempdir().unwrap();+ let p = d.path();+ let wt = p.join("wt");+ std::fs::create_dir_all(p.join("phys/sub")).unwrap();+ std::fs::create_dir_all(p.join("phys/decoy/.git/worktrees/x")).unwrap();+ std::fs::write(p.join("phys/decoy/.git/worktrees/x/HEAD"), "ref: refs/heads/main\n").unwrap();+ std::fs::create_dir_all(wt.join("decoy/.git/worktrees/x")).unwrap();+ std::fs::write(wt.join("decoy/.git/worktrees/x/HEAD"), "ref: refs/heads/feature\n").unwrap();+ symlink(p.join("phys/sub"), wt.join("link")).unwrap();+ std::fs::write(wt.join(".git"), "gitdir: link/../decoy/.git/worktrees/x\n").unwrap();+ // What git and the kernel open: the physical copy, on `main`+ let kernel = std::fs::read_to_string(wt.join("link/../decoy/.git/worktrees/x/HEAD")).unwrap();+ assert_eq!(kernel, "ref: refs/heads/main\n");+ assert_eq!(head_branch(&wt), None, "M1: must not read the text path's `feature`");+ // `..` after a real folder still reads+ std::fs::create_dir_all(wt.join("real")).unwrap();+ std::fs::write(wt.join(".git"), "gitdir: real/../decoy/.git/worktrees/x\n").unwrap();+ assert_eq!(head_branch(&wt).as_deref(), Some("feature"));+ }++ /// F-3, M2: the start folder sits under a symlink, and the gitdir's leading `..` climb over it.+ /// The text lands beside the link; the kernel lands beside the link's target.+ #[cfg(unix)]+ #[test]+ fn head_branch_refuses_dotdot_over_a_symlink_in_the_start_folder() {+ use std::os::unix::fs::symlink;+ let d = tempfile::tempdir().unwrap();+ let p = d.path();+ let real = p.join("deep/er/real");+ std::fs::create_dir_all(real.join("wt")).unwrap();+ symlink(&real, p.join("lnk")).unwrap();+ std::fs::create_dir_all(p.join("deep/er/decoy2/.git/worktrees/x")).unwrap();+ std::fs::write(p.join("deep/er/decoy2/.git/worktrees/x/HEAD"), "ref: refs/heads/main\n").unwrap();+ std::fs::create_dir_all(p.join("decoy2/.git/worktrees/x")).unwrap();+ std::fs::write(p.join("decoy2/.git/worktrees/x/HEAD"), "ref: refs/heads/feature\n").unwrap();+ std::fs::write(real.join("wt/.git"), "gitdir: ../../decoy2/.git/worktrees/x\n").unwrap();+ let start = p.join("lnk/wt");+ let kernel = std::fs::read_to_string(start.join("../../decoy2/.git/worktrees/x/HEAD")).unwrap();+ assert_eq!(kernel, "ref: refs/heads/main\n");+ assert_eq!(head_branch(&start), None, "M2: must not read the text path's `feature`");+ }++ #[test]+ fn head_branch_only_follows_a_worktree_gitdir() {+ let (d, root) = head_repo();+ let wt = d.path().join("wt");+ std::fs::create_dir_all(&wt).unwrap();+ for bad in [+ format!("gitdir: {}", root.join(".git").display()),+ format!("gitdir: {}", root.display()),+ "gitdir: ../repo/.git/worktrees".into(),+ "gitdir: ../../../../../../../../../../../../../../../../../../.git/worktrees/x".into(),+ "worktree pointer".into(),+ ] {+ std::fs::create_dir_all(root.join(".git/worktrees/x")).unwrap();+ std::fs::write(root.join(".git/worktrees/x/HEAD"), "ref: refs/heads/x\n").unwrap();+ std::fs::write(wt.join(".git"), &bad).unwrap();+ assert_eq!(head_branch(&wt), None, "{bad}");+ }+ // A relative gitdir (what `worktree.useRelativePaths` writes) reads+ std::fs::write(wt.join(".git"), "gitdir: ../repo/.git/worktrees/x\n").unwrap();+ assert_eq!(head_branch(&wt).as_deref(), Some("x"));+ // The branch must be a plain ref+ for head in ["ref: refs/heads/../../x", "ref: refs/heads/a b", "ref: refs/tags/v1", "ref: refs/heads/"] {+ std::fs::write(root.join(".git/HEAD"), head).unwrap();+ assert_eq!(head_branch(&root), None, "{head}");+ }+ }++ /// A worktree beside its repo, made by git itself, still reads its own branch.+ #[test]+ fn head_branch_reads_a_real_sibling_worktree() {+ let d = tempfile::tempdir().unwrap();+ let git = |dir: &Path, args: &[&str]| {+ let ok = std::process::Command::new("git")+ .args(["-c", "user.name=t", "-c", "user.email=t@t", "-c", "init.defaultBranch=main"])+ .args(args)+ .current_dir(dir)+ .status()+ .unwrap()+ .success();+ assert!(ok, "git {args:?}");+ };+ let main = d.path().join("main");+ std::fs::create_dir_all(&main).unwrap();+ git(&main, &["init", "-q"]);+ git(&main, &["commit", "-q", "--allow-empty", "-m", "x"]);+ let wt = d.path().join("main-wt");+ git(&main, &["worktree", "add", "-q", "-b", "juno/side-1", wt.to_str().unwrap()]);+ assert_eq!(head_branch(&main).as_deref(), Some("main"));+ assert_eq!(head_branch(&wt).as_deref(), Some("juno/side-1"));+ std::fs::create_dir_all(wt.join("src")).unwrap();+ assert_eq!(head_branch(&wt.join("src")).as_deref(), Some("juno/side-1"));+ }+fn backup(space: &str, ok: bool, age_s: i64) -> crate::db::dev_mode::DevBackupRow {crate::db::dev_mode::DevBackupRow {id: format!("dvb_{}", uuid::Uuid::new_v4().simple()),diff --git a/src/server/gitfile.rs b/src/server/gitfile.rsnew file mode 100644index 00000000..b634a4ec--- /dev/null+++ b/src/server/gitfile.rs@@ -0,0 +1,82 @@+//! Reading small files under a `.git` that a bot can write (missions' `git_head`, Dev mode's+//! `head_branch`). Everything there is untrusted: never follow a symlink, never block on a FIFO,+//! socket or device, never read a hard link (it may be a host file's other name), and cap sizes.++use std::io::Read;+use std::path::Path;++/// HEAD, a loose ref or a `.git` file is tiny; a bigger one isn't git's.+pub(crate) const REF_MAX: u64 = 4 * 1024;+/// packed-refs can list many refs, but never this many for a bot's folder.+pub(crate) const PACKED_MAX: u64 = 1024 * 1024;++/// Reads `<base>/<rel>` (`rel` is `/`-separated and already checked) only when every component+/// below `base` is a real folder and the last a regular file of at most `max` bytes with one+/// link. Never follows a symlink and never blocks on a FIFO, socket or device.+pub(crate) fn read(base: &Path, rel: &str, max: u64) -> Option<String> {+ let mut at = base.to_path_buf();+ let parts: Vec<&str> = rel.split('/').collect();+ for (i, p) in parts.iter().enumerate() {+ at.push(p);+ let ft = std::fs::symlink_metadata(&at).ok()?.file_type();+ let last = i + 1 == parts.len();+ if (last && !ft.is_file()) || (!last && !ft.is_dir()) {+ return None;+ }+ }+ let mut o = std::fs::OpenOptions::new();+ o.read(true);+ #[cfg(unix)]+ {+ use std::os::unix::fs::OpenOptionsExt;+ // A symlink swapped in after the check fails to open; a FIFO opens without waiting+ o.custom_flags(libc::O_NOFOLLOW | libc::O_NONBLOCK);+ }+ let f = o.open(&at).ok()?;+ let m = f.metadata().ok()?;+ if !m.file_type().is_file() || m.len() > max {+ return None;+ }+ #[cfg(unix)]+ {+ use std::os::unix::fs::MetadataExt;+ // git never hard-links these; a second name may be a host file's+ if m.nlink() > 1 {+ return None;+ }+ }+ let mut buf = Vec::with_capacity(m.len() as usize);+ f.take(max + 1).read_to_end(&mut buf).ok()?;+ if buf.len() as u64 > max {+ return None;+ }+ String::from_utf8(buf).ok()+}++/// A `ref:` target git would write: `refs/` then plain components of `[A-Za-z0-9._-]`, so it+/// can't leave `.git` (no `..`, no empty or absolute part, no backslash).+pub(crate) fn plain_ref(r: &str) -> bool {+ r.len() <= 1024+ && r.starts_with("refs/")+ && !r.contains("..")+ && r.bytes().all(|b| b.is_ascii_alphanumeric() || matches!(b, b'.' | b'_' | b'/' | b'-'))+ && r.split('/').all(|c| !c.is_empty() && c != ".")+}++#[cfg(all(test, unix))]+mod tests {+ use super::*;++ #[test]+ fn reads_one_regular_file_and_nothing_else() {+ let d = tempfile::tempdir().unwrap();+ std::fs::create_dir_all(d.path().join("a")).unwrap();+ std::fs::write(d.path().join("a/f"), "hi").unwrap();+ assert_eq!(read(d.path(), "a/f", 10).as_deref(), Some("hi"));+ assert_eq!(read(d.path(), "a/f", 1), None, "over the cap");+ assert_eq!(read(d.path(), "a", 10), None, "a folder");+ std::fs::hard_link(d.path().join("a/f"), d.path().join("g")).unwrap();+ assert_eq!(read(d.path(), "a/f", 10), None, "a file with two names");+ assert_eq!(read(d.path(), "g", 10), None);+ }+}diff --git a/src/server/missions/checkpoints.rs b/src/server/missions/checkpoints.rsindex f2e21fd3..a2743fc0 100644--- a/src/server/missions/checkpoints.rs+++ b/src/server/missions/checkpoints.rs@@ -10,6 +10,7 @@ use super::routes::{bad, internal, load_task, not_found};use super::{emit_task, int_setting, task_folder, task_json, transition, Actor};use crate::db::checkpoints::{new_id, Checkpoint, NewCheckpoint, ToolCall};use crate::db::missions::{Claim, Task, TaskChange};+use crate::server::gitfile::{self, plain_ref};use crate::server::AppState;use axum::{extract::{Path, RawQuery, State},@@ -195,24 +196,19 @@ fn store_blob(blobs: &FsPath, src: &FsPath) -> std::io::Result<String> {Ok(sha)}-/// HEAD and loose ref files are tiny; a bigger one isn't git's.-const GIT_REF_MAX: u64 = 4 * 1024;-/// packed-refs can list many refs, but never this many for a task folder.-const GIT_PACKED_MAX: u64 = 1024 * 1024;-/// The commit the folder's git checkout is on, if it is one (read-only info).////// The folder is written by the task's bots, so everything under `.git` is untrusted: it never-/// follows a symlink (at any component), never opens anything but a regular file (a FIFO can't-/// block it), reads at most [`GIT_REF_MAX`] / [`GIT_PACKED_MAX`] bytes, only follows a `ref:`-/// that is a plain `refs/...` name, and only returns 40 or 64 hex characters. Anything else,+/// follows a symlink (at any component), never opens anything but a regular file with one link (a+/// FIFO can't block it, a hard link can't name a host file), reads at most [`gitfile::REF_MAX`] /+/// [`gitfile::PACKED_MAX`] bytes, only follows a `ref:` that is a plain `refs/...` name, and only returns 40 or 64 hex characters. Anything else,/// including a `.git` file (a `gitdir:` worktree pointer), is `None`.fn git_head(folder: &FsPath) -> Option<String> {let git = folder.join(".git");if !std::fs::symlink_metadata(&git).ok()?.file_type().is_dir() {return None;}- let head = read_git_file(&git, "HEAD", GIT_REF_MAX)?;+ let head = gitfile::read(&git, "HEAD", gitfile::REF_MAX)?;let head = head.trim();let Some(r) = head.strip_prefix("ref: ") else {return commit_id(head);@@ -221,10 +217,10 @@ fn git_head(folder: &FsPath) -> Option<String> {if !plain_ref(r) {return None;}- if let Some(s) = read_git_file(&git, r, GIT_REF_MAX) {+ if let Some(s) = gitfile::read(&git, r, gitfile::REF_MAX) {return commit_id(s.trim());}- let packed = read_git_file(&git, "packed-refs", GIT_PACKED_MAX)?;+ let packed = gitfile::read(&git, "packed-refs", gitfile::PACKED_MAX)?;packed.lines().filter(|l| !l.starts_with('#') && !l.starts_with('^'))@@ -232,56 +228,11 @@ fn git_head(folder: &FsPath) -> Option<String> {.and_then(commit_id)}-/// A `ref:` target git would write: `refs/` then plain components of `[A-Za-z0-9._-]`, so it-/// can't leave `.git` (no `..`, no empty or absolute part, no backslash).-fn plain_ref(r: &str) -> bool {- r.len() <= 1024- && r.starts_with("refs/")- && !r.contains("..")- && r.bytes().all(|b| b.is_ascii_alphanumeric() || matches!(b, b'.' | b'_' | b'/' | b'-'))- && r.split('/').all(|c| !c.is_empty() && c != ".")-}-/// A full SHA-1 (40) or SHA-256 (64) commit id, lowercased.fn commit_id(s: &str) -> Option<String> {((s.len() == 40 || s.len() == 64) && s.bytes().all(|b| b.is_ascii_hexdigit())).then(|| s.to_ascii_lowercase())}-/// Reads `<git>/<rel>` (`rel` is `/`-separated and already checked) only when every component-/// below `git` is a real folder and the last a regular file of at most `max` bytes. Never-/// follows a symlink and never blocks on a FIFO, socket or device.-fn read_git_file(git: &FsPath, rel: &str, max: u64) -> Option<String> {- let mut at = git.to_path_buf();- let parts: Vec<&str> = rel.split('/').collect();- for (i, p) in parts.iter().enumerate() {- at.push(p);- let ft = std::fs::symlink_metadata(&at).ok()?.file_type();- let last = i + 1 == parts.len();- if (last && !ft.is_file()) || (!last && !ft.is_dir()) {- return None;- }- }- let mut o = std::fs::OpenOptions::new();- o.read(true);- #[cfg(unix)]- {- use std::os::unix::fs::OpenOptionsExt;- // A symlink swapped in after the check fails to open; a FIFO opens without waiting- o.custom_flags(libc::O_NOFOLLOW | libc::O_NONBLOCK);- }- let f = o.open(&at).ok()?;- let m = f.metadata().ok()?;- if !m.file_type().is_file() || m.len() > max {- return None;- }- let mut buf = Vec::with_capacity(m.len() as usize);- f.take(max + 1).read_to_end(&mut buf).ok()?;- if buf.len() as u64 > max {- return None;- }- String::from_utf8(buf).ok()-}-struct Taken {manifest: Manifest,bytes: u64,@@ -1467,6 +1418,21 @@ mod tests {assert_eq!(run(folder), None, "ref and packed-refs FIFOs");}+ /// F-2: a hard link is a regular file, so a HEAD (or ref) that is another name for a host+ /// file whose whole content is hex would pass every other check.+ #[cfg(unix)]+ #[test]+ fn git_head_never_reads_a_hard_link() {+ let (_d, folder, canary) = repo();+ let git = folder.join(".git");+ std::fs::hard_link(&canary, git.join("HEAD")).unwrap();+ assert_eq!(head_of(&folder), None, "HEAD hard-linked to a host file");+ std::fs::remove_file(git.join("HEAD")).unwrap();+ std::fs::write(git.join("HEAD"), "ref: refs/heads/main\n").unwrap();+ std::fs::hard_link(&canary, git.join("refs/heads/main")).unwrap();+ assert_eq!(head_of(&folder), None, "loose ref hard-linked to a host file");+ }+#[test]fn a_run_owns_what_it_created_changed_or_deleted() {let before = m(&[("same", "1"), ("edited", "1"), ("gone", "1")], &["big", "node_modules/"]);diff --git a/src/server/mod.rs b/src/server/mod.rsindex 9a83eab5..18acab30 100644--- a/src/server/mod.rs+++ b/src/server/mod.rs@@ -58,6 +58,7 @@ pub mod goals;pub mod app_update_routes;pub mod smart_results;pub mod dev;+pub(crate) mod gitfile;pub mod show_once;pub mod agents_connect;pub mod teams;