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/edit.c | 122 +------------------------------------------------------------ 1 file changed, 2 insertions(+), 120 deletions(-) (limited to 'src/edit.c') 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); - } -} -- cgit v1.2.3