From a2a702cb81eaf73a8989cc31c682592c8acb769a Mon Sep 17 00:00:00 2001 From: dev Date: Sat, 14 Nov 2026 11:16:29 +0000 Subject: Nine bugs the suite did not reach, and a test for each 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. --- test/parse.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++ test/run.sh | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 151 insertions(+) (limited to 'test') diff --git a/test/parse.c b/test/parse.c index 5c8cb5f..49fdb55 100644 --- a/test/parse.c +++ b/test/parse.c @@ -2,6 +2,9 @@ // poisons the whole file. #include "test.h" +#include +#include + static void test_parse(void) { policy_t p; @@ -117,12 +120,80 @@ static void test_known_users(void) policy_free(&p); } +// A field count of exactly MAX_FIELDS is not "too many": the array has room for +// it. Only the 65th field on a line is the one that does not fit. +static void test_split_field_limit(void) +{ + char line[MAX_FIELDS * 3]; + char *f[MAX_FIELDS]; + int i; + int n; + + line[0] = '\0'; + for (i = 0; i < MAX_FIELDS; i++) + strcat(line, i ? " x" : "x"); + n = authz_split_ws(line, f, MAX_FIELDS); + CHECK_INT(n, MAX_FIELDS); + + // splitting writes over the text, so the line is built again + line[0] = '\0'; + for (i = 0; i <= MAX_FIELDS; i++) + strcat(line, i ? " x" : "x"); + n = authz_split_ws(line, f, MAX_FIELDS); + CHECK(n > MAX_FIELDS); // one field too many, said so +} + +// A rule line with exactly MAX_FIELDS fields is a rule like any other: 61 ref +// patterns on a user and repository. It used to be refused as "too many". +static void test_rule_at_the_field_limit(void) +{ + policy_t p; + char text[MAX_FIELDS * 20]; + int i; + int rc; + + strcpy(text, "bob team write"); + for (i = 0; i < MAX_FIELDS - 3; i++) + strcat(text, " refs/heads/r"); + strcat(text, "\n"); + + rc = load_text(&p, text); + CHECK_INT(rc, 0); + CHECK_INT(p.nrules, 1); + if (p.nrules == 1) // else the CHECK above already said so + CHECK_INT(p.rules[0].nrefs, MAX_FIELDS - 3); + policy_free(&p); +} + +// A directory is not a policy that happens to be empty. Nothing can be read out +// of it, which is the same broken host as a missing file, and has to answer 3 +// rather than 1 so a monitor can tell the two apart. +static void test_policy_that_is_a_directory(void) +{ + policy_t p; + int rc; + + if (mkdir(tmproot, 0755) != 0 && errno != EEXIST) { + note("cannot make %s; skipped", tmproot); + return; + } + + policy_init(&p, tmproot); + rc = policy_load(&p); + CHECK_INT(rc, -2); + CHECK(p.unreadable); + policy_free(&p); +} + int main(void) { test_init(""); test_parse(); test_known_users(); + test_split_field_limit(); + test_rule_at_the_field_limit(); + test_policy_that_is_a_directory(); return test_report("parse"); } diff --git a/test/run.sh b/test/run.sh index bc1a9e4..a608468 100755 --- a/test/run.sh +++ b/test/run.sh @@ -388,6 +388,34 @@ 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" +# ------------------------------------------------------------ option forms + +# The usage text writes these as two words ("--user U", "--ff auto|yes|no"), and +# validate accepts both spellings, so authorize has to accept them too: a person +# following the help must not be told the option is unknown. + +section "authorize: the options as the usage spells them" + +out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x | + "$AUTHZ" --policy "$P" authorize --user bob --repo team --no-git 2>&1); rc=$? +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" + +out=$(printf '%s %s %s\n' "$B" "$ZERO" refs/heads/main | + "$AUTHZ" --policy "$P" authorize --user bob --repo team --head none 2>&1); rc=$? +expect "--head none as two words" 1 "-" "$rc" "$out" + +out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x | + "$AUTHZ" --policy "$P" authorize --user=bob --repo=team --no-git 2>&1); rc=$? +expect "and the = form still works" 0 "-" "$rc" "$out" + +out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x | + "$AUTHZ" --policy "$P" authorize --user 2>&1); rc=$? +expect "an option with no value says so" 64 "wants a value" "$rc" "$out" + # --------------------------------------------------------- the hook by name section "authorize: reached as pre-receive" @@ -644,6 +672,33 @@ else fi expect "warnings alone still let the host serve" 0 "is in force" "$rc" "$out" +# sshd reads a directive once and keeps the first answer it got, and "none" is +# how a Match block turns a ForceCommand back off - both of those are the same +# wide-open state as having no ForceCommand at all, so validate has to see them +# that way too. + +mkhost +printf 'PermitUserEnvironment yes\nForceCommand none\n' >"$H/sshd_config" +out=$(host_validate); rc=$? +expect "ForceCommand none is as open as no ForceCommand" 1 "interactive shell" "$rc" "$out" + +mkhost +printf 'PermitUserEnvironment no\nPermitUserEnvironment yes\n' >"$H/sshd_config" +out=$(host_validate); rc=$? +expect "sshd keeps the first PermitUserEnvironment, not the last" 1 "anonymous" "$rc" "$out" + +mkhost +printf 'PermitUserEnvironment yes\nForceCommand /usr/bin/git-shell\nForceCommand none\n' >"$H/sshd_config" +out=$(host_validate); rc=$? +expect "and the first ForceCommand is the one that counts" 0 "ForceCommand /usr/bin/git-shell" "$rc" "$out" + +# git spells section names case-insensitively, so [CORE] sets core.hooksPath just +# as [core] does; refusing to see it reports a broken host that is not broken. +mkhost +printf '[CORE]\n\thooksPath = %s/hooks\n' "$H" >"$H/gitconfig" +out=$(host_validate); rc=$? +expect "a gitconfig section name is case-insensitive, like git's own" 0 "core.hooksPath" "$rc" "$out" + # -------------------------------------------------------------- edit (grant) section "grant and revoke" @@ -697,6 +752,31 @@ out=$("$AUTHZ" --policy "$TMP/edit.conf" grant bob group/team write 2>&1); rc=$? expect "grant rejects a repository name with a slash in it" 64 "invalid repo pattern" "$rc" "$out" out=$("$AUTHZ" --policy "$TMP/edit.conf" grant bob team write '!refs/heads/main' 2>&1); rc=$? expect "grant has no deny" 64 "no deny" "$rc" "$out" + +# A ref argument has to be one field. The line it lands on is split on whitespace +# again when the file is read, and '#' starts a comment, so writing an argument +# verbatim would either grant more than was asked for or leave a file that no +# longer parses - and a policy that does not parse denies every push there is. +cp "$TMP/edit.conf" "$TMP/edit.conf.before" +out=$("$AUTHZ" --policy "$TMP/edit.conf" grant bob team write 'refs/heads/a b' 2>&1); rc=$? +expect "a ref argument with a space in it is refused" 64 "not a ref pattern" "$rc" "$out" +out=$("$AUTHZ" --policy "$TMP/edit.conf" grant bob team write 'refs/heads/a#b' 2>&1); rc=$? +expect "a ref argument with a comment marker is refused" 64 "not a ref pattern" "$rc" "$out" +out=$("$AUTHZ" --policy "$TMP/edit.conf" grant bob team write "$(printf 'refs/heads/a\nb')" 2>&1); rc=$? +expect "a ref argument with a newline in it is refused" 64 "not a ref pattern" "$rc" "$out" +cmp -s "$TMP/edit.conf" "$TMP/edit.conf.before" || + bad "a refused grant still changed the file" + +# The pair's rule being the last line of the file is still a replacement: nothing +# was appended, and the message says which line was taken over. +cat >"$TMP/lastline.conf" <<'EOF' +alice * admin +bob team write refs/heads/feature/* +EOF +out=$("$AUTHZ" --policy "$TMP/lastline.conf" grant bob team admin 2>&1); rc=$? +expect "replacing the last line says replaced, not appended" 0 "replaced line 2" "$rc" "$out" +out=$("$AUTHZ" --policy "$TMP/lastline.conf" grant zed team admin 2>&1); rc=$? +expect "and a rule with no pair yet is still an append" 0 "appended as line 3" "$rc" "$out" out=$("$AUTHZ" --policy "$TMP/edit.conf" protect refs/heads/main 2>&1); rc=$? expect "protect is gone, and says it is unknown" 64 "unknown command" "$rc" "$out" printf 'set protected-refs refs/heads/main\n' >>"$TMP/edit.conf" -- cgit v1.2.3