diff options
| author | dev <dev@example> | 2026-11-15 12:10:23 +0000 |
|---|---|---|
| committer | dev <dev@example> | 2026-11-15 12:10:23 +0000 |
| commit | 85b7fa7f8456c84f65a0d81b760c430098ae7fd8 (patch) | |
| tree | d941ae68672ff71d2181fcb1a0e168f9314f135c | |
| parent | 2e6dd4a8e9e5302efae6b7532f7f4609882fc21d (diff) | |
| download | access-control-85b7fa7f8456c84f65a0d81b760c430098ae7fd8.tar.gz access-control-85b7fa7f8456c84f65a0d81b760c430098ae7fd8.zip | |
Denials to the pusher: one line, and the reason moved to check
The person pushing used to get a sentence about a file they cannot read:
deny: bob holds write on team, but not on refs/heads/main; that ref needs write
It is true, and it is not theirs. The level they hold, the level the ref costs
and why are all facts about somebody else's configuration, and quoting them at a
terminal sends a colleague to a file that is not theirs to edit while telling a
monitor nothing the exit status did not. A refusal is now one line naming the
input it refused:
git-authz: permission denied [user=bob, obj=refs/heads/main]
git-authz: permission denied [obj=refs/heads/main] # no identity
and `check` is where whoever owns the policy reads the reason out, unchanged.
`authorize --explain` prints the same sentence in the hook's own seat, for the
operator who is already standing there.
The other two denials got the same treatment and a reclassification:
invalid policy file exit 3, for an unreadable policy and an unparsable one
alike - no path, no line number, no errno
invalid operation exit 3, was 1, for a stdin line that is not an update:
not three fields, not object ids, not a ref under refs/
Exit 3 now means "the host is broken, not this person" and covers both, which is
what the split was always for: no real git writes those lines, so a hook that
rejects one has a pipe, a wrapper or a debugging session behind it, not a pusher.
Nothing about enforcement changed - a non-zero pre-receive exit stops the push
either way - only which status it is and what the line says.
To keep the pusher's output to one line, policy_load writes its findings to
p->log, stderr by default and NULL for the hook and for authorize mode: the mode
is the audience, not the name it was reached by. check and validate still print
line numbers and errno, so nothing an operator needs has gone missing.
src/ and cmd/ are 1,488 non-blank lines. make strict, make test and
make sanitizer are clean - 128 assertions, up from 113, now pinning the exact
terse line, that a denial is one line, that no policy prose leaks to the pusher,
and that nothing of a rejected line is echoed back.
| -rw-r--r-- | README | 44 | ||||
| -rw-r--r-- | cmd/main.c | 49 | ||||
| -rw-r--r-- | src/authz.h | 22 | ||||
| -rw-r--r-- | src/hook.c | 42 | ||||
| -rw-r--r-- | src/policy.c | 17 | ||||
| -rwxr-xr-x | test/run.sh | 118 |
6 files changed, 219 insertions, 73 deletions
@@ -28,14 +28,17 @@ git invokes. `validate` asks whether the policy is one the hook can use. Exit status is the interface, and it does not change: 0 allowed - 1 denied - 3 no usable policy - the host is broken, not this person + 1 denied - this person may not + 3 the host is broken, not this person 64 usage error -Three is separate from one on purpose. A push refused because the policy file -was not mounted produces one line in a log somewhere; a push refused because the -user is not allowed produces a message to a colleague. They must not sound the -same, and a monitor must not have to parse prose to tell them apart. +Three is separate from one on purpose, and it covers two different breakages: a +policy the hook cannot use says `invalid policy file`, and a hook handed +something that is not a push - which no real git ever does - says `invalid +operation`. A push refused because the person is not allowed is theirs, and says +`permission denied` with the name they came as and the ref they were refused. +They must not sound the same, and a monitor must not have to parse prose to tell +them apart - which is also why each of them is one line. The policy @@ -167,25 +170,44 @@ Warnings do not stop it; errors do, and `--strict` makes warnings stop it too. Reading this as an operator --------------------------- +A refused push says one line, and it says only the input it refused: + + git-authz: permission denied [user=bob, obj=refs/heads/main] + +That is the whole of what the person pushing is told, on purpose: the reason is +a sentence about a file they cannot read, and quoting it at them sends them to +somebody else's configuration. The reason is here instead: + who may write to team? git-authz check bob team write refs/heads/feature/login - why was my push refused? + why was that push refused? - Because the message ends with what it would have taken: + git-authz check bob team write refs/heads/main deny: bob holds write on team, but not on refs/heads/main; that ref needs write or, when the ref is covered by a rule but costs more than it looks: + git-authz check bob team write refs/notes/x + deny: bob has write on team refs/notes/x; it needs admin (neither a branch nor a tag) - which is the whole design in one line: a refusal that names the level held, - the ref, the level required and why, so the fix is obvious to the person who - hit it instead of a ticket to whoever set this up. + which is the whole design in two lines: the refusal names who and what, the + answer names the level held, the ref, the level required and why, so the fix + is obvious to whoever owns the policy and the person who hit it is not left + guessing either. + +`authorize --explain` prints that sentence in the hook's own seat, for when the +operator is the one standing there. A policy the hook cannot use says `invalid +policy file` and nothing else at exit 3; `validate` is what says what is wrong +with it, with the line numbers. Exit 3 has one other cause: `invalid operation`, +for a hook handed a line that is not an update - three fields, two object ids and +a ref under `refs/`. No git writes that, so it is the pipe, the wrapper or +whoever was debugging, and never the person pushing. Layout @@ -1,7 +1,8 @@ // One program, because check and the hook have to agree exactly about what a // rule means: a check that answers differently from the hook is a program that -// lies. Exit status is the interface: 0 allowed, 1 denied, 3 no usable policy, -// 64 usage. +// lies. Exit status is the interface: 0 allowed, 1 denied, 3 the host is +// broken rather than this person (no usable policy, or input that was never a +// push), 64 usage. #include "authz.h" @@ -20,18 +21,21 @@ static void usage(FILE *f) "usage: git-authz [--policy PATH] <command> [arguments]\n" "\n" " check <user> <repo> <write|force|admin> [ref]\n" -" one question: allow or deny on stdout, the reason with it.\n" +" one question: allow or deny on stdout, the reason with it. This is\n" +" where the reason for a refusal is read out; the hook does not give it.\n" " Without a ref it asks about the repository as a whole.\n" "\n" " authorize read a push exactly as git's receive hook does - identity from\n" " GIT_HOST_USER (ssh) or REMOTE_USER (http), one \"old new ref\" line per\n" " ref on stdin - and deny the push on the first ref it may not allow.\n" " This also runs as 'pre-receive', which is how core.hooksPath finds it.\n" +" A denial is one line naming the input it refused, and nothing else:\n" +" the reason is what check is for.\n" " --user U --repo R override the identity and the repository\n" " --ff auto|yes|no whether to ask git if old is an ancestor of new\n" " --head auto|none|REF the default branch, or how to treat deletions\n" " --no-git ask git nothing (an unknown HEAD is not guessed)\n" -" --explain report the allows too\n" +" --explain print the reason, and the allows too\n" "\n" " validate is the policy parseable, and is it sane?\n" " --strict warnings are failures too\n" @@ -41,17 +45,20 @@ static void usage(FILE *f) "same branch; a bare name is always a branch and never a tag. A rule says what\n" "it says: nothing else in the file can raise the price of a ref.\n" "\n" -"Exit status: 0 allowed, 1 denied, 3 the policy could not be read or does not\n" -"parse - nothing is allowed on a policy that cannot be read - and 64 for usage.\n"); +"Exit status: 0 allowed, 1 denied, 64 for usage, and 3 when the host is broken\n" +"rather than this person: nothing is allowed on a policy that cannot be read\n" +"('invalid policy file'), and a hook handed something that is not a push says\n" +"'invalid operation' - a real git never sends that.\n"); } // 3 and not 1: a push refused because the host is broken has to sound different -// from one refused because the person is not allowed. +// from one refused because the person is not allowed. Every 3 is one line, and +// which of them it is is all a monitor has to tell; what is wrong with the policy +// is `validate`'s job, and it says so with line numbers. -static int deny_policy(const char *path, int rc) +static int deny_policy(void) { - fprintf(stderr, "%s: deny: the policy at %s %s\n", progname, path, - rc == -2 ? "cannot be read" : "does not parse"); + fprintf(stderr, "%s: invalid policy file\n", progname); return 3; } @@ -91,7 +98,7 @@ static int parse_validate(int argc, char **argv, validate_opts *vo) } static int dispatch(policy_t *p, int rc, const char *cmd, int argc, char **argv, - const char *path, hook_opts ho, validate_opts vo) + hook_opts ho, validate_opts vo) { // argv[0] is the command name, which is where getopt_long expects argv[0] // to be. A command's own positional arguments therefore start at argv[1]. @@ -107,7 +114,7 @@ static int dispatch(policy_t *p, int rc, const char *cmd, int argc, char **argv, if (strcmp(cmd, "authorize") == 0) { if (rc != 0) - return deny_policy(path, rc); + return deny_policy(); return cmd_authorize(p, ho, argc, argv); } @@ -131,7 +138,7 @@ static int dispatch(policy_t *p, int rc, const char *cmd, int argc, char **argv, } level = authz_level_of(argv[2]); if (rc != 0) - return deny_policy(path, rc); + return deny_policy(); authz_check(p, argv[0], argv[1], (level_t)level, argc > 3 ? argv[3] : NULL, &d); printf("%s: %s\n", d.allowed ? "allow" : "deny", d.why); return d.allowed ? 0 : 1; @@ -178,10 +185,16 @@ int main(int argc, char **argv) char *hook_argv[] = { (char *)"pre-receive", NULL }; policy_init(&p, path); + + // Reached by the name git calls, so everything it prints is read by the + // person pushing. The policy's own complaints - line number, errno - + // are for whoever owns the file, and are not in this output. + + p.log = NULL; rc = policy_load(&p); if (rc != 0) { policy_free(&p); - return deny_policy(path, rc); + return deny_policy(); } status = cmd_authorize(&p, ho, 1, hook_argv); policy_free(&p); @@ -231,8 +244,14 @@ int main(int argc, char **argv) } policy_init(&p, path); + + // authorize mode answers a person pushing, whoever invoked it: the mode is + // the audience, so the file's line numbers stay out of its output. + + if (strcmp(cmd, "authorize") == 0) + p.log = NULL; rc = policy_load(&p); - status = dispatch(&p, rc, cmd, argc, argv, path, ho, vo); + status = dispatch(&p, rc, cmd, argc, argv, ho, vo); policy_free(&p); return status; } diff --git a/src/authz.h b/src/authz.h index f9fbfa3..6665e03 100644 --- a/src/authz.h +++ b/src/authz.h @@ -1,14 +1,16 @@ // No regex, no fnmatch, no libgit2: a hook must not depend on anything that can // be missing at 3am. // -// The exit status is the interface: 0 allowed, 1 denied, 3 no usable policy, -// 64 usage. +// The exit status is the interface: 0 allowed, 1 denied (this person may not), +// 3 the host is broken and not this person (no usable policy, or input that was +// never a push), 64 usage. #ifndef AUTHZ_H #define AUTHZ_H #include <getopt.h> #include <stddef.h> +#include <stdio.h> #define MAX_FIELDS 64 // one rule line, split on whitespace #define AUTHZ_MSGSZ 512 // one complaint, in the words the caller prints @@ -38,12 +40,20 @@ typedef struct { int errors; int unreadable; // the file itself could not be opened char *path; // what messages call the file + FILE *log; // where the reading of the file complains: line + // numbers and errno, which an operator needs and a + // person pushing does not. NULL says nothing; the + // hook sets that, because its stderr is the + // pusher's terminal and `validate` has the detail. } policy_t; typedef struct { int allowed; level_t need; // what it cost in the end - char why[512]; // the sentence the person pushing reads + char why[512]; // the sentence about the policy: what was held, the + // ref, what it cost and why. `check` prints it, + // and so does the hook under --explain; the person + // pushing gets one terse line instead. } decision_t; // glob.c -------------------------------------------------------------------- @@ -59,7 +69,8 @@ int glob_match(const char *pattern, const char *s); void policy_init(policy_t *p, const char *path); void policy_free(policy_t *p); -// 0 usable, -1 errors, -2 unreadable. Errors print as they are found. +// 0 usable, -1 errors, -2 unreadable. Errors print as they are found, to +// p->log, which is stderr until a caller says otherwise. int policy_load(policy_t *p); @@ -123,7 +134,8 @@ typedef struct { const char *ff; // "auto" (ask git), "yes", "no" const char *head; // "auto" (ask git), "none", or a refname int use_git; // 0 never runs git, whatever 'ff'/'head' say - int explain; // print every verdict, not only the denials + int explain; // the sentence, and the allows too, not only the + // terse denial a pusher is given } hook_opts; int cmd_authorize(const policy_t *p, hook_opts o, int argc, char **argv); @@ -278,6 +278,33 @@ static int parse_options(int argc, char **argv, hook_opts *o) return 0; } +// The one line a person pushing is given: the input they presented, and the +// verdict. Nothing else. The reason is a sentence about a policy file they +// cannot read, and quoting it here sends them to somebody else's configuration +// while telling a monitor nothing the exit status did not. `check` is where +// whoever owns the policy reads the reason out, and --explain below prints it in +// this seat for the operator who is already standing here. + +static void deny_ref(const char *user, const char *ref) +{ + if (user != NULL && user[0] != '\0') + fprintf(stderr, "%s: permission denied [user=%s, obj=%s]\n", + progname, user, ref); + else + fprintf(stderr, "%s: permission denied [obj=%s]\n", progname, ref); +} + +// Input that is not a push at all: not three fields, not object ids, not a ref +// under refs/. None of it is echoed - the person on the other end can do +// nothing with a line they did not write, and exit 3 already says whose problem +// it is. A real git never writes these lines, so what to look at is the pipe, +// the wrapper, or whoever was debugging the hook by hand. + +static void invalid_operation(void) +{ + fprintf(stderr, "%s: invalid operation\n", progname); +} + int cmd_authorize(const policy_t *p, hook_opts o, int argc, char **argv) { char *line = NULL; @@ -328,9 +355,8 @@ int cmd_authorize(const policy_t *p, hook_opts o, int argc, char **argv) if (nf == 0) continue; if (nf != 3) { - fprintf(stderr, "%s: deny: expected \"old new ref\", got %d field(s)\n", - progname, nf); - status = 1; + invalid_operation(); + status = 3; // the host's fault, not the pusher's break; } @@ -341,9 +367,8 @@ int cmd_authorize(const policy_t *p, hook_opts o, int argc, char **argv) if (!object_id_ok(old) || !object_id_ok(new) || strncmp(ref, "refs/", 5) != 0) { - fprintf(stderr, "%s: deny: \"%s %s %s\" is not an update git would send\n", - progname, old, new, ref); - status = 1; + invalid_operation(); + status = 3; break; } @@ -362,7 +387,10 @@ int cmd_authorize(const policy_t *p, hook_opts o, int argc, char **argv) authz_check(p, user, repo, need, ref, &d); if (!d.allowed) { - fprintf(stderr, "%s: deny: %s\n", progname, d.why); + if (o.explain) + fprintf(stderr, "%s: deny: %s\n", progname, d.why); + else + deny_ref(user, ref); status = 1; break; } diff --git a/src/policy.c b/src/policy.c index b2d4348..db00c78 100644 --- a/src/policy.c +++ b/src/policy.c @@ -86,13 +86,18 @@ static void perr(policy_t *p, int line, const char *fmt, ...) { va_list ap; + // A person pushing has no use for a line number in a file they cannot read, + // so the hook loads its policy with the log switched off. + p->errors++; - fprintf(stderr, "%s:%d: error: ", + if (p->log == NULL) + return; + fprintf(p->log, "%s:%d: error: ", p->path != NULL ? p->path : "authz.conf", line); va_start(ap, fmt); - vfprintf(stderr, fmt, ap); + vfprintf(p->log, fmt, ap); va_end(ap); - fputc('\n', stderr); + fputc('\n', p->log); } int authz_user_pattern_ok(const char *s) @@ -290,6 +295,7 @@ void policy_init(policy_t *p, const char *path) { memset(p, 0, sizeof(*p)); p->path = xstrdup(path); + p->log = stderr; // whoever reads the file hears what is wrong } void policy_free(policy_t *p) @@ -324,8 +330,9 @@ int policy_load(policy_t *p) rc = authz_read_lines(p->path, &lines); if (rc != 0) { p->unreadable = 1; - fprintf(stderr, "%s: cannot read the policy: %s\n", p->path, - strerror(errno)); + if (p->log != NULL) + fprintf(p->log, "%s: cannot read the policy: %s\n", p->path, + strerror(errno)); lines_free(&lines); return -2; } diff --git a/test/run.sh b/test/run.sh index d3268b0..674c475 100755 --- a/test/run.sh +++ b/test/run.sh @@ -275,10 +275,12 @@ expect "creating a branch is write" 0 "allow" "$rc" "$out" out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/notes/x | push bob team); rc=$? -expect "a ref that is neither a branch nor a tag is for admins" 1 "needs admin" "$rc" "$out" +expect "a ref that is neither a branch nor a tag is for admins" 1 \ + "permission denied [user=bob, obj=refs/notes/x]" "$rc" "$out" out=$(POLICY="$TMP/plain.conf" printf '%s %s %s\n' "$ZERO" "$A" refs/heads/release | POLICY="$TMP/plain.conf" push bob team); rc=$? -expect "a branch outside the pattern is denied" 1 "deny" "$rc" "$out" +expect "a branch outside the pattern is denied" 1 \ + "permission denied [user=bob, obj=refs/heads/release]" "$rc" "$out" out=$(printf '%s %s %s\n' "$A" "$B" refs/heads/feature/x | push bob team --ff=yes --explain); rc=$? @@ -286,15 +288,18 @@ expect "a fast-forward is write" 0 "allow" "$rc" "$out" out=$(printf '%s %s %s\n' "$A" "$B" refs/heads/feature/x | push bob team --ff=no); rc=$? -expect "a rewrite is force" 1 "needs force" "$rc" "$out" +expect "a rewrite is force" 1 \ + "permission denied [user=bob, obj=refs/heads/feature/x]" "$rc" "$out" out=$(printf '%s %s %s\n' "$A" "$B" refs/heads/feature/x | push carol other --ff=no); rc=$? -expect "a rewrite in a repository she only reaches through @all" 1 "needs force" "$rc" "$out" +expect "a rewrite in a repository she only reaches through @all" 1 \ + "permission denied [user=carol, obj=refs/heads/feature/x]" "$rc" "$out" out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/feature/x | push bob team); rc=$? -expect "deleting is force" 1 "needs force" "$rc" "$out" +expect "deleting is force" 1 \ + "permission denied [user=bob, obj=refs/heads/feature/x]" "$rc" "$out" out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/feature/x | push carol team); rc=$? @@ -302,7 +307,8 @@ expect "carol may delete her own kind of branch" 0 "-" "$rc" "$out" out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/dev | push carol team --head=refs/heads/dev); rc=$? -expect "deleting the default branch is admin" 1 "needs admin" "$rc" "$out" +expect "deleting the default branch is admin" 1 \ + "permission denied [user=carol, obj=refs/heads/dev]" "$rc" "$out" out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/feature/x | push carol team --head=refs/heads/other); rc=$? @@ -310,7 +316,7 @@ expect "deleting a branch she holds force on is allowed" 0 "-" "$rc" "$out" out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/dev | push carol team --head=refs/heads/other); rc=$? expect "and only force on that ref will do, not force somewhere else" 1 \ - "carol holds force on team, but not on refs/heads/dev" "$rc" "$out" + "permission denied [user=carol, obj=refs/heads/dev]" "$rc" "$out" out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/dev | push dave team --head=refs/heads/dev); rc=$? @@ -319,11 +325,13 @@ expect "an admin deletes the default branch" 0 "-" "$rc" "$out" out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/dev | env PATH=/nonexistent GIT_HOST_USER=carol GIT_HOST_REPO=team \ "$AUTHZ" --policy "$P" authorize 2>&1); rc=$? -expect "a HEAD that cannot be read is charged as the default branch" 1 "needs admin" "$rc" "$out" +expect "a HEAD that cannot be read is charged as the default branch" 1 \ + "permission denied [user=carol, obj=refs/heads/dev]" "$rc" "$out" out=$(printf '%s %s %s\n' "$A" "$B" refs/heads/feature/x | push bob team --ff=auto); rc=$? -expect "with no git to ask, an update is a rewrite" 1 "needs force" "$rc" "$out" +expect "with no git to ask, an update is a rewrite" 1 \ + "permission denied [user=bob, obj=refs/heads/feature/x]" "$rc" "$out" out=$(printf '%s %s %s\n' "$A" "$B" refs/pull/7/head | push dave team); rc=$? @@ -332,7 +340,8 @@ expect "an admin writes a fetch ref" 0 "-" "$rc" "$out" section "authorize: identity" out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x | push none team); rc=$? -expect "anonymous is denied" 1 "no identity" "$rc" "$out" +expect "anonymous is denied" 1 \ + "permission denied [obj=refs/heads/feature/x]" "$rc" "$out" out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x | env -u GIT_HOST_USER REMOTE_USER=bob GIT_HOST_REPO=team \ @@ -371,23 +380,59 @@ out=$(printf '%s %s %s\n%s %s %s\n' \ "$ZERO" "$A" refs/heads/main \ "$ZERO" "$B" refs/heads/feature/one | push bob team); rc=$? -expect "the refusal says who, what and what it would take" 1 \ - "bob holds write on team, but not on refs/heads/main" "$rc" "$out" +expect "the refusal names the input and the verdict, and nothing else" 1 \ + "permission denied [user=bob, obj=refs/heads/main]" "$rc" "$out" +expect_not "and does not quote the policy at the person pushing" 1 "write" "$rc" "$out" expect_not "and stops at the first refusal" 1 "feature/one" "$rc" "$out" section "authorize: input that is not a push" +# Input that is not a push is not a refusal of a person either: no git writes +# these lines, so it is exit 3 with the host, not exit 1 with the pusher. And +# none of the line comes back: whoever broke it is reading a log, not a terminal. + out=$(printf 'nonsense\n' | push bob team); rc=$? -expect "a line with too few fields" 1 "expected" "$rc" "$out" +expect "a line with too few fields" 3 "invalid operation" "$rc" "$out" +expect_not "and it is not dressed up as a permission problem" 3 "permission" "$rc" "$out" out=$(printf '%s %s %s\n' "$A" "$B" heads/feature/x | push bob team); rc=$? -expect "a refname that is not a refname" 1 "not an update git would send" "$rc" "$out" +expect "a refname that is not a refname" 3 "invalid operation" "$rc" "$out" +expect_not "and none of the line is echoed back" 3 "$A" "$rc" "$out" out=$(printf '%s %s %s\n' notanoid "$B" refs/heads/x | push bob team); rc=$? -expect "an object id that is not one" 1 "not an update git would send" "$rc" "$out" +expect "an object id that is not one" 3 "invalid operation" "$rc" "$out" +expect_not "and the bad id is not echoed either" 3 "notanoid" "$rc" "$out" out=$(printf '' | push bob team); rc=$? expect "no refs at all is nothing to deny" 0 "-" "$rc" "$out" out=$(printf '\n\n' | push bob team); rc=$? expect "blank lines are skipped" 0 "-" "$rc" "$out" +# --------------------------------------------------------- what a refusal says +# +# One line: the input presented and the verdict. The reason is a sentence about +# a file the person pushing cannot read, and `check` is where whoever can read it +# says what it would have taken; --explain is the same sentence in the hook's own +# seat, for whoever is standing there. + +section "what a refusal says to the person who hit it" + +out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/main | push bob team); rc=$? +expect "one line, named by the program that refused" 1 \ + "git-authz: permission denied [user=bob, obj=refs/heads/main]" "$rc" "$out" +if [ "$(printf '%s\n' "$out" | wc -l)" != 1 ]; then + bad "a denial is one line, and this was: $out" +else + ok +fi +expect_not "with no advice about levels held or needed" 1 "needs" "$rc" "$out" + +out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/main | push bob team --explain); rc=$? +expect "--explain is where the reason is asked for" 1 \ + "bob holds write on team, but not on refs/heads/main" "$rc" "$out" + +out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/main | push none team); rc=$? +expect "no identity means no user is named" 1 \ + "permission denied [obj=refs/heads/main]" "$rc" "$out" +expect_not "not even an empty one" 1 "user=" "$rc" "$out" + # ------------------------------------------------------------ option forms # The usage text writes these as two words ("--user U", "--ff auto|yes|no"), and @@ -402,7 +447,8 @@ expect "--user and --repo as two words" 0 "-" "$rc" "$out" out=$(printf '%s %s %s\n' "$A" "$B" refs/heads/feature/x | "$AUTHZ" --policy "$P" authorize --user bob --repo team --ff no 2>&1); rc=$? -expect "--ff no as two words costs force" 1 "needs force" "$rc" "$out" +expect "--ff no as two words costs force" 1 \ + "permission denied [user=bob, obj=refs/heads/feature/x]" "$rc" "$out" out=$(printf '%s %s %s\n' "$B" "$ZERO" refs/heads/main | "$AUTHZ" --policy "$P" authorize --user bob --repo team --head none 2>&1); rc=$? @@ -427,15 +473,18 @@ expect "the hook name selects authorize, and git's argument is ignored" 0 "-" "$ out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/release | POLICY="$TMP/plain.conf" hook bob "$TMP/root/team.git"); rc=$? expect "and denies what authorize denies, on the repository it found" 1 \ - "bob holds write on team, but not on refs/heads/release" "$rc" "$out" + "permission denied [user=bob, obj=refs/heads/release]" "$rc" "$out" out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x | POLICY="$TMP/plain.conf" hook none "$TMP/root/team.git"); rc=$? -expect "an anonymous key is denied at the hook" 1 "no identity" "$rc" "$out" +expect "an anonymous key is denied at the hook" 1 \ + "permission denied [obj=refs/heads/feature/x]" "$rc" "$out" out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x | POLICY="$TMP/broken.conf" hook bob "$TMP/root/team.git"); rc=$? -expect "a hook on a policy that does not parse stops the push" 3 "deny" "$rc" "$out" +expect "a hook on a policy that does not parse stops the push" 3 \ + "invalid policy file" "$rc" "$out" +expect_not "and says nothing else to the person pushing" 3 "error" "$rc" "$out" # the repository, taken from the working directory under GIT_REPO_ROOT out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x | @@ -485,7 +534,8 @@ expect "a deletion is force, not admin, when HEAD is somewhere else" 0 "-" "$rc" out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/main | hookgit carol "$TMP/root/team.git" PATH="$TMP/fakegit" FAKE_HEAD=refs/heads/main FAKE_FF=1); rc=$? -expect "but the branch HEAD points at may not go" 1 "needs admin" "$rc" "$out" +expect "but the branch HEAD points at may not go" 1 \ + "permission denied [user=carol, obj=refs/heads/main]" "$rc" "$out" out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/main | hookgit dave "$TMP/root/team.git" PATH="$TMP/fakegit" FAKE_HEAD=refs/heads/main FAKE_FF=1); rc=$? @@ -493,7 +543,8 @@ expect "which is what an admin is for" 0 "-" "$rc" "$out" out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/feature/x | hookgit carol "$TMP/root/team.git" PATH="$TMP/fakegit" FAKE_HEAD=refs/heads/feature/x FAKE_FF=1); rc=$? -expect "and it is HEAD that decides, not the name of the branch" 1 "needs admin" "$rc" "$out" +expect "and it is HEAD that decides, not the name of the branch" 1 \ + "permission denied [user=carol, obj=refs/heads/feature/x]" "$rc" "$out" out=$(printf '%s %s %s\n' "$A" "$B" refs/heads/feature/x | hookgit bob "$TMP/root/team.git" PATH="$TMP/fakegit" FAKE_HEAD=refs/heads/main FAKE_FF=0); rc=$? @@ -501,17 +552,20 @@ expect "an update git calls a fast-forward costs write" 0 "-" "$rc" "$out" out=$(printf '%s %s %s\n' "$A" "$B" refs/heads/feature/x | hookgit bob "$TMP/root/team.git" PATH="$TMP/fakegit" FAKE_HEAD=refs/heads/main FAKE_FF=1); rc=$? -expect "and one it does not, force" 1 "needs force" "$rc" "$out" +expect "and one it does not, force" 1 \ + "permission denied [user=bob, obj=refs/heads/feature/x]" "$rc" "$out" # No git to ask: the hook must not guess in the caller's favour. out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/feature/x | hookgit carol "$TMP/root/team.git" PATH="$TMP/empty"); rc=$? -expect "with no git to ask, a deletion is the expensive kind" 1 "needs admin" "$rc" "$out" +expect "with no git to ask, a deletion is the expensive kind" 1 \ + "permission denied [user=carol, obj=refs/heads/feature/x]" "$rc" "$out" out=$(printf '%s %s %s\n' "$A" "$B" refs/heads/feature/x | hookgit bob "$TMP/root/team.git" PATH="$TMP/empty"); rc=$? -expect "and an update cannot be shown to be a fast-forward" 1 "needs force" "$rc" "$out" +expect "and an update cannot be shown to be a fast-forward" 1 \ + "permission denied [user=bob, obj=refs/heads/feature/x]" "$rc" "$out" # -------------------------------------------------------------- fail closed @@ -519,17 +573,21 @@ section "fail closed" printf 'alice web admin\nbroken line\n' >"$TMP/broken.conf" out=$("$AUTHZ" --policy "$TMP/broken.conf" check alice web admin refs/heads/x 2>&1); rc=$? -expect "a policy that does not parse denies" 3 "does not parse" "$rc" "$out" +expect "a policy that does not parse denies" 3 "invalid policy file" "$rc" "$out" +expect "and says which line, to whoever can read it" 3 ":2: error" "$rc" "$out" out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/x | env GIT_HOST_USER=alice GIT_HOST_REPO=web "$AUTHZ" --policy "$TMP/broken.conf" \ authorize --no-git 2>&1); rc=$? -expect "and the hook denies" 3 "deny" "$rc" "$out" +expect "and the hook denies" 3 "invalid policy file" "$rc" "$out" +expect_not "in that one line and nothing more" 3 "error" "$rc" "$out" out=$("$AUTHZ" --policy "$TMP/no-such.conf" check alice web admin 2>&1); rc=$? -expect "a policy that is not there denies" 3 "cannot be read" "$rc" "$out" +expect "a policy that is not there denies" 3 "invalid policy file" "$rc" "$out" +expect "and says that too, to whoever can read it" 3 "cannot read the policy" "$rc" "$out" out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/x | env GIT_HOST_USER=alice GIT_HOST_REPO=web "$AUTHZ" --policy "$TMP/no-such.conf" \ authorize 2>&1); rc=$? -expect "and the hook denies" 3 "deny" "$rc" "$out" +expect "and the hook denies" 3 "invalid policy file" "$rc" "$out" +expect_not "without saying why, which is not the pusher's to know" 3 "No such file" "$rc" "$out" printf 'alice web admin\n' >"$TMP/secret.conf" chmod 000 "$TMP/secret.conf" @@ -537,7 +595,7 @@ if [ "$(id -u)" = 0 ]; then skip_it "an unreadable policy denies" "running as root, who can read anything" else out=$("$AUTHZ" --policy "$TMP/secret.conf" check alice web admin 2>&1); rc=$? - expect "an unreadable policy denies" 3 "cannot be read" "$rc" "$out" + expect "an unreadable policy denies" 3 "invalid policy file" "$rc" "$out" fi chmod 644 "$TMP/secret.conf" @@ -602,7 +660,7 @@ else ( cd "$S/var/lib/git/team.git" && env GIT_HOST_USER=bob \ GIT_REPO_ROOT="$S/var/lib/git" GIT_HOST_AUTHZ="$S/etc/git-host/authz.conf" \ "$S/usr/local/share/git-host/hooks/pre-receive" "." 2>&1 )); rc=$? - expect "and denies what it denies" 1 "deny" "$rc" "$out" + expect "and denies what it denies" 1 "permission denied" "$rc" "$out" contains "$out" "pre-receive:" || bad "the hook should say so in its own name: $out" printf 'someone * admin\n' >"$S/etc/git-host/authz.conf" |
