diff --git a/.github/post-pin-bump.paths b/.github/post-pin-bump.paths new file mode 100644 index 00000000..335f29d0 --- /dev/null +++ b/.github/post-pin-bump.paths @@ -0,0 +1,5 @@ +# SPDX-License-Identifier: MPL-2.0 +# Paths scripts/post-pin-bump.sh may change, one per line. The Action pins applier +# stages exactly these (and nothing else) after running the hook. +scripts/lib/pinned-reusable-sources.json +scripts/lib/lock-closure-receipts.tsv diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml index 47bbf55d..e1e4db77 100644 --- a/.github/workflows/e2e.yml +++ b/.github/workflows/e2e.yml @@ -76,6 +76,8 @@ jobs: run: printf 'PINNED_SOURCE_CACHE=%s/fleet-pinned-sources\n' "$RUNNER_TEMP" >> "$GITHUB_ENV" - name: Test pinned-source failure modes run: bash scripts/tests/pinned-reusable-sources-test.sh + - name: Test the post-pin-bump hook + run: bash scripts/tests/post-pin-bump-test.sh - name: Authenticate pinned reusable sources run: bash scripts/audit-pinned-reusable-sources.sh --fetch-sources - name: Verify live scanner caller and negative controls diff --git a/.machine_readable/departures.jsonl b/.machine_readable/departures.jsonl index 9b336777..5293cef3 100644 --- a/.machine_readable/departures.jsonl +++ b/.machine_readable/departures.jsonl @@ -1,3 +1,2 @@ {"added":"2026-11-22","blocked_on":"upstream serenity/poise/sqlx still depend; replace with jiff/time when they move","checks":[{"kind":"grep-present","path":"bots/gsbot/Cargo.lock","pattern":"^name = \"chrono\"$"}],"claim":"RUSTSEC-2026-0543: chrono 0.4.44 is unmaintained (all versions affected, no safe upgrade). bots/gsbot/Cargo.lock still resolves it, via gsbot itself and via serenity, poise and sqlx. The GSBot dependency audit runs with --warn unmaintained, so this is reported as a warning and does not fail the audit.","fix":"blocked","id":"gsbot-rustsec-2026-0543-chrono","repo":"hyperpolymath/gitbot-fleet","ruling":"bots/gsbot/deny.toml: no vulnerability advisories are ignored; the GSBot dependency audit in .github/workflows/rust.yml must pass"} {"added":"2026-11-22","blocked_on":"upstream poise still depends on derivative; clears when poise removes it or replaces it with a maintained derive crate","checks":[{"kind":"grep-present","path":"bots/gsbot/Cargo.lock","pattern":"^name = \"derivative\"$"}],"claim":"RUSTSEC-2024-0388: derivative 2.2.0 is unmaintained (no safe upgrade). bots/gsbot/Cargo.lock still resolves it, via poise 0.6.2. The GSBot dependency audit runs with --warn unmaintained, so this is reported as a warning and does not fail the audit.","fix":"blocked","id":"gsbot-rustsec-2024-0388-derivative","repo":"hyperpolymath/gitbot-fleet","ruling":"bots/gsbot/deny.toml: no vulnerability advisories are ignored; the GSBot dependency audit in .github/workflows/rust.yml must pass"} -{"added":"2026-11-26","blocked_on":"gitbot-fleet #717 (scripts/post-pin-bump.sh) lands, after the standards applier hook contract (part A); merge order A, then #717. Clears when scripts/post-pin-bump.sh exists on main.","checks":[{"kind":"path-absent","path":"scripts/post-pin-bump.sh"}],"claim":"The applier writes only .github/workflows and .github/actions and skips first-party owners, so when it moves a pinned reusable-workflow commit, scripts/lib/pinned-reusable-sources.json and scripts/lib/lock-closure-receipts.tsv are not updated and Pinned source closure goes red on main (as with #699). The repo hook scripts/post-pin-bump.sh (#717) re-attests the pin-only case, but it is not on main, so the gap is open.","fix":"blocked","id":"applier-bump-closure-gap","repo":"hyperpolymath/gitbot-fleet","ruling":"Owner ruling \"single pin writer\" (2026-11-26): the standards Action pins applier is the only writer of third-party action pins and actions.lock; Dependabot github-actions is off (#716)."} diff --git a/scripts/audit-pinned-reusable-sources.sh b/scripts/audit-pinned-reusable-sources.sh index 3154a906..03c056bc 100644 --- a/scripts/audit-pinned-reusable-sources.sh +++ b/scripts/audit-pinned-reusable-sources.sh @@ -7,7 +7,7 @@ set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" MANIFEST=""; LOCK=""; RECEIPTS=""; CACHE="${PINNED_SOURCE_CACHE:-${XDG_CACHE_HOME:-${HOME:-/tmp}/.cache}/fleet-pinned-sources}" -FETCH=0; JSON=0 +FETCH=0; JSON=0; ROOTS_ONLY=0 while [[ "$#" -gt 0 ]]; do case "$1" in --manifest|--source-manifest) MANIFEST="${2:?manifest file required}"; shift ;; @@ -17,7 +17,8 @@ while [[ "$#" -gt 0 ]]; do --cache|--source-cache) CACHE="${2:?cache directory required}"; shift ;; --fetch-sources) FETCH=1 ;; --json) JSON=1 ;; - -h|--help) printf '%s\n' 'Usage: audit-pinned-reusable-sources.sh [--manifest FILE] [--repo-root DIR] [--lock FILE] [--receipts FILE] [--cache DIR] [--fetch-sources] [--json]'; exit 0 ;; + --roots-only) ROOTS_ONLY=1 ;; + -h|--help) printf '%s\n' 'Usage: audit-pinned-reusable-sources.sh [--manifest FILE] [--repo-root DIR] [--lock FILE] [--receipts FILE] [--cache DIR] [--fetch-sources] [--json] [--roots-only]'; exit 0 ;; *) printf 'Unknown argument: %s\n' "$1" >&2; exit 64 ;; esac shift @@ -179,6 +180,15 @@ for workflow in "$ROOT"/.github/workflows/*.yml "$ROOT"/.github/workflows/*.yaml done shopt -u nullglob jq -s 'sort_by(.workflow,.job)' "$TMP/roots" > "$TMP/actual-roots.json" +if [[ "$ROOTS_ONLY" -eq 1 ]]; then + # Print the live remote-reusable roots (workflow, job, repository, path, + # commit) and stop: no source is fetched and nothing is compared. Used by + # scripts/post-pin-bump.sh to learn which pins a bump moved. + if [[ -s "$TMP/findings" ]] || [[ -s "$TMP/notrun" ]]; then + jq -r '"\(.rule)\t\(.subject)\t\(.detail)"' "$TMP/findings" "$TMP/notrun" >&2; exit 1 + fi + cat "$TMP/actual-roots.json"; exit 0 +fi jq '[.sources[]|select(.role=="active")|. as $s|.callers[]|{workflow,job,repository:($s.repository|ascii_downcase),path:$s.path,commit:$s.commit}]|sort_by(.workflow,.job)' "$MANIFEST" > "$TMP/expected-roots.json" if ! cmp -s "$TMP/actual-roots.json" "$TMP/expected-roots.json"; then finding SOURCE-ACTIVE-MISSING "$MANIFEST" 'Active caller/job/path/commit bindings differ from the actual workflow roots (historical paths cannot satisfy active calls)' diff --git a/scripts/post-pin-bump.sh b/scripts/post-pin-bump.sh new file mode 100755 index 00000000..dccc4bbf --- /dev/null +++ b/scripts/post-pin-bump.sh @@ -0,0 +1,211 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: MPL-2.0 +# SPDX-FileCopyrightText: 2026 Jonathan D.A. Jewell +# +# Post-pin-bump hook: the repo's half of the "single pin writer" contract. +# +# The Action pins applier (metadatastician/standards) moves third-party +# `uses:` SHAs and actions.lock. It cannot write scripts/lib/*, and it skips +# first-party owners, so a bump of a pinned reusable-workflow commit leaves the +# source closure files (the manifest and the receipts) describing the old +# commit and main goes red. This hook re-attests the PIN-ONLY case and refuses +# everything else: +# +# * a pin-only bump means the caller's reusable workflow now points at a new +# commit of the SAME repository and path, and the workflow file there is +# byte-identical (same blob, same content_sha256, same direct edges); +# * anything else (another blob, another repository or path, a commit shared +# with a source that did not move, edges that changed, unreadable input) is +# refused with a non-zero exit and NO file is changed. A human reviews it. +# +# New source is fetched as DATA over git by scripts/audit-pinned-reusable-sources.sh +# into its isolated bare cache and is never checked out or executed. +# +# Output: the paths the hook changed, NUL-separated, on stdout (nothing when no +# pin moved). Messages go to stderr. Exit 0 = nothing to do or re-attested, +# 1 = refused or self-audit failed (files restored), 64 = usage. +# +# Fetching new source is on when POST_PIN_BUMP=1 (set by the applier) or with +# --fetch-sources; otherwise the hook is offline and reads only the cache. +# +# Usage: post-pin-bump.sh [--repo-root DIR] [--cache DIR] [--fetch-sources] +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" +CACHE="${PINNED_SOURCE_CACHE:-${XDG_CACHE_HOME:-${HOME:-/tmp}/.cache}/fleet-pinned-sources}" +# The Action pins applier runs this hook with no arguments and POST_PIN_BUMP=1 (the +# contract in metadatastician/standards), so that variable also turns fetching on. +FETCH=0 +[[ "${POST_PIN_BUMP:-}" != 1 ]] || FETCH=1 +while [[ "$#" -gt 0 ]]; do + case "$1" in + --repo-root) ROOT="${2:?repository root required}"; shift ;; + --cache) CACHE="${2:?cache directory required}"; shift ;; + --fetch-sources) FETCH=1 ;; + -h|--help) printf '%s\n' 'Usage: post-pin-bump.sh [--repo-root DIR] [--cache DIR] [--fetch-sources]'; exit 0 ;; + *) printf 'post-pin-bump: unknown argument: %s\n' "$1" >&2; exit 64 ;; + esac + shift +done +ROOT="$(cd "$ROOT" && pwd)" +for tool in git jq awk sha256sum cmp; do + command -v "$tool" >/dev/null || { printf 'post-pin-bump: missing prerequisite: %s\n' "$tool" >&2; exit 2; } +done + +AUDIT="$SCRIPT_DIR/audit-pinned-reusable-sources.sh" +MANIFEST_REL='scripts/lib/pinned-reusable-sources.json' +RECEIPTS_REL='scripts/lib/lock-closure-receipts.tsv' +ALLOW="$ROOT/.github/post-pin-bump.paths" +MANIFEST="$ROOT/$MANIFEST_REL" +RECEIPTS="$ROOT/$RECEIPTS_REL" + +TMP="$(mktemp -d "${TMPDIR:-/tmp}/post-pin-bump.XXXXXX")" +trap 'rm -rf "$TMP"' EXIT +refuse() { printf 'post-pin-bump: REFUSED: %s\n' "$*" >&2; exit 1; } + +[[ -f "$ALLOW" ]] || refuse "missing allow-list $ALLOW" +[[ -f "$MANIFEST" && -f "$RECEIPTS" ]] || refuse 'closure manifest or receipts missing' +[[ -x "$AUDIT" || -f "$AUDIT" ]] || refuse 'audit script missing' + +audit_args=(--repo-root "$ROOT" --cache "$CACHE") +[[ "$FETCH" -eq 0 ]] || audit_args+=(--fetch-sources) + +# 1. The live remote-reusable roots, read by the audit's own parser. Anything +# the parser cannot read stops here. +bash "$AUDIT" --repo-root "$ROOT" --roots-only > "$TMP/roots.json" 2> "$TMP/roots.err" || + refuse "workflow roots unreadable: $(tr '\n' ' ' < "$TMP/roots.err")" + +# 2. Which active sources moved? One (index, old, new) per moved source. +jq -rn --slurpfile roots "$TMP/roots.json" --slurpfile m "$MANIFEST" ' + $m[0].sources as $src | $roots[0] as $actual | + [ range(0; $src|length) as $i | $src[$i] | select(.role=="active") as $s | + ($s.callers | map(. as $c | ($actual | map(select(.workflow==$c.workflow and .job==$c.job)))[0])) as $found | + if ($found|any(.==null)) then "ERR\t\($s.repository)@\($s.commit)\tcaller no longer present in the workflows" + elif ($found|any((.repository != ($s.repository|ascii_downcase)) or .path != $s.path)) then "ERR\t\($s.repository)@\($s.commit)\trepository or path of a caller changed (not pin-only)" + elif ($found|map(.commit)|unique|length) != 1 then "ERR\t\($s.repository)@\($s.commit)\tcallers disagree on the commit" + elif $found[0].commit != $s.commit then "BUMP\t\($i)\t\($s.commit)\t\($found[0].commit)" + else empty end ] | .[]' > "$TMP/plan.tsv" + +if grep -q '^ERR' "$TMP/plan.tsv"; then refuse "$(grep '^ERR' "$TMP/plan.tsv" | tr '\t' ' ' | tr '\n' ';')"; fi +if [[ ! -s "$TMP/plan.tsv" ]]; then exit 0; fi + +# 3. Rewrite into temporary copies, never into the tree. +cp "$MANIFEST" "$TMP/manifest.json" +cp "$RECEIPTS" "$TMP/receipts.tsv" +# Plan rows grouped by (repository, old commit): sources that share an old commit +# and ALL move together to the same new commit are one group and are rewritten +# once. A sharer that stays put, or members moving to different targets, is refused. +: > "$TMP/members.tsv" +while IFS=$'\t' read -r _ idx old new; do + [[ "$idx" =~ ^[0-9]+$ && "$old" =~ ^[0-9a-f]{40}$ && "$new" =~ ^[0-9a-f]{40}$ ]] || refuse 'malformed plan row' + jq -r --argjson i "$idx" --arg old "$old" --arg new "$new" \ + '[(.sources[$i].repository|ascii_downcase), $old, $new, ($i|tostring)] | @tsv' "$TMP/manifest.json" >> "$TMP/members.tsv" +done < <(grep '^BUMP' "$TMP/plan.tsv") +# Chained or overlapping moves (A->B with B->C, or a swap) make the result depend on +# the order the rewrites run in; refuse them before any rewrite, whatever the order. +chained="$(awk -F'\t' '{ old[$1 FS $2] = 1; nw[$1 FS $3] = 1 } END { for (k in nw) if (k in old) { split(k, a, FS); print a[1] } }' "$TMP/members.tsv" | LC_ALL=C sort -u | tr '\n' ' ')" +[[ -z "$chained" ]] || refuse "chained or overlapping pin moves for ${chained% }; review by hand" +# A lock record still resolving to repo@old would lose its receipt rows when they move +# to the new commit (the full audit reports CLOSURE-NO-RECEIPT), so it must be bumped +# too: refuse rather than leave the lock pointing at a commit without receipts. +bash "$SCRIPT_DIR/audit-lock-closure.sh" --dump-lock-records --lock "$ROOT/.github/workflows/actions.lock" \ + --workflows "$ROOT/.github/workflows" --repo-root "$ROOT" --receipts "$RECEIPTS" --baseline /dev/null > "$TMP/lockrecs.json" 2> "$TMP/lockrecs.err" || + refuse "lock records unreadable: $(tr '\n' ' ' < "$TMP/lockrecs.err" | cut -c1-300)" +while IFS=$'\t' read -r repo old; do + if jq -e --arg k "$repo@$old" '[(keys[], (.[] | .uses[]?)) | ascii_downcase | select(. == $k)] | length > 0' "$TMP/lockrecs.json" >/dev/null; then + refuse "actions.lock still resolves $repo@$old; bump the lock record too, or review by hand" + fi +done < <(cut -f1,2 "$TMP/members.tsv" | LC_ALL=C sort -u) +while IFS=$'\t' read -r repo old; do + targets="$(awk -F'\t' -v r="$repo" -v o="$old" '$1==r && $2==o {print $3}' "$TMP/members.tsv" | LC_ALL=C sort -u)" + [[ "$(printf '%s\n' "$targets" | wc -l)" -eq 1 ]] || + refuse "$repo@$old moves to conflicting commits ($(printf '%s' "$targets" | tr '\n' ' ')); review by hand" + new="$targets" + idxs="$(awk -F'\t' -v r="$repo" -v o="$old" '$1==r && $2==o {print $4}' "$TMP/members.tsv" | paste -sd, -)" + # Receipt rows and the comment cover every source of repo@old: a commit shared + # with a source outside this bump plan cannot be re-pointed by guessing. + shared="$(jq -r --arg r "$repo" --arg c "$old" --argjson ids "[$idxs]" \ + '[.sources|to_entries[]|select((.key as $k | $ids|index($k)|not) and (.value.repository|ascii_downcase)==$r and .value.commit==$c)]|length' "$MANIFEST")" + [[ "$shared" -eq 0 ]] || refuse "$repo@$old is shared with $shared other source(s) that did not move; review by hand" + # Re-point each member's commit and its recognised provenance fields (source_url, + # local_file), by literal split/join. Any other mention of $old is refused below. + jq --argjson ids "[$idxs]" --arg old "$old" --arg new "$new" ' + def swap: if type=="string" then split($old)|join($new) else . end; + reduce $ids[] as $i (.; .sources[$i] |= (.commit=$new | (if has("source_url") then .source_url|=swap else . end) | (if has("local_file") then .local_file|=swap else . end)))' \ + "$TMP/manifest.json" > "$TMP/manifest.next" + mv "$TMP/manifest.next" "$TMP/manifest.json" + # Receipts: rows "repoold..." and "# Reviewed source: ...repo/...@old" lines, + # literal matching only (no regex on data). + awk -F'\t' -v OFS='\t' -v repo="${repo,,}" -v old="$old" -v new="$new" ' + BEGIN { tag = "# Reviewed source: " } + substr($0, 1, length(tag)) == tag { + i = index(tolower($0), repo "/") + if (i > 0) { j = index($0, "@" old); if (j > 0 && j > i) $0 = substr($0, 1, j) new substr($0, j + 1 + length(old)) } + print; next + } + $0 !~ /^#/ && tolower($1) == repo && $2 == old { $2 = new } + { print }' "$TMP/receipts.tsv" > "$TMP/receipts.next" + mv "$TMP/receipts.next" "$TMP/receipts.tsv" + if grep -qF "$old" "$TMP/receipts.tsv"; then + # The only tolerated mention is prose in the header comment block. + grep -F "$old" "$TMP/receipts.tsv" | grep -qv '^#' && refuse "receipts still name $old after the rewrite" + fi + # Any other mention of the old commit in the manifest, in a value or an object key (e.g. dependency keys), is + # something this hook did not review. + if jq -e --arg c "$old" '[.. | (strings, (objects | keys[])) | select(contains($c))] | length > 0' "$TMP/manifest.json" >/dev/null; then + refuse "manifest still names $old after the rewrite (dependency records); review by hand" + fi +done < <(cut -f1,2 "$TMP/members.tsv" | LC_ALL=C sort -u) + +# 4. The audit is the authority: with the rewritten copies it must find, at the +# NEW commit, the same blob (a fetched blob mismatch is a FATAL finding) and +# the same direct edges, with every receipt consistent. Not-run is a refusal. +if ! bash "$AUDIT" "${audit_args[@]}" --manifest "$TMP/manifest.json" --receipts "$TMP/receipts.tsv" > "$TMP/audit.out" 2>&1; then + refuse "the new commit does not re-attest as pin-only: $(tr '\n' ' ' < "$TMP/audit.out" | cut -c1-600)" +fi + +# 5. content_sha256, from the cached object the audit just verified by hash. +while IFS=$'\t' read -r _ idx _ new; do + want="$(jq -r --argjson i "$idx" '.sources[$i].content_sha256 // empty' "$TMP/manifest.json")" + [[ -n "$want" ]] || continue + repo="$(jq -r --argjson i "$idx" '.sources[$i].repository|ascii_downcase' "$TMP/manifest.json")" + blob="$(jq -r --argjson i "$idx" '.sources[$i].blob' "$TMP/manifest.json")" + got="$(env GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1 git --no-replace-objects --git-dir "$CACHE/$repo.git" cat-file blob "$blob" | sha256sum | cut -d' ' -f1)" || + refuse "cannot read verified blob $blob of $repo@$new" + [[ "$got" = "$want" ]] || refuse "content_sha256 of $repo@$new differs from the manifest" +done < <(grep '^BUMP' "$TMP/plan.tsv") + +# 6. Only the allow-listed paths may change. +: > "$TMP/changed" +cmp -s "$MANIFEST" "$TMP/manifest.json" || printf '%s\n' "$MANIFEST_REL" >> "$TMP/changed" +cmp -s "$RECEIPTS" "$TMP/receipts.tsv" || printf '%s\n' "$RECEIPTS_REL" >> "$TMP/changed" +while IFS= read -r p; do + grep -qxF -- "$p" "$ALLOW" || refuse "$p is not in .github/post-pin-bump.paths" +done < "$TMP/changed" + +# 7. Install, self-audit the real tree. Any failure (a write that stops half way, or +# a failed self-audit) restores BOTH files, so the pair never describes different commits. +cp -p "$MANIFEST" "$TMP/manifest.orig"; cp -p "$RECEIPTS" "$TMP/receipts.orig" +restore() { + # Attempt BOTH restores independently: a failed first one must not skip the second. + local failed=0 keep + cat "$TMP/manifest.orig" > "$MANIFEST" || failed=1 + cat "$TMP/receipts.orig" > "$RECEIPTS" || failed=1 + [[ "$failed" -eq 1 ]] || return 0 + # $TMP is deleted on exit, so keep the originals somewhere that survives it. If + # that copy fails too, disarm the deletion and leave them in $TMP itself. + if keep="$(mktemp -d "${TMPDIR:-/tmp}/post-pin-bump-backup.XXXXXX")" && cp -p "$TMP/manifest.orig" "$TMP/receipts.orig" "$keep/"; then + refuse "cannot restore the closure files after a failed install; originals kept in $keep (restore from there or from git)" + fi + trap - EXIT + refuse "cannot restore the closure files after a failed install; originals kept in $TMP (copy to a separate backup dir failed; restore from there or from git)" +} +if ! { cat "$TMP/manifest.json" > "$MANIFEST" && cat "$TMP/receipts.tsv" > "$RECEIPTS"; }; then + restore; refuse 'writing the closure files failed (files restored)' +fi +if ! bash "$AUDIT" "${audit_args[@]}" > "$TMP/final.out" 2>&1; then + restore + refuse "self-audit failed after the rewrite (files restored): $(tr '\n' ' ' < "$TMP/final.out" | cut -c1-600)" +fi +while IFS= read -r p; do printf '%s\0' "$p"; done < "$TMP/changed" diff --git a/scripts/tests/post-pin-bump-test.sh b/scripts/tests/post-pin-bump-test.sh new file mode 100755 index 00000000..99562206 --- /dev/null +++ b/scripts/tests/post-pin-bump-test.sh @@ -0,0 +1,257 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: MPL-2.0 +# SPDX-FileCopyrightText: 2026 Jonathan D.A. Jewell +# Offline fixtures for scripts/post-pin-bump.sh: bare git repos stand in for the +# reusable-workflow source; no network, host configuration or credentials. +set -euo pipefail +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" +HOOK="$ROOT/scripts/post-pin-bump.sh" +WORK="$(mktemp -d "${TMPDIR:-/tmp}/post-pin-bump-test.XXXXXX")" +trap 'rm -rf "$WORK"' EXIT +export GIT_CONFIG_NOSYSTEM=1 GIT_CONFIG_GLOBAL=/dev/null +PASS=0; FAIL=0 +for tool in git jq timeout; do command -v "$tool" >/dev/null || { echo "Missing test prerequisite: $tool" >&2; exit 64; }; done +[[ -f "$HOOK" ]] || { echo 'Missing hook; prerequisite failure, not a successful rejection.' >&2; exit 64; } + +commit_tree() { + printf '%s\n' "$3" | GIT_AUTHOR_DATE='2026-11-07T00:00:00Z' GIT_COMMITTER_DATE='2026-11-07T00:00:00Z' \ + git -c user.name='Hook fixture' -c user.email='fixture@example.invalid' -c commit.gpgsign=false --git-dir="$1" commit-tree "$2" +} +# source_object REPO TEXT: write TEXT at .github/workflows/reuse.yml; sets NEW_BLOB, NEW_COMMIT. +source_object() { + local repo="$1" text="$2" index tree + NEW_BLOB="$(printf '%s\n' "$text" | git --git-dir="$repo" hash-object -w --stdin)" + index="$(mktemp "$WORK/index.XXXXXX")"; rm "$index" + GIT_INDEX_FILE="$index" git --git-dir="$repo" read-tree --empty + GIT_INDEX_FILE="$index" git --git-dir="$repo" update-index --add --cacheinfo "100644,$NEW_BLOB,.github/workflows/reuse.yml" + tree="$(GIT_INDEX_FILE="$index" git --git-dir="$repo" write-tree)" + NEW_COMMIT="$(commit_tree "$repo" "$tree" "Fixture $RANDOM$RANDOM")" + rm "$index" +} + +# source_object2 REPO TEXT: TEXT at BOTH reuse.yml and reuse2.yml; sets NEW_COMMIT. +source_object2() { + local repo="$1" text="$2" index tree blob + blob="$(printf '%s\n' "$text" | git --git-dir="$repo" hash-object -w --stdin)" + index="$(mktemp "$WORK/index.XXXXXX")"; rm "$index" + GIT_INDEX_FILE="$index" git --git-dir="$repo" read-tree --empty + GIT_INDEX_FILE="$index" git --git-dir="$repo" update-index --add --cacheinfo "100644,$blob,.github/workflows/reuse.yml" + GIT_INDEX_FILE="$index" git --git-dir="$repo" update-index --add --cacheinfo "100644,$blob,.github/workflows/reuse2.yml" + tree="$(GIT_INDEX_FILE="$index" git --git-dir="$repo" write-tree)" + NEW_COMMIT="$(commit_tree "$repo" "$tree" "Fixture2 $RANDOM$RANDOM")" + rm "$index" +} + +BASE="$WORK/base"; mkdir -p "$BASE/cache/org" "$BASE/repo/.github/workflows" "$BASE/repo/scripts/lib" +SRC="$BASE/cache/org/reusable.git"; git init -q --bare "$SRC" +git init -q --bare "$BASE/cache/org/child.git" +CHILD_SHA="$(commit_tree "$BASE/cache/org/child.git" "$(git --git-dir="$BASE/cache/org/child.git" mktree "$BASE/repo/.github/workflows/test.yml" < "$BASE/repo/.github/workflows/actions.lock" +CONTENT="$(git --git-dir="$SRC" cat-file blob "$BLOB" | sha256sum | cut -d' ' -f1)" +jq -n --arg sha "$OLD" --arg blob "$BLOB" --arg cs "$CONTENT" --arg child "$CHILD_SHA" --arg key "$CHILD_KEY" ' + {version:1,sources:[{repository:"org/reusable",commit:$sha,path:".github/workflows/reuse.yml",blob:$blob,content_sha256:$cs, + role:"active",callers:[{workflow:".github/workflows/test.yml",job:"gate"}],uses:[{kind:"step",ref:$key}]}], + dependencies:{}}' > "$BASE/repo/scripts/lib/pinned-reusable-sources.json" +printf '# SPDX-License-Identifier: MPL-2.0\n# Reviewed source: org/reusable/.github/workflows/reuse.yml@%s\norg/reusable\t%s\t%s\n' "$OLD" "$OLD" "$CHILD_KEY" > "$BASE/repo/scripts/lib/lock-closure-receipts.tsv" +printf 'scripts/lib/pinned-reusable-sources.json\nscripts/lib/lock-closure-receipts.tsv\n' > "$BASE/repo/.github/post-pin-bump.paths" +mkdir -p "$BASE/repo/scripts"; cp "$ROOT"/scripts/audit-pinned-reusable-sources.sh "$ROOT"/scripts/audit-lock-closure.sh "$BASE/repo/scripts/" +[[ -d "$ROOT/scripts/lib" ]] && for f in "$ROOT"/scripts/lib/*; do [[ -f "$BASE/repo/scripts/lib/$(basename "$f")" ]] || cp "$f" "$BASE/repo/scripts/lib/"; done +cp "$ROOT/scripts/post-pin-bump.sh" "$BASE/repo/scripts/" +git init -q "$BASE/repo" +fix() { CASE="$WORK/$1"; mkdir "$CASE"; cp -a "$BASE/." "$CASE/"; R="$CASE/repo"; } +bump() { sed -i "s/@$OLD/@$1/" "$R/.github/workflows/test.yml"; } +run() { rc=0; (cd "$R" && timeout 30s bash scripts/post-pin-bump.sh --repo-root "$R" --cache "$CASE/cache" > "$CASE/out" 2> "$CASE/err") || rc=$?; } +ok() { PASS=$((PASS+1)); printf 'PASS: %s\n' "$1"; } +bad() { FAIL=$((FAIL+1)); printf 'FAIL: %s\n' "$1"; cat "$CASE/err" 2>/dev/null || true; } +unchanged() { cmp -s "$BASE/repo/scripts/lib/pinned-reusable-sources.json" "$R/scripts/lib/pinned-reusable-sources.json" && cmp -s "$BASE/repo/scripts/lib/lock-closure-receipts.tsv" "$R/scripts/lib/lock-closure-receipts.tsv"; } + +# fixg NAME: a fixture where two active sources (reuse.yml, reuse2.yml) share OLD2, with +# provenance fields that name it. Everything is derived from the BASE fixture. +fixg() { + fix "$1" + cat > "$R/.github/workflows/test.yml" < "$CASE/g.json" + cp "$CASE/g.json" "$R/scripts/lib/pinned-reusable-sources.json" + printf '# SPDX-License-Identifier: MPL-2.0\n# Reviewed source: org/reusable/.github/workflows/reuse.yml@%s\n# Reviewed source: org/reusable/.github/workflows/reuse2.yml@%s\norg/reusable\t%s\t%s\n' "$OLD2" "$OLD2" "$OLD2" "$CHILD_KEY" > "$R/scripts/lib/lock-closure-receipts.tsv" + cp "$R/scripts/lib/pinned-reusable-sources.json" "$CASE/pre.json"; cp "$R/scripts/lib/lock-closure-receipts.tsv" "$CASE/pre.tsv" +} +gbump() { sed -i "/^ $1:/{n;s/@$OLD2/@$2/}" "$R/.github/workflows/test.yml"; } +gunchanged() { cmp -s "$CASE/pre.json" "$R/scripts/lib/pinned-reusable-sources.json" && cmp -s "$CASE/pre.tsv" "$R/scripts/lib/lock-closure-receipts.tsv"; } + +fix noop; run +if [[ $rc == 0 && ! -s "$CASE/out" ]] && unchanged; then ok 'no pin moved: exit 0, no output, no change'; else bad 'no pin moved'; fi + +fix pin-only; bump "$SAME"; run +paths="$(tr '\0' '\n' < "$CASE/out")" +if [[ $rc == 0 && "$paths" == $'scripts/lib/pinned-reusable-sources.json\nscripts/lib/lock-closure-receipts.tsv' ]] && + jq -e --arg s "$SAME" '.sources[0].commit==$s' "$R/scripts/lib/pinned-reusable-sources.json" >/dev/null && + grep -q "reuse.yml@$SAME" "$R/scripts/lib/lock-closure-receipts.tsv" && grep -q "^org/reusable $SAME " "$R/scripts/lib/lock-closure-receipts.tsv" && + ! grep -q "$OLD" "$R/scripts/lib/lock-closure-receipts.tsv" && + [[ "$(tr -cd '\0' < "$CASE/out" | wc -c)" == 2 ]]; then ok 'pin-only bump: both files re-attested, paths NUL-separated'; else bad 'pin-only bump'; fi + +fix idempotent; bump "$SAME"; run; cp -a "$R/scripts/lib" "$CASE/after"; run +if [[ $rc == 0 && ! -s "$CASE/out" ]] && diff -r "$CASE/after" "$R/scripts/lib" >/dev/null; then ok 'second run is a no-op (idempotent)'; else bad 'idempotent'; fi + +for pair in "changed-blob:$CHANGED" "changed-edges:$EDGES"; do + fix "${pair%%:*}"; bump "${pair#*:}"; run + if [[ $rc == 1 && ! -s "$CASE/out" ]] && unchanged && grep -q REFUSED "$CASE/err"; then ok "refuses ${pair%%:*}; nothing written"; else bad "${pair%%:*} must be refused"; fi +done + +fix other-repo; sed -i "s#org/reusable/#org/elsewhere/#; s#@$OLD#@$SAME#" "$R/.github/workflows/test.yml"; run +if [[ $rc == 1 ]] && unchanged; then ok 'refuses a changed repository (not pin-only)'; else bad 'changed repository'; fi + +fix unreachable-source; bump "$SAME"; rm -rf "$CASE/cache/org/reusable.git"; run +if [[ $rc == 1 && ! -s "$CASE/out" ]] && unchanged; then ok 'refuses (not guesses) when the new source is unavailable offline'; else bad 'unavailable source'; fi + +fix shared-commit; bump "$SAME" +jq '.sources += [(.sources[0] | .role="retained-review" | .callers=[] | .path=".github/workflows/retained.yml")]' "$R/scripts/lib/pinned-reusable-sources.json" > "$CASE/m.json" +jq '.sources[1].path=".github/workflows/retained.yml" | .sources[1].repository="org/reusable"' "$CASE/m.json" > "$R/scripts/lib/pinned-reusable-sources.json" +cp "$R/scripts/lib/pinned-reusable-sources.json" "$CASE/pre.json"; run +if [[ $rc == 1 ]] && cmp -s "$CASE/pre.json" "$R/scripts/lib/pinned-reusable-sources.json" && grep -q "is shared with" "$CASE/err"; then ok 'refuses a commit shared with a source that did not move'; else bad 'shared commit'; fi + +fix allow-list; bump "$SAME"; printf 'scripts/lib/pinned-reusable-sources.json\n' > "$R/.github/post-pin-bump.paths"; run +if [[ $rc == 1 ]] && unchanged; then ok 'refuses to change a path outside the allow-list'; else bad 'allow-list'; fi + +# Fetch policy: POST_PIN_BUMP=1 (no arguments) must make the hook fetch; without it, offline. +fix fetch-policy; bump "$SAME" +mv "$R/scripts/audit-pinned-reusable-sources.sh" "$R/scripts/audit-real.sh" +printf '#!/usr/bin/env bash\nprintf "%%s\\n" "$*" >> "%s/args"\nexec bash "$(dirname "$0")/audit-real.sh" "$@"\n' "$CASE" > "$R/scripts/audit-pinned-reusable-sources.sh" +rc=0; (cd "$R" && POST_PIN_BUMP=1 timeout 30s bash scripts/post-pin-bump.sh --repo-root "$R" --cache "$CASE/cache" > "$CASE/out" 2> "$CASE/err") || rc=$? +if [[ $rc == 0 ]] && grep -q -- '--fetch-sources' "$CASE/args" && ! grep -v -- '--roots-only' "$CASE/args" | grep -qv -- '--fetch-sources'; then ok 'POST_PIN_BUMP=1 turns fetching on for every source audit call'; else bad 'POST_PIN_BUMP=1 did not enable fetching'; fi +fix fetch-offline; bump "$SAME" +mv "$R/scripts/audit-pinned-reusable-sources.sh" "$R/scripts/audit-real.sh" +printf '#!/usr/bin/env bash\nprintf "%%s\\n" "$*" >> "%s/args"\nexec bash "$(dirname "$0")/audit-real.sh" "$@"\n' "$CASE" > "$R/scripts/audit-pinned-reusable-sources.sh" +rc=0; (cd "$R" && env -u POST_PIN_BUMP timeout 30s bash scripts/post-pin-bump.sh --repo-root "$R" --cache "$CASE/cache" > "$CASE/out" 2> "$CASE/err") || rc=$? +if [[ $rc == 0 && -s "$CASE/args" ]] && ! grep -q -- '--fetch-sources' "$CASE/args"; then ok 'without POST_PIN_BUMP the hook stays offline'; else bad 'default is not offline'; fi + +# Review 1: sources sharing an old commit that all move to the same new commit. +fixg group-baseline; run +if [[ $rc == 0 && ! -s "$CASE/out" ]] && gunchanged; then ok 'group fixture: audited clean with nothing moved (baseline is valid)'; else bad 'group baseline'; fi +fixg group-move; gbump gate "$SAME2"; gbump gate2 "$SAME2"; run +if [[ $rc == 0 ]] && jq -e --arg s "$SAME2" 'all(.sources[]; .commit==$s)' "$R/scripts/lib/pinned-reusable-sources.json" >/dev/null && + [[ "$(grep -c '^org/reusable ' "$R/scripts/lib/lock-closure-receipts.tsv")" == 1 ]] && + grep -q "^org/reusable $SAME2 " "$R/scripts/lib/lock-closure-receipts.tsv" && + grep -q "reuse.yml@$SAME2" "$R/scripts/lib/lock-closure-receipts.tsv" && grep -q "reuse2.yml@$SAME2" "$R/scripts/lib/lock-closure-receipts.tsv" && + ! grep -q "$OLD2" "$R/scripts/lib/lock-closure-receipts.tsv"; then ok 'sources sharing a commit that move together: both re-pointed, receipt union rewritten once'; else bad 'group move'; fi +fixg group-stays; gbump gate "$SAME2"; run +if [[ $rc == 1 ]] && gunchanged && grep -q 'is shared with 1 other' "$CASE/err"; then ok 'refuses when one sharer stays put (shared-source guard)'; else bad 'sharer staying put must be refused'; fi +fixg group-conflict; gbump gate "$SAME2"; gbump gate2 "$SAME3"; run +if [[ $rc == 1 ]] && gunchanged && grep -q 'conflicting commits' "$CASE/err"; then ok 'refuses sharers moving to conflicting targets'; else bad 'conflicting targets must be refused'; fi + +# Review 2: provenance fields follow the pin; any other mention of the old commit is refused. +if jq -e --arg s "$SAME2" 'all(.sources[]; (.source_url|contains($s)) and (.local_file|contains($s)))' "$CASE/../group-move/repo/scripts/lib/pinned-reusable-sources.json" >/dev/null && + ! grep -q "$OLD2" "$CASE/../group-move/repo/scripts/lib/pinned-reusable-sources.json"; then ok 'source_url and local_file are updated to the new commit'; else bad 'provenance fields not updated'; fi +fixg stray-ref; gbump gate "$SAME2"; gbump gate2 "$SAME2" +jq --arg o "$OLD2" '.sources[0].note="reviewed at \($o)"' "$R/scripts/lib/pinned-reusable-sources.json" > "$CASE/n.json"; cp "$CASE/n.json" "$R/scripts/lib/pinned-reusable-sources.json" +cp "$R/scripts/lib/pinned-reusable-sources.json" "$CASE/pre.json"; run +if [[ $rc == 1 ]] && gunchanged && grep -q 'manifest still names' "$CASE/err"; then ok 'still refuses an unrecognised old-commit reference'; else bad 'stray old-commit reference must be refused'; fi + +# Review 3: a failed receipts write rolls the manifest back too. A cat shim fails only +# when installing the receipts, after the manifest has already been written. +mkdir -p "$WORK/shim" +# shellcheck disable=SC2016 # the shim body is written literally +printf '#!/usr/bin/env bash\n[[ -n "${SHIM_FAIL:-}" && "$*" == */receipts.tsv ]] && exit 1\nexec /bin/cat "$@"\n' > "$WORK/shim/cat"; chmod +x "$WORK/shim/cat" +fix rollback; bump "$SAME"; rc=0 +(cd "$R" && SHIM_FAIL=1 PATH="$WORK/shim:$PATH" timeout 30s bash scripts/post-pin-bump.sh --repo-root "$R" --cache "$CASE/cache" > "$CASE/out" 2> "$CASE/err") || rc=$? +if [[ $rc == 1 && ! -s "$CASE/out" ]] && unchanged && grep -q 'writing the closure files failed' "$CASE/err"; then ok 'receipts write failure after the manifest write: both files rolled back'; else bad 'rollback after a failed receipts write'; fi +fix rollback-off; bump "$SAME"; rc=0 +(cd "$R" && PATH="$WORK/shim:$PATH" timeout 30s bash scripts/post-pin-bump.sh --repo-root "$R" --cache "$CASE/cache" > "$CASE/out" 2> "$CASE/err") || rc=$? +if [[ $rc == 0 ]] && ! unchanged && jq -e --arg s "$SAME" '.sources[0].commit==$s' "$R/scripts/lib/pinned-reusable-sources.json" >/dev/null; then ok 'same shim with no failure: the install is kept (no spurious rollback)'; else bad 'install without failure'; fi + +# Review 3b: if the manifest restore itself fails, the receipts restore must still be +# attempted and the originals must survive in a kept directory named in the refusal. +mkdir -p "$WORK/shim2" +# shellcheck disable=SC2016 # the shim body is written literally +printf '#!/usr/bin/env bash\nprintf "%%s\\n" "$*" >> "$SHIM_LOG"\n[[ "$*" == */receipts.tsv ]] && exit 1\n[[ "$*" == */manifest.orig ]] && exit 1\nexec /bin/cat "$@"\n' > "$WORK/shim2/cat"; chmod +x "$WORK/shim2/cat" +fix restore-fails; bump "$SAME"; rc=0; : > "$CASE/shim.log"; mkdir "$CASE/tmpd" +(cd "$R" && TMPDIR="$CASE/tmpd" SHIM_LOG="$CASE/shim.log" PATH="$WORK/shim2:$PATH" timeout 30s bash scripts/post-pin-bump.sh --repo-root "$R" --cache "$CASE/cache" > "$CASE/out" 2> "$CASE/err") || rc=$? +keep="$(sed -n 's/.*originals kept in \(.*\) (restore from.*/\1/p' "$CASE/err")" +if [[ $rc == 1 ]] && grep -q 'receipts.orig' "$CASE/shim.log" && [[ -n "$keep" && "$keep" == "$CASE/tmpd/post-pin-bump-backup."* ]] && + cmp -s "$keep/manifest.orig" "$BASE/repo/scripts/lib/pinned-reusable-sources.json" && cmp -s "$keep/receipts.orig" "$BASE/repo/scripts/lib/lock-closure-receipts.tsv"; then + ok 'a failed manifest restore still attempts the receipts restore and keeps the originals'; else bad 'restore failure handling'; fi + +# Review 3c: both restores fail AND the backup directory cannot be made: the originals +# must still survive, in $TMP (whose deletion is disarmed), and the refusal must name it. +mkdir -p "$WORK/shim3"; REAL_MKTEMP="$(command -v mktemp)" +# shellcheck disable=SC2016 # the shim bodies are written literally +printf '#!/usr/bin/env bash\n[[ "$*" == */receipts.tsv || "$*" == */manifest.orig || "$*" == */receipts.orig ]] && exit 1\nexec /bin/cat "$@"\n' > "$WORK/shim3/cat" +# shellcheck disable=SC2016 +printf '#!/usr/bin/env bash\n[[ "$*" == *post-pin-bump-backup* ]] && exit 1\nexec %s "$@"\n' "$REAL_MKTEMP" > "$WORK/shim3/mktemp"; chmod +x "$WORK/shim3/cat" "$WORK/shim3/mktemp" +fix restore-and-backup-fail; bump "$SAME"; rc=0; mkdir "$CASE/tmpd" +(cd "$R" && TMPDIR="$CASE/tmpd" PATH="$WORK/shim3:$PATH" timeout 30s bash scripts/post-pin-bump.sh --repo-root "$R" --cache "$CASE/cache" > "$CASE/out" 2> "$CASE/err") || rc=$? +keep="$(sed -n 's/.*originals kept in \(.*\) (copy to a separate.*/\1/p' "$CASE/err")" +if [[ $rc == 1 && -n "$keep" && -d "$keep" ]] && cmp -s "$keep/manifest.orig" "$BASE/repo/scripts/lib/pinned-reusable-sources.json" && cmp -s "$keep/receipts.orig" "$BASE/repo/scripts/lib/lock-closure-receipts.tsv"; then + ok 'restore and backup-dir creation both failing: originals survive in the named directory'; else bad 'originals lost when the backup copy fails'; fi + +# An old-commit mention that is only an object KEY (no string value names it) is refused too. +fix key-only-ref; bump "$SAME" +jq --arg o "$OLD" '.sources[0].review_note={($o):"x"}' "$R/scripts/lib/pinned-reusable-sources.json" > "$CASE/m.json" +cp "$CASE/m.json" "$R/scripts/lib/pinned-reusable-sources.json"; cp "$R/scripts/lib/pinned-reusable-sources.json" "$CASE/pre.json"; cp "$R/scripts/lib/lock-closure-receipts.tsv" "$CASE/pre.tsv"; run +if [[ $rc == 1 ]] && gunchanged && grep -q 'manifest still names' "$CASE/err"; then ok 'refuses an old commit that survives only as an object key'; else bad 'key-only old-commit reference'; fi + +# Chained plans: A->B with B->C refuses identically whichever order the sources are listed in. +fixc() { # NAME REVERSE: reuse.yml at OLD2 moving to SAME2, reuse2.yml at SAME2 moving to THIRD2 + fix "$1" + cat > "$R/.github/workflows/test.yml" < "$CASE/c.json" + cp "$CASE/c.json" "$R/scripts/lib/pinned-reusable-sources.json" + cp "$R/scripts/lib/pinned-reusable-sources.json" "$CASE/pre.json"; cp "$R/scripts/lib/lock-closure-receipts.tsv" "$CASE/pre.tsv" +} +for rev in false true; do + fixc "chain-$rev" "$rev"; run + if [[ $rc == 1 && ! -s "$CASE/out" ]] && gunchanged && grep -q 'chained or overlapping pin moves for org/reusable' "$CASE/err"; then ok "refuses chained moves A->B, B->C (sources reversed: $rev); files unchanged"; else bad "chained moves (reversed: $rev)"; fi +done + +# An actions.lock record still resolving to repo@old would lose its receipt: refused. +fix lock-still-old; bump "$SAME" +printf " 'org/reusable@%s':\n ref: 'v1'\n commit: 'sha1-%s'\n" "$OLD" "$OLD" >> "$R/.github/workflows/actions.lock" +cp "$R/scripts/lib/pinned-reusable-sources.json" "$CASE/pre.json"; cp "$R/scripts/lib/lock-closure-receipts.tsv" "$CASE/pre.tsv"; run +if [[ $rc == 1 && ! -s "$CASE/out" ]] && gunchanged && grep -q 'actions.lock still resolves org/reusable@' "$CASE/err"; then ok 'refuses when a lock record still resolves the old commit; files unchanged'; else bad 'lock record at the old commit'; fi + +# Mutant: if the hook skipped both audits it would accept the changed blob. +fix mutant; bump "$CHANGED"; sed -i 's/^if ! bash "\$AUDIT" "\${audit_args\[@\]}".*/if false; then/' "$R/scripts/post-pin-bump.sh"; run +if [[ $rc == 0 ]] && ! unchanged; then ok 'mutant (both audits disabled) accepts a changed blob, so the audit is what refuses'; else bad 'mutant did not demonstrate the audit is load-bearing'; fi + +printf '%s passed, %s failed\n' "$PASS" "$FAIL" +[[ "$FAIL" -eq 0 ]]