Files
fleet/scripts/ci/test-origin-proof.sh
CatoandClaude Fable 5.1 c637b0bd23 fix(origin-proof): two stamps in one second must not share a filename (#68)
Manifest names are second-resolution and the chain is ordered by name. On
CI's faster runner the shrink test's second stamp landed in the same second
as the one before it, overwrote it, and broke the chain link — the suite went
red on #67 after it had merged. The script now waits for a free name; the test
no longer sleeps before that run so the collision path is exercised.


Claude-Session: https://claude.ai/code/session_01DzY8KuwgUM7u95GVTpRL2m

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-13 18:55:24 -07:00

149 lines
8.0 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# origin-proof.sh against a fake `gh` and a fake `ots`, no network.
#
# The proof's value is that it CANNOT be produced for content that did not
# exist, and that the chain cannot be edited afterwards without --check going
# red. So the interesting assertions are the negative ones: a private name never
# reaches the public manifest, an unchanged org produces no new manifest, and a
# manifest edited after stamping fails the check. The fake ots records the
# sha256 it was asked to stamp, which is exactly what the real one does before
# the calendar round-trip.
set -uo pipefail
cd "$(dirname "$0")"
SCRIPT="$PWD/origin-proof.sh"
PASS=0 FAIL=0
ok() { printf ' ✓ %s\n' "$1"; PASS=$((PASS + 1)); }
no() { printf ' ✗ %s\n' "$1"; FAIL=$((FAIL + 1)); }
TMP="$(mktemp -d)"
MAINPID=$$
trap '[ "$$" = "$MAINPID" ] && rm -rf "$TMP"' EXIT
# ── fakes ──────────────────────────────────────────────────────────────────
mkdir -p "$TMP/bin"
# The org: three repos, one private, one fork. HEADs come from $TMP/heads so a
# test can move one.
cat > "$TMP/heads" <<EOF
pubkit 1111111111111111111111111111111111111111
secret-thing 2222222222222222222222222222222222222222
EOF
cat > "$TMP/bin/gh" <<'FAKE'
#!/usr/bin/env bash
case "$1 $2" in
"repo list")
if [[ "$*" == *isFork* && "$*" == *defaultBranchRef* ]]; then
printf 'pubkit\tPUBLIC\tmain\n'
[ -n "${FAKE_HIDE_PRIVATE:-}" ] || printf 'secret-thing\tPRIVATE\tmain\n'
else
printf 'pubkit\tPUBLIC\nsecret-thing\tPRIVATE\n'
fi ;;
"api repos/testorg/pubkit/commits/main")
printf '%s\t2026-01-01T00:00:00Z\n' "$(awk '$1=="pubkit"{print $2}' "$FAKE_HEADS")" ;;
"api repos/testorg/secret-thing/commits/main")
printf '%s\t2026-01-02T00:00:00Z\n' "$(awk '$1=="secret-thing"{print $2}' "$FAKE_HEADS")" ;;
*) echo "fake gh: unhandled: $*" >&2; exit 99 ;;
esac
FAKE
# `ots stamp F` writes F.ots holding sha256(F); `ots info F.ots` prints it the
# way the real client does; `ots upgrade` marks it anchored.
cat > "$TMP/bin/ots" <<'FAKE'
#!/usr/bin/env bash
while [[ "${1:-}" == -* ]]; do shift; done
case "$1" in
stamp) sha256sum "$2" | cut -c1-64 > "$2.ots" ;;
info) printf 'File sha256 hash: %s\nTimestamp:\n' "$(head -1 "$2")"
grep -q anchored "$2" && echo "verify BitcoinBlockHeaderAttestation(900000)"; exit 0 ;;
upgrade) echo anchored >> "$2"; cp "$2" "$2.bak" ;;
*) echo "fake ots: unhandled: $*" >&2; exit 99 ;;
esac
FAKE
chmod +x "$TMP/bin/gh" "$TMP/bin/ots"
run() {
PATH="$TMP/bin:$PATH" FAKE_HEADS="$TMP/heads" ORG=testorg PROOF_DIR="$TMP/proofs" \
bash "$SCRIPT" "$@" 2>&1
}
# ── first stamp ────────────────────────────────────────────────────────────
echo "first run"
out="$(run --stamp)"; rc=$?
m1="$(ls "$TMP/proofs"/*.json 2>/dev/null | head -1)"
[ $rc -eq 0 ] && [ -n "$m1" ] && [ -s "$m1.ots" ] \
&& ok "writes a manifest and a proof beside it" \
|| no "expected manifest + .ots (rc=$rc): $out"
[ "$(jq -r '.repos | length' "$m1")" = "2" ] && ok "one row per non-fork repo" || no "row count: $(jq '.repos' "$m1")"
jq -e '.repos[] | select(.repo == "testorg/pubkit" and .head == "1111111111111111111111111111111111111111")' "$m1" >/dev/null \
&& ok "public repo appears by name with its HEAD" || no "public row missing"
! grep -rq "secret-thing" "$TMP/proofs" \
&& ok "the private repo's name never reaches the proof directory" || no "private name leaked"
label="$(printf 'testorg/secret-thing' | sha256sum | cut -c1-64)"
jq -e --arg l "$label" '.repos[] | select(.repoSha256 == $l)' "$m1" >/dev/null \
&& ok "private repo appears as sha256(org/name), recomputable by whoever knows the name" || no "private label wrong"
[ "$(jq -r '.previous' "$m1")" = "null" ] && ok "first manifest has no predecessor" || no "first manifest claims a previous"
[ "$(head -1 "$m1.ots")" = "$(sha256sum "$m1" | cut -c1-64)" ] \
&& ok "the proof is over the manifest's final bytes" || no "proof hash != file hash"
# ── nothing moved ──────────────────────────────────────────────────────────
echo "second run, no HEAD moved"
sleep 1
out="$(run --stamp)"
[ "$(ls "$TMP/proofs"/*.json | wc -l)" = "1" ] && grep -q "nothing new to prove" <<<"$out" \
&& ok "an unchanged org produces no new manifest" || no "duplicate manifest or wrong message: $out"
# ── a HEAD moved ───────────────────────────────────────────────────────────
echo "third run, one HEAD moved"
sed -i 's/^pubkit\t.*/pubkit\t3333333333333333333333333333333333333333/' "$TMP/heads"
out="$(run --stamp)"
m2="$(ls "$TMP/proofs"/*.json | sort | tail -1)"
[ "$m2" != "$m1" ] && ok "a moved HEAD produces a second manifest" || no "no second manifest: $out"
[ "$(jq -r '.previous.file' "$m2")" = "$(basename "$m1")" ] \
&& [ "$(jq -r '.previous.sha256' "$m2")" = "$(sha256sum "$m1" | cut -c1-64)" ] \
&& ok "the second manifest links to the first by name and sha256" || no "chain link wrong: $(jq '.previous' "$m2")"
# ── the org shrank ─────────────────────────────────────────────────────────
# A token that reads public repos only sees fewer repos — the manifest must
# NOT be written, or the private repos silently drop out of the chain.
echo "fourth run, the token sees one repo fewer"
cp "$TMP/heads" "$TMP/heads.full"
sed -i '/^secret-thing/d' "$TMP/heads"
FAKE_HIDE_PRIVATE=1 out="$(FAKE_HIDE_PRIVATE=1 run --stamp)"; rc=$?
[ $rc -ne 0 ] && grep -q "refusing: 1 repos now, 2" <<<"$out" && [ "$(ls "$TMP/proofs"/*.json | wc -l)" = "2" ] \
&& ok "fewer repos than the last manifest is refused, nothing written" || no "shrink not refused (rc=$rc): $out"
# No sleep before this one on purpose: it can land in the same second as the
# previous manifest, and the script must pick a distinct, later name rather
# than overwrite it (which is what happened on CI's faster runner).
out="$(FAKE_HIDE_PRIVATE=1 ORIGIN_PROOF_ALLOW_SHRINK=1 run --stamp)"; rc=$?
[ $rc -eq 0 ] && [ "$(ls "$TMP/proofs"/*.json | wc -l)" = "3" ] \
&& ok "ORIGIN_PROOF_ALLOW_SHRINK=1 confirms a real deletion, under a distinct name" || no "allow-shrink did not write (rc=$rc): $out"
m3="$(ls "$TMP/proofs"/*.json | sort | tail -1)"
cp "$TMP/heads.full" "$TMP/heads"
# ── check ──────────────────────────────────────────────────────────────────
echo "check"
out="$(run --check)"; rc=$?
[ $rc -eq 0 ] && grep -q "chain intact" <<<"$out" && ok "--check passes on an untouched chain" || no "check failed on clean chain: $out"
out="$(run --upgrade)"
grep -q "anchored in Bitcoin: 3, awaiting a block: 0" <<<"$out" \
&& ok "--upgrade reports attestations and removes .bak files" || no "upgrade tally: $out"
[ -z "$(ls "$TMP/proofs"/*.bak 2>/dev/null)" ] && ok "no .bak litter after upgrade" || no ".bak left behind"
# Tamper with the FIRST manifest after stamping: both its own proof and the
# second manifest's link must go red.
jq '.repos[0].head = "4444444444444444444444444444444444444444"' "$m1" > "$m1.tmp" && mv "$m1.tmp" "$m1"
out="$(run --check)"; rc=$?
[ $rc -ne 0 ] && grep -q "proof is over" <<<"$out" && grep -q "previous link does not match" <<<"$out" \
&& ok "editing a stamped manifest fails its proof AND breaks the next link" || no "tamper not detected (rc=$rc): $out"
rm "$m3.ots"
out="$(run --check)"; rc=$?
[ $rc -ne 0 ] && grep -q "has no .ots proof" <<<"$out" \
&& ok "a manifest without a proof is reported" || no "missing proof not detected: $out"
echo
echo "$PASS passed, $FAIL failed"
[ "$FAIL" -eq 0 ]