From f87a792743227ea06cfa1580f8d30a618b4d1a04 Mon Sep 17 00:00:00 2001 From: dev Date: Fri, 13 Nov 2026 19:04:50 +0000 Subject: git-authz: one C program for who may move which refs A git host that anonymous-clones and shared-pushes has one question left to answer: which human may move which ref. This answers it from one plain file, in one binary, with no runtime beyond libc. Four modes, because they must agree exactly about what a rule means - a check that answers differently from the hook is a program that lies: check one question, for an operator or a script authorize a whole push, the way core.hooksPath hands it over: identity from GIT_HOST_USER (ssh key option) or REMOTE_USER (http auth), and one "old new ref" line per ref on stdin. Also runs as "pre-receive", which is how git finds it. validate does the policy parse, and is the host wired to run it edit grant, revoke, protect (the deployed policy is a read-only mount on purpose, so these are for the source copy and say so when told) The verdict is the exit status, since the caller is a hook: 0 allowed, 1 denied, 3 no usable policy, 64 usage. A policy that cannot be read or does not parse denies everything rather than guessing, and one bad line poisons the whole file on purpose: a mistyped rule that quietly widens access is worse than an evening of refused pushes. Semantics kept from the shell engine this replaces: levels are monotone (admin implies force implies write), rules add up rather than order, "@" is only everyone the policy already names, protected refs cost one step more than the action asked for, and refs that are neither branches nor tags are for admins. "main", "heads/main" and "refs/heads/main" are one branch; a bare name is never a tag. Two facts a push does not carry - is old an ancestor of new, and which branch HEAD points at - are asked of git, and an answer that cannot be had is taken the expensive way. tests/unit.c covers the matcher directly (173 checks); the behavioural suite that drives this the way git does follows next. --- src/util.c | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 src/util.c (limited to 'src/util.c') diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..a7513b3 --- /dev/null +++ b/src/util.c @@ -0,0 +1,118 @@ +/* util.c - allocation that stops rather than limps, and a name for messages. + * + * A hook that cannot allocate is not in a position to decide anything, so + * these exit with the policy-unavailable status rather than returning NULL + * somewhere the caller forgot to look. */ +#include "authz.h" + +#include +#include +#include +#include + +const char *progname = "git-authz"; + +static void oom(void) +{ + fprintf(stderr, "%s: out of memory\n", progname); + exit(3); +} + +void sb_init(strbuf_t *b) +{ + b->cap = 64; + b->len = 0; + b->s = xmalloc(b->cap); + b->s[0] = '\0'; +} + +static void sb_room(strbuf_t *b, size_t extra) +{ + if (b->len + extra + 1 <= b->cap) + return; + while (b->len + extra + 1 > b->cap) + b->cap *= 2; + b->s = xrealloc(b->s, b->cap); +} + +void sb_add(strbuf_t *b, const char *s) +{ + size_t n; + + if (s == NULL) + return; + n = strlen(s); + sb_room(b, n); + memcpy(b->s + b->len, s, n + 1); + b->len += n; +} + +void sb_addf(strbuf_t *b, const char *fmt, ...) +{ + va_list ap; + int n; + char *out; + + va_start(ap, fmt); + n = vsnprintf(NULL, 0, fmt, ap); + va_end(ap); + if (n < 0) + return; + sb_room(b, (size_t)n); + va_start(ap, fmt); + out = b->s + b->len; + n = vsnprintf(out, b->cap - b->len, fmt, ap); + va_end(ap); + if (n < 0) { + b->s[b->len] = '\0'; + return; + } + b->len += (size_t)n; +} + +void sb_free(strbuf_t *b) +{ + free(b->s); + b->s = NULL; + b->len = b->cap = 0; +} + +void *xmalloc(size_t n) +{ + void *p = malloc(n != 0 ? n : 1); + + if (p == NULL) + oom(); + return p; +} + +void *xrealloc(void *ptr, size_t n) +{ + void *p = realloc(ptr, n != 0 ? n : 1); + + if (p == NULL) + oom(); + return p; +} + +char *xstrdup(const char *s) +{ + size_t n; + char *p; + + if (s == NULL) + return NULL; + n = strlen(s) + 1; + p = xmalloc(n); + memcpy(p, s, n); + return p; +} + +char *xsubstr(const char *s, size_t len) +{ + char *p = xmalloc(len + 1); + + memcpy(p, s, len); + p[len] = '\0'; + return p; +} -- cgit v1.2.3