summaryrefslogtreecommitdiffstats
path: root/src/hook.c
Commit message (Collapse)AuthorAgeFilesLines
* Options: getopt_long instead of three hand-rolled parsersdev2026-11-141-53/+63
| | | | | | | | | | | | | | | | | | | | | option() in hook.c and one_option() in cmd/main.c were the same function under two names, and validate's seven options were a chain of seven calls to one of them. getopt_long does all of it - both --opt value and --opt=value, the lot - so the parsers are gone and what is left is a table and what each entry sets. Two small helpers carry what getopt_long cannot: the name behind a long option's val, for the 'wants a value' message, and the optind reset that lets a run parse options twice on two different argv slices. A command now gets argv[0] as its own name, which is where getopt_long expects argv[0]; the commands that take names step over it. strip() in validate.c was authz_trim() again, and cmd_authorize freed the same three pointers on four exit paths. One behaviour change: 'authorize --' is now the end-of-options marker rather than an unknown argument. Anything after it is still refused.
* Nine bugs the suite did not reach, and a test for eachdev2026-11-141-12/+30
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Every one was found by reading a module against what the program promises, or by driving the built binary with input the suite never sends. None of them was visible from a green run: the run was already green. Two are validate saying "ok" about a machine that is wide open: ForceCommand none is how a Match block turns a forced command back off. It is the same state as having none at all, which is the state this check exists to catch. sshd directives sshd keeps the first value it reads for a keyword, not the last. "PermitUserEnvironment no" with a later "yes" read as yes, and every push arrives anonymous. One is a grant that grants more than was typed. A ref argument went into the file verbatim, so grant x team write 'refs/heads/a b' landed as "x team write refs/heads/a b", which reads back as two patterns and quietly grants refs/heads/b as well. A '#' argument was cut short at the comment marker, and a newline argument wrote a file that no longer parses - which denies every push there is. An argument that is not one field is refused now. authorize did not accept the options its own usage text prints. The help says "--user U" and "--ff auto|yes|no"; the parser took only --user=U, so the documented spelling came back "unknown argument" while validate accepted both forms. Both work now, and an option with no value says so. Three are the boundary and status distinctions the exit codes are made of. A rule with exactly MAX_FIELDS fields was refused as "too many", and a file that does not parse denies every push. A directory passed as the policy read as a policy that happens to be empty, so every push answered 1, person not allowed, where the host is broken and the answer has to be 3. A gitconfig section name was matched case-sensitively, so [CORE] reported a broken host that is not broken; git folds section names without regard to case. Two are smaller. "appended as line N" was printed when the pair's rule was the last line and had been replaced rather than appended. And the Makefile tracked no header dependencies, so editing src/authz.h rebuilt nothing and make test passed against a binary that was not the code on disk. Each fix went in as a failing test first, so the suite goes from 162 cases to 176 and no existing test was changed. Clean under -Werror and clean under ASan/UBSan.
* Comments: cut to what the code cannot saydev2026-11-141-41/+32
| | | | | | | | | | | | | | | | | | | | | | | | The comments had grown into essays. A file header explaining why the file exists, then a paragraph above each function restating what the next eight lines do, then a paragraph above that one explaining why that matters. The reader gets no further than the first paragraph, and the second is the one that goes stale: it describes code that has since changed, and nothing fails when it does. Cut by about half. What is left is the three things a comment can do that code cannot: say why the constraint exists (no regex because a hook must not depend on what might be missing at 3am), say what is not obvious from the shape (a bare ref name is a branch, never a tag; the seventh field of /etc/passwd is the shell), and say where it hurts (the NULL in an execvp array, the mode and owner that have to survive an atomic rewrite, the child that must not keep the hook's stdin). Block comments became // lines, which is also what the other C trees here use, and the file headers went to two or three lines. Section banners stayed: they are navigation, not prose. Behaviour is unchanged and the suites prove it: 171 direct checks, 162 behavioural, clean under -Werror and under ASan/UBSan.
* The two git calls were missing their NULL, so every push cost one level too muchdev2026-11-141-9/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | execvp walks argv until it finds a NULL. head_ref built a four-element array and fast_forward a five-element one, filled every slot, and never wrote the terminator - so the fifth and sixth entries were whatever the stack happened to be holding, and the exec either failed or ran something that was not the argument list anybody meant. Both answers were load-bearing, and both failures were silent: symbolic-ref fails the hook cannot tell which branch HEAD is, so it charges every deletion the most: admin merge-base fails the update cannot be shown to be a fast-forward, so it costs force Together that meant a user with write could create a branch and then never update it, and nobody but an admin could delete anything - with the refusal pointing at the policy, which was fine, and at a level the user did not hold, which looked deliberate. It was not. It was the shape of a C array. Fixed by writing the arrays out with their NULL visible, and commented, because the compiler will not catch this one: the array is big enough, the types are right, and the bug is in the element that is not there. The core suite could not see this, and that was the real problem - it passes --ff and --head precisely so that it needs no git, which leaves the asking itself untested. So there is now a stand-in named git in the suite, and the real one kept out of PATH: it answers symbolic-ref to order and exits merge-base either way, which covers fast-forward and non-fast-forward, HEAD's branch and not, and the case with no git at all, where the hook must fail toward denying rather than toward guessing. Eight checks, all of which failed before the fix. The container suite caught this by being the only suite that runs the real thing, which is worth noting for the next time someone suggests the integration suite is the slow one that can be skipped.
* git-authz: one C program for who may move which refsdev2026-11-131-0/+381
A git host that anonymous-clones and shared-pushes has one question left to answer: which human may move which ref. This answers it from one plain file, in one binary, with no runtime beyond libc. Four modes, because they must agree exactly about what a rule means - a check that answers differently from the hook is a program that lies: check one question, for an operator or a script authorize a whole push, the way core.hooksPath hands it over: identity from GIT_HOST_USER (ssh key option) or REMOTE_USER (http auth), and one "old new ref" line per ref on stdin. Also runs as "pre-receive", which is how git finds it. validate does the policy parse, and is the host wired to run it edit grant, revoke, protect (the deployed policy is a read-only mount on purpose, so these are for the source copy and say so when told) The verdict is the exit status, since the caller is a hook: 0 allowed, 1 denied, 3 no usable policy, 64 usage. A policy that cannot be read or does not parse denies everything rather than guessing, and one bad line poisons the whole file on purpose: a mistyped rule that quietly widens access is worse than an evening of refused pushes. Semantics kept from the shell engine this replaces: levels are monotone (admin implies force implies write), rules add up rather than order, "@" is only everyone the policy already names, protected refs cost one step more than the action asked for, and refs that are neither branches nor tags are for admins. "main", "heads/main" and "refs/heads/main" are one branch; a bare name is never a tag. Two facts a push does not carry - is old an ancestor of new, and which branch HEAD points at - are asked of git, and an answer that cannot be had is taken the expensive way. tests/unit.c covers the matcher directly (173 checks); the behavioural suite that drives this the way git does follows next.