interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / infra / test / sandbox-community.sh

239 lines · 15885 bytes Raw Blame History
1# shellcheck shell=bash
2# infra/test/sandbox-community.sh -- sourced by infra/test/sandbox.sh after its community mount checks.
3# Every /community/ route is requested through forged running inside infra/spark/forged-sandbox.sh
4# (salvage on, the record store bound writable, the read-only root at /community, a private
5# listener), as live. Uses T, H, DATA, PORT, OMC, WRAPPER, PID, stop and port_free from sandbox.sh.
6#
7# Why (2026-11-11): GET /community/part/<id> made the live forged exit 103 (trap 3) three times. The
8# live store's filesystem listed parts/<id>/k/ as "<key> . <key> ..": a 24-byte entry ended the
9# getdents buffer and cm_row's `if or lt rl 84 ne get d 83 0` read byte 83 of it (or is eager in
10# Omega). /community/key/<id> answered 404 because cm_hexarg matched "/community/msg/" by length.
11# No gate had requested either route through a sandboxed forged.
12#
13# Fixtures have the shapes of the live store set aside that day: one runner whose part folder lists
14# "." and ".." last (names chosen so this filesystem lists them before "." and ".."; on a filesystem
15# that cannot, the check says BLOCKED instead of passing), a part with empty k/ and g/, a part with
16# no record, two agreeing runners and one disagreeing, registered and left keys, a signed message,
17# the ledger, the refusal log and a set-aside ledger. Negative controls: the same-length wrong prefix,
18# ids under the other kind's route, short and non-hex ids, unknown paths. A mutant that puts the
19# eager read back must take the server down on the first part page (caught), and the fixed server
20# must answer every route and stay up.
21
22CMR_PUB=$T/community-pub
23cmr_fail=0
24cmr_want() { # cmr_want WHAT GOT WANT
25 if [[ $2 = "$3" ]]; then echo "PASS $1 ($2)"; else echo "FAIL $1: want $3, got $2"; cmr_fail=$((cmr_fail + 1)); fi
26}
27cmr_get() { # cmr_get PATH [curl args] -> status; body in $T/cmr.body
28 local p=$1; shift
29 curl --max-time 20 -s -o "$T/cmr.body" -w '%{http_code}' "$@" "http://127.0.0.1:$PORT$p"
30}
31cmr_same() { # cmr_same PATH FILE -> "<status>:<same|differs>"
32 local c; c=$(cmr_get "$1")
33 if cmp -s "$T/cmr.body" "$2"; then echo "$c:same"; else echo "$c:differs"; fi
34}
35cmr_hex() { head -c "$1" /dev/urandom | od -An -v -tx1 | tr -d ' \n'; }
36
37# cmr_names K: K 64-hex names that the data filesystem lists before "." and ".." in a one-block
38# folder (ext4 with dir_index hashes "." and ".." there like any name), so a folder holding only such
39# names ends its listing with a 24-byte entry, as the live parts/<id>/k/ did.
40cmr_names() {
41 local b d
42 : > "$T/cmr.names"
43 for b in $(seq 1 400); do
44 d=$DATA/cmr-names.$b
45 mkdir "$d" || return 1
46 cmr_hex 1280 | fold -w 64 | (cd "$d" && xargs touch)
47 ls -f "$d" | awk 'f == 0 && ($0 == "." || $0 == "..") {f = 1} f == 0 {print}' >> "$T/cmr.names"
48 rm -rf "$d"
49 if (($(wc -l < "$T/cmr.names") >= $1)); then head -n "$1" "$T/cmr.names"; return 0; fi
50 done
51 return 1
52}
53
54# cmr_run PART KEY BIN OUT EXIT TIME: the hub's record of KEY's result for PART (k/ and g/)
55cmr_run() {
56 local P=$DATA/community/parts/$1 v h m
57 mkdir -p -m 700 "$P/k" "$P/g"
58 v="$3 $4 $5"
59 h=$(printf '%s\n' "$v" | sha256sum | cut -c1-64)
60 m=$(printf 'fixture message %s %s\n' "$1" "$2" | sha256sum | cut -c1-64)
61 printf '%s\n' "$v" > "$P/g/$h.v"
62 mkdir -p -m 700 "$P/g/$h"
63 : > "$P/g/$h/$2"
64 printf '%s %s %s %s %s\n' "$3" "$4" "$5" "$6" "$m" > "$P/k/$2"
65}
66cmr_key() { printf '%s 30 %s %s\n' "$(cmr_hex 65)" "$2" "$3" > "$DATA/community/keys/$1"; }
67
68cmr_fixture() {
69 local C=$DATA/community S=$DATA/salvage n r bin out names
70 rm -rf "$C" "$S" "$CMR_PUB"
71 mkdir -m 700 "$S" "$C" "$C/keys" "$C/msgs" "$C/parts" || return 1
72 mkdir -m 700 -p "$CMR_PUB/dist" || return 1
73 printf '#!/bin/sh\necho community route fixture\n' > "$CMR_PUB/dist/join.sh"
74 (cd "$CMR_PUB/dist" && sha256sum join.sh > SHA256SUMS)
75 : > "$CMR_PUB/deny"
76 printf 'The fixture runners are on the same host.\n' > "$CMR_PUB/runners-note"
77 # Parts published through the real salvage routes, as the community gate does.
78 { sed 's/^fn main to int$/fn fmainx to int/' "$T/forged.om"; cat community/tests/seed.om; } > "$T/cmr-seed.om"
79 "$OMC" < "$T/cmr-seed.om" > "$T/cmr-seed" || return 1
80 chmod 755 "$T/cmr-seed"
81 : > "$T/cmr.in"
82 printf 'hello from a community machine\n' > "$T/cmr.exp"
83 for n in one empty none three; do
84 printf 'note community route fixture %s\ntext m "hello from a community machine\\n"\nfn main to int\nk = write 1 m 0 len m\nret 0\nend\n' "$n" > "$T/cmr-$n.om"
85 OMEGA_COMPILER=$OMC "$T/cmr-seed" "$S/" "$T/cmr-$n.om" "$T/cmr.in" "$T/cmr.exp" "route fixture $n" \
86 > "$T/cmr-part.$n" 2> "$T/cmr-seed.$n.err" || { cat "$T/cmr-seed.$n.err"; return 1; }
87 [[ $(wc -c < "$T/cmr-part.$n") = 65 ]] || return 1
88 printf -v "CMR_${n^^}" '%s' "$(head -c 64 "$T/cmr-part.$n")"
89 done
90 r=$S/receipts/$CMR_ONE
91 bin=$(head -c 129 "$r" | tail -c 64); out=$(head -c 193 "$r" | tail -c 64)
92 [[ $bin =~ ^[0-9a-f]{64}$ && $out =~ ^[0-9a-f]{64}$ ]] || return 1
93 names=$(cmr_names 4) || { echo "BLOCKED no names list before '.' and '..' on this filesystem"; return 1; }
94 CMR_K1=$(sed -n 1p <<< "$names"); CMR_KA=$(sed -n 2p <<< "$names")
95 CMR_KB=$(sed -n 3p <<< "$names"); CMR_KC=$(sed -n 4p <<< "$names")
96 CMR_KL=$(cmr_hex 32)
97 for k in "$CMR_K1" "$CMR_KA" "$CMR_KB" "$CMR_KC"; do cmr_key "$k" 1794414223 j; done
98 cmr_key "$CMR_KL" 1794414224 left
99 cmr_run "$CMR_ONE" "$CMR_K1" "$bin" "$out" 0 1794414226
100 mkdir -p -m 700 "$C/parts/$CMR_EMPTY/k" "$C/parts/$CMR_EMPTY/g"
101 r=$S/receipts/$CMR_THREE
102 bin=$(head -c 129 "$r" | tail -c 64); out=$(head -c 193 "$r" | tail -c 64)
103 cmr_run "$CMR_THREE" "$CMR_KA" "$bin" "$out" 0 1794414301
104 cmr_run "$CMR_THREE" "$CMR_KB" "$bin" "$out" 0 1794414302
105 cmr_run "$CMR_THREE" "$CMR_KC" "$bin" "$(cmr_hex 32)" 0 1794414303
106 printf 'ipcc1 register\npub %s\ntime 1794414294\nrate 30\nsig %s\n' "$(cmr_hex 65)" "$(cmr_hex 64)" > "$T/cmr.msg"
107 CMR_M=$(sha256sum < "$T/cmr.msg" | cut -c1-64)
108 cp "$T/cmr.msg" "$C/msgs/$CMR_M"
109 printf '1794414223 register %s %s - ok\n' "$CMR_K1" "$CMR_M" > "$C/ledger"
110 printf '1794414250 refused fixture\n' > "$C/refused"
111 printf '1794400000 register fixture - ok\n' > "$C/ledger.1794400000"
112 # the shape that trapped the live server: the part's k/ ends with "." or ".."
113 for n in "$CMR_ONE" "$CMR_THREE"; do
114 case $(ls -f "$C/parts/$n/k" | tail -n 1) in .|..) ;; *) echo "BLOCKED k/ of $n does not end with . or .."; return 1 ;; esac
115 done
116 echo "fixture: parts one=$CMR_ONE empty=$CMR_EMPTY none=$CMR_NONE three=$CMR_THREE; k/ of one lists: $(ls -f "$C/parts/$CMR_ONE/k" | cut -c1-8 | tr '\n' ' ')"
117}
118
119cmr_start() { # cmr_start BIN NAME: forged in the wrapper on the private listener
120 local i
121 port_free || return 1
122 FORGED_SALVAGE=on FORGED_COMMUNITY_PUB=$CMR_PUB FORGED_LISTEN_PORT=$PORT \
123 setsid bash "$WRAPPER" "$1" > "$T/cmr-server.$2.log" 2>&1 &
124 PID=$!
125 for i in $(seq 1 100); do
126 kill -0 "$PID" 2>/dev/null || { cat "$T/cmr-server.$2.log"; return 1; }
127 if curl --max-time 1 -fsS "http://127.0.0.1:$PORT/" -o /dev/null; then return 0; fi
128 sleep .1
129 done
130 return 1
131}
132
133cmr_routes() {
134 local C=$DATA/community P=$CMR_PUB/dist i bad
135 cmr_want "GET /community/ through the sandbox" "$(cmr_get /community/)" 200
136 cmr_want "the index counts the registered runners (the left key not)" "$(grep -c '4 runners registered' "$T/cmr.body")" 1
137 cmr_want "the index shows Interplane's note on its own runners" "$(grep -c 'The fixture runners are on the same host.' "$T/cmr.body")" 1
138 cmr_want "the index lists each registered runner key" "$(grep -o "community/key/\($CMR_K1\|$CMR_KA\|$CMR_KB\|$CMR_KC\)" "$T/cmr.body" | sort -u | wc -l)" 4
139 cmr_want "control: the left key is not listed" "$(grep -c "community/key/$CMR_KL" "$T/cmr.body")" 0
140 cmr_want "the index says x86-64 Linux and Mac are not covered" "$(grep -c 'x86-64, most PCs and servers: not covered yet' "$T/cmr.body"):$(grep -c 'Mac: not covered' "$T/cmr.body")" 1:1
141 cmr_want "GET /community" "$(cmr_get /community)" 200
142 cmr_want "GET /community/join is dist/join.sh" "$(cmr_same /community/join "$P/join.sh")" 200:same
143 cmr_want "GET /community/dl/SHA256SUMS" "$(cmr_same /community/dl/SHA256SUMS "$P/SHA256SUMS")" 200:same
144 cmr_want "GET /community/part/<one runner, k/ ends with . or ..>" "$(cmr_get "/community/part/$CMR_ONE")" 200
145 cmr_want "its record line" "$(grep -o 'Ran again by 1 community runner: 1 agree' "$T/cmr.body" | head -1)" 'Ran again by 1 community runner: 1 agree'
146 cmr_want "its developer view lists the key" "$(grep -c "community/key/$CMR_K1" "$T/cmr.body")" 1
147 cmr_want "GET /community/part/<empty k/ and g/>" "$(cmr_get "/community/part/$CMR_EMPTY"):$(grep -c 'Not run again by community runners yet.' "$T/cmr.body")" 200:1
148 cmr_want "GET /community/part/<no record>" "$(cmr_get "/community/part/$CMR_NONE"):$(grep -c 'Not run again by community runners yet.' "$T/cmr.body")" 200:1
149 cmr_want "GET /community/part/<two agree, one disagrees>" "$(cmr_get "/community/part/$CMR_THREE")" 200
150 cmr_want "its record line" "$(grep -o 'Ran again by 3 community runners: 2 agree, 1 disagree.' "$T/cmr.body" | head -1)" 'Ran again by 3 community runners: 2 agree, 1 disagree.'
151 cmr_want "GET /community/key/<registered key> is the key record" "$(cmr_same "/community/key/$CMR_K1" "$C/keys/$CMR_K1")" 200:same
152 cmr_want "GET /community/key/<left key> is the key record" "$(cmr_same "/community/key/$CMR_KL" "$C/keys/$CMR_KL")" 200:same
153 cmr_want "GET /community/msg/<message> is the message" "$(cmr_same "/community/msg/$CMR_M" "$C/msgs/$CMR_M")" 200:same
154 cmr_want "GET /community/ledger" "$(cmr_same /community/ledger "$C/ledger")" 200:same
155 cmr_want "GET /community/refused" "$(cmr_same /community/refused "$C/refused")" 200:same
156 cmr_want "GET /community/ledger.1794400000 (set aside)" "$(cmr_same /community/ledger.1794400000 "$C/ledger.1794400000")" 200:same
157 cmr_want "control: GET /community/alarms with no alarm log" "$(cmr_get /community/alarms)" 404
158 cmr_want "control: a message id under /community/key/" "$(cmr_get "/community/key/$CMR_M")" 404
159 cmr_want "control: a key id under /community/msg/" "$(cmr_get "/community/msg/$CMR_K1")" 404
160 cmr_want "control: a same-length wrong prefix /community/kez/<key>" "$(cmr_get "/community/kez/$CMR_K1")" 404
161 cmr_want "control: a same-length wrong prefix /community/pare/<part>" "$(cmr_get "/community/pare/$CMR_ONE")" 404
162 cmr_want "control: a key id under /community/part/" "$(cmr_get "/community/part/$CMR_K1")" 404
163 cmr_want "control: 63 hex digits" "$(cmr_get "/community/key/${CMR_K1:0:63}")" 404
164 cmr_want "control: 64 non-hex characters" "$(cmr_get "/community/part/$(printf 'g%.0s' $(seq 64))")" 404
165 cmr_want "control: unknown community path" "$(cmr_get /community/nope)" 404
166 cmr_want "control: /community/dl/../keys sent as is (the request parser refuses a .. segment)" "$(cmr_get /community/dl/../keys --path-as-is)" 400
167 cmr_want "control: /community/dl/.hidden" "$(cmr_get /community/dl/.hidden)" 404
168 cmr_want "control: /community/ledger.x1" "$(cmr_get /community/ledger.x1)" 404
169 cmr_want "control: POST to a page" "$(cmr_get "/community/part/$CMR_ONE" --data-binary x)" 405
170 # The boundary: exactly 200 registered keys (4 + 196) and nothing else in keys/ but the left key, so
171 # 201 entries: the count reads 200 key files, then sees one more entry and says "At least".
172 { cmr_hex $((196 * 97)) | fold -w 194; echo; } | while read -r l; do
173 printf '%s 30 1794414223 j\n' "${l:64:130}" > "$C/keys/${l:0:64}"
174 done
175 rm -f "$C/keys/$CMR_KL"
176 cmr_want "exactly 200 registered keys: an exact count" "$(cmr_get /community/ >/dev/null; grep -c '>200 runners registered\|^200 runners registered\|<p>200 runners registered' "$T/cmr.body"):$(grep -c 'At least' "$T/cmr.body")" 1:0
177 cmr_key "$CMR_KL" 1794414224 left
178 cmr_want "one more key file (201 entries): the count says At least" "$(cmr_get /community/ >/dev/null; grep -c 'At least [0-9]* runners registered' "$T/cmr.body")" 1
179 # Keys are free to make: /community/ reads at most 200 key files per pass however many exist
180 # (security review 2026-11-11, CM-RUNNERS-READ-AMPLIFICATION). 3000 more registered keys:
181 { cmr_hex $((3000 * 97)) | fold -w 194; echo; } | while read -r l; do
182 printf '%s 30 1794414223 j\n' "${l:64:130}" > "$C/keys/${l:0:64}"
183 done
184 cmr_want "GET /community/ with 3000 more registered keys" "$(cmr_get /community/)" 200
185 cmr_want "the count stops at 200 key files and says so" "$(grep -c 'At least [0-9]* runners registered' "$T/cmr.body"):$(grep -c 'The count stops after the first 200 keys' "$T/cmr.body")" 1:1
186 cmr_want "the list shows at most 200 keys" "$(($(grep -o 'community/key/[0-9a-f]\{64\}' "$T/cmr.body" | wc -l) <= 200))" 1
187 bad=0
188 for i in $(seq 1 50); do
189 for p in "/community/part/$CMR_ONE" "/community/part/$CMR_THREE" "/community/key/$CMR_K1" /community/; do
190 [[ $(cmr_get "$p") = 200 ]] || bad=$((bad + 1))
191 done
192 done
193 cmr_want "50 more rounds of both part pages, a key and the index" "$bad" 0
194 cmr_want "the server that answered all of them is still the first one" "$(kill -0 "$PID" 2>/dev/null && echo up || echo gone):$(cmr_get /)" up:200
195}
196
197community_routes_check() {
198 cmr_fail=0
199 cmr_fixture || { echo 'FAIL community route fixture'; return 1; }
200 cmr_start "$T/forged" routes || { echo 'FAIL sandboxed forged did not start with the community store'; return 1; }
201 grep -q 'forged-sandbox: on' "$T/cmr-server.routes.log" || { echo 'FAIL server not in the sandbox'; stop; return 1; }
202 cmr_routes
203 stop || return 1
204 ((cmr_fail == 0)) || { echo "FAIL $cmr_fail community route checks"; return 1; }
205 echo 'PASS every community route through the sandboxed forged, the live shapes, controls refused, server up'
206}
207
208# community_routes_mutant: succeeds when the eager read put back in cm_row (the first "if lt rl 84";
209# cm_kent has the second) is caught: the first part page takes the sandboxed server down with trap 3.
210community_routes_mutant() {
211 local code rc i
212 sed '0,/^if lt rl 84$/s//if or lt rl 84 ne get d 83 0/' "$T/forged.om" > "$T/cmr-mutant.om"
213 (($(grep -c '^if or lt rl 84 ne get d 83 0$' "$T/cmr-mutant.om") == 1)) || { echo 'FAIL mutant not made'; return 1; }
214 "$OMC" < "$T/cmr-mutant.om" > "$T/forged-cmr-mutant" || return 1
215 chmod 755 "$T/forged-cmr-mutant"
216 cmr_start "$T/forged-cmr-mutant" mutant || { echo 'FAIL mutant did not start'; return 1; }
217 code=$(cmr_get "/community/part/$CMR_ONE")
218 for i in $(seq 1 50); do kill -0 "$PID" 2>/dev/null || break; sleep .1; done
219 if kill -0 "$PID" 2>/dev/null; then
220 echo "FAIL mutant not caught: status $code and the server is still up"; stop; return 1
221 fi
222 rc=0; wait "$PID" || rc=$?; PID=
223 port_free || return 1
224 [[ $code != 200 && $rc = 103 ]] || { echo "FAIL mutant: status $code exit $rc (want no answer and exit 103)"; return 1; }
225 echo "PASS mutant with the eager read caught: no answer (status $code), sandboxed forged exit $rc (trap 3)"
226 # second mutant: the key-file cap lifted must be caught by the many-keys check (keys/ still holds
227 # the 3000 extra keys from community_routes_check)
228 sed 's/^const cmkmax ulong 200$/const cmkmax ulong 1000000/' "$T/forged.om" > "$T/cmr-capmutant.om"
229 (($(grep -c '^const cmkmax ulong 1000000$' "$T/cmr-capmutant.om") == 1)) || { echo 'FAIL cap mutant not made'; return 1; }
230 "$OMC" < "$T/cmr-capmutant.om" > "$T/forged-cmr-capmutant" || return 1
231 chmod 755 "$T/forged-cmr-capmutant"
232 cmr_start "$T/forged-cmr-capmutant" capmutant || { echo 'FAIL cap mutant did not start'; return 1; }
233 code=$(cmr_get /community/)
234 stop || return 1
235 if grep -q 'At least' "$T/cmr.body"; then
236 echo "FAIL cap mutant not caught: the page still says At least"; return 1
237 fi
238 echo "PASS cap mutant caught: status $code, it read every key file ($(grep -o '[0-9]* runners registered' "$T/cmr.body" | head -1))"
239}