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/policy.c | 651 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 651 insertions(+) create mode 100644 src/policy.c (limited to 'src/policy.c') diff --git a/src/policy.c b/src/policy.c new file mode 100644 index 0000000..ffce34d --- /dev/null +++ b/src/policy.c @@ -0,0 +1,651 @@ +/* policy.c - reading the policy, and answering from it. + * + * Grammar (see authz.conf for the prose version): + * + * set protected-refs ... globally, default refs/heads/main + * set protected-refs. ... for one repository, replacing it + * [ ...] + * + * '#' starts a comment wherever it appears. Rules are additive and the + * strongest match wins; there is no 'deny', so a rule is taken away by editing + * the file, not by ordering it after another one. + * + * A line that does not parse is an error, and a policy with errors is not used + * at all: it is better for every push to be refused for one evening than for a + * mistyped line to quietly widen somebody's access. */ +#include "authz.h" + +#include +#include +#include +#include +#include + +#define DEFAULT_PROTECTED "refs/heads/main" + +/* The characters a name may contain, spelled out rather than as a regular + * expression: the engine has none, and the message says why. */ +static const char letters[] = + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; +static const char user_first[] = "_@*?["; +static const char user_rest[] = "_@*?[].-"; +static const char repo_chars[] = "._*?-"; + +static int in_set(unsigned char c, const char *extra) +{ + return c != '\0' && (strchr(letters, (int)c) != NULL || + strchr(extra, (int)c) != NULL); +} + +/* ------------------------------------------------------------------ little */ + +/* Trims in place: leading by moving the pointer, trailing by writing over it. */ +char *authz_trim(char *s) +{ + char *end; + + while (*s != '\0' && isspace((unsigned char)*s)) + s++; + end = s + strlen(s); + while (end > s && isspace((unsigned char)end[-1])) + end--; + *end = '\0'; + return s; +} + +int authz_split_ws(char *line, char **f, int max) +{ + int n = 0; + char *p = line; + + while (*p != '\0') { + while (*p != '\0' && isspace((unsigned char)*p)) + p++; + if (*p == '\0') + break; + f[n++] = p; + while (*p != '\0' && !isspace((unsigned char)*p)) + p++; + if (*p != '\0') + *p++ = '\0'; + if (n == max) + return n + 1; /* "too many", the caller decides */ + } + return n; +} + +static void report(policy_t *p, int line, const char *kind, + const char *fmt, va_list ap) +{ + fprintf(stderr, "%s:%d: %s: ", p->path != NULL ? p->path : "authz.conf", line, kind); + vfprintf(stderr, fmt, ap); + fputc('\n', stderr); +} + +static void perr(policy_t *p, int line, const char *fmt, ...) +{ + va_list ap; + + p->errors++; + va_start(ap, fmt); + report(p, line, "error", fmt, ap); + va_end(ap); +} + +static void pwarn(policy_t *p, int line, const char *fmt, ...) +{ + va_list ap; + + p->warnings++; + va_start(ap, fmt); + report(p, line, "warning", fmt, ap); + va_end(ap); +} + +/* ------------------------------------------------------------ name shapes */ + +int authz_user_pattern_ok(const char *s) +{ + size_t i; + + if (s[0] == '\0' || !in_set((unsigned char)s[0], user_first)) + return 0; + for (i = 1; s[i] != '\0'; i++) { + if (!in_set((unsigned char)s[i], user_rest)) + return 0; + } + return 1; +} + +int authz_repo_pattern_ok(const char *s) +{ + size_t i; + + if (s[0] == '\0') + return 0; + for (i = 0; s[i] != '\0'; i++) { + if (!in_set((unsigned char)s[i], repo_chars)) + return 0; + } + return 1; +} + +/* ------------------------------------------------------------------ levels */ + +const char *authz_level_name(level_t l) +{ + switch (l) { + case LV_WRITE: return "write"; + case LV_FORCE: return "force"; + case LV_ADMIN: return "admin"; + default: return "none"; + } +} + +int authz_level_of(const char *name) +{ + if (name == NULL) + return -1; + if (strcmp(name, "write") == 0) + return LV_WRITE; + if (strcmp(name, "force") == 0) + return LV_FORCE; + if (strcmp(name, "admin") == 0) + return LV_ADMIN; + return -1; +} + +/* ----------------------------------------------------------- normalisation */ + +char *authz_norm_repo(const char *in) +{ + size_t n; + + if (in == NULL) + return xstrdup(""); + if (strcmp(in, "*") == 0) + return xstrdup("*"); + n = strlen(in); + if (n > 4 && strcmp(in + n - 4, ".git") == 0) + return xsubstr(in, n - 4); + return xstrdup(in); +} + +/* One spelling for a ref, so that "main", "heads/main" and "refs/heads/main" + * are the same branch - and so that a bare name can never mean a tag. */ +char *authz_norm_ref(const char *in) +{ + size_t n; + char *out; + + if (in == NULL || *in == '\0') + return xstrdup("refs/*"); + if (strcmp(in, "*") == 0 || strcmp(in, "all") == 0 || + strcmp(in, "refs") == 0 || strcmp(in, "refs/*") == 0) + return xstrdup("refs/*"); + if (strncmp(in, "refs/", 5) == 0) + return xstrdup(in); + + /* "heads/main", "tags/v1" and "notes/x" are half a refname: the "refs/" is + * missing and nothing else. Anything at all, including a path with slashes + * in it, is a branch name. */ + if (strncmp(in, "heads/", 6) == 0 || strncmp(in, "tags/", 5) == 0 || + strncmp(in, "notes/", 6) == 0) { + n = strlen(in) + 6; + out = xmalloc(n); + snprintf(out, n, "refs/%s", in); + return out; + } + n = strlen(in) + 12; + out = xmalloc(n); + snprintf(out, n, "refs/heads/%s", in); + return out; +} + +/* A whole list of them, space separated, the way 'set' lines store them. */ +static char *norm_ref_list(const char *value) +{ + char *copy = xstrdup(value); + char *fields[MAX_FIELDS]; + strbuf_t out; + int i, n; + + sb_init(&out); + n = authz_split_ws(copy, fields, MAX_FIELDS); + for (i = 0; i < n; i++) { + char *one = authz_norm_ref(fields[i]); + + if (out.len != 0) + sb_add(&out, " "); + sb_add(&out, one); + free(one); + } + free(copy); + return out.s; +} + +/* ------------------------------------------------------------------ tables */ + +static rule_t *rule_slot(policy_t *p) +{ + rule_t *r; + + if (p->nrules == p->rcap) { + p->rcap = p->rcap != 0 ? p->rcap * 2 : 32; + p->rules = xrealloc(p->rules, p->rcap * sizeof(*p->rules)); + } + r = &p->rules[p->nrules++]; + memset(r, 0, sizeof(*r)); + return r; +} + +static void set_protected_for(policy_t *p, const char *repo, const char *refs, int line) +{ + size_t i; + + for (i = 0; i < p->nprot; i++) { + if (strcmp(p->prot_repo[i], repo) == 0) { + pwarn(p, line, "protected-refs for %s is set twice; the last one wins", repo); + free(p->prot_refs[i]); + p->prot_refs[i] = xstrdup(refs); + return; + } + } + if (p->nprot == p->pcap) { + p->pcap = p->pcap != 0 ? p->pcap * 2 : 8; + p->prot_repo = xrealloc(p->prot_repo, p->pcap * sizeof(*p->prot_repo)); + p->prot_refs = xrealloc(p->prot_refs, p->pcap * sizeof(*p->prot_refs)); + } + p->prot_repo[p->nprot] = xstrdup(repo); + p->prot_refs[p->nprot] = xstrdup(refs); + p->nprot++; +} + +/* ---------------------------------------------------------------- the file */ + +static void parse_set(policy_t *p, char *line, int lineno) +{ + char *body = authz_trim(line + 3); + char *sp = body; + char *key, *val, *repo; + + while (*sp != '\0' && !isspace((unsigned char)*sp)) + sp++; + if (*sp == '\0') { + perr(p, lineno, "set needs a value"); + return; + } + key = xsubstr(body, (size_t)(sp - body)); + val = authz_trim(sp); + + if (strcmp(key, "protected-refs") == 0) { + if (p->protected_global != NULL) + pwarn(p, lineno, "set protected-refs appears twice; the last one wins"); + free(p->protected_global); + p->protected_global = norm_ref_list(val); + } else if (strncmp(key, "protected-refs.", 15) == 0) { + repo = authz_norm_repo(key + 15); + if (repo[0] == '\0' || strcmp(repo, "*") == 0) { + perr(p, lineno, "protected-refs. needs one repository, not \"%s\"", key + 15); + } else { + set_protected_for(p, repo, val, lineno); + } + free(repo); + } else { + perr(p, lineno, "unknown setting \"%s\"", key); + } + free(key); +} + +static void parse_rule(policy_t *p, char *line, int lineno) +{ + char *fields[MAX_FIELDS]; + rule_t *r; + int n, i, level; + + n = authz_split_ws(line, fields, MAX_FIELDS); + if (n > MAX_FIELDS) { + perr(p, lineno, "more than %d fields on one line", MAX_FIELDS); + return; + } + if (n < 3) { + perr(p, lineno, "expected \" [ref ...]\": %s", line); + return; + } + level = authz_level_of(fields[2]); + if (level < 0) { + perr(p, lineno, "unknown access level \"%s\" (write force admin)", fields[2]); + return; + } + if (strcmp(fields[0], "@all") != 0 && !authz_user_pattern_ok(fields[0])) { + perr(p, lineno, "invalid user pattern \"%s\"", fields[0]); + return; + } + if (!authz_repo_pattern_ok(fields[1])) { + perr(p, lineno, "invalid repo pattern \"%s\" (flat names only, no slashes)", + fields[1]); + return; + } + for (i = 3; i < n; i++) { + if (fields[i][0] == '!' || fields[i][0] == '-') { + perr(p, lineno, "ref patterns are additive, there is no deny: %s", fields[i]); + return; + } + } + + r = rule_slot(p); + r->user = xstrdup(fields[0]); + r->repo = authz_norm_repo(fields[1]); + r->level = (level_t)level; + r->line = lineno; + if (n > 3) { + r->refs = xmalloc((size_t)(n - 3) * sizeof(*r->refs)); + for (i = 3; i < n; i++) + r->refs[r->nrefs++] = authz_norm_ref(fields[i]); + } +} + +void policy_init(policy_t *p, const char *path) +{ + memset(p, 0, sizeof(*p)); + p->path = xstrdup(path); +} + +void policy_free(policy_t *p) +{ + size_t i; + size_t j; + + for (i = 0; i < p->nrules; i++) { + free(p->rules[i].user); + free(p->rules[i].repo); + for (j = 0; j < p->rules[i].nrefs; j++) + free(p->rules[i].refs[j]); + free(p->rules[i].refs); + } + free(p->rules); + free(p->protected_global); + for (i = 0; i < p->nprot; i++) { + free(p->prot_repo[i]); + free(p->prot_refs[i]); + } + free(p->prot_repo); + free(p->prot_refs); + free(p->path); + memset(p, 0, sizeof(*p)); +} + +int policy_load(policy_t *p) +{ + FILE *f; + char *line = NULL; + size_t cap = 0; + ssize_t len; + int lineno = 0; + + f = fopen(p->path, "r"); + if (f == NULL) { + fprintf(stderr, "%s: cannot read the policy: ", p->path); + perror(""); + return -2; + } + while ((len = getline(&line, &cap, f)) != -1) { + char *hash; + char *text; + + lineno++; + text = authz_trim(line); + hash = strchr(text, '#'); + if (hash != NULL) { + *hash = '\0'; + text = authz_trim(text); + } + if (*text == '\0') + continue; + + if (strncmp(text, "set", 3) == 0 && isspace((unsigned char)text[3])) + parse_set(p, text, lineno); + else + parse_rule(p, text, lineno); + } + free(line); + fclose(f); + return p->errors > 0 ? -1 : 0; +} + +/* ------------------------------------------------------------- the answers */ + +static int user_match(const policy_t *p, const char *pat, const char *user) +{ + if (user == NULL || user[0] == '\0') + return 0; + if (strcmp(pat, "@all") == 0) + return authz_user_known(p, user); + return glob_match(pat, user); +} + +static int repo_match(const char *pat, const char *repo) +{ + char *want; + int hit; + + if (strcmp(pat, "*") == 0) + return 1; + want = authz_norm_repo(repo); + hit = glob_match(pat, want); + free(want); + return hit; +} + +int authz_user_known(const policy_t *p, const char *user) +{ + size_t i; + + if (user == NULL || user[0] == '\0') + return 0; + for (i = 0; i < p->nrules; i++) { + if (strcmp(p->rules[i].user, "@all") == 0) + continue; + if (glob_match(p->rules[i].user, user)) + return 1; + } + return 0; +} + +int authz_repo_reachable(const policy_t *p, const char *repo) +{ + size_t i; + + for (i = 0; i < p->nrules; i++) { + if (repo_match(p->rules[i].repo, repo)) + return 1; + } + return 0; +} + +const char *authz_protected_for(const policy_t *p, const char *repo) +{ + char *want; + size_t i; + + want = authz_norm_repo(repo); + for (i = 0; i < p->nprot; i++) { + if (strcmp(p->prot_repo[i], want) == 0) { + free(want); + return p->prot_refs[i]; + } + } + free(want); + if (p->protected_global != NULL && p->protected_global[0] != '\0') + return p->protected_global; + return DEFAULT_PROTECTED; +} + +static int is_protected(const policy_t *p, const char *repo, const char *ref) +{ + const char *list = authz_protected_for(p, repo); + char *copy = xstrdup(list); + char *fields[MAX_FIELDS]; + int n, i, hit = 0; + + n = authz_split_ws(copy, fields, MAX_FIELDS); + for (i = 0; i < n; i++) { + if (glob_match(fields[i], ref)) { + hit = 1; + break; + } + } + free(copy); + return hit; +} + +static int ordinary_ref(const char *ref) +{ + return strncmp(ref, "refs/heads/", 11) == 0 || strncmp(ref, "refs/tags/", 10) == 0; +} + +level_t authz_need_for(const policy_t *p, const char *repo, const char *ref, + level_t have, char *raised, size_t raisedsz) +{ + if (raised != NULL && raisedsz > 0) + raised[0] = '\0'; + + /* Anything that is not a branch or a tag is for maintainers: refs/notes, + * refs/merge-requests, whatever a tool invents next. */ + if (!ordinary_ref(ref) && have != LV_ADMIN) { + if (raised != NULL) + snprintf(raised, raisedsz, "neither a branch nor a tag"); + return LV_ADMIN; + } + if (!is_protected(p, repo, ref)) + return have; + if (have == LV_WRITE || have == LV_FORCE) { + if (raised != NULL) + snprintf(raised, raisedsz, "protected"); + return have == LV_WRITE ? LV_FORCE : LV_ADMIN; + } + return have; +} + +/* Everything one user may do to one repository at or above 'min': whether any + * of it is unscoped, whether 'ref' is covered, and the strongest level the user + * holds there at all (which is what a denial names). */ +typedef struct { + int unscoped; /* a rule with no ref list: the whole repository */ + int scoped; /* a rule with a ref list, at or above the level */ + int hit; /* and one of its patterns is this ref */ + level_t strongest; /* the best this user holds there at all */ + int any; /* any rule at all, at any level */ +} scope_t; + +static void scope(const policy_t *p, const char *user, const char *repo, + level_t min, const char *ref, scope_t *s) +{ + size_t i; + size_t j; + + s->unscoped = 0; + s->scoped = 0; + s->hit = 0; + s->strongest = LV_NONE; + s->any = 0; + + for (i = 0; i < p->nrules; i++) { + if (!user_match(p, p->rules[i].user, user)) + continue; + if (!repo_match(p->rules[i].repo, repo)) + continue; + s->any = 1; + if (p->rules[i].level > s->strongest) + s->strongest = p->rules[i].level; + if (p->rules[i].level < min) + continue; + if (p->rules[i].nrefs == 0) { + s->unscoped = 1; + continue; + } + s->scoped = 1; + for (j = 0; ref != NULL && j < p->rules[i].nrefs; j++) { + if (glob_match(p->rules[i].refs[j], ref)) { + s->hit = 1; + break; + } + } + } +} + +void authz_check(const policy_t *p, const char *user, const char *repo, + level_t need, const char *ref, decision_t *out) +{ + scope_t s; + char raised[64]; + char *nref = NULL; + level_t want = need; + + out->allowed = 0; + out->need = need; + out->why[0] = '\0'; + + if (user == NULL || user[0] == '\0') { + out->need = need; + snprintf(out->why, sizeof(out->why), "no identity was presented"); + return; + } + + if (ref == NULL) { + /* No ref named: "may this user do this somewhere in the repository?" */ + scope(p, user, repo, need, NULL, &s); + if (s.unscoped || s.scoped) { + snprintf(out->why, sizeof(out->why), "%s has %s on %s of %s", + user, authz_level_name(need), + s.unscoped ? "all" : "some refs", authz_norm_repo(repo)); + out->allowed = 1; + } else { + snprintf(out->why, sizeof(out->why), "%s has no %s on %s", + user, authz_level_name(need), authz_norm_repo(repo)); + } + return; + } + + nref = authz_norm_ref(ref); + want = authz_need_for(p, repo, nref, need, raised, sizeof(raised)); + out->need = want; + + scope(p, user, repo, want, nref, &s); + + if (s.unscoped || s.hit) { + char *r = authz_norm_repo(repo); + + if (raised[0] != '\0') + snprintf(out->why, sizeof(out->why), + "%s has %s on %s %s (raised from %s: %s is %s)", + user, authz_level_name(want), r, nref, + authz_level_name(need), nref, raised); + else + snprintf(out->why, sizeof(out->why), "%s has %s on %s %s", + user, authz_level_name(want), r, nref); + free(r); + out->allowed = 1; + free(nref); + return; + } + + { + char *r = authz_norm_repo(repo); + + if (!s.any) + snprintf(out->why, sizeof(out->why), "%s has no rule for %s; %s needs %s", + user, authz_norm_repo(repo), nref, authz_level_name(want)); + else if (raised[0] != '\0') + snprintf(out->why, sizeof(out->why), + "%s has %s on %s; %s needs %s (raised from %s: %s is %s)", + user, authz_level_name(s.strongest), r, nref, + authz_level_name(want), authz_level_name(need), nref, raised); + else + snprintf(out->why, sizeof(out->why), "%s has %s on %s; %s needs %s", + user, authz_level_name(s.strongest), r, nref, + authz_level_name(want)); + free(r); + } + free(nref); +} -- cgit v1.2.3