From 7c4cd4b0c9c6d52bccb2d8abe0b4595939299c11 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 26 Nov 2026 12:44:45 +0000 Subject: [PATCH 1/9] feat(closure): post-pin-bump hook that re-attests pin-only reusable-source bumps scripts/post-pin-bump.sh is the repo half of the single-pin-writer contract. After the Action pins applier moves a pinned reusable-workflow commit it re-points the source manifest and the closure receipts, but only when the workflow file at the new commit is byte-identical (same blob, content_sha256 and direct edges). Anything else is refused and nothing is written. New source is fetched as data by the existing audit; changed paths are printed NUL-separated; .github/post-pin-bump.paths allow-lists exactly two paths. Takes effect only once the applier side merges (standards): order is standards, then this change. Adds audit --roots-only and an offline test wired into e2e. --- .github/post-pin-bump.paths | 5 + .github/workflows/e2e.yml | 2 + scripts/audit-pinned-reusable-sources.sh | 14 +- scripts/post-pin-bump.sh | 157 +++++++++++++++++++++++ scripts/tests/post-pin-bump-test.sh | 110 ++++++++++++++++ 5 files changed, 286 insertions(+), 2 deletions(-) create mode 100644 .github/post-pin-bump.paths create mode 100755 scripts/post-pin-bump.sh create mode 100755 scripts/tests/post-pin-bump-test.sh 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 1c20f698..659d30ae 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/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..7072a165 --- /dev/null +++ b/scripts/post-pin-bump.sh @@ -0,0 +1,157 @@ +#!/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. +# +# 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}" +FETCH=0 +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" +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' + repo="$(jq -r --argjson i "$idx" '.sources[$i].repository' "$TMP/manifest.json")" + path="$(jq -r --argjson i "$idx" '.sources[$i].path' "$TMP/manifest.json")" + # Receipt rows and the comment cover every source of repo@old: a commit shared + # with a source that did not move cannot be re-pointed by guessing. + shared="$(jq -r --arg r "$repo" --arg c "$old" --argjson i "$idx" \ + '[.sources|to_entries[]|select(.key!=$i and (.value.repository|ascii_downcase)==($r|ascii_downcase) and .value.commit==$c)]|length' "$TMP/manifest.json")" + [[ "$shared" -eq 0 ]] || refuse "$repo@$old is shared with $shared other source(s) that did not move; review by hand" + jq --argjson i "$idx" --arg new "$new" '.sources[$i].commit=$new' "$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 (e.g. dependency keys) is + # something this hook did not review. + if jq -e --arg c "$old" '[.. | strings | 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 < <(grep '^BUMP' "$TMP/plan.tsv") + +# 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, restore on failure. +cp -p "$MANIFEST" "$TMP/manifest.orig"; cp -p "$RECEIPTS" "$TMP/receipts.orig" +cat "$TMP/manifest.json" > "$MANIFEST"; cat "$TMP/receipts.tsv" > "$RECEIPTS" +if ! bash "$AUDIT" "${audit_args[@]}" > "$TMP/final.out" 2>&1; then + cat "$TMP/manifest.orig" > "$MANIFEST"; cat "$TMP/receipts.orig" > "$RECEIPTS" + 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..dbf9f088 --- /dev/null +++ b/scripts/tests/post-pin-bump-test.sh @@ -0,0 +1,110 @@ +#!/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" +} + +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"; } + +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/reuse.yml")]' "$R/scripts/lib/pinned-reusable-sources.json" > "$CASE/m.json" +jq '.sources[1].path=".github/workflows/reuse.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"; 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 + +# 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 ]] From 37e52ba4e1814f0347ecde9d219a0dfd53b8f476 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 26 Nov 2026 13:22:15 +0000 Subject: [PATCH 2/9] fix(closure): post-pin-bump fetches sources when POST_PIN_BUMP=1 The applier runs the hook with no arguments and POST_PIN_BUMP=1, per the owner ruling for #717. The variable now enables source fetching; without it (or --fetch-sources) the hook stays offline. Test covers both. --- scripts/post-pin-bump.sh | 6 ++++++ scripts/tests/post-pin-bump-test.sh | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/scripts/post-pin-bump.sh b/scripts/post-pin-bump.sh index 7072a165..1fdacb36 100755 --- a/scripts/post-pin-bump.sh +++ b/scripts/post-pin-bump.sh @@ -25,13 +25,19 @@ # 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 ;; diff --git a/scripts/tests/post-pin-bump-test.sh b/scripts/tests/post-pin-bump-test.sh index dbf9f088..0896b7ef 100755 --- a/scripts/tests/post-pin-bump-test.sh +++ b/scripts/tests/post-pin-bump-test.sh @@ -102,6 +102,18 @@ if [[ $rc == 1 ]] && cmp -s "$CASE/pre.json" "$R/scripts/lib/pinned-reusable-sou 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 + # 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 From 0b22f34d22d9e72a619d4c94af0d2bd4489d6f87 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 27 Nov 2026 01:02:10 +0000 Subject: [PATCH 3/9] chore(departures): prune applier-bump-closure-gap (scripts/post-pin-bump.sh lands here) The standards applier hook (part A) merged as metadatastician/standards#1711 (a34bb670). This PR adds scripts/post-pin-bump.sh, so the row's path-absent check no longer holds; pruned with departures.sh prune. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0136P7ZnWneAryckd4S2EFd3 --- .machine_readable/departures.jsonl | 1 - 1 file changed, 1 deletion(-) 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)."} From cda94b4ac647db77484730f6e785293a184ef400 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 27 Nov 2026 11:51:58 +0000 Subject: [PATCH 4/9] fix(post-pin-bump): group sources sharing a commit, update provenance, roll back both files - Sources that share an old commit and all move to the same new commit are one group: rewritten once; a sharer that stays put or a conflicting target is refused. - source_url and local_file follow the pin; any other old-commit mention is refused. - A failed receipts write after the manifest write restores both files. - Tests: distinct path and 'is shared with' assertion; positive and negative cases for each of the three behaviours. --- scripts/post-pin-bump.sh | 43 +++++++++++---- scripts/tests/post-pin-bump-test.sh | 81 +++++++++++++++++++++++++++-- 2 files changed, 111 insertions(+), 13 deletions(-) diff --git a/scripts/post-pin-bump.sh b/scripts/post-pin-bump.sh index 1fdacb36..06d907f9 100755 --- a/scripts/post-pin-bump.sh +++ b/scripts/post-pin-bump.sh @@ -93,16 +93,32 @@ 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' - repo="$(jq -r --argjson i "$idx" '.sources[$i].repository' "$TMP/manifest.json")" - path="$(jq -r --argjson i "$idx" '.sources[$i].path' "$TMP/manifest.json")" + 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") +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 that did not move cannot be re-pointed by guessing. - shared="$(jq -r --arg r "$repo" --arg c "$old" --argjson i "$idx" \ - '[.sources|to_entries[]|select(.key!=$i and (.value.repository|ascii_downcase)==($r|ascii_downcase) and .value.commit==$c)]|length' "$TMP/manifest.json")" + # 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' "$TMP/manifest.json")" [[ "$shared" -eq 0 ]] || refuse "$repo@$old is shared with $shared other source(s) that did not move; review by hand" - jq --argjson i "$idx" --arg new "$new" '.sources[$i].commit=$new' "$TMP/manifest.json" > "$TMP/manifest.next" + # 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). @@ -125,7 +141,7 @@ while IFS=$'\t' read -r _ idx old new; do if jq -e --arg c "$old" '[.. | strings | 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 < <(grep '^BUMP' "$TMP/plan.tsv") +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 @@ -153,11 +169,18 @@ 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, restore on failure. +# 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" -cat "$TMP/manifest.json" > "$MANIFEST"; cat "$TMP/receipts.tsv" > "$RECEIPTS" +restore() { + if cat "$TMP/manifest.orig" > "$MANIFEST" && cat "$TMP/receipts.orig" > "$RECEIPTS"; then return 0; fi + refuse 'cannot restore the closure files after a failed install; restore them 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 - cat "$TMP/manifest.orig" > "$MANIFEST"; cat "$TMP/receipts.orig" > "$RECEIPTS" + 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 index 0896b7ef..c3c76fda 100755 --- a/scripts/tests/post-pin-bump-test.sh +++ b/scripts/tests/post-pin-bump-test.sh @@ -29,6 +29,19 @@ source_object() { 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" @@ -41,6 +54,10 @@ source_object "$SRC" "$TEXT"; SAME="$NEW_COMMIT" # Same text means the same blob; a distinct commit comes from the unique message. source_object "$SRC" "$TEXT"$'\n# changed'; CHANGED="$NEW_COMMIT" source_object "$SRC" "${TEXT/org\/child@$CHILD_SHA/org\/other@$CHILD_SHA}"; EDGES="$NEW_COMMIT" +# Two files in one commit: OLD2 is shared by two sources; SAME2 and SAME3 are pin-only successors. +source_object2 "$SRC" "$TEXT"; OLD2="$NEW_COMMIT" +source_object2 "$SRC" "$TEXT"; SAME2="$NEW_COMMIT" +source_object2 "$SRC" "$TEXT"; SAME3="$NEW_COMMIT" cat > "$BASE/repo/.github/workflows/test.yml" </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 @@ -94,10 +135,10 @@ 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/reuse.yml")]' "$R/scripts/lib/pinned-reusable-sources.json" > "$CASE/m.json" -jq '.sources[1].path=".github/workflows/reuse.yml" | .sources[1].repository="org/reusable"' "$CASE/m.json" > "$R/scripts/lib/pinned-reusable-sources.json" +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"; then ok 'refuses a commit shared with a source that did not move'; else bad 'shared commit'; fi +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 @@ -114,6 +155,40 @@ printf '#!/usr/bin/env bash\nprintf "%%s\\n" "$*" >> "%s/args"\nexec bash "$(dir 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 + # 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 From 54108f8cc194759de076ceabb09b5f8f2d4ecaba Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 27 Nov 2026 11:52:21 +0000 Subject: [PATCH 5/9] fix(post-pin-bump): evaluate the whole bump plan, refresh provenance, roll back on install failure - Sources moving from one old commit to the same new commit are re-attested together (receipts rewritten once); stationary sharers and conflicting targets are still refused. - source_url and local_file are refreshed to the new commit; other mentions of the old commit are still refused. - Any failure after the first write restores both the manifest and the receipts before cleanup. - Tests: distinct-path shared-commit fixture asserting 'is shared with', joint move, provenance, unresolved reference, rollback on receipts write failure. --- scripts/post-pin-bump.sh | 62 ++++++++++++++++++++--------- scripts/tests/post-pin-bump-test.sh | 60 ++++++++++++++++++++++++++-- 2 files changed, 101 insertions(+), 21 deletions(-) diff --git a/scripts/post-pin-bump.sh b/scripts/post-pin-bump.sh index 1fdacb36..08f39427 100755 --- a/scripts/post-pin-bump.sh +++ b/scripts/post-pin-bump.sh @@ -90,23 +90,42 @@ jq -rn --slurpfile roots "$TMP/roots.json" --slurpfile m "$MANIFEST" ' 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. +# 3. Evaluate the WHOLE bump plan, grouped by repo@old. Sources that move from +# the same old commit to the same new commit are re-pointed together and their +# receipt rows rewritten once. Refused: a source still at that old commit that +# did not move (stationary sharer), sources of one old commit moving to +# different commits (conflicting targets), and a new commit that is another +# group's old commit (a chain). +jq -rn --slurpfile m "$MANIFEST" --rawfile plan "$TMP/plan.tsv" ' + $m[0].sources as $src | + [ $plan | split("\n")[] | select(startswith("BUMP\t")) | split("\t") | + {i:(.[1]|tonumber), old:.[2], new:.[3], repo:($src[.[1]|tonumber].repository|ascii_downcase)} ] as $rows | + ($rows | group_by([.repo, .old])[]) as $g | $g[0] as $f | + ($g | map(.new) | unique) as $news | + ([$src[] | select((.repository|ascii_downcase)==$f.repo and .commit==$f.old)] | length) as $all | + if ($news|length) != 1 then "ERR\t\($f.repo)@\($f.old) moves to \($news|length) different commits (\($news|join(","))); review by hand" + elif $all != ($g|length) then "ERR\t\($f.repo)@\($f.old) is shared with \($all - ($g|length)) other source(s) that did not move; review by hand" + elif any($rows[]; .repo==$f.repo and .old==$news[0]) then "ERR\t\($f.repo)@\($news[0]) is both a new and an old commit in this bump; review by hand" + else "GROUP\t\($f.repo)\t\($f.old)\t\($news[0])\t\($g|map(.i|tostring)|join(","))" end' > "$TMP/groups.tsv" +if grep -q '^ERR' "$TMP/groups.tsv"; then refuse "$(grep '^ERR' "$TMP/groups.tsv" | cut -f2- | tr '\n' ';')"; fi + +# Rewrite into temporary copies, never into the tree. cp "$MANIFEST" "$TMP/manifest.json" cp "$RECEIPTS" "$TMP/receipts.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' - repo="$(jq -r --argjson i "$idx" '.sources[$i].repository' "$TMP/manifest.json")" - path="$(jq -r --argjson i "$idx" '.sources[$i].path' "$TMP/manifest.json")" - # Receipt rows and the comment cover every source of repo@old: a commit shared - # with a source that did not move cannot be re-pointed by guessing. - shared="$(jq -r --arg r "$repo" --arg c "$old" --argjson i "$idx" \ - '[.sources|to_entries[]|select(.key!=$i and (.value.repository|ascii_downcase)==($r|ascii_downcase) and .value.commit==$c)]|length' "$TMP/manifest.json")" - [[ "$shared" -eq 0 ]] || refuse "$repo@$old is shared with $shared other source(s) that did not move; review by hand" - jq --argjson i "$idx" --arg new "$new" '.sources[$i].commit=$new' "$TMP/manifest.json" > "$TMP/manifest.next" +while IFS=$'\t' read -r _ repo old new idxs; do + [[ "$idxs" =~ ^[0-9]+(,[0-9]+)*$ && "$old" =~ ^[0-9a-f]{40}$ && "$new" =~ ^[0-9a-f]{40}$ ]] || refuse 'malformed plan row' + # New commit, plus the provenance fields that name the old commit (source_url, + # local_file) refreshed by literal substitution. Any other mention is caught by + # the residual check below. + jq --argjson idx "[$idxs]" --arg old "$old" --arg new "$new" ' + reduce $idx[] as $i (.; .sources[$i].commit = $new + | reduce ("source_url", "local_file") as $k (.; + if (.sources[$i][$k]|type) == "string" then .sources[$i][$k] |= (split($old) | join($new)) 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" ' + # literal matching only (no regex on data). Once per repo@old group. + 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 "/") @@ -121,11 +140,11 @@ while IFS=$'\t' read -r _ idx old new; do 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 (e.g. dependency keys) is - # something this hook did not review. + # an unresolved reference this hook did not review. if jq -e --arg c "$old" '[.. | strings | 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 < <(grep '^BUMP' "$TMP/plan.tsv") +done < <(grep '^GROUP' "$TMP/groups.tsv") # 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 @@ -153,11 +172,18 @@ 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, restore on failure. +# 7. Install and self-audit the real tree. Any failure from the first write +# onward (a write error, the audit, a signal) restores BOTH originals before +# cleanup; the new pair is kept only once the self-audit has passed. cp -p "$MANIFEST" "$TMP/manifest.orig"; cp -p "$RECEIPTS" "$TMP/receipts.orig" -cat "$TMP/manifest.json" > "$MANIFEST"; cat "$TMP/receipts.tsv" > "$RECEIPTS" +restore_originals() { cat "$TMP/manifest.orig" > "$MANIFEST" && cat "$TMP/receipts.orig" > "$RECEIPTS"; } +INSTALLING=1 +trap 'if [[ "${INSTALLING:-0}" -eq 1 ]]; then restore_originals || printf "post-pin-bump: FAILED to restore originals; git checkout %s %s\n" "$MANIFEST_REL" "$RECEIPTS_REL" >&2; fi; rm -rf "$TMP"' EXIT +if ! { cat "$TMP/manifest.json" > "$MANIFEST" && cat "$TMP/receipts.tsv" > "$RECEIPTS"; }; then + refuse 'could not write the rewritten files (originals restored)' +fi if ! bash "$AUDIT" "${audit_args[@]}" > "$TMP/final.out" 2>&1; then - cat "$TMP/manifest.orig" > "$MANIFEST"; cat "$TMP/receipts.orig" > "$RECEIPTS" refuse "self-audit failed after the rewrite (files restored): $(tr '\n' ' ' < "$TMP/final.out" | cut -c1-600)" fi +INSTALLING=0 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 index 0896b7ef..8a5813c2 100755 --- a/scripts/tests/post-pin-bump-test.sh +++ b/scripts/tests/post-pin-bump-test.sh @@ -28,6 +28,18 @@ source_object() { NEW_COMMIT="$(commit_tree "$repo" "$tree" "Fixture $RANDOM$RANDOM")" rm "$index" } +# source_object2 REPO TEXT: like source_object, but the same blob is also written at reuse2.yml. +source_object2() { + 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" + GIT_INDEX_FILE="$index" git --git-dir="$repo" update-index --add --cacheinfo "100644,$NEW_BLOB,.github/workflows/reuse2.yml" + tree="$(GIT_INDEX_FILE="$index" git --git-dir="$repo" write-tree)" + NEW_COMMIT="$(commit_tree "$repo" "$tree" "Fixture $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" @@ -41,6 +53,7 @@ source_object "$SRC" "$TEXT"; SAME="$NEW_COMMIT" # Same text means the same blob; a distinct commit comes from the unique message. source_object "$SRC" "$TEXT"$'\n# changed'; CHANGED="$NEW_COMMIT" source_object "$SRC" "${TEXT/org\/child@$CHILD_SHA/org\/other@$CHILD_SHA}"; EDGES="$NEW_COMMIT" +source_object2 "$SRC" "$TEXT"; OLD2="$NEW_COMMIT"; source_object2 "$SRC" "$TEXT"; SAME2="$NEW_COMMIT" cat > "$BASE/repo/.github/workflows/test.yml" < "$CASE/m.json" -jq '.sources[1].path=".github/workflows/reuse.yml" | .sources[1].repository="org/reusable"' "$CASE/m.json" > "$R/scripts/lib/pinned-reusable-sources.json" +jq '.sources += [(.sources[0] | .role="retained-review" | .callers=[] | .path=".github/workflows/other.yml")]' "$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"; run -if [[ $rc == 1 ]] && cmp -s "$CASE/pre.json" "$R/scripts/lib/pinned-reusable-sources.json"; then ok 'refuses a commit shared with a source that did not move'; else bad 'shared commit'; fi +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 + +# Provenance: source_url and local_file name the old commit and must follow a pin-only bump. +fix provenance; bump "$SAME" +jq --arg o "$OLD" '.sources[0].source_url="https://github.com/org/reusable/blob/"+$o+"/.github/workflows/reuse.yml" | .sources[0].local_file="C:\\src\\"+$o+"\\reuse.yml"' "$R/scripts/lib/pinned-reusable-sources.json" > "$CASE/m.json" +cp "$CASE/m.json" "$R/scripts/lib/pinned-reusable-sources.json"; run +if [[ $rc == 0 ]] && jq -e --arg s "$SAME" '.sources[0] | .commit==$s and (.source_url|contains($s)) and (.local_file|contains($s))' "$R/scripts/lib/pinned-reusable-sources.json" >/dev/null && + ! grep -q "$OLD" "$R/scripts/lib/pinned-reusable-sources.json"; then ok 'pin-only bump refreshes source_url and local_file provenance'; else bad 'provenance refresh'; fi + +fix dep-ref; bump "$SAME" +jq --arg o "$OLD" '.sources[0].review_note="reviewed at "+$o' "$R/scripts/lib/pinned-reusable-sources.json" > "$CASE/m.json" +cp "$CASE/m.json" "$R/scripts/lib/pinned-reusable-sources.json"; cp "$CASE/m.json" "$CASE/pre.json"; run +if [[ $rc == 1 ]] && cmp -s "$CASE/pre.json" "$R/scripts/lib/pinned-reusable-sources.json" && grep -q 'dependency records' "$CASE/err"; then ok 'still refuses an unresolved dependency reference to the old commit'; else bad 'dependency reference'; fi + +# Joint move: two sources at the same old commit moving to the same new commit. +joint() { # sets up the fixture with a second active source (reuse2.yml) at $OLD2 + fix "$1" + sed -i "s/@$OLD/@$OLD2/" "$R/.github/workflows/test.yml" + sed 's/test.yml/test2.yml/; s/gate:/gate2:/; s#reuse.yml#reuse2.yml#' "$R/.github/workflows/test.yml" > "$R/.github/workflows/test2.yml" + sed -i "s# '.github/workflows/test.yml':#&\n '.github/workflows/test2.yml':#" "$R/.github/workflows/actions.lock" + jq --arg o "$OLD2" '.sources[0].commit=$o | .sources += [(.sources[0] | .path=".github/workflows/reuse2.yml" | .callers=[{workflow:".github/workflows/test2.yml",job:"gate2"}])]' \ + "$BASE/repo/scripts/lib/pinned-reusable-sources.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 -a "$R/scripts/lib" "$CASE/lib0" +} +joint joint-move; sed -i "s/@$OLD2/@$SAME2/" "$R/.github/workflows/test.yml" "$R/.github/workflows/test2.yml"; run +if [[ $rc == 0 ]] && jq -e --arg s "$SAME2" '[.sources[].commit]|all(.==$s)' "$R/scripts/lib/pinned-reusable-sources.json" >/dev/null && + ! grep -q "$OLD2" "$R/scripts/lib/lock-closure-receipts.tsv" && [[ "$(grep -c "^org/reusable $SAME2 " "$R/scripts/lib/lock-closure-receipts.tsv")" == 1 ]] && + grep -q "reuse2.yml@$SAME2" "$R/scripts/lib/lock-closure-receipts.tsv"; then ok 'two sources moving from one commit to the same new commit are re-attested together'; else bad 'joint move'; fi + +joint joint-stationary; sed -i "s/@$OLD2/@$SAME2/" "$R/.github/workflows/test.yml"; run +if [[ $rc == 1 ]] && diff -r "$CASE/lib0" "$R/scripts/lib" >/dev/null && grep -q 'is shared with' "$CASE/err"; then ok 'refuses when one of two sharing sources stays at the old commit'; else bad 'stationary sharer'; fi + +joint joint-conflict; sed -i "s/@$OLD2/@$SAME2/" "$R/.github/workflows/test.yml"; sed -i "s/@$OLD2/@$CHANGED/" "$R/.github/workflows/test2.yml"; run +if [[ $rc == 1 ]] && diff -r "$CASE/lib0" "$R/scripts/lib" >/dev/null && grep -q 'different commits' "$CASE/err"; then ok 'refuses sources of one commit moving to different commits'; else bad 'conflicting targets'; fi + +# Rollback: the manifest write succeeds, the receipts write fails; both files must end as they began. +fix rollback; bump "$SAME"; cp -a "$R/scripts/lib" "$CASE/lib0" +mkdir "$CASE/fakebin" +printf '#!/usr/bin/env bash\ncase "${1:-}" in */post-pin-bump.*/receipts.tsv) exit 1 ;; esac\nexec /bin/cat "$@"\n' > "$CASE/fakebin/cat"; chmod +x "$CASE/fakebin/cat" +rc=0; (cd "$R" && PATH="$CASE/fakebin:$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" ]] && diff -r "$CASE/lib0" "$R/scripts/lib" >/dev/null && grep -q 'could not write' "$CASE/err"; then ok 'a failed receipts write restores both files'; else bad 'rollback on write failure'; 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 From 4ecd7887091ad89a83fb0992d9f63f8b9d98bced Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 27 Nov 2026 11:56:11 +0000 Subject: [PATCH 6/9] fix(post-pin-bump): restore both files independently and keep backups when a restore fails A failed manifest restore no longer skips the receipts restore, and the originals are copied outside the cleaned-up temp dir, with the path named in the refusal. Adds a negative test with a shim that fails the manifest restore. --- scripts/post-pin-bump.sh | 11 +++++++++-- scripts/tests/post-pin-bump-test.sh | 12 ++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/scripts/post-pin-bump.sh b/scripts/post-pin-bump.sh index 06d907f9..77aca6fb 100755 --- a/scripts/post-pin-bump.sh +++ b/scripts/post-pin-bump.sh @@ -173,8 +173,15 @@ done < "$TMP/changed" # 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() { - if cat "$TMP/manifest.orig" > "$MANIFEST" && cat "$TMP/receipts.orig" > "$RECEIPTS"; then return 0; fi - refuse 'cannot restore the closure files after a failed install; restore them from git' + # 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. + keep="$(mktemp -d "${TMPDIR:-/tmp}/post-pin-bump-backup.XXXXXX")" && + cp -p "$TMP/manifest.orig" "$TMP/receipts.orig" "$keep/" || keep='(copying the backups failed)' + refuse "cannot restore the closure files after a failed install; originals kept in $keep (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)' diff --git a/scripts/tests/post-pin-bump-test.sh b/scripts/tests/post-pin-bump-test.sh index c3c76fda..a1eaca5a 100755 --- a/scripts/tests/post-pin-bump-test.sh +++ b/scripts/tests/post-pin-bump-test.sh @@ -189,6 +189,18 @@ 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 + # 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 From 59835b4691d678b23f3993acbb5af65038ae8b80 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 27 Nov 2026 12:06:05 +0000 Subject: [PATCH 7/9] fix(post-pin-bump): keep the originals in $TMP when the separate backup copy fails If both restores fail and the backup directory cannot be created or filled, the EXIT trap no longer deletes the only copies: it is disarmed and the refusal names $TMP. Adds a negative test (cat and mktemp shims). --- scripts/post-pin-bump.sh | 11 +++++++---- scripts/tests/post-pin-bump-test.sh | 13 +++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/scripts/post-pin-bump.sh b/scripts/post-pin-bump.sh index 77aca6fb..af274d43 100755 --- a/scripts/post-pin-bump.sh +++ b/scripts/post-pin-bump.sh @@ -178,10 +178,13 @@ restore() { 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. - keep="$(mktemp -d "${TMPDIR:-/tmp}/post-pin-bump-backup.XXXXXX")" && - cp -p "$TMP/manifest.orig" "$TMP/receipts.orig" "$keep/" || keep='(copying the backups failed)' - refuse "cannot restore the closure files after a failed install; originals kept in $keep (restore from there or from git)" + # $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)' diff --git a/scripts/tests/post-pin-bump-test.sh b/scripts/tests/post-pin-bump-test.sh index a1eaca5a..99b45ded 100755 --- a/scripts/tests/post-pin-bump-test.sh +++ b/scripts/tests/post-pin-bump-test.sh @@ -201,6 +201,19 @@ if [[ $rc == 1 ]] && grep -q 'receipts.orig' "$CASE/shim.log" && [[ -n "$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 '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 + # 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 From bfcf81941850e84b2e7a47b55f4addca37273cb9 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 27 Nov 2026 12:06:59 +0000 Subject: [PATCH 8/9] fix(post-pin-bump): the residual old-commit check also covers object keys jq's strings filter skipped keys, so a dependency record keyed by the old commit slipped through. Adds a negative test where the only remaining mention is a key. --- scripts/post-pin-bump.sh | 4 ++-- scripts/tests/post-pin-bump-test.sh | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/scripts/post-pin-bump.sh b/scripts/post-pin-bump.sh index af274d43..ba87328f 100755 --- a/scripts/post-pin-bump.sh +++ b/scripts/post-pin-bump.sh @@ -136,9 +136,9 @@ while IFS=$'\t' read -r repo old; do # 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 (e.g. dependency keys) is + # 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 | select(contains($c))] | length > 0' "$TMP/manifest.json" >/dev/null; then + 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) diff --git a/scripts/tests/post-pin-bump-test.sh b/scripts/tests/post-pin-bump-test.sh index 99b45ded..5e2d7420 100755 --- a/scripts/tests/post-pin-bump-test.sh +++ b/scripts/tests/post-pin-bump-test.sh @@ -214,6 +214,12 @@ keep="$(sed -n 's/.*originals kept in \(.*\) (copy to a separate.*/\1/p' "$CASE/ 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 + # 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 From b60775889eaffee0c475e93edad2160dde5b5c78 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 27 Nov 2026 12:15:49 +0000 Subject: [PATCH 9/9] fix(post-pin-bump): refuse chained moves and unmoved lock records; read the original manifest for sharers - A->B with B->C (or a swap) in one repo is refused deterministically before any rewrite. - A lock record still resolving to repo@old is refused (it would lose its receipt rows). - The shared-source check reads the original manifest, not the partly rewritten copy. - Tests: chained plans in both source orders, and a lock record at the old commit. --- scripts/post-pin-bump.sh | 17 ++++++++++++++++- scripts/tests/post-pin-bump-test.sh | 29 +++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/scripts/post-pin-bump.sh b/scripts/post-pin-bump.sh index ba87328f..dccc4bbf 100755 --- a/scripts/post-pin-bump.sh +++ b/scripts/post-pin-bump.sh @@ -102,6 +102,21 @@ while IFS=$'\t' read -r _ idx old new; do 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 ]] || @@ -111,7 +126,7 @@ while IFS=$'\t' read -r repo old; do # 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' "$TMP/manifest.json")" + '[.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. diff --git a/scripts/tests/post-pin-bump-test.sh b/scripts/tests/post-pin-bump-test.sh index 5e2d7420..99562206 100755 --- a/scripts/tests/post-pin-bump-test.sh +++ b/scripts/tests/post-pin-bump-test.sh @@ -220,6 +220,35 @@ jq --arg o "$OLD" '.sources[0].review_note={($o):"x"}' "$R/scripts/lib/pinned-re 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