summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/hook.c17
-rwxr-xr-xtest/run.sh71
2 files changed, 78 insertions, 10 deletions
diff --git a/src/hook.c b/src/hook.c
index d4d801b..d581bc8 100644
--- a/src/hook.c
+++ b/src/hook.c
@@ -135,7 +135,11 @@ static int object_id_ok(const char *s)
static char *head_ref(hook_opts o, int *unknown)
{
- const char *argv[4];
+ /* Terminated by hand, and by name: execvp walks this array until it finds
+ * the NULL, and an array that runs off the end takes whatever is on the
+ * stack next as an argument - which is not a crash you can read, and made
+ * every deletion cost admin because the answer was never received. */
+ const char *argv[5] = { "git", "symbolic-ref", "-q", "HEAD", NULL };
char *out = NULL;
int rc;
@@ -147,10 +151,6 @@ static char *head_ref(hook_opts o, int *unknown)
if (!o.use_git)
return NULL; /* told not to ask, so do not guess either way */
- argv[0] = "git";
- argv[1] = "symbolic-ref";
- argv[2] = "-q";
- argv[3] = "HEAD";
rc = spawn(argv, 1, &out);
if (rc == 0 && out != NULL && out[0] != '\0')
return out;
@@ -162,7 +162,9 @@ static char *head_ref(hook_opts o, int *unknown)
/* Is old an ancestor of new? */
static int fast_forward(hook_opts o, const char *old, const char *new)
{
- const char *argv[5];
+ /* Same as head_ref: the NULL is part of the contract, not padding. */
+ const char *argv[6] = { "git", "merge-base", "--is-ancestor",
+ NULL, NULL, NULL };
int rc;
if (strcmp(o.ff, "yes") == 0)
@@ -172,9 +174,6 @@ static int fast_forward(hook_opts o, const char *old, const char *new)
if (!o.use_git)
return 0;
- argv[0] = "git";
- argv[1] = "merge-base";
- argv[2] = "--is-ancestor";
argv[3] = old;
argv[4] = new;
rc = spawn(argv, 0, NULL);
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"