summaryrefslogtreecommitdiffstats
path: root/test/run.sh
diff options
context:
space:
mode:
authordev <dev@example>2026-11-15 12:10:23 +0000
committerdev <dev@example>2026-11-15 12:10:23 +0000
commit85b7fa7f8456c84f65a0d81b760c430098ae7fd8 (patch)
treed941ae68672ff71d2181fcb1a0e168f9314f135c /test/run.sh
parent2e6dd4a8e9e5302efae6b7532f7f4609882fc21d (diff)
downloadaccess-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.
Diffstat (limited to 'test/run.sh')
-rwxr-xr-xtest/run.sh118
1 files changed, 88 insertions, 30 deletions
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"