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] `check` answers one question and is for humans and scripts. `authorize` is what git invokes. `validate` asks whether the policy is one the hook can use. Exit status is the interface, and it does not change: 0 allowed 1 denied - this person may not 3 the host is broken, not this person 64 usage error Three is separate from one on purpose, and it covers two different breakages: a policy the hook cannot use says `invalid policy file`, and a hook handed something that is not a push - which no real git ever does - says `invalid operation`. A push refused because the person is not allowed is theirs, and says `permission denied` with the name they came as and the ref they were refused. They must not sound the same, and a monitor must not have to parse prose to tell them apart - which is also why each of them is one line. 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`. This project builds the executable and installs it; the server that runs it - cgit, sshd, the web server, the container that assembles them around the installed binary - is configured in the git-host project, not here. `validate` is what an entrypoint runs before it starts serving: git-authz validate || exit 1 It reports a policy that does not parse, with the line numbers the parser already printed, and a policy that cannot be read at all. Either one denies every push, which is why both are errors and not warnings: nothing is allowed on a policy the hook cannot read. It warns when a rule gives `@all` the whole of a repository, and when the file is not readable by the account the hook runs as. Warnings do not stop it; errors do, and `--strict` makes warnings stop it too. Reading this as an operator --------------------------- A refused push says one line, and it says only the input it refused: git-authz: permission denied [user=bob, obj=refs/heads/main] That is the whole of what the person pushing is told, on purpose: the reason is a sentence about a file they cannot read, and quoting it at them sends them to somebody else's configuration. The reason is here instead: who may write to team? git-authz check bob team write refs/heads/feature/login why was that push refused? git-authz check bob team write refs/heads/main 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: git-authz check bob team write refs/notes/x deny: bob has write on team refs/notes/x; it needs admin (neither a branch nor a tag) which is the whole design in two lines: the refusal names who and what, the answer names the level held, the ref, the level required and why, so the fix is obvious to whoever owns the policy and the person who hit it is not left guessing either. `authorize --explain` prints that sentence in the hook's own seat, for when the operator is the one standing there. A policy the hook cannot use says `invalid policy file` and nothing else at exit 3; `validate` is what says what is wrong with it, with the line numbers. Exit 3 has one other cause: `invalid operation`, for a hook handed a line that is not an update - three fields, two object ids and a ref under `refs/`. No git writes that, so it is the pipe, the wrapper or whoever was debugging, and never the person pushing. 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 ci/ the pipeline src/glob.c the pattern matching, such as it is src/util.c allocation, string buffers, reading a file 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 add up 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, and messages. 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.