fix(hooks): scrub git env before apm pre-push hooks run #229

Merged
Defame1297 merged 3 commits from fix/apm-audit-git-env into main 2026-11-27 08:04:31 +00:00
8 changed files with 191 additions and 7 deletions
Showing only changes of commit f2c483ca2c - Show all commits

No files matched your search

+2 -2
View File
@@ -173,8 +173,8 @@ repos:
- id: apm-pack-check-clean
name: apm pack --check-clean
description: Release gate -- verify .claude-plugin/marketplace.json still matches what apm.yml + .apm/ would currently generate, and that per-package versions agree with the per_package versioning strategy. Closes issue #90's deferred item 3 (a check-clean-equivalent gate) using apm's own flag instead of custom drift logic.
entry: apm pack --check-versions --check-clean --dry-run
description: Release gate -- verify .claude-plugin/marketplace.json still matches what apm.yml + .apm/ would currently generate, and that per-package versions agree with the per_package versioning strategy. Closes issue #90's deferred item 3 (a check-clean-equivalent gate) using apm's own flag instead of custom drift logic. Runs via scripts/apm-pack-check-clean.sh so apm gets a scrubbed env (issue #204).
entry: scripts/apm-pack-check-clean.sh
language: system
stages: [pre-push]
pass_filenames: false
+7 -1
View File
@@ -144,8 +144,14 @@ The guard added above (`tests/test-no-pipefail-early-exit-grep.sh`) reported the
## 2026-11-13 — Hook-exported GIT_DIR turned test fixtures loose on the real repo
Under the `run-tests` pre-push hook, git exports `GIT_DIR` to the hook, so a bats fixture's `git -C "$tmp" init/commit/update-ref` hit this repo instead of its temp dir: 84 failures, `core.bare=true` and an emptied index on every push. The 2026-07-05 "repo root is a bare checkout" entry was likely this bug's symptom, not the repo's real shape. Fix: `tests/run-tests.sh` unsets git's repo-locating env vars before any suite runs. Rule: a test that builds its own repo must never inherit the caller's git environment.
Under the `run-tests` pre-push hook, git exported `GIT_DIR` to the hook, so a bats fixture's `git -C "$tmp" init/commit/update-ref` hit this repo instead of its temp dir: 84 failures, `core.bare=true` and an emptied index on every push. The 2026-07-05 "repo root is a bare checkout" entry was likely this bug's symptom, not the repo's real shape. Fix: `tests/run-tests.sh` unsets git's repo-locating env vars before any suite runs. Rule: a test that builds its own repo must never inherit the caller's git environment.
*Corrected 2026-11-26 (#204):* git exports `GIT_DIR` to hooks only when the push runs from a **linked worktree**, where it is the absolute `<main>/.git/worktrees/<name>`. From the main checkout it exports none. So the bug reproduces only on pushes from a worktree.
## 2026-11-22 — A local-path install skips the validation a remote install runs
#205 shipped `ansible` and `k8s` as `apm.yml`-only skeletons after a scratch consumer installed all four new packages from `../pkgs/<name>` with exit 0. Fetched from the remote, the same packages fail: apm accepts a remote subdirectory only when it has `.apm/`, declared dependencies or a `skills/` bundle (`detect_package_type` in `apm_cli/models/validation.py`). `gitops` failed too, through its dependency on `k8s`. So `main` published uninstallable packages until #206 added `.apm/.gitkeep`. A green local-path install says nothing about whether consumers can install. Fix: before merging a new or restructured package, push the branch and install it with `git:`/`path:`/`ref: <branch>`, or run `validate_apm_package` on a `git archive` export. `file://` URLs are rejected in the object form, so they can't stand in for the remote.
## 2026-11-26 — A hook's GIT_DIR leaks through any tool that runs git
#204: pushing from a linked worktree left the shared `.git/config` with `core.bare=true` and the worktree on a detached HEAD at the lock-pinned commit. git exports an absolute `GIT_DIR` to hooks run from a linked worktree. `apm-audit-ci` ran `apm audit --ci`, and its drift replay (apm 0.28.0) runs `git init` and `git checkout` in a temp dir with an env copied from `os.environ`. The inherited `GIT_DIR` won, so both operations hit this repo instead of the temp dir. The 2026-11-13 fix did not cover this: it scrubbed the env only inside `tests/run-tests.sh`, and the apm hooks never pass through that file. Fix: `scripts/lib/clean-git-env.sh` unsets the repo-locating vars, and both `scripts/apm-audit-ci.sh` and the new `scripts/apm-pack-check-clean.sh` source it before calling apm. `tests/test-apm-hooks-git-env.sh` reproduces the leak with a stub `apm`. Rule: any hook that shells out to a tool that runs git must scrub `GIT_*` first. Scrubbing one entry point does not protect the others.
+15 -3
View File
@@ -73,7 +73,7 @@ version-blind, so a stale key deploys fine (see [apm gates](#apm-gates)).
| Hook | Guards |
|---|---|
| `apm-audit-ci` | `scripts/apm-audit-ci.sh` — `apm audit --ci` once per manifest, root plus each plugin package under `plugins/`, waiving only a package's `lockfile-exists` (see [below](#apm-audit-ci)) |
| `apm-pack-check-clean` | `apm pack --check-versions --check-clean --dry-run` — the compiled marketplace still matches what `apm.yml` + `.apm/` would generate, and per-package versions agree with the `per_package` strategy |
| `apm-pack-check-clean` | `scripts/apm-pack-check-clean.sh` — `apm pack --check-versions --check-clean --dry-run` with a scrubbed git env (see [`apm-audit-ci`](#apm-audit-ci)) — the compiled marketplace still matches what `apm.yml` + `.apm/` would generate, and per-package versions agree with the `per_package` strategy |
**Host validators** (needs the `claude` CLI on PATH)
@@ -87,8 +87,9 @@ version-blind, so a stale key deploys fine (see [apm gates](#apm-gates)).
|---|---|
| `check-skill-version-bump` | fails if a skill directory changed since the pushed commit's merge-base with `main` without its `metadata.version` rising above both the merge-base's and `main`'s tip's (see [below](#check-skill-version-bump)) |
Two of these shell out to `apm`: `apm-audit-ci` and `apm-pack-check-clean`. The second is a bare
`apm …` entry and the first is `scripts/apm-audit-ci.sh`, which calls `apm` once per manifest, so
Two of these shell out to `apm`: `apm-audit-ci` and `apm-pack-check-clean`. The second is
`scripts/apm-pack-check-clean.sh`, a one-call wrapper, and the first is `scripts/apm-audit-ci.sh`,
which calls `apm` once per manifest, so
without the CLI on PATH the push dies on a "command not found" from inside the hook. Install with `apm-install`, or
`curl -sSL https://aka.ms/apm-unix | sh`; verify with `apm --version`.
@@ -1163,6 +1164,17 @@ Verified: a malformed dependency entry passes
`apm pack --check-versions --check-clean --dry-run` and fails `apm audit --ci` in that package's
directory. Costs ~0.5s per package.
**apm runs with git's repo-locating env scrubbed.** Both apm hooks source
`scripts/lib/clean-git-env.sh` before calling `apm`, which unsets `GIT_DIR`, `GIT_WORK_TREE`,
`GIT_INDEX_FILE`, `GIT_OBJECT_DIRECTORY`, `GIT_COMMON_DIR` and everything else
`git rev-parse --local-env-vars` lists. The reason: `git push` from a **linked worktree** exports an
absolute `GIT_DIR=<main>/.git/worktrees/<name>` to its hooks, and from the main checkout it exports
none. `drift`'s install replay (apm 0.28.0) runs `git init` and `git checkout` in a temp dir with an
env copied from `os.environ`, so the inherited `GIT_DIR` wins. The `init` wrote `core.bare=true` into
the shared `.git/config`, and the `checkout` moved the worktree's HEAD to the lock-pinned commit
(issue #204). Pre-commit runs hooks from the worktree root, so once the vars are gone plain
discovery finds the right repo. `tests/test-apm-hooks-git-env.sh` pins this for both hooks.
**What it actually runs is asymmetric**, and the two manifest classes are not comparable. Verified by
running `apm audit --ci` (apm 0.28.0) at the repo root and in `plugins/lint/`, reading the check
names straight off its own compliance table:
+7
View File
@@ -55,6 +55,13 @@
set -uo pipefail
# Scrub git's repo-locating env before apm runs: pushed from a linked worktree,
# the hook inherits an absolute GIT_DIR, and apm's drift replay runs `git init`
# / `git checkout` in a temp dir with it -- flipping core.bare=true and moving
# the worktree's HEAD (issue #204). See the helper for the full account.
# shellcheck source=scripts/lib/clean-git-env.sh
source "$(dirname "${BASH_SOURCE[0]}")/lib/clean-git-env.sh"
readonly WAIVED_CHECK='declares dependencies but apm.lock.yaml is absent'
readonly SOLE_FAILURE='1 of 1 check(s) failed'
+12
View File
@@ -0,0 +1,12 @@
#!/usr/bin/env bash
# apm-pack-check-clean pre-push hook: `apm pack --check-versions --check-clean
# --dry-run`, run with git's repo-locating env scrubbed first. A script rather
# than a bare `apm ...` entry only so it can source that scrub -- see
# scripts/lib/clean-git-env.sh and issue #204. Full rationale for the gate
# itself: docs/spec/gates.md, "apm-pack-check-clean".
set -euo pipefail
# shellcheck source=scripts/lib/clean-git-env.sh
source "$(dirname "${BASH_SOURCE[0]}")/lib/clean-git-env.sh"
exec apm pack --check-versions --check-clean --dry-run
+26
View File
@@ -0,0 +1,26 @@
#!/usr/bin/env bash
# Scrubs git's repo-locating environment variables from the current shell, so
# every git process it spawns -- directly or through another tool -- discovers
# its repository from its own working directory. Issue #204.
#
# Why: `git push` from a LINKED worktree exports an absolute
# GIT_DIR=<main>/.git/worktrees/<name> to its hooks (from the main checkout it
# exports none). A hook that runs a tool which itself runs git in a temp dir --
# apm's drift replay does `git init` / `git checkout` there with os.environ --
# hands that GIT_DIR on, and the temp-dir git operates on THIS repo instead:
# core.bare=true in the shared .git/config, and the worktree's HEAD moved to a
# lock-pinned commit. Hooks run from the worktree root, so plain discovery
# finds the right repo once these are gone.
#
# The explicit list runs first so the scrub holds even if `git rev-parse
# --local-env-vars` is unavailable; that call then adds anything else git
# considers repo-local (GIT_CONFIG_PARAMETERS, GIT_PREFIX, ...), making the
# result a superset of the explicit list.
#
# Not meant to be executed directly -- source it before any git call.
unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE GIT_OBJECT_DIRECTORY GIT_COMMON_DIR
for _clean_git_env_var in $(git rev-parse --local-env-vars 2>/dev/null); do
unset "$_clean_git_env_var"
done
unset _clean_git_env_var
+1 -1
View File
@@ -741,7 +741,7 @@ EXPECTED = {
'check-apm-agents-valid': (
'bash scripts/check-apm-agents-valid.sh', ['pre-push']),
'apm-pack-check-clean': (
'apm pack --check-versions --check-clean --dry-run', ['pre-push']),
'scripts/apm-pack-check-clean.sh', ['pre-push']),
'check-scope-walkup-sync': (
'bash scripts/check-scope-walkup-sync.sh', ['pre-push']),
'check-provenance-corpus': (
+121
View File
@@ -0,0 +1,121 @@
#!/usr/bin/env bash
# Regression test for issue #204: the apm pre-push hooks must not hand a
# hook-exported GIT_DIR to apm.
#
# `git push` from a LINKED worktree exports GIT_DIR=<main>/.git/worktrees/<name>
# (absolute) to its hooks; from the main checkout it exports none. apm 0.28.0's
# drift replay runs `git init` / `git checkout` in a temp dir with an env copied
# from os.environ, so the inherited GIT_DIR wins: `git init` wrote
# core.bare=true into the shared .git/config, and a later checkout moved the
# worktree's HEAD to the lock-pinned commit.
#
# The stub `apm` below reproduces that pattern -- record GIT_DIR, then `git init`
# and `git checkout` inside a fresh `mktemp -d` -- and each hook script is run
# from a linked worktree of a throwaway repo with GIT_DIR exported the way git
# exports it. Nothing here touches the repo this suite lives in.
set -euo pipefail
# This suite builds its own repos; it must never inherit the caller's.
unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE GIT_OBJECT_DIRECTORY GIT_COMMON_DIR
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
PASS=0
FAIL=0
pass() { echo " PASS: $1"; PASS=$((PASS + 1)); }
fail() { echo " FAIL: $1"; FAIL=$((FAIL + 1)); }
FIXTURES=()
cleanup() { [[ ${#FIXTURES[@]} -eq 0 ]] || rm -rf "${FIXTURES[@]}"; }
trap cleanup EXIT
g() { git -c user.name=fixture -c user.email=[email protected] "$@"; }
# Builds <dir>/main (two commits on `main`) and <dir>/wt, a linked worktree on
# branch `feature`, with a plugins/demo/ package dir so the audit script's
# per-package loop has something to iterate. Also writes <dir>/bin/apm, the stub.
make_fixture() {
local dir="$1"
mkdir -p "$dir/bin"
g init -q -b main "$dir/main"
echo one > "$dir/main/f"
g -C "$dir/main" add f
g -C "$dir/main" commit -q -m one
echo two > "$dir/main/f"
g -C "$dir/main" commit -q -am two
g -C "$dir/main" worktree add -q -b feature "$dir/wt"
mkdir -p "$dir/wt/plugins/demo"
# The "locked sha" the replay checks out: the first commit.
local locked
locked="$(g -C "$dir/main" rev-parse HEAD~1)"
cat > "$dir/bin/apm" <<EOF
#!/usr/bin/env bash
printf '%s\n' "\${GIT_DIR-<unset>}" >> "$dir/apm-saw-git-dir"
tmp="\$(mktemp -d)"
( cd "\$tmp" && git init -q . >/dev/null 2>&1; git checkout -q --detach $locked >/dev/null 2>&1 ) || true
rm -rf "\$tmp"
exit 0
EOF
chmod +x "$dir/bin/apm"
}
# check_hook <label> <script>
check_hook() {
local label="$1" script="$2" dir rc=0
dir="$(mktemp -d)"
FIXTURES+=("$dir")
make_fixture "$dir"
echo ""
echo "--- $label: run from a linked worktree with GIT_DIR exported ---"
if [[ ! -f "$script" ]]; then
fail "$label: $script does not exist"
return
fi
(
cd "$dir/wt"
export GIT_DIR="$dir/main/.git/worktrees/wt"
export PATH="$dir/bin:$PATH"
bash "$script"
) > "$dir/out" 2>&1 || rc=$?
if [[ "$rc" -eq 0 ]]; then
pass "$label: exits 0 when apm passes"
else
fail "$label: exited $rc; output: $(cat "$dir/out")"
fi
local bare
bare="$(git -C "$dir/main" config --get core.bare || true)"
if [[ "$bare" == "false" ]]; then
pass "$label: main repo core.bare is still false"
else
fail "$label: main repo core.bare is '$bare', expected false"
fi
local branch
branch="$(git -C "$dir/wt" symbolic-ref -q --short HEAD || echo '<detached>')"
if [[ "$branch" == "feature" ]]; then
pass "$label: worktree still on its branch"
else
fail "$label: worktree HEAD is '$branch', expected feature"
fi
if [[ ! -s "$dir/apm-saw-git-dir" ]]; then
fail "$label: stub apm never ran"
elif grep -qvxF '<unset>' "$dir/apm-saw-git-dir"; then
fail "$label: apm inherited GIT_DIR: $(sort -u "$dir/apm-saw-git-dir" | tr '\n' ' ')"
else
pass "$label: apm saw no GIT_DIR"
fi
}
check_hook apm-audit-ci "$REPO_ROOT/scripts/apm-audit-ci.sh"
check_hook apm-pack-check-clean "$REPO_ROOT/scripts/apm-pack-check-clean.sh"
echo ""
echo "Results: $PASS passed, $FAIL failed"
[[ "$FAIL" -eq 0 ]]