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 | |
| 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.
| -rw-r--r-- | src/authz.h | 11 | ||||
| -rw-r--r-- | src/policy.c | 56 | ||||
| -rw-r--r-- | src/validate.c | 23 | ||||
| -rw-r--r-- | tests/unit.c | 9 |
4 files changed, 79 insertions, 20 deletions
diff --git a/src/authz.h b/src/authz.h index 9ca0eb6..53f251d 100644 --- a/src/authz.h +++ b/src/authz.h @@ -56,6 +56,7 @@ typedef struct { int errors; int warnings; + int unreadable; /* the file itself could not be opened */ char *path; /* what messages call the file */ } policy_t; @@ -178,10 +179,16 @@ typedef struct { const char *out; /* NULL edits in place; "-" is stdout */ } edit_opts; -/* Shared by the three editing commands: leading --out/--stdout flags, and how - * many arguments they consumed. Returns -1 on an unknown option. */ +/* Shared by the three 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. */ int authz_parse_out(int argc, char **argv, edit_opts *o, int *consumed); +/* A ref pattern or name that starts with "--" is a mistyped option, not + * something to grant. Prints and returns 1 when it finds one. */ +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); 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); diff --git a/src/validate.c b/src/validate.c index fd8abb9..ab8348c 100644 --- a/src/validate.c +++ b/src/validate.c @@ -303,22 +303,28 @@ static void check_account(const policy_t *p, validate_opts o) return; } while ((len = getline(&line, &cap, f)) != -1) { - char *fields[9]; + char *fields[8]; int nf = 0; char *q = line; + /* name:password:uid:gid:gecos:home:shell - the seventh field decides + * whether a key can open a shell at all. */ while (*q != '\0') { + char *start = q; + while (*q != '\0' && *q != ':') q++; - fields[nf++] = q; - if (*q == '\0') + if (*q == '\0') { + fields[nf++] = start; break; + } *q++ = '\0'; - if (nf == 8) + fields[nf++] = start; + if (nf == 7) break; } - if (nf == 8 && strcmp(fields[0], "git") == 0) { - char *shell = strip(fields[7]); + if (nf >= 7 && strcmp(fields[0], "git") == 0) { + char *shell = strip(fields[6]); found = 1; if (strstr(shell, "git-shell") == NULL) @@ -440,6 +446,11 @@ int cmd_validate(const policy_t *p, validate_opts o) /* Anything the parser already complained about went to stderr with its line * number. A policy with errors is not used at all, so nothing else here * matters until it is fixed. */ + if (p->unreadable) { + verr("%s cannot be read: the hook fails closed on this, so every push is " + "denied until it is mounted", p->path); + return 3; + } if (p->errors > 0) { verr("%s: %d error(s): the policy is not used, and every push is denied", p->path, p->errors); diff --git a/tests/unit.c b/tests/unit.c index 5932d46..0d21ee3 100644 --- a/tests/unit.c +++ b/tests/unit.c @@ -490,8 +490,13 @@ static void test_scope_edges(void) 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"); - expect_deny(&p, "bob", "team", LV_FORCE, "refs/heads/dev", "has force on team"); - expect_deny(&p, "bob", "team", LV_WRITE, "refs/heads/other", "needs write"); + /* 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", + "nothing at all on refs/heads/other"); policy_free(&p); /* per-repository protection replaces the global list, it does not add to it */ |
