From 50f296cf7609ce77d41b9fb3df51d6aeec8330d6 Mon Sep 17 00:00:00 2001 From: dev Date: Fri, 13 Nov 2026 22:28:22 +0000 Subject: Remove protected-refs: a rule covers what it names, and nothing else `set protected-refs` raised the price of a listed ref by one level: write needed force on main, force needed admin. It was a second, invisible list, and it was the one part of the model that could not be read off the rules. The question it was supposed to answer - "who can move main?" - stopped having an answer in the file: you had to hold both the rules and the protection list in your head and combine them, and the combination was where surprises lived. Giving someone force so they could rewrite their own branches quietly let them fast-forward main, because a fast-forward costs write, protection raised it to force, and there it was. Now a rule says what it says. An unscoped rule covers main; a rule that means to leave main alone names what it covers instead, and the refusal says "bob holds write on team, but not on refs/heads/main" rather than blaming a list nobody remembered. Excluding one person from one branch is a matter of what their lines name, which is the same mechanism as everything else here. The setting is refused by name rather than ignored. A policy that still carries one would otherwise mean "everyone with write can fast-forward main" while looking like it said the opposite, and the operator would learn that from a push rather than from the parser; so `set protected-refs` is now an error that says what to write instead, and the suite asserts both halves - that it is refused, and that cleaning it out makes the file valid again. What stays: refs outside refs/heads and refs/tags still need admin whatever a rule appears to grant about them. That one the policy genuinely cannot name in advance, because it is the part of the namespace no policy was written with in mind. `protect` goes with it, so the editing commands are grant and revoke. The example policy now names frank, whose two refs are main and the release tags, because that is the shape the file is now meant to be written in. --- src/authz.h | 34 +++++------- src/edit.c | 122 +----------------------------------------- src/main.c | 13 ++--- src/policy.c | 164 +++++++++------------------------------------------------ src/validate.c | 3 +- 5 files changed, 46 insertions(+), 290 deletions(-) (limited to 'src') diff --git a/src/authz.h b/src/authz.h index 53f251d..794c12c 100644 --- a/src/authz.h +++ b/src/authz.h @@ -49,20 +49,14 @@ typedef struct { rule_t *rules; size_t nrules, rcap; - char *protected_global; /* space separated normalised patterns, or NULL */ - char **prot_repo; /* repositories with their own list */ - char **prot_refs; /* and the list that goes with each */ - size_t nprot, pcap; - int errors; - int warnings; int unreadable; /* the file itself could not be opened */ char *path; /* what messages call the file */ } policy_t; typedef struct { int allowed; - level_t need; /* what it cost in the end, after any raise */ + level_t need; /* what it cost in the end */ char why[512]; /* the sentence the person pushing reads */ } decision_t; @@ -83,8 +77,8 @@ void policy_init(policy_t *p, const char *path); void policy_free(policy_t *p); /* Read and parse. Returns 0 when the file is usable, -1 when it produced - * errors, -2 when it could not be read at all. Errors and warnings are - * reported as they are found, on stderr, with the line number. */ + * errors, -2 when it could not be read at all. Errors are reported as they are + * found, on stderr, with the line number. */ int policy_load(policy_t *p); /* Refs and repositories have one spelling each internally. A bare name is a @@ -115,13 +109,14 @@ int authz_level_of(const char *name); * "everybody this policy already knows about", and never invents a name. */ int authz_user_known(const policy_t *p, const char *user); -/* What a ref really costs. Anything that is not a branch or a tag is for - * maintainers; on a protected ref 'write' is not enough and 'force' is not - * enough either. When the answer is higher than 'have', the reason - * ("protected", "neither a branch nor a tag") is written to raised, which must - * hold at least 64 bytes. */ -level_t authz_need_for(const policy_t *p, const char *repo, const char *ref, - level_t have, char *raised, size_t raisedsz); +/* What a ref really costs beyond what the rule said. Anything that is not a + * branch or a tag is for maintainers, whatever a rule about it appears to + * grant: refs/notes, refs/merge-requests, whatever a tool invents next, are not + * part of the namespace a policy was written with in mind, and the safe reading + * of "not mentioned" is "not granted". When the answer is higher than 'have', + * the reason is written to raised, which must hold at least 64 bytes. */ +level_t authz_need_for(const char *ref, level_t have, char *raised, + size_t raisedsz); /* The decision. 'ref' may be NULL, which asks "may this user do this * somewhere in the repository" rather than about one ref. */ @@ -133,10 +128,6 @@ void authz_check(const policy_t *p, const char *user, const char *repo, * matching rather than a decision. */ int authz_repo_reachable(const policy_t *p, const char *repo); -/* The protected list in force for one repository, as stored (normalised - * patterns, space separated). Never NULL. */ -const char *authz_protected_for(const policy_t *p, const char *repo); - /* --------------------------------------------------------------- hook.c */ /* authorize mode: identity from the environment, ref lines from stdin, the @@ -179,7 +170,7 @@ typedef struct { const char *out; /* NULL edits in place; "-" is stdout */ } edit_opts; -/* Shared by the three editing commands: pulls --out and --stdout out of the +/* Shared by the two editing commands: pulls --out and --stdout out of the * argument list wherever they sit (an option at the end reads better in a * shell history) and compacts what is left into argv, returning the count in * *consumed. Returns -1 on a missing value. */ @@ -191,7 +182,6 @@ int authz_reject_options(int argc, char **argv); int cmd_grant(const char *path, edit_opts o, int argc, char **argv); int cmd_revoke(const char *path, edit_opts o, int argc, char **argv); -int cmd_protect(const char *path, edit_opts o, int argc, char **argv); /* ---------------------------------------------------------------- main.c * Shared little helpers, so that no file invents its own way to fail. */ diff --git a/src/edit.c b/src/edit.c index 1c34ae4..9b3c233 100644 --- a/src/edit.c +++ b/src/edit.c @@ -205,29 +205,6 @@ static int is_rule_for(const char *line, const char *user, const char *repo) return same; } -static int is_set_for(const char *line, const char *key) -{ - char *copy = xstrdup(line); - char *text = authz_trim(copy); - char *hash = strchr(text, '#'); - size_t klen = strlen(key); - int hit = 0; - - if (hash != NULL) - *hash = '\0'; - if (strncmp(text, "set", 3) == 0 && isspace((unsigned char)text[3])) { - text = authz_trim(text + 3); - hit = strncmp(text, key, klen) == 0 && - (text[klen] == '\0' || isspace((unsigned char)text[klen])); - } - free(copy); - return hit; -} - -/* --------------------------------------------------------------- emitting */ - -/* "user repo level ref ref" - one line, no alignment tricks, because the next - * tool to read this is diff. */ static char *rule_text(const char *user, const char *repo, const char *level, int nrefs, char **refs) { @@ -392,7 +369,8 @@ int cmd_grant(const char *path, edit_opts o, int argc, char **argv) lines_push(&lines, xstrdup("# " "[ref pattern ...] levels stack: " "admin > force > write\n")); - lines_push(&lines, xstrdup("set protected-refs refs/heads/main\n")); + lines_push(&lines, xstrdup("# Name the refs a rule covers: with no " + "patterns it covers the whole repository.\n")); } else if (read_lines(path, &lines) != 0) return 3; @@ -493,99 +471,3 @@ int cmd_revoke(const char *path, edit_opts o, int argc, char **argv) return finish(&lines, path, o, what); } } - -int cmd_protect(const char *path, edit_opts o, int argc, char **argv) -{ - lines_t lines; - const char *repo = NULL; - char key[256]; - char *line; - strbuf_t text; - int i; - int replaced = -1; - int created = 0; - for (i = 0; i < argc; i++) { /* --repo anywhere, like --out */ - if (strcmp(argv[i], "--repo") != 0) - continue; - if (i + 1 >= argc) { - fprintf(stderr, "%s: --repo wants a repository name\n", progname); - return 64; - } - repo = argv[i + 1]; - for (; i + 2 < argc; i++) - argv[i] = argv[i + 2]; - argc -= 2; - break; - } - if (argc < 1) { - fprintf(stderr, "%s: protect wants [--repo ] ...\n", - progname); - return 64; - } - if (repo != NULL && !authz_repo_pattern_ok(repo)) { - fprintf(stderr, "%s: invalid repo pattern \"%s\"\n", progname, repo); - return 64; - } - for (i = 0; i < argc; i++) { - if (argv[i][0] == '-' && argv[i][1] == '-') { - fprintf(stderr, "%s: unknown option \"%s\" (the only one here is " - "--repo, which comes first)\n", progname, argv[i]); - return 64; - } - if (argv[i][0] == '!' || argv[i][0] == '-') { - fprintf(stderr, "%s: ref patterns are additive, there is no deny: %s\n", - progname, argv[i]); - return 64; - } - } - if (refuse_broken(path, &created) != 0) - return 3; - if (created) { - lines_init(&lines); - lines_push(&lines, xstrdup("# git-authz policy\n")); - } else if (read_lines(path, &lines) != 0) - return 3; - - if (repo != NULL) - snprintf(key, sizeof(key), "protected-refs.%s", repo); - else - snprintf(key, sizeof(key), "protected-refs"); - - sb_init(&text); - sb_addf(&text, "set %s", key); - for (i = 0; i < argc; i++) { - char *one = authz_norm_ref(argv[i]); - - sb_addf(&text, " %s", one); - free(one); - } - sb_add(&text, "\n"); - - line = xstrdup(text.s); /* the array owns its own copy; text is - * released by sb_free below */ - for (i = 0; i < (int)lines.n; i++) { - if (!is_set_for(lines.v[i], key)) - continue; - free(lines.v[i]); - lines.v[i] = line; - replaced = i + 1; - break; - } - if (replaced < 0) { - lines_push(&lines, line); - replaced = (int)lines.n; - } - sb_free(&text); - - { - char what[512]; - - if (created) - snprintf(what, sizeof(what), "protected refs for %s (%s, new policy)", - repo != NULL ? repo : "every repository", key); - else - snprintf(what, sizeof(what), "protected refs for %s (%s, line %d)", - repo != NULL ? repo : "every repository", key, replaced); - return finish(&lines, path, o, what); - } -} diff --git a/src/main.c b/src/main.c index 368751b..8a42b72 100644 --- a/src/main.c +++ b/src/main.c @@ -3,7 +3,7 @@ * check an operator asking, or a script * authorize git asking, once per push (also reached as 'pre-receive') * validate the entrypoint and CI asking before anything is served - * edit changing the file (grant, revoke, protect) + * edit changing the file (grant, revoke) * * They are one program because they must agree, exactly, about what a rule * means: a check that answers differently from the hook is worse than two @@ -58,14 +58,14 @@ static void usage(FILE *f) "\n" " grant [ref ...] add one rule, or replace that pair's\n" " revoke take it away again\n" -" protect [--repo R] ... set which refs need an extra level\n" " --out FILE | --stdout write elsewhere instead of in place\n" " The deployed policy is a read-only mount on purpose; these are for the\n" " source copy, and they say so when they are pointed at a read-only file.\n" "\n" "Levels are monotone: admin implies force implies write. '@all' means everyone\n" "the policy already names. \"main\", \"heads/main\" and \"refs/heads/main\" are the\n" -"same branch; a bare name is always a branch and never a tag.\n" +"same branch; a bare name is always a branch and never a tag. A rule says what\n" +"it says: nothing else in the file can raise the price of a ref.\n" "\n" "Exit status: 0 allowed, 1 denied, 3 the policy could not be read or does not\n" "parse - nothing is allowed on a policy that cannot be read - and 64 for usage.\n", @@ -274,8 +274,7 @@ int main(int argc, char **argv) /* The three commands that change the file. They read it themselves, and * they must not disagree with the checker about what a rule means, which is * why they are in the same binary. */ - if (strcmp(cmd, "grant") == 0 || strcmp(cmd, "revoke") == 0 || - strcmp(cmd, "protect") == 0) { + if (strcmp(cmd, "grant") == 0 || strcmp(cmd, "revoke") == 0) { int consumed = 0; if (authz_parse_out(argc, argv, &eo, &consumed) != 0) @@ -283,9 +282,7 @@ int main(int argc, char **argv) argc -= consumed; /* what is left was compacted to the front */ if (strcmp(cmd, "grant") == 0) return cmd_grant(path, eo, argc, argv); - if (strcmp(cmd, "revoke") == 0) - return cmd_revoke(path, eo, argc, argv); - return cmd_protect(path, eo, argc, argv); + return cmd_revoke(path, eo, argc, argv); } policy_init(&p, path); diff --git a/src/policy.c b/src/policy.c index 1866bb2..ef429e0 100644 --- a/src/policy.c +++ b/src/policy.c @@ -2,8 +2,6 @@ * * 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 @@ -92,16 +90,6 @@ static void perr(policy_t *p, int line, const char *fmt, ...) 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) @@ -203,29 +191,6 @@ char *authz_norm_ref(const char *in) } /* 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; @@ -239,35 +204,11 @@ static rule_t *rule_slot(policy_t *p) 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; + char *key; while (*sp != '\0' && !isspace((unsigned char)*sp)) sp++; @@ -276,24 +217,19 @@ static void parse_set(policy_t *p, char *line, int lineno) 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 { + + /* protected-refs was removed, and it is named rather than left to "unknown + * setting" on purpose. A policy that still carries one of these lines has + * to be refused rather than read as if the line were a comment: ignoring it + * would leave every branch one fast-forward away from whoever holds 'write' + * on them, which is the opposite of what the line was there to prevent, and + * the operator would learn that from a push instead of from the parser. */ + if (strncmp(key, "protected-refs", 14) == 0) + perr(p, lineno, "protected-refs no longer exists: name the refs a rule " + "covers instead, for example \"dave team admin refs/heads/main\""); + else perr(p, lineno, "unknown setting \"%s\"", key); - } + free(key); } @@ -364,13 +300,6 @@ void policy_free(policy_t *p) 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)); } @@ -464,67 +393,26 @@ int authz_repo_reachable(const policy_t *p, const char *repo) 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) +level_t authz_need_for(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. */ + /* Anything that is not a branch or a tag is for maintainers, whatever a rule + * appears to grant about it. Nothing else raises a price here: a rule says + * what it says, and there is no second, invisible list that can make a ref + * cost more behind the operator's back. */ 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; } @@ -637,7 +525,7 @@ void authz_check(const policy_t *p, const char *user, const char *repo, } nref = authz_norm_ref(ref); - want = authz_need_for(p, repo, nref, need, raised, sizeof(raised)); + want = authz_need_for(nref, need, raised, sizeof(raised)); out->need = want; scope(p, user, repo, want, nref, &s); @@ -647,9 +535,9 @@ void authz_check(const policy_t *p, const char *user, const char *repo, if (raised[0] != '\0') snprintf(out->why, sizeof(out->why), - "%s has %s on %s %s (raised from %s: %s is %s)", + "%s has %s on %s %s (this ref costs %s, not %s: %s)", user, authz_level_name(want), r, nref, - authz_level_name(need), nref, raised); + authz_level_name(want), authz_level_name(need), raised); else snprintf(out->why, sizeof(out->why), "%s has %s on %s %s", user, authz_level_name(want), r, nref); @@ -667,17 +555,17 @@ void authz_check(const policy_t *p, const char *user, const char *repo, user, r, nref, authz_level_name(want)); else if (s.here == LV_NONE) snprintf(out->why, sizeof(out->why), - "%s has %s on %s, and nothing at all on %s; it needs %s%s%s%s", + "%s holds %s on %s, but not on %s; that ref needs %s%s%s%s", user, authz_level_name(s.strongest), r, nref, authz_level_name(want), - raised[0] != '\0' ? " (raised: " : "", + raised[0] != '\0' ? " (" : "", raised[0] != '\0' ? raised : "", raised[0] != '\0' ? ")" : ""); else if (raised[0] != '\0') snprintf(out->why, sizeof(out->why), - "%s has %s on %s %s; it needs %s (raised from %s: %s)", + "%s has %s on %s %s; it needs %s (%s)", user, authz_level_name(s.here), r, nref, - authz_level_name(want), authz_level_name(need), raised); + authz_level_name(want), raised); else snprintf(out->why, sizeof(out->why), "%s has %s on %s %s; it needs %s", user, authz_level_name(s.here), r, nref, authz_level_name(want)); diff --git a/src/validate.c b/src/validate.c index ab8348c..81865d5 100644 --- a/src/validate.c +++ b/src/validate.c @@ -456,8 +456,7 @@ int cmd_validate(const policy_t *p, validate_opts o) p->path, p->errors); return 3; } - vok("%s: %zu rule(s), protected refs: %s", p->path, p->nrules, - authz_protected_for(p, "*")); + vok("%s: %zu rule(s)", p->path, p->nrules); if (stat(p->path, &st) == 0 && (st.st_mode & 0004) == 0) vwarn("%s is not readable by anyone outside its owner and group: the hook " -- cgit v1.2.3