git-authz - who may move which refs =================================== One C program that decides, on every push, whether the person on the other end of the connection is allowed to move the refs they are trying to move. C99 and libc, no dependencies, one binary, one plain-text policy file. make make test make install DESTDIR=/tmp/stage It exists because a git host that authenticates pushes but not *what* is pushed has one ACL for the whole server, and the only way to widen it is to hand somebody the key to everything. Here: reads stay anonymous, and writing is per user, per repository, per ref pattern. Commands -------- git-authz [--policy PATH] check [ref] git-authz [--policy PATH] authorize [options] git-authz [--policy PATH] validate [options] git-authz [--policy PATH] grant [ref ...] git-authz [--policy PATH] revoke git-authz [--policy PATH] protect [--repo R] ... `check` answers one question and is for humans and scripts. `authorize` is what git invokes. `validate` asks whether the host is wired to run the policy at all. The last three edit the file. Exit status is the interface, and it does not change: 0 allowed 1 denied 3 no usable policy - the host is broken, not this person 64 usage error Three is separate from one on purpose. A push refused because the policy file was not mounted produces one line in a log somewhere; a push refused because the user is not allowed produces a message to a colleague. They must not sound the same, and a monitor must not have to parse prose to tell them apart. The policy ---------- [ ... ] alice * admin @all * write refs/heads/* bob team write refs/heads/feature/* carol team force refs/heads/feature/* dave team admin Levels are monotone: admin implies force implies write. Rules are additive and the strongest match wins; there is no `deny`, so access is taken away by deleting the rule that granted it, which means the file always says everything the file grants. `` is flat: a name, optionally with a `*`, and `*` alone for everything. Names are compared with `.git` removed, so `team` and `team.git` are one repository. A ref pattern is a glob in which `*` crosses `/`; `?`, `[abc]`, `[a-z]` and `[!abc]` work. Refs are normalised before matching, so `main`, `heads/main` and `refs/heads/main` are the same branch, and a bare name is *always* a branch - a tag has to be written `refs/tags/v1` or `tags/v1`. A rule with no patterns covers the whole repository, refs and all. `@all` means everyone the policy already names somewhere. It is not a wildcard for anonymous users: a name that appears nowhere in the file reaches nothing, so adding a rule for a person is the whole of onboarding and `@all` cannot be used to hand out access to someone who was never meant to have any. What an update costs -------------------- The hook reads ` ` lines on stdin, exactly as git writes them, and works out what the update *is* before it asks who may do it: create a ref write fast-forward write rewrite (non-fast-forward) force delete a ref force delete the branch HEAD points at admin a ref outside refs/heads, refs/tags admin `set protected-refs ` raises the price of the listed refs by one level: write needs force, force needs admin. `set protected-refs.` replaces that list for one repository, so a repo with different customs does not inherit somebody else's. Anything that is not a branch or a tag - `refs/notes`, `refs/merge-requests`, whatever a tool invents next - is admin-only. It is the part of the namespace no policy was written with in mind, and the safe reading of "not mentioned" is "not granted". Identity -------- There is one operating-system account, `git`, as there is on any host that wants ssh to be usable. A person is not the account; a person is the key: environment="GIT_HOST_USER=alice",restrict ssh-ed25519 AAAA... alice@laptop Over HTTP the identity is `REMOTE_USER`, set by the web server's basic auth and passed through the CGI shim to the backend, which passes it to the hook. A push with no identity is denied, whatever the policy says. `@all` does not rescue it, because whoever is anonymous is not somebody the policy named. Two facts belong to git ----------------------- Whether `old` is an ancestor of `new`, and which branch `HEAD` points at, are facts about the object store. The hook asks git, and only git: git merge-base --is-ancestor git symbolic-ref -q HEAD Everything else - who this is, what the policy says, what the update costs - it works out itself, so a push is denied rather than allowed if git is missing, or the repository is unreadable, or the answer does not come: * an update whose ancestry cannot be determined is treated as a rewrite, which costs `force`; * a deletion whose HEAD cannot be determined is treated as the default branch, which costs `admin`. `--ff=yes|no` and `--head=REF|none` supply those answers directly, and `--no-git` asks nothing at all. That is how the test suite pins the consequences of every answer without building a repository for each one. Wiring ------ core.hooksPath = /usr/local/share/git-host/hooks -> pre-receive, a relative symlink to the binary, whose name selects authorize mode `make install` puts the binary in `$(BINDIR)`, the example policy in `$(SYSCONFDIR)/authz.conf` (leaving an existing one alone), and the symlink in `$(HOOKDIR)/pre-receive`. `validate` is what an entrypoint runs before it starts serving: git-authz validate || exit 1 It reports a policy that does not parse, a policy no user can push to any repository with, a repository nobody can push to, a missing or non-executable hook, a `core.hooksPath` pointing somewhere else, `PermitUserEnvironment no`, no `ForceCommand`, a `git` account with a real shell, key identities with no rule, and htpasswd users with no rule. Warnings do not stop it; errors do, and `--strict` makes warnings stop it too. `--no-host` checks the policy without looking at the machine, which is what you want from CI on a checkout. The point of that last set of checks is the failure mode where the policy is fine, the server is up, and every push is anonymous - because ssh was not told to accept environment variables, or the hook path moved, and nothing said so. Editing the file ---------------- git-authz grant bob team write refs/heads/feature/* git-authz revoke bob team git-authz protect --repo team refs/heads/release Each writes the file atomically - a temporary in the same directory, then a rename, keeping the mode and owner - and preserves everything else in the file, comments and blank lines included, so a grant does not undo the ordering an operator laid out. `grant` collapses several rules for the same user and repository into one line, in the place of the first, because the strongest wins and a second rule that does nothing is a trap for the next reader. A file that does not exist yet is started, with the grammar written at the top of it; a file that does not parse is refused, because editing something the parser cannot read would rewrite the parts that parse and keep the damage. The deployed file is a read-only mount - a ConfigMap - by design: the hook should not be able to change who can push, and neither should whatever else runs as uid 1000. These commands are for the copy in git. Pointed at a read-only file they fail with that explanation rather than writing somewhere surprising, and `--out` or `--stdout` writes the result elsewhere without touching the input. Reading this as an operator --------------------------- who may write to team? git-authz check bob team write refs/heads/feature/login why was my push refused? Because the message ends with what it would have taken: deny: bob has write on team refs/heads/main; it needs force (raised from write: refs/heads/main is protected) which is the whole design in one line: a refusal that names the level held, the ref, the level required and why, so the fix is obvious to the person who hit it instead of a ticket to whoever set this up. Tests ----- make test `tests/unit.c` tests the policy engine directly - matching, normalisation, the ladder, `@all`, protection, the sentences - and is the place where the semantics are pinned. `tests/run.sh` drives the binary the way git does: an identity in the environment, `old new ref` lines on stdin, the repository as the working directory, the hook reached through its own name. It uses no git at all, so it runs anywhere a compiler does, and it tests the real binary rather than a model of it: argument parsing, exit codes, messages, and the editing commands writing files. `make clean && make sanitizer` runs both suites under AddressSanitizer and UBSan. Everything frees what it reads, which matters only because it keeps that run empty.