interplane.Get Omega
Read in your language

aien-architecture-mv

Canonical architecture for the AIEN ecosystem: flows, boundaries, ADRs, and the map from design to implementation repositories.

Brought by drakestapleton

branch main Branches Tags History

aien-architecture-mv / tools / crumb / test-hostile.sh

153 lines · 10907 bytes Raw Blame History
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).
8C=${1:-./crumb}; C=$(cd "$(dirname "$C")" && pwd)/$(basename "$C")
9W=${2:-5}
10T=$(mktemp -d); trap 'rm -rf "$T"' EXIT
11export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_SYSTEM=/dev/null
12unset CRUMB_COORD_ROOT CRUMB_EXPECT_STORE_ID CRUMB_STRICT_STORE_ID
13fail=0; ok() { echo "PASS $1"; }; bad() { echo "FAIL $1"; fail=1; }
14
15run() { # 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}
27refused() { # 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}
30why() { echo " (rc=$RC hung=$HUNG) $(printf '%s' "$OUT" | head -2 | cut -c1-160)"; }
31mk() { # 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}
36sum() { find "$1" -type f -exec cksum {} + 2>/dev/null | sort; }
37
38echo "crumb under test: $C (watchdog ${W}s, 1 GiB limit)"
39# ---- H: hostile store files (bounded refusal: no hang, no OOM, nothing replaced) ----
40mk h1; run "$R" "$C" claim a src a.c x
41rm -f "$STORE/dirs/src.json"; mkfifo "$STORE/dirs/src.json"
42run "$R" "$C" sniff b src
43refused 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"; }
44run "$WT" "$C" claim b src b.c y
45refused 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
48mk h2; run "$R" "$C" claim a src a.c x
49mkfifo "$STORE/agents/planted.json"
50run "$R" "$C" claim c src b.c y
51refused 4 'agents/planted.json is a fifo' && ok "H2 FIFO agents/*.json: claim refused, no hang" || { why; bad "H2 FIFO agent record"; }
52
53mk h3; run "$R" "$C" claim a src a.c x
54rm -f "$STORE/dirs/src.json"; ln -s /dev/zero "$STORE/dirs/src.json"
55run "$R" "$C" sniff b src
56refused 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
59mk h4; run "$R" "$C" claim a src a.c x
60ln -s /dev/zero "$STORE/agents/zero.json"
61run "$R" "$C" claim c src b.c y
62refused 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
64mk h5; run "$R" "$C" claim a src a.c x
65pad() { printf '{"schema_version": "1.0.0", "directory": "%s", "pad": "' "$1"; head -c "$2" /dev/zero | tr '\0' x; printf '"}\n'; }
66pad big 1048600 > "$STORE/dirs/big.json"; s0=$(cksum < "$STORE/dirs/big.json")
67run "$R" "$C" sniff b src
68refused 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"
70n=$((1048576 - $(pad edge 0 | wc -c))); pad edge "$n" > "$STORE/dirs/big.json"
71run "$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
74mk h6; run "$R" "$C" claim a src a.c x
75rm -f "$STORE/.lock"; mkdir "$STORE/.lock"
76run "$R" "$C" claim b src b.c y
77refused 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
79mk h7; run "$R" "$C" claim a src a.c x
80rm -f "$STORE/.lock"; mkfifo "$STORE/.lock"
81run "$R" "$C" claim b src b.c y
82refused 4 '\.lock is a fifo' && [ -p "$STORE/.lock" ] && ok "H7 FIFO planted as .lock: refused, no hang" || { why; bad "H7 .lock FIFO"; }
83
84mk h8; run "$R" "$C" claim a src a.c x
85rm -f "$STORE/.lock"; ln -s "$T/h8-elsewhere" "$STORE/.lock"
86run "$R" "$C" claim b src b.c y
87refused 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
89mk h9; run "$R" "$C" claim a src a.c x
90mkdir -p "$T/h9-outside"
91printf '{"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"
92ln -s "$T/h9-outside" "$STORE/dirs/evil"; o0=$(sum "$T/h9-outside")
93run "$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"; }
95run "$R" "$C" claim b . evil/x/g.c y
96refused 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
98mk h10; run "$R" "$C" claim a src a.c x
99rm -f "$STORE/STORE_ID"; mkfifo "$STORE/STORE_ID"
100run "$R" "$C" sniff b src
101refused 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
103mk h11; run "$R" "$C" claim a src a.c x
104printf 'hello\n' > "$STORE/STORE_ID"
105run "$R" "$C" claim b src b.c y
106refused 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
108mk h12; run "$R" "$C" claim a src a.c x
109mkdir -p "$STORE/continuations"; mkfifo "$STORE/continuations/cc-fifo.json"
110run "$R" "$C" explain src
111refused 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) ----
114mk s1; run "$R" "$C" claim a src a.c x
115grep -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"; }
116ID=$(cut -d' ' -f2 "$STORE/STORE_ID" 2>/dev/null)
117run "$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"; }
119git -C "$R" config crumb.storeId "$ID"
120run "$WT" "$C" claim b src a.c y
121refused 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"; }
122run "$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
125mk s3w; run "$R" "$C" claim w src a.c "other store"; WRONG=$STORE; w0=$(sum "$WRONG")
126mk s3; git -C "$R" config crumb.storeId "${ID:-00000000000000000000000000000000}"
127run "$R" env CRUMB_COORD_ROOT="$WRONG" "$C" claim a src b.c y
128refused 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"; }
129run "$R" env CRUMB_COORD_ROOT="$WRONG" "$C" sniff a src
130refused 3 STORE_ID_MISMATCH && ok "S3 read-only sniff refuses the wrong root too" || { why; bad "S3 sniff wrong root"; }
131
132run "$R" env CRUMB_COORD_ROOT="$T/s4-none/crumb/v1" "$C" claim a src a.c x
133refused 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"; }
134mkdir -p "$T/s4-bare/dirs"
135run "$R" env CRUMB_COORD_ROOT="$T/s4-bare" "$C" claim a src a.c x
136refused 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
138mk s5; run "$R" env CRUMB_STRICT_STORE_ID=1 "$C" claim a src a.c x
139refused 3 'STORE_UNREGISTERED' && [ ! -e "$STORE" ] && ok "S5 strict (CRUMB_STRICT_STORE_ID=1) refuses an unregistered root, creates nothing" || { why; bad "S5 strict env"; }
140run "$R" "$C" --strict sniff a src
141refused 3 'STORE_UNREGISTERED' && ok "S5 strict (--strict) refuses an unregistered root on read too" || { why; bad "S5 strict flag"; }
142
143mk s6; run "$R" "$C" claim a src a.c x; ID6=$(cut -d' ' -f2 "$STORE/STORE_ID" 2>/dev/null)
144git -C "$R" config crumb.storeId "${ID6:-x}"
145run "$R" env CRUMB_EXPECT_STORE_ID=0123456789abcdef0123456789abcdef "$C" sniff a src
146refused 3 'STORE_ID_MISMATCH' && ok "S6 CRUMB_EXPECT_STORE_ID overrides git config crumb.storeId" || { why; bad "S6 env precedence"; }
147run "$R" env CRUMB_EXPECT_STORE_ID=0123456789ABCDEF0123456789ABCDEF "$C" sniff a src
148refused 3 'STORE_ID_CONFIG_INVALID' && ok "S6 malformed expected id refused (exit 3)" || { why; bad "S6 invalid id"; }
149run "$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
152echo "hostile/identity: $( [ $fail -eq 0 ] && echo ALL PASS || echo FAILURES )"
153exit $fail