From 85b7fa7f8456c84f65a0d81b760c430098ae7fd8 Mon Sep 17 00:00:00 2001 From: dev Date: Sun, 15 Nov 2026 12:10:23 +0000 Subject: 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. --- cmd/main.c | 49 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 15 deletions(-) (limited to 'cmd') diff --git a/cmd/main.c b/cmd/main.c index cb71a4f..852f131 100644 --- a/cmd/main.c +++ b/cmd/main.c @@ -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] [arguments]\n" "\n" " check [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; } -- cgit v1.2.3