summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authordev <dev@example>2026-11-14 00:21:48 +0000
committerdev <dev@example>2026-11-14 00:21:48 +0000
commit4dfb9a5787b1554280807aaa28687f6c1a0d2db6 (patch)
treeca56b31548f69c1a07822b263a0d2990a2a4f895 /test
parentbc7f4da406fbd802b08985ba583e52f040bdba85 (diff)
downloadaccess-control-4dfb9a5787b1554280807aaa28687f6c1a0d2db6.tar.gz
access-control-4dfb9a5787b1554280807aaa28687f6c1a0d2db6.zip
The two git calls were missing their NULL, so every push cost one level too much
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.
Diffstat (limited to 'test')
-rwxr-xr-xtest/run.sh71
1 files changed, 70 insertions, 1 deletions
diff --git a/test/run.sh b/test/run.sh
index ab1de2d..39f26b7 100755
--- a/test/run.sh
+++ b/test/run.sh
@@ -144,7 +144,7 @@ erin secrets admin
EOF
# A repository directory, and the hook name this binary is reached under.
-mkdir -p "$TMP/root/team.git" "$TMP/bin"
+mkdir -p "$TMP/root/team.git" "$TMP/bin" "$TMP/empty"
ln -sf "$AUTHZ_ABS" "$TMP/bin/pre-receive"
if [ ! -x "$TMP/bin/pre-receive" ]; then
cp "$AUTHZ" "$TMP/bin/pre-receive"
@@ -427,6 +427,75 @@ out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
GIT_REPO_ROOT=/var/lib/git "$AUTHZ_ABS" --policy "$P" authorize --no-git --explain 2>&1 )); rc=$?
expect "and the last path component works when it is not the root" 0 "team" "$rc" "$out"
+# ------------------------------------------- the two facts that belong to git
+#
+# Everything else the hook knows it works out of the line git gave it. Two
+# facts do not come that way: whether HEAD is a branch, and whether old is an
+# ancestor of new. Those are asked of git, and asking them is a code path the
+# rest of this suite cannot reach, because there is no git in it.
+#
+# So: a stand-in named git, and the real one kept out of PATH. A fake that
+# answers whatever it is told is worth more here than the real thing, which
+# would answer a question about the host instead of the question the hook asked.
+
+mkdir -p "$TMP/fakegit"
+cat >"$TMP/fakegit/git" <<'EOF'
+#!/bin/sh
+case $1 in
+symbolic-ref) printf '%s\n' "${FAKE_HEAD:-refs/heads/main}"; exit 0 ;;
+merge-base) exit "${FAKE_FF:-1}" ;;
+esac
+exit 125
+EOF
+chmod 755 "$TMP/fakegit/git"
+
+# hookgit <user> <dir> [PATH=...] - the hook, with the fake git in front.
+hookgit() {
+ user=$1
+ dir=$2
+ shift 2
+ ( cd "$dir" && env "$@" GIT_HOST_USER="$user" GIT_HOST_AUTHZ="$POLICY" \
+ "$TMP/bin/pre-receive" "$dir" 2>&1 )
+}
+
+section "what the hook asks git"
+
+POLICY="$TMP/authz.conf"
+
+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/main FAKE_FF=1); rc=$?
+expect "a deletion is force, not admin, when HEAD is somewhere else" 0 "-" "$rc" "$out"
+
+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"
+
+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=$?
+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"
+
+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=$?
+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"
+
+# No git at all: the hook cannot ask, so it must not guess in the caller's
+# favour. A deletion it cannot place is charged the most, not the least.
+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"
+
+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"
+
# -------------------------------------------------------------- fail closed
section "fail closed"