diff options
| author | dev <dev@example> | 2026-11-13 20:33:51 +0000 |
|---|---|---|
| committer | dev <dev@example> | 2026-11-13 20:34:17 +0000 |
| commit | 1fd64267f678b8c6d17b2bcae702d577df1fc9e4 (patch) | |
| tree | 01a7d8a235c8e78c80c511dbee8cf738f7646053 /src/policy.c | |
| parent | f87a792743227ea06cfa1580f8d30a618b4d1a04 (diff) | |
| download | access-control-1fd64267f678b8c6d17b2bcae702d577df1fc9e4.tar.gz access-control-1fd64267f678b8c6d17b2bcae702d577df1fc9e4.zip | |
A refusal names the level held on the ref in question
"carol has force on team; refs/heads/main needs force", said while refusing
carol a force push to main, is a sentence nobody can act on: her force is on
refs/heads/feature/*, and the message reported the strongest level she holds
somewhere in the repository rather than the one she holds on the ref that was
refused. scope() now records both, and the refusal uses the level that covers
the ref, with a separate sentence for the case where nothing covers it at all -
"a rule for team but nothing on this ref" is a different problem from "not
enough for this ref", and the person pushing cannot tell them apart without
reading the file the way the matcher does.
Also: a policy that could not be opened is remembered (unreadable), so
validate reports "every push is denied until it is mounted" instead of
announcing that an empty policy is in force, and /etc/passwd is split by its
seven fields rather than eight - the old loop marked the colons as the fields
and so never found the git account at all, which meant a login shell for git
passed validation.
Both were found by driving the binary the way a receive hook is driven; that
suite is in a later commit of this series.
Diffstat (limited to 'src/policy.c')
| -rw-r--r-- | src/policy.c | 56 |
1 files changed, 46 insertions, 10 deletions
diff --git a/src/policy.c b/src/policy.c index ffce34d..1866bb2 100644 --- a/src/policy.c +++ b/src/policy.c @@ -385,6 +385,7 @@ int policy_load(policy_t *p) f = fopen(p->path, "r"); if (f == NULL) { + p->unreadable = 1; fprintf(stderr, "%s: cannot read the policy: ", p->path); perror(""); return -2; @@ -535,6 +536,7 @@ typedef struct { 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 */ + level_t here; /* the best held on this ref specifically */ int any; /* any rule at all, at any level */ } scope_t; @@ -548,6 +550,7 @@ static void scope(const policy_t *p, const char *user, const char *repo, s->scoped = 0; s->hit = 0; s->strongest = LV_NONE; + s->here = LV_NONE; s->any = 0; for (i = 0; i < p->nrules; i++) { @@ -558,6 +561,26 @@ static void scope(const policy_t *p, const char *user, const char *repo, s->any = 1; if (p->rules[i].level > s->strongest) s->strongest = p->rules[i].level; + + /* What this rule says about the ref in question, whatever its level: a + * refusal has to name the level the user actually holds on this ref, + * because "carol has force on team" while refusing her a force push to + * main is a sentence that sends somebody to open the file and stare. */ + if (ref != NULL) { + level_t covers = LV_NONE; + + if (p->rules[i].nrefs == 0) + covers = p->rules[i].level; + for (j = 0; j < p->rules[i].nrefs; j++) { + if (glob_match(p->rules[i].refs[j], ref)) { + covers = p->rules[i].level; + break; + } + } + if (covers > s->here) + s->here = covers; + } + if (p->rules[i].level < min) continue; if (p->rules[i].nrefs == 0) { @@ -593,17 +616,23 @@ void authz_check(const policy_t *p, const char *user, const char *repo, } if (ref == NULL) { - /* No ref named: "may this user do this somewhere in the repository?" */ + /* No ref named: "may this user do this somewhere in the repository?" + * This is the question 'can this person push anything at all here', and + * it is answered by whether any rule reaches the repository, not by + * which refs a scoped rule would cover. */ + char *r = authz_norm_repo(repo); + 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)); + s.unscoped ? "all" : "some refs", r); 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)); + user, authz_level_name(need), r); } + free(r); return; } @@ -635,16 +664,23 @@ void authz_check(const policy_t *p, const char *user, const char *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') + user, r, nref, authz_level_name(want)); + else if (s.here == LV_NONE) snprintf(out->why, sizeof(out->why), - "%s has %s on %s; %s needs %s (raised from %s: %s is %s)", + "%s has %s on %s, and nothing at all on %s; it needs %s%s%s%s", user, authz_level_name(s.strongest), r, nref, - authz_level_name(want), authz_level_name(need), nref, raised); + authz_level_name(want), + raised[0] != '\0' ? " (raised: " : "", + 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)", + user, authz_level_name(s.here), r, nref, + authz_level_name(want), authz_level_name(need), 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)); + 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)); free(r); } free(nref); |
