summaryrefslogtreecommitdiffstats
path: root/tests/unit.c
diff options
context:
space:
mode:
authordev <dev@example>2026-11-13 22:28:22 +0000
committerdev <dev@example>2026-11-13 22:28:22 +0000
commit50f296cf7609ce77d41b9fb3df51d6aeec8330d6 (patch)
treea84e367145fa7f86f285b844144c55b3b3452cf3 /tests/unit.c
parente12fc6e1c14afaab8711c6c9ac695ce911d83df5 (diff)
downloadaccess-control-50f296cf7609ce77d41b9fb3df51d6aeec8330d6.tar.gz
access-control-50f296cf7609ce77d41b9fb3df51d6aeec8330d6.zip
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.
Diffstat (limited to 'tests/unit.c')
-rw-r--r--tests/unit.c134
1 files changed, 67 insertions, 67 deletions
diff --git a/tests/unit.c b/tests/unit.c
index 0d21ee3..cf55282 100644
--- a/tests/unit.c
+++ b/tests/unit.c
@@ -237,8 +237,7 @@ static void test_parse(void)
" \n"
"alice web admin # trailing comment\n"
"bob web write refs/heads/feature/* refs/heads/dev\n"
- "@all team write main\n"
- "set protected-refs main refs/tags/*\n");
+ "@all team write main\n");
CHECK_INT(rc, 0);
CHECK_INT(p.errors, 0);
CHECK_INT(p.nrules, 3);
@@ -250,7 +249,6 @@ static void test_parse(void)
CHECK_STR(p.rules[1].refs[0], "refs/heads/feature/*");
CHECK_INT(p.rules[1].nrefs, 2);
CHECK_STR(p.rules[2].refs[0], "refs/heads/main"); /* normalised at parse time */
- CHECK_STR(p.protected_global, "refs/heads/main refs/tags/*");
policy_free(&p);
/* Errors: each of these must stop the file being used at all. The complaints
@@ -288,7 +286,14 @@ static void test_parse(void)
CHECK_INT(rc, -1);
policy_free(&p);
- rc = load_text(&p, "set protected-refs\n");
+ /* Removed settings are refused by name, not ignored: a policy that
+ * still carries one would otherwise mean "everyone with write can fast
+ * -forward main" while looking like it said the opposite. */
+ rc = load_text(&p, "set protected-refs refs/heads/main\n");
+ CHECK_INT(rc, -1);
+ policy_free(&p);
+
+ rc = load_text(&p, "set protected-refs.web refs/heads/main\n");
CHECK_INT(rc, -1);
policy_free(&p);
@@ -301,19 +306,10 @@ static void test_parse(void)
close(keep);
}
- /* The default protected list, and the two ways to replace it. */
+ /* There is no settings list any more: a rule says what it says. */
rc = load_text(&p, "alice * admin\n");
CHECK_INT(rc, 0);
- CHECK_STR(authz_protected_for(&p, "web"), "refs/heads/main");
- policy_free(&p);
-
- rc = load_text(&p,
- "set protected-refs refs/heads/main refs/heads/stable\n"
- "set protected-refs.web refs/heads/release\n"
- "alice * admin\n");
- CHECK_INT(rc, 0);
- CHECK_STR(authz_protected_for(&p, "web"), "refs/heads/release");
- CHECK_STR(authz_protected_for(&p, "other"), "refs/heads/main refs/heads/stable");
+ CHECK_INT(p.nrules, 1);
policy_free(&p);
}
@@ -347,35 +343,32 @@ static void test_known_users(void)
static void test_need_for(void)
{
- policy_t p;
char raised[64];
- int rc;
- rc = load_text(&p,
- "set protected-refs refs/heads/main refs/tags/*\n"
- "alice * admin\n");
- CHECK_INT(rc, 0);
- CHECK_INT(authz_need_for(&p, "web", "refs/heads/dev", LV_WRITE, raised,
- sizeof(raised)), LV_WRITE);
+ /* A branch or a tag costs exactly what the rule said: there is no list
+ * elsewhere in the file that can raise the price of one. */
+ CHECK_INT(authz_need_for("refs/heads/dev", LV_WRITE, raised, sizeof(raised)),
+ LV_WRITE);
CHECK_STR(raised, "");
- CHECK_INT(authz_need_for(&p, "web", "refs/heads/main", LV_WRITE, raised,
- sizeof(raised)), LV_FORCE);
- CHECK_STR(raised, "protected");
- CHECK_INT(authz_need_for(&p, "web", "refs/heads/main", LV_FORCE, raised,
- sizeof(raised)), LV_ADMIN);
- CHECK_INT(authz_need_for(&p, "web", "refs/heads/main", LV_ADMIN, raised,
- sizeof(raised)), LV_ADMIN);
- CHECK_STR(raised, ""); /* admin is already the top */
- CHECK_INT(authz_need_for(&p, "web", "refs/tags/v1", LV_WRITE, raised,
- sizeof(raised)), LV_FORCE);
- CHECK_INT(authz_need_for(&p, "web", "refs/pull/1/head", LV_WRITE, raised,
- sizeof(raised)), LV_ADMIN);
+ CHECK_INT(authz_need_for("refs/heads/main", LV_WRITE, raised, sizeof(raised)),
+ LV_WRITE);
+ CHECK_INT(authz_need_for("refs/tags/v1", LV_WRITE, raised, sizeof(raised)),
+ LV_WRITE);
+ CHECK_INT(authz_need_for("refs/heads/feature/x", LV_FORCE, raised, sizeof(raised)),
+ LV_FORCE);
+
+ /* Anything outside refs/heads and refs/tags is for maintainers, whatever a
+ * rule appears to grant about it. */
+ CHECK_INT(authz_need_for("refs/pull/1/head", LV_WRITE, raised, sizeof(raised)),
+ LV_ADMIN);
CHECK_STR(raised, "neither a branch nor a tag");
- CHECK_INT(authz_need_for(&p, "web", "refs/pull/1/head", LV_FORCE, raised,
- sizeof(raised)), LV_ADMIN);
- CHECK_INT(authz_need_for(&p, "web", "refs/notes/x", LV_ADMIN, raised,
- sizeof(raised)), LV_ADMIN);
- policy_free(&p);
+ CHECK_INT(authz_need_for("refs/pull/1/head", LV_FORCE, raised, sizeof(raised)),
+ LV_ADMIN);
+ CHECK_INT(authz_need_for("refs/notes/x", LV_WRITE, raised, sizeof(raised)),
+ LV_ADMIN);
+ CHECK_INT(authz_need_for("refs/notes/x", LV_ADMIN, raised, sizeof(raised)),
+ LV_ADMIN);
+ CHECK_STR(raised, ""); /* admin is already the top */
}
static void test_check(void)
@@ -384,9 +377,9 @@ static void test_check(void)
int rc;
rc = load_text(&p,
- "set protected-refs refs/heads/main refs/tags/*\n"
"alice * admin\n"
- "@all * write refs/heads/*\n"
+ "@all * write refs/heads/feature/*\n"
+ "frank team write refs/heads/main refs/tags/*\n"
"bob team write refs/heads/feature/*\n"
"carol team force refs/heads/feature/*\n"
"dave team admin\n"
@@ -407,19 +400,27 @@ static void test_check(void)
/* '@all' reaches the named, and nobody else. Note that the '@all *' rule
* below reaches every repository, so a named user is not confined to the
* repository their own rule names - that is what the rule says. */
- expect_allow(&p, "carol", "web", LV_WRITE, "refs/heads/any");
- expect_allow(&p, "erin", "web", LV_WRITE, "refs/heads/any");
+ expect_allow(&p, "carol", "web", LV_WRITE, "refs/heads/feature/any");
+ expect_allow(&p, "erin", "web", LV_WRITE, "refs/heads/feature/any");
expect_deny(&p, "nobody", "web", LV_WRITE, "refs/heads/any", "no rule");
expect_deny(&p, "", "web", LV_WRITE, "refs/heads/any", "no identity");
expect_deny(&p, NULL, "web", LV_WRITE, "refs/heads/any", "no identity");
- /* protected refs raise the cost, and say so */
- expect_deny(&p, "bob", "team", LV_WRITE, "refs/heads/main", "raised from write");
- expect_deny(&p, "carol", "team", LV_FORCE, "refs/heads/main", "raised from force");
- /* the raise applies to the level the action asked for, not to the best
- * level the user happens to hold somewhere else */
- expect_deny(&p, "carol", "team", LV_WRITE, "refs/heads/main", "needs force");
+ /* main is not special: it is refused here because no rule of bob's or
+ * carol's names it, and allowed for dave because his names everything. */
+ expect_deny(&p, "bob", "team", LV_WRITE, "refs/heads/main",
+ "but not on refs/heads/main");
+ expect_deny(&p, "carol", "team", LV_FORCE, "refs/heads/main",
+ "but not on refs/heads/main");
expect_allow(&p, "dave", "team", LV_WRITE, "refs/heads/main");
+ /* and the person whose rule does name main, and nothing else, gets main */
+ expect_allow(&p, "frank", "team", LV_WRITE, "refs/heads/main");
+ /* frank reaches the '@all' rule like anyone else the policy names, so what
+ * he lacks is a branch nobody's rule names, and a rewrite of his own */
+ expect_allow(&p, "frank", "team", LV_WRITE, "refs/heads/feature/x");
+ expect_deny(&p, "frank", "team", LV_WRITE, "refs/heads/dev",
+ "but not on refs/heads/dev");
+ expect_deny(&p, "frank", "team", LV_FORCE, "refs/heads/main", "needs force");
/* refs that are not branches or tags: only an admin, at any request level */
expect_allow(&p, "dave", "team", LV_WRITE, "refs/pull/1/head");
@@ -427,9 +428,9 @@ static void test_check(void)
expect_deny(&p, "bob", "team", LV_ADMIN, "refs/notes/x", "needs admin");
/* the same branch, three spellings */
- expect_deny(&p, "bob", "team", LV_WRITE, "main", "protected");
- expect_deny(&p, "bob", "team", LV_WRITE, "heads/main", "protected");
- expect_deny(&p, "bob", "team", LV_WRITE, "refs/heads/main", "protected");
+ expect_deny(&p, "bob", "team", LV_WRITE, "main", "but not on refs/heads/main");
+ expect_deny(&p, "bob", "team", LV_WRITE, "heads/main",
+ "but not on refs/heads/main");
/* repository matching: .git is the same repository */
expect_allow(&p, "bob", "team.git", LV_WRITE, "refs/heads/feature/x");
@@ -465,14 +466,14 @@ static void test_scope_edges(void)
policy_t p;
int rc;
- /* a rule with no refs is the whole repository, protected refs included */
+ /* a rule with no refs is the whole repository: every branch, main included,
+ * which is exactly why naming the refs is the honest way to write one */
rc = load_text(&p, "bob team write\n");
CHECK_INT(rc, 0);
expect_allow(&p, "bob", "team", LV_WRITE, "refs/heads/anything");
- expect_deny(&p, "bob", "team", LV_WRITE, "refs/heads/main", "protected");
- /* write on everything still is not force: the raise asks for the next step
- * up, and bob never got it */
- expect_deny(&p, "bob", "team", LV_FORCE, "refs/heads/main", "needs admin");
+ expect_allow(&p, "bob", "team", LV_WRITE, "refs/heads/main");
+ /* write on everything still is not force */
+ expect_deny(&p, "bob", "team", LV_FORCE, "refs/heads/main", "needs force");
policy_free(&p);
/* a bare name is a branch, and only a branch */
@@ -496,18 +497,17 @@ static void test_scope_edges(void)
expect_deny(&p, "bob", "team", LV_FORCE, "refs/heads/dev",
"bob has write on team refs/heads/dev; it needs force");
expect_deny(&p, "bob", "team", LV_WRITE, "refs/heads/other",
- "nothing at all on refs/heads/other");
+ "but not on refs/heads/other");
policy_free(&p);
- /* per-repository protection replaces the global list, it does not add to it */
- rc = load_text(&p,
- "set protected-refs refs/heads/main\n"
- "set protected-refs.web refs/heads/release\n"
- "bob web write refs/heads/*\n");
+ /* naming refs in a rule is the whole of the mechanism: what is not named is
+ * not granted, and the refusal says so rather than blaming a list */
+ rc = load_text(&p, "bob web write refs/heads/feature/* refs/heads/dev\n");
CHECK_INT(rc, 0);
- expect_allow(&p, "bob", "web", LV_WRITE, "refs/heads/main");
- expect_deny(&p, "bob", "web", LV_WRITE, "refs/heads/release", "protected");
- expect_deny(&p, "bob", "api", LV_WRITE, "refs/heads/release", "no rule");
+ expect_allow(&p, "bob", "web", LV_WRITE, "refs/heads/dev");
+ expect_allow(&p, "bob", "web", LV_WRITE, "refs/heads/feature/x");
+ expect_deny(&p, "bob", "web", LV_WRITE, "refs/heads/main", "but not on");
+ expect_deny(&p, "bob", "api", LV_WRITE, "refs/heads/dev", "no rule");
policy_free(&p);
/* reachable-by-nobody detection, which validate reports */