// The decision, and the sentence that goes with it: a refusal names the level // held, the ref, and the level required. #include "test.h" static void test_check(void) { policy_t p; int rc; rc = load_text(&p, "alice * admin\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" "erin secrets admin\n"); CHECK_INT(rc, 0); // the ladder expect_allow(&p, "bob", "team", LV_WRITE, "refs/heads/feature/x"); expect_deny(&p, "bob", "team", LV_FORCE, "refs/heads/feature/x", "needs force"); expect_deny(&p, "bob", "team", LV_ADMIN, "refs/heads/feature/x", "needs admin"); expect_allow(&p, "carol", "team", LV_FORCE, "refs/heads/feature/x"); expect_allow(&p, "dave", "team", LV_ADMIN, "refs/heads/main"); // strongest match wins, additively expect_allow(&p, "dave", "team", LV_WRITE, "refs/heads/feature/x"); expect_allow(&p, "alice", "anything", LV_ADMIN, "refs/heads/main"); // '@all' reaches the named and nobody else. The '@all *' rule reaches every // repository, so a named user is not confined to their own rule's repo. 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"); // main is not special: refused because no rule of theirs names it, 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 '@all' like anyone named, 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"); expect_allow(&p, "dave", "team", LV_ADMIN, "refs/pull/1/head"); 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", "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"); expect_allow(&p, "erin", "secrets.git", LV_ADMIN, "refs/heads/main"); // the question without a ref expect_allow(&p, "dave", "team", LV_FORCE, NULL); expect_allow(&p, "alice", "team", LV_ADMIN, NULL); expect_allow(&p, "bob", "team", LV_WRITE, NULL); expect_deny(&p, "bob", "team", LV_FORCE, NULL, "no force"); expect_allow(&p, "erin", "team", LV_WRITE, NULL); // via '@all *' expect_deny(&p, "nobody", "team", LV_WRITE, NULL, "no write"); // what a denial names: the level held, and the level needed { decision_t d; authz_check(&p, "bob", "team", LV_ADMIN, "refs/heads/feature/x", &d); CHECK(!d.allowed); CHECK_INT(d.need, LV_ADMIN); CHECK(strstr(d.why, "bob has write") != NULL); CHECK(strstr(d.why, "needs admin") != NULL); authz_check(&p, "nobody", "team", LV_WRITE, "refs/heads/dev", &d); CHECK(!d.allowed); CHECK(strstr(d.why, "nobody has no rule for team") != NULL); } policy_free(&p); } static void test_scope_edges(void) { policy_t p; int rc; // 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_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 rc = load_text(&p, "bob web write refs/heads/*\n"); CHECK_INT(rc, 0); expect_allow(&p, "bob", "web", LV_WRITE, "v1"); // refs/heads/v1 expect_deny(&p, "bob", "web", LV_WRITE, "refs/tags/v1", "needs write"); expect_deny(&p, "bob", "web", LV_WRITE, "tags/v1", "needs write"); policy_free(&p); // a user may hold several rules for one repository; all of them count rc = load_text(&p, "bob team write refs/heads/dev\n" "bob team force refs/heads/feature/*\n"); CHECK_INT(rc, 0); expect_allow(&p, "bob", "team", LV_WRITE, "refs/heads/dev"); expect_allow(&p, "bob", "team", LV_FORCE, "refs/heads/feature/x"); // A refusal names the level held on the ref in question, not the strongest // level held somewhere in the repository: "you have force here" next to a // refusal is a sentence nobody can act on. 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", "but not on refs/heads/other"); policy_free(&p); // 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/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); } int main(void) { test_init(""); test_check(); test_scope_edges(); return test_report("check"); }