aien-architecture-mv
Canonical architecture for the AIEN ecosystem: flows, boundaries, ADRs, and the map from design to implementation repositories.
Brought by drakestapleton
aien-architecture-mv / tools / crumb / test-hostile.sh
| 1 | #!/bin/sh |
| 2 | # Hostile coordination-store files and registered store identity (aien-architecture#234; synapse#58, synapse#15). |
| 3 | # usage: sh test-hostile.sh ./crumb [watchdog_seconds]. Scratch directories only. |
| 4 | # Every crumb run goes through run(): a background watchdog kills the child after W seconds (default 5) and the |
| 5 | # child runs under a 1 GiB address-space limit, so a tool that blocks on a FIFO shows up as HUNG and a tool that |
| 6 | # reads /dev/zero without bound shows up as out of memory, instead of stalling or exhausting the machine. |
| 7 | # Exit codes checked: 3 = store identity refusal, 4 = refused store file (hostile record). |
| 8 | C=${1:-./crumb}; C=$(cd "$(dirname "$C")" && pwd)/$(basename "$C") |
| 9 | W=${2:-5} |
| 10 | T=$(mktemp -d); trap 'rm -rf "$T"' EXIT |
| 11 | export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_SYSTEM=/dev/null |
| 12 | unset CRUMB_COORD_ROOT CRUMB_EXPECT_STORE_ID CRUMB_STRICT_STORE_ID |
| 13 | fail=0; ok() { echo "PASS $1"; }; bad() { echo "FAIL $1"; fail=1; } |
| 14 | |
| 15 | run() { # run <dir> <command...> -> RC, OUT (stdout+stderr), HUNG=1 if the watchdog had to kill it |
| 16 | d=$1; shift |
| 17 | o="$T/run.out"; rm -f "$o" "$o.hung" |
| 18 | ( cd "$d" && ulimit -v 1048576 && exec "$@" ) > "$o" 2>&1 < /dev/null & |
| 19 | pid=$! |
| 20 | ( n=0; while kill -0 "$pid" 2>/dev/null; do n=$((n + 1)); if [ "$n" -gt $((W * 10)) ]; then : > "$o.hung"; kill -9 "$pid" 2>/dev/null; exit 0; fi; sleep 0.1; done ) & |
| 21 | wd=$! |
| 22 | wait "$pid"; RC=$? |
| 23 | wait "$wd" 2>/dev/null |
| 24 | HUNG=0; [ -e "$o.hung" ] && HUNG=1 |
| 25 | OUT=$(cat "$o") |
| 26 | } |
| 27 | refused() { # refused <exit-code> <grep-pattern>: bounded refusal with that code and message |
| 28 | [ "$HUNG" -eq 0 ] && [ "$RC" -eq "$1" ] && printf '%s\n' "$OUT" | grep -q "$2" |
| 29 | } |
| 30 | why() { echo " (rc=$RC hung=$HUNG) $(printf '%s' "$OUT" | head -2 | cut -c1-160)"; } |
| 31 | mk() { # mk <name>: repo $T/<name> + linked worktree $T/<name>-wt; sets R, WT, STORE |
| 32 | R="$T/$1"; WT="$T/$1-wt"; mkdir -p "$R/src" |
| 33 | (cd "$R" && git init -q -b main . && echo a > src/a.c && echo b > src/b.c && git add -A && git -c user.name=t -c user.email=t@t commit -qm init && git worktree add -q -b wt "$WT") >/dev/null 2>&1 |
| 34 | STORE="$R/.git/crumb/v1" |
| 35 | } |
| 36 | sum() { find "$1" -type f -exec cksum {} + 2>/dev/null | sort; } |
| 37 | |
| 38 | echo "crumb under test: $C (watchdog ${W}s, 1 GiB limit)" |
| 39 | # ---- H: hostile store files (bounded refusal: no hang, no OOM, nothing replaced) ---- |
| 40 | mk h1; run "$R" "$C" claim a src a.c x |
| 41 | rm -f "$STORE/dirs/src.json"; mkfifo "$STORE/dirs/src.json" |
| 42 | run "$R" "$C" sniff b src |
| 43 | refused 4 'REFUSED_RECORD: .*dirs/src.json is a fifo' && ok "H1 FIFO dirs/src.json: sniff refused (exit 4), no hang" || { why; bad "H1 FIFO dirs record sniff"; } |
| 44 | run "$WT" "$C" claim b src b.c y |
| 45 | refused 4 'dirs/src.json is a fifo' && ok "H1 FIFO dirs/src.json: claim from linked worktree refused, no hang" || { why; bad "H1 FIFO dirs record claim"; } |
| 46 | [ -p "$STORE/dirs/src.json" ] && ok "H1 FIFO left in place (never replaced)" || bad "H1 FIFO replaced" |
| 47 | |
| 48 | mk h2; run "$R" "$C" claim a src a.c x |
| 49 | mkfifo "$STORE/agents/planted.json" |
| 50 | run "$R" "$C" claim c src b.c y |
| 51 | refused 4 'agents/planted.json is a fifo' && ok "H2 FIFO agents/*.json: claim refused, no hang" || { why; bad "H2 FIFO agent record"; } |
| 52 | |
| 53 | mk h3; run "$R" "$C" claim a src a.c x |
| 54 | rm -f "$STORE/dirs/src.json"; ln -s /dev/zero "$STORE/dirs/src.json" |
| 55 | run "$R" "$C" sniff b src |
| 56 | refused 4 'dirs/src.json is a symbolic link' && ok "H3 symlink dirs/src.json -> /dev/zero: refused, no unbounded read" || { why; bad "H3 /dev/zero dirs record"; } |
| 57 | [ "$(readlink "$STORE/dirs/src.json")" = /dev/zero ] && ok "H3 symlink left as found" || bad "H3 symlink changed" |
| 58 | |
| 59 | mk h4; run "$R" "$C" claim a src a.c x |
| 60 | ln -s /dev/zero "$STORE/agents/zero.json" |
| 61 | run "$R" "$C" claim c src b.c y |
| 62 | refused 4 'agents/zero.json is a symbolic link' && ok "H4 symlink agents/*.json -> /dev/zero: claim refused" || { why; bad "H4 /dev/zero agent record"; } |
| 63 | |
| 64 | mk h5; run "$R" "$C" claim a src a.c x |
| 65 | pad() { printf '{"schema_version": "1.0.0", "directory": "%s", "pad": "' "$1"; head -c "$2" /dev/zero | tr '\0' x; printf '"}\n'; } |
| 66 | pad big 1048600 > "$STORE/dirs/big.json"; s0=$(cksum < "$STORE/dirs/big.json") |
| 67 | run "$R" "$C" sniff b src |
| 68 | refused 4 'dirs/big.json is larger than 1048576 bytes' && ok "H5 oversize record (> 1 MiB) refused" || { why; bad "H5 oversize record"; } |
| 69 | [ "$(cksum < "$STORE/dirs/big.json")" = "$s0" ] && ok "H5 oversize record left byte-identical" || bad "H5 oversize rewritten" |
| 70 | n=$((1048576 - $(pad edge 0 | wc -c))); pad edge "$n" > "$STORE/dirs/big.json" |
| 71 | run "$R" "$C" sniff b src |
| 72 | [ "$(wc -c < "$STORE/dirs/big.json")" -eq 1048576 ] && [ "$HUNG" -eq 0 ] && [ "$RC" -eq 0 ] && ok "H5 record of exactly 1 MiB still accepted (cap is inclusive)" || { why; bad "H5 1 MiB boundary"; } |
| 73 | |
| 74 | mk h6; run "$R" "$C" claim a src a.c x |
| 75 | rm -f "$STORE/.lock"; mkdir "$STORE/.lock" |
| 76 | run "$R" "$C" claim b src b.c y |
| 77 | refused 4 'REFUSED_RECORD: .*\.lock is a directory' && [ -d "$STORE/.lock" ] && ok "H6 directory planted as .lock: named refusal, left in place" || { why; bad "H6 .lock directory"; } |
| 78 | |
| 79 | mk h7; run "$R" "$C" claim a src a.c x |
| 80 | rm -f "$STORE/.lock"; mkfifo "$STORE/.lock" |
| 81 | run "$R" "$C" claim b src b.c y |
| 82 | refused 4 '\.lock is a fifo' && [ -p "$STORE/.lock" ] && ok "H7 FIFO planted as .lock: refused, no hang" || { why; bad "H7 .lock FIFO"; } |
| 83 | |
| 84 | mk h8; run "$R" "$C" claim a src a.c x |
| 85 | rm -f "$STORE/.lock"; ln -s "$T/h8-elsewhere" "$STORE/.lock" |
| 86 | run "$R" "$C" claim b src b.c y |
| 87 | refused 4 '\.lock is a symbolic link' && [ ! -e "$T/h8-elsewhere" ] && ok "H8 symlinked .lock: refused, link target never created" || { why; bad "H8 .lock symlink"; } |
| 88 | |
| 89 | mk h9; run "$R" "$C" claim a src a.c x |
| 90 | mkdir -p "$T/h9-outside" |
| 91 | printf '{"schema_version": "1.0.0", "directory": "evil/x", "locks": {"evil/x/f.c": {"holder": "outside-holder", "acquired_at": "%s", "ttl_seconds": 3600}}}\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" > "$T/h9-outside/x.json" |
| 92 | ln -s "$T/h9-outside" "$STORE/dirs/evil"; o0=$(sum "$T/h9-outside") |
| 93 | run "$R" "$C" sniff b src |
| 94 | [ "$HUNG" -eq 0 ] && [ "$RC" -eq 0 ] && ! printf '%s\n' "$OUT" | grep -q outside-holder && ok "H9 symlinked directory under dirs/ is not walked (its records stay invisible)" || { why; bad "H9 collect_json followed a symlinked directory"; } |
| 95 | run "$R" "$C" claim b . evil/x/g.c y |
| 96 | refused 4 'dirs/evil is a symbolic link' && [ "$(sum "$T/h9-outside")" = "$o0" ] && ok "H9 record path through a symlinked directory refused, nothing written outside the store" || { why; bad "H9 write through symlinked directory"; } |
| 97 | |
| 98 | mk h10; run "$R" "$C" claim a src a.c x |
| 99 | rm -f "$STORE/STORE_ID"; mkfifo "$STORE/STORE_ID" |
| 100 | run "$R" "$C" sniff b src |
| 101 | refused 4 'STORE_ID is a fifo' && [ -p "$STORE/STORE_ID" ] && ok "H10 FIFO planted as STORE_ID: refused, no hang, left in place" || { why; bad "H10 STORE_ID FIFO"; } |
| 102 | |
| 103 | mk h11; run "$R" "$C" claim a src a.c x |
| 104 | printf 'hello\n' > "$STORE/STORE_ID" |
| 105 | run "$R" "$C" claim b src b.c y |
| 106 | refused 3 'STORE_ID_UNREADABLE: .*STORE_ID is not a store identity marker' && [ "$(cat "$STORE/STORE_ID")" = hello ] && ok "H11 foreign STORE_ID content refused (exit 3), never rewritten" || { why; bad "H11 foreign STORE_ID"; } |
| 107 | |
| 108 | mk h12; run "$R" "$C" claim a src a.c x |
| 109 | mkdir -p "$STORE/continuations"; mkfifo "$STORE/continuations/cc-fifo.json" |
| 110 | run "$R" "$C" explain src |
| 111 | refused 4 'continuations/cc-fifo.json is a fifo' && ok "H12 FIFO in continuations/: explain refused, no hang" || { why; bad "H12 continuation FIFO"; } |
| 112 | |
| 113 | # ---- S: registered store identity (fail closed; compatibility mode; strict mode) ---- |
| 114 | mk s1; run "$R" "$C" claim a src a.c x |
| 115 | grep -Eqx 'crumb-coord-store-id/1 [0-9a-f]{32}' "$STORE/STORE_ID" 2>/dev/null && [ "$(wc -c < "$STORE/STORE_ID")" -eq 56 ] && ok "S1 compatibility: first mutation of an unregistered store writes the synapse-coord marker format" || { why; bad "S1 marker not created"; } |
| 116 | ID=$(cut -d' ' -f2 "$STORE/STORE_ID" 2>/dev/null) |
| 117 | run "$R" "$C" sniff a src |
| 118 | [ "$RC" -eq 0 ] && printf '%s\n' "$OUT" | grep -q "STORE_UNREGISTERED (dry run).*git config crumb.storeId $ID" && ok "S1 compatibility: unregistered store with a marker works and warns (dry run)" || { why; bad "S1 dry-run warning"; } |
| 119 | git -C "$R" config crumb.storeId "$ID" |
| 120 | run "$WT" "$C" claim b src a.c y |
| 121 | refused 2 "BLOCKED src/a.c held by a" && ! printf '%s\n' "$OUT" | grep -q STORE_UNREGISTERED && ok "S2 registered correct root: linked worktree sees the same holder, no warning" || { why; bad "S2 registered root"; } |
| 122 | run "$WT" "$C" --strict claim b src b.c y |
| 123 | [ "$RC" -eq 0 ] && ok "S2 strict mode accepts the registered correct root" || { why; bad "S2 strict on registered root"; } |
| 124 | |
| 125 | mk s3w; run "$R" "$C" claim w src a.c "other store"; WRONG=$STORE; w0=$(sum "$WRONG") |
| 126 | mk s3; git -C "$R" config crumb.storeId "${ID:-00000000000000000000000000000000}" |
| 127 | run "$R" env CRUMB_COORD_ROOT="$WRONG" "$C" claim a src b.c y |
| 128 | refused 3 "STORE_ID_MISMATCH: coordination store $WRONG is store .*, expected ${ID:-0}" && [ "$(sum "$WRONG")" = "$w0" ] && ok "S3 deliberately wrong registered root refused (exit 3), its files unchanged" || { why; bad "S3 wrong root"; } |
| 129 | run "$R" env CRUMB_COORD_ROOT="$WRONG" "$C" sniff a src |
| 130 | refused 3 STORE_ID_MISMATCH && ok "S3 read-only sniff refuses the wrong root too" || { why; bad "S3 sniff wrong root"; } |
| 131 | |
| 132 | run "$R" env CRUMB_COORD_ROOT="$T/s4-none/crumb/v1" "$C" claim a src a.c x |
| 133 | refused 3 'STORE_ID_MISSING: .*has no STORE_ID marker' && [ ! -e "$T/s4-none" ] && ok "S4 registered id but missing root: refused, nothing created" || { why; bad "S4 missing root"; } |
| 134 | mkdir -p "$T/s4-bare/dirs" |
| 135 | run "$R" env CRUMB_COORD_ROOT="$T/s4-bare" "$C" claim a src a.c x |
| 136 | refused 3 STORE_ID_MISSING && [ ! -e "$T/s4-bare/STORE_ID" ] && [ ! -e "$T/s4-bare/.lock" ] && ok "S4 registered id, store without marker: refused, marker never created" || { why; bad "S4 missing marker"; } |
| 137 | |
| 138 | mk s5; run "$R" env CRUMB_STRICT_STORE_ID=1 "$C" claim a src a.c x |
| 139 | refused 3 'STORE_UNREGISTERED' && [ ! -e "$STORE" ] && ok "S5 strict (CRUMB_STRICT_STORE_ID=1) refuses an unregistered root, creates nothing" || { why; bad "S5 strict env"; } |
| 140 | run "$R" "$C" --strict sniff a src |
| 141 | refused 3 'STORE_UNREGISTERED' && ok "S5 strict (--strict) refuses an unregistered root on read too" || { why; bad "S5 strict flag"; } |
| 142 | |
| 143 | mk s6; run "$R" "$C" claim a src a.c x; ID6=$(cut -d' ' -f2 "$STORE/STORE_ID" 2>/dev/null) |
| 144 | git -C "$R" config crumb.storeId "${ID6:-x}" |
| 145 | run "$R" env CRUMB_EXPECT_STORE_ID=0123456789abcdef0123456789abcdef "$C" sniff a src |
| 146 | refused 3 'STORE_ID_MISMATCH' && ok "S6 CRUMB_EXPECT_STORE_ID overrides git config crumb.storeId" || { why; bad "S6 env precedence"; } |
| 147 | run "$R" env CRUMB_EXPECT_STORE_ID=0123456789ABCDEF0123456789ABCDEF "$C" sniff a src |
| 148 | refused 3 'STORE_ID_CONFIG_INVALID' && ok "S6 malformed expected id refused (exit 3)" || { why; bad "S6 invalid id"; } |
| 149 | run "$R" "$C" store-id src |
| 150 | [ "$RC" -eq 0 ] && [ "$(printf '%s\n' "$OUT" | head -1)" = "${ID6:-x}" ] && ok "S6 store-id prints the registered id" || { why; bad "S6 store-id"; } |
| 151 | |
| 152 | echo "hostile/identity: $( [ $fail -eq 0 ] && echo ALL PASS || echo FAILURES )" |
| 153 | exit $fail |