git / trust

Cloning a repo, entering its directory, or saving a file in Neovim runs no code that the repo chooses. I gate repo-chosen tools behind git trust, a bash script in my laptop repo.

What trust gates

A new clone is untrusted. In an untrusted repo, my tools use my own installed versions and configs:

When I trust every person who can push to a repo, I run git trust in the repo or in any of its worktrees:

git trust

In a trusted repo, format-on-save runs the goimports and pgfmt versions pinned in go.mod or tools.mod (see go / checks). It also reads the repo's dprint.json and runs tree.setup and tree.teardown. To revoke trust, I run git trust --unset.

Clone tokens

A path string alone cannot identify a repo instance. If I delete a trusted clone and clone another repo at the same path, a path-only store accepts the new occupant.

git trust pairs the physical path of the main clone with a random 128-bit hex token. It writes the token to <root>/.git/laptop-trust and records <token> <root> under laptop.trusted in ~/.gitconfig.local:

token=$(od -An -tx1 -N16 /dev/urandom | tr -d ' \t\n')
printf '%s\n' "$token" >"$root/.git/laptop-trust"
git -C / config --file "$store" --add laptop.trusted "$token $root"

No repo can write ~/.gitconfig.local, and a git clone, checkout, or archive cannot guess the token. When I delete a clone and place another repo at the same path, <root>/.git/laptop-trust is missing and git trust --check fails until I run git trust again.

Worktrees

Trust covers the main clone and its worktrees. git trust --check DIR runs no git command inside DIR, because DIR can be hostile. It walks up from the physical path of DIR to find the nearest .git, then reads only ~/.gitconfig.local, <root>/.git/laptop-trust, <root>/.git/worktrees/*/gitdir, and the worktree's .git file.

Removing a worktree with rm -rf instead of git worktree remove leaves its gitdir file behind under <root>/.git/worktrees/. If another repo later occupies that path, the forward pointer still names it. To close that gap, --check requires both links to agree:

worktree_points_back() {
  local worktree=$1 admin=$2 back="" back_dir
  [ -f "$worktree/.git" ] && [ ! -L "$worktree/.git" ] || return 1
  exact back "$worktree/.git" || return 1
  case $back in
    "gitdir: "*) back=${back#"gitdir: "} ;;
    *) return 1 ;;
  esac
  case $back in
    /*) ;;
    *) back=$worktree/$back ;;
  esac
  back_dir=$(resolve "$back") || return 1
  [ "$back_dir" = "$admin" ]
}

A standalone clone has a .git directory rather than a regular file, and a worktree of another repo points to a different admin directory. Both fail the back-pointer check.

Path boundaries

git trust --check resolves every directory with pwd -P and rejects any path that contains a newline, because shell command substitution drops trailing newlines. Relative worktree paths created with git worktree add --relative-paths resolve from the gitdir file's directory rather than from the caller's working directory.

Go searches for go.mod and go.work starting from $PWD, so a symlink in an untrusted clone pointing into a trusted repo can put the untrusted repo's go.work in effect. Both goimportslocal and the pgfmt formatter in vim/init.lua set GOWORK=off and resolve PWD to the physical module root before running go tool.

Git and search paths

git/gitconfig sets safe.bareRepository = explicit:

[safe]
  bareRepository = explicit

With that setting, git refuses to read a tracked subdirectory laid out like a bare repository (HEAD, objects/, refs/, and config) unless I pass --git-dir or set GIT_DIR.

The shell and Neovim strip relative entries from search paths before loading tools or modules:

Testing the boundaries

test/trust_test.sh, test/git_test.sh, and test/nvim_test.sh in my laptop repo test each boundary against hostile fixtures. Each fixture plants a fake program, git config hook, or Lua module that writes a marker file if executed. A test fails if any marker file appears.

← All articles