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 `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 two 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 That is the whole of it. A rule says what it says: there is no second list somewhere else in the file that can make one ref cost more than another, so the answer to "who can move main" is the lines that name main, and nothing else. Which means an unscoped rule really does cover main: dave team admin # every ref in team, main included and a rule that means to leave main alone has to say so by naming what it covers instead: bob team write refs/heads/feature/* # main is not named, so not granted Anything that is not a branch or a tag - `refs/notes`, `refs/merge-requests`, whatever a tool invents next - is admin-only whatever a rule appears to grant about it. That is the one price the program raises on its own, and it is the one the policy cannot name in advance: 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 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 holds write on team, but not on refs/heads/main; that ref needs write or, when the ref is covered by a rule but costs more than it looks: deny: bob has write on team refs/notes/x; it needs admin (neither a branch nor a tag) 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. Layout ------ cmd/main.c the command line: dispatch, usage, exit status src/ the engine, as a library the command line adds nothing to test/ one program per area, plus the behavioural suite build/ helpers used while building and installing container/ the image: Dockerfile, entrypoint, server configuration ci/ the pipeline src/glob.c the pattern matching, such as it is src/util.c allocation, string buffers, writing a file atomically src/policy.c reading the file, and answering from it src/hook.c the mode git calls, and the only one a push depends on src/validate.c does this policy, and the host around it, add up src/edit.c grant and revoke, without losing the file Everything compiles into `obj/`, test runners included, so a build leaves the tree as it found it apart from the binary. `git-authz` is one program rather than several because every one of them has to answer "what does this rule mean" identically: a `check` that disagrees with the hook is not two programs, it is one that lies. Tests ----- make test The programs in `test/` test the engine directly - matching, normalisation, the ladder, `@all`, scope, the sentences - and are where the semantics are pinned. `test/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. Two facts do not come out of the line git passes, and the hook asks git for them: which branch `HEAD` is, and whether `old` is an ancestor of `new`. Those are covered with a stand-in named `git` in `PATH`, which answers to order and lets the asking be tested without git - including the case where git is not there at all, where the hook has to fail toward denying rather than toward guessing. `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.