From 19a3eb704479fa01c383960ca21a057e8c5fbaf7 Mon Sep 17 00:00:00 2001 From: dev Date: Fri, 13 Nov 2026 20:34:47 +0000 Subject: Install: the hook is a relative symlink, and the policy is configuration The hook is a symlink named pre-receive, because that name is what core.hooksPath looks for and what selects authorize mode inside the binary. It was built by cd'ing into the hook directory first, which left the awk script writing the relative path out of reach, and the recipe failed with the link never created. The path is computed where the script is, and the link is made from there. Relative rather than absolute for the reason that matters during an install: a staged tree under DESTDIR, an unpacked prefix, or an image assembled somewhere else must point at the binary that came with them, not at whatever lives at the same absolute path on the machine reading the link. relpath.awk walks both paths component by component - it is not a string-prefix operation, because /usr/local/share and /usr/local/bin share a prefix only up to the point where ".." starts - and falls back to the absolute path when a component cannot be named from where it stands. authz.conf is the example policy, and the installer treats it as configuration: an existing file is an operator's, so it is kept and the new example lands beside it as authz.conf.new rather than over the rules someone wrote down. `make sanitizer' builds both suites under AddressSanitizer and UBSan, which is how the editing bugs in the previous commit were found. --- Makefile | 35 ++++++++++++++++++++---- authz.conf | 46 ++++++++++++++++++++++++++++++++ build/relpath.awk | 79 ++++++++++++++++++++++++++++++++++++++++++------------- 3 files changed, 136 insertions(+), 24 deletions(-) create mode 100644 authz.conf diff --git a/Makefile b/Makefile index 167c6ed..1f93122 100644 --- a/Makefile +++ b/Makefile @@ -36,6 +36,18 @@ $(PROG): $(OBJS) src/%.o: src/%.c src/authz.h $(CC) $(STD) $(WARN) $(CPPFLAGS) $(CFLAGS) -c -o $@ $< +# AddressSanitizer/UBSan build of everything, so that the behavioural suite - +# which is what actually exercises the editing code paths - runs against them: +# +# make clean && make sanitizer +# +# Leaks are checked, which is why every command frees what it read: a program +# that exits in a millisecond does not need the memory back, but it does need +# the leak report to stay empty, or the one real leak hides among forty. +sanitizer: CFLAGS := -O1 -g -fsanitize=address,undefined -fno-omit-frame-pointer +sanitizer: LDFLAGS := -fsanitize=address,undefined +sanitizer: test + # The unit test links the parts it tests, not the command line, so that a # broken argument parser cannot hide a working matcher (or the other way round). $(UNIT): tests/unit.c src/util.o src/glob.o src/policy.o @@ -47,14 +59,27 @@ test: $(PROG) $(UNIT) $(UNIT) AUTHZ=./$(PROG) sh tests/run.sh +# The hook is a symlink to the program, named 'pre-receive', because that name +# is what core.hooksPath looks for and what selects authorize mode inside the +# binary. The link is relative so that a staged install, an unpacked prefix and +# the image built from either all resolve to the binary that came with them +# rather than to whatever lives at the same absolute path on the reading side. +# +# The policy is configuration: an existing one is an operator's file, so it is +# kept and the new one arrives beside it as authz.conf.new. install: $(PROG) install -d $(DESTDIR)$(BINDIR) $(DESTDIR)$(HOOKDIR) $(DESTDIR)$(SYSCONFDIR) install -m 0755 $(PROG) $(DESTDIR)$(BINDIR)/$(PROG) - install -m 0644 authz.conf $(DESTDIR)$(SYSCONFDIR)/authz.conf - rm -f $(DESTDIR)$(HOOKDIR)/pre-receive - cd $(DESTDIR)$(HOOKDIR) && \ - ln -s "`awk -v from='$(SHAREDIR)/hooks' -v to='$(BINDIR)/$(PROG)' \ - -f build/relpath.awk`" pre-receive + @if [ -e $(DESTDIR)$(SYSCONFDIR)/authz.conf ]; then \ + install -m 0644 authz.conf $(DESTDIR)$(SYSCONFDIR)/authz.conf.new; \ + echo " kept $(DESTDIR)$(SYSCONFDIR)/authz.conf; new example is authz.conf.new"; \ + else \ + install -m 0644 authz.conf $(DESTDIR)$(SYSCONFDIR)/authz.conf; \ + fi + link=`awk -v from='$(HOOKDIR)' -v to='$(BINDIR)/$(PROG)' -f build/relpath.awk`; \ + rm -f $(DESTDIR)$(HOOKDIR)/pre-receive; \ + ln -s "$$link" $(DESTDIR)$(HOOKDIR)/pre-receive; \ + echo " $(HOOKDIR)/pre-receive -> $$link" uninstall: rm -f $(DESTDIR)$(BINDIR)/$(PROG) $(DESTDIR)$(HOOKDIR)/pre-receive diff --git a/authz.conf b/authz.conf new file mode 100644 index 0000000..afea7d2 --- /dev/null +++ b/authz.conf @@ -0,0 +1,46 @@ +# git-host access policy. +# +# One file, read by the receive hook on every push and by nothing that serves +# reads. The deployed copy is mounted read-only - a ConfigMap - so changes are +# made to the copy in git and rolled out; `git-authz grant`, `revoke` and +# `protect` edit a source copy and refuse, with an explanation, when pointed at +# the read-only one. +# +# [ ... ] +# +# Levels stack: admin implies force implies write, and every rule for a user +# counts, so the strongest match wins. There is no 'deny': access is taken +# away by deleting the rule that gave it. +# +# A ref pattern is a glob in which '*' crosses '/'. "main", "heads/main" and +# "refs/heads/main" name the same branch; a bare name is always a branch, so a +# tag has to be written "refs/tags/v1" or "tags/v1". With no patterns at all +# the rule covers the whole repository, refs included. +# +# '@all' means everyone this policy already names somewhere. It is not a +# wildcard for anonymous users: nobody reaches it who has no rule of their own. + +# What costs an extra level to move. A write on these needs force, a force +# needs admin, and refs outside refs/heads and refs/tags need admin regardless. +set protected-refs refs/heads/main refs/tags/* + +# The account for break-glass and for the refs nobody else should touch: it is +# unscoped, so it covers the whole repository including refs/notes and friends. +alice * admin + +# Default for everyone the policy knows: their own branches, anywhere. Reads +# are open, so this is about writing, and there is no rule here to be the +# fall-through for someone the policy has never mentioned. +@all * write refs/heads/* + +# Contributor on team: feature branches only. +bob team write refs/heads/feature/* + +# Senior on team: may rewrite his feature branches and delete them. +carol team force refs/heads/feature/* + +# Maintainer of team: everything, including main, tags, and deletions. +dave team admin + +# Team A's repository, with an unrelated ACL. +erin secrets admin diff --git a/build/relpath.awk b/build/relpath.awk index 6c9a031..ea5ca99 100644 --- a/build/relpath.awk +++ b/build/relpath.awk @@ -1,22 +1,63 @@ -# The path from one directory to a file, expressed relative to that directory. +# relpath.awk - the path from one directory to one file, relative to the +# directory. Used by `make install' for the hook symlink: # -# The pre-receive hook is this same binary reached through a symlink, because -# that is how git's core.hooksPath finds it. An absolute symlink would be a -# hook that works at exactly one prefix; a relative one survives a staged -# install (DESTDIR), a prefix built elsewhere, and a container image that moves -# /usr/local around - and `ls -l` still says what the hook is. +# awk -v from=/usr/local/share/git-host/hooks -v to=/usr/local/bin/git-authz \ +# -f build/relpath.awk -> ../../../bin/git-authz +# +# A relative link survives being moved: a staged install under DESTDIR, an +# unpacked prefix, a container image built somewhere else. An absolute one +# points at whatever happens to live at that path on the machine reading it, +# which during a staged install is the wrong answer and after a failed one is a +# broken link nothing explains. +# +# POSIX awk, so busybox awk and mawk and gawk all agree. + +function split_path(p, out, n, parts, i, depth) { + n = split(p, parts, "/") + depth = 0 + for (i = 1; i <= n; i++) { + if (parts[i] == "" || parts[i] == ".") + continue # empty runs, and "." changes nothing + if (parts[i] == "..") { + if (depth > 0) + depth-- # already inside a component we know + else + out[++depth] = ".." # above the root we cannot see into + continue + } + out[++depth] = parts[i] + } + return depth +} + BEGIN { - n = split(from, f, "/") - m = split(to, t, "/") - i = 1 - while (i <= n && i <= m && f[i] == t[i]) - i++ - rel = "" - for (k = i; k <= n; k++) - rel = rel (rel == "" ? "" : "/") ".." - for (k = i; k <= m; k++) - rel = rel (rel == "" ? "" : "/") t[k] - if (rel == "") - rel = "." - print rel + if (from == "" || to == "") { + print "relpath: needs -v from=DIR -v to=FILE" > "/dev/stderr" + exit 64 + } + + nf = split_path(from, f) + nt = split_path(to, t) + + common = 0 + while (common < nf && common < nt && f[common + 1] == t[common + 1]) + common++ + + # Anything above a ".." we could not resolve has to be answered with an + # absolute path: there is no relative route into a directory we cannot name. + if ((nf > common && f[common + 1] == "..") || + (nt > common && t[common + 1] == "..")) { + print to + exit 0 + } + + res = "" + for (i = common + 1; i <= nf; i++) + res = res ".." "/" + for (i = common + 1; i <= nt; i++) + res = res t[i] "/" + sub(/\/$/, "", res) + if (res == "") + res = "." # the file is the directory's own name + print res } -- cgit v1.2.3