interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / collab / tests / bigstore.sh

141 lines · 9527 bytes Raw Blame History
1#!/bin/bash
2# ORACLE: host test driver (bash + curl + git + python3), not product code.
3# collab/tests/bigstore.sh: very large issue actions in served storage must neither end forged nor
4# come back as a cut 200. Findings N1 and N2 of the review of 3edf9201 (the objects are the
5# reviewer's, r3-private/attacks.py):
6# keys one action object of 5,242,916 bytes with 1,048,577 repeated keys. cokey
7# (collab/co1json.om) walked the keys under bound 1048576: trap 4, exit 104.
8# list a label action of 3,145,765 bytes with 1,048,578 labels. coslist, same bound.
9# ctl a comment of 1,500,000 control bytes: its JSON form is 9 MB, over the 8 MiB body.
10# lt a comment of 3,000,000 "<": its HTML form is 12 MB and its JSON form 18 MB (each "<"
11# is written as \u003c), both over the 8 MiB body.
12# For ctl and lt the answer used to be 200 with exactly 8,388,608 bytes, cut mid-document. Now it
13# is a complete 500 (forge/error/1 for a JSON client, the error page otherwise).
14# Everything is private to a temp dir: a plain bare repository stands in for Radicle storage, each
15# action is one git commit under refs/namespaces/fixture/refs/cobs/xyz.radicle.issue/. Nothing here
16# shows that a Radicle node would accept such an object from a peer.
17# Negative controls: the old bound in cokey, the old bound in coslist (each must exit 104), and the
18# overflow check taken out of fsend (must give the cut 200 again).
19# Usage: collab/tests/bigstore.sh (CORES default 14; BIGSTORE_PORT; KEEP=1 keeps the temp dir)
20set -u
21H=$(cd "$(dirname "$0")" && pwd)
22export PORT=${BIGSTORE_PORT:-18502} CORES=${CORES:-14}
23T=$(mktemp -d /tmp/cpbig.XXXXXX); W=$T/w
24PASS=0; FAIL=0; PID=
25ok() { PASS=$((PASS+1)); echo "PASS $*"; }
26bad() { FAIL=$((FAIL+1)); echo "FAIL $*"; }
27check() { if [ "$1" = "$2" ]; then ok "$3 ($1)"; else bad "$3: want $2, got $1"; fi; }
28trap '[ -n "$PID" ] && kill $PID 2>/dev/null; wait 2>/dev/null; [ -n "${KEEP:-}" ] || rm -rf "$T"' EXIT
29if ss -ltn | grep -q "127.0.0.1:$PORT "; then echo "NOT_RUN: port $PORT busy"; exit 3; fi
30RID=zBigStoreFixture1
31S=$T/storage/$RID
32mkdir -p "$T/storage" "$T/data" "$T/social"
33git init -q -b main "$W" && echo hi > "$W/a.txt" && git -C "$W" add a.txt \
34 && git -C "$W" -c user.name=t -c user.email=t@t.invalid commit -qm init \
35 && git clone -q --bare "$W" "$S" || { echo "fixture repo failed"; exit 1; }
36printf '# test allow-list\n\n| repo | RID | default branch |\n|---|---|---|\n| bigstore | rad:%s | main |\n' "$RID" > "$T/rids.md"
37python3 - "$T" <<'PY'
38import sys, json
39t = sys.argv[1]
40open(t + "/keys.json", "wb").write(b'{' + b'"":0,' * 1048577 + b'"type":"edit","title":"large"}')
41open(t + "/list.json", "wb").write(b'{"type":"label","labels":[' + b'"",' * 1048577 + b'"last"]}')
42open(t + "/ctl.json", "wb").write(json.dumps(dict(type="comment", body="\x01" * 1500000), separators=(",", ":")).encode())
43open(t + "/lt.json", "wb").write(json.dumps(dict(type="comment", body="<" * 3000000), separators=(",", ":")).encode())
44PY
45check "$(stat -c %s "$T/keys.json")" 5242916 "fixture: keys object bytes"
46check "$(stat -c %s "$T/list.json")" 3145765 "fixture: list object bytes"
47check "$(stat -c %s "$T/ctl.json")" 9000028 "fixture: control-byte comment bytes"
48check "$(stat -c %s "$T/lt.json")" 3000028 "fixture: 3,000,000 < comment bytes"
49REF=
50plant() { # plant NAME: one issue whose only action is $T/NAME.json; sets OID and REF
51 local blob tree
52 blob=$(git -C "$S" hash-object -w "$T/$1.json") && tree=$(printf '100644 blob %s\t0\n' "$blob" | git -C "$S" mktree) \
53 && OID=$(git -C "$S" -c user.name=fixture -c user.email=fixture@t.invalid commit-tree "$tree" -m "stored action $1" < /dev/null) || return 1
54 REF=refs/namespaces/fixture/refs/cobs/xyz.radicle.issue/$OID
55 git -C "$S" update-ref "$REF" "$OID"
56}
57unplant() { [ -n "$REF" ] && git -C "$S" update-ref -d "$REF"; REF=; }
58start() { # start OUT [SEDX]: build and start a test forged; PID set
59 DATA=$T/data/ STORE=$T/storage/ RIDS=$T/rids.md SEDX=${2:-} "$H/tbuild.sh" "$1" || return 1
60 FORGE_SOCIAL_DIR=$T/social/ taskset -c "$CORES" nice -n 10 "$1" 2>> "$1.log" &
61 PID=$!
62 for _ in $(seq 50); do curl -s -o /dev/null "http://127.0.0.1:$PORT/explore" && return 0; sleep 0.1; done
63 return 1
64}
65stop() { [ -n "$PID" ] && { kill $PID 2> /dev/null; wait $PID 2> /dev/null; }; PID=; sleep 0.3; }
66# rcwait: RC = the exit code of the server if it ends within 10 s, else "alive" (and it is stopped)
67rcwait() {
68 for _ in $(seq 50); do kill -0 $PID 2> /dev/null || break; sleep 0.2; done
69 if kill -0 $PID 2> /dev/null; then stop; RC=alive; else wait $PID 2> /dev/null; RC=$?; PID=; fi
70}
71# get FORM: GET the page of issue OID as json or html into $T/body; prints the status (000 = no answer)
72get() {
73 if [ "$1" = json ]; then curl -s -o "$T/body" -D "$T/hdr" --max-time 60 -w '%{http_code}' -H 'Accept: application/json' "http://127.0.0.1:$PORT/drakestapleton/bigstore/issues/$OID"
74 else curl -s -o "$T/body" -D "$T/hdr" --max-time 60 -w '%{http_code}' "http://127.0.0.1:$PORT/drakestapleton/bigstore/issues/$OID"; fi
75}
76isjson() { python3 -c 'import json,sys; json.load(open(sys.argv[1],"rb"))' "$T/body" 2> /dev/null && echo valid || echo invalid; }
77iserr() { python3 -c 'import json,sys; d=json.load(open(sys.argv[1],"rb")); print("yes" if d.get("schema")=="forge/error/1" and d.get("status")==500 and d.get("message") else "no")' "$T/body" 2> /dev/null || echo no; }
78ends() { tail -c 16 "$T/body" | grep -q '</html>' && echo whole || echo cut; }
79# liveness: the process is there and answers; a dead server is named with its exit code and started again
80alive() {
81 sleep 0.2
82 if kill -0 $PID 2> /dev/null && [ "$(curl -s -o /dev/null --max-time 5 -w '%{http_code}' "http://127.0.0.1:$PORT/explore")" = 200 ]; then ok "$1: server still answers"; return 0; fi
83 for _ in 1 2 3 4 5 6 7 8 9 10; do kill -0 $PID 2> /dev/null || break; sleep 0.2; done
84 if kill -0 $PID 2> /dev/null; then bad "$1: server runs but does not answer"; return 1; fi
85 wait $PID 2> /dev/null; rc=$?; PID=
86 bad "$1: server exited rc=$rc"
87 start "$T/forged" > /dev/null || { echo "server did not start again"; exit 1; }
88 return 1
89}
90start "$T/forged" || { bad "test forged did not start"; exit 1; }
91for n in keys list; do
92 plant $n || { bad "fixture $n"; exit 1; }
93 c=$(get json); check "$c" 200 "$n: issue page as JSON"
94 check "$(isjson)" valid "$n: the JSON parses"
95 alive "$n as JSON"
96 c=$(get html); check "$c" 200 "$n: issue page as HTML"
97 check "$(ends)" whole "$n: the HTML page is whole"
98 alive "$n as HTML"
99 unplant
100done
101plant ctl || { bad "fixture ctl"; exit 1; }
102c=$(get json); check "$c" 500 "ctl: a JSON form over 8 MiB is refused"
103check "$(iserr)" yes "ctl: the answer is a whole forge/error/1 document"
104check "$(grep -i -c '^content-type: application/json' "$T/hdr")" 1 "ctl: content type is JSON"
105check "$(grep -i -c '^etag:' "$T/hdr")" 0 "ctl: no ETag on the refusal"
106[ "$(stat -c %s "$T/body")" -lt 1000 ] && ok "ctl: the refusal is short ($(stat -c %s "$T/body") bytes)" || bad "ctl: body is $(stat -c %s "$T/body") bytes"
107alive "ctl as JSON"
108unplant
109plant lt || { bad "fixture lt"; exit 1; }
110c=$(get html); check "$c" 500 "lt: an HTML form over 8 MiB is refused"
111check "$(ends)" whole "lt: the answer is a whole error page"
112grep -q 'too large' "$T/body" && ok "lt: the page says why" || bad "lt: the page does not say why"
113[ "$(stat -c %s "$T/body")" -lt 20000 ] && ok "lt: the refusal is short ($(stat -c %s "$T/body") bytes)" || bad "lt: body is $(stat -c %s "$T/body") bytes"
114alive "lt as HTML"
115c=$(get json); check "$c" 500 "lt: its JSON form (18 MB) is refused too"
116check "$(iserr)" yes "lt: that answer is a whole forge/error/1 document"
117unplant
118stop
119check "$(grep -a -c "issues/[0-9a-f]* 500 " "$T/forged.log")" 3 "access log records the three refusals as 500"
120# ---- negative controls
121plant keys
122start "$T/mutk" 's/^while lt p e bound 16777216$/while lt p e bound 1048576/' || { bad "cokey mutant did not start"; exit 1; }
123[ "$(grep -c '^while lt p e bound 1048576$' "$T/mutk.om")" = 1 ] && [ "$(grep -c '^while lt p e bound 16777216$' "$T/forged.om")" = 1 ] \
124 && ok "cokey mutant: the old bound is back in one loop" || bad "cokey mutant: sed did not change exactly one loop"
125get json > /dev/null; rcwait
126check "$RC" 104 "NEG cokey with the old bound exits on the keys object (trap 4)"
127unplant
128plant list
129start "$T/mutl" '/^fn coslist /,/^if ne get fxo q 34$/s/^while lt q e bound 16777216$/while lt q e bound 1048576/' || { bad "coslist mutant did not start"; exit 1; }
130[ "$(grep -c '^while lt q e bound 1048576$' "$T/mutl.om")" = 1 ] && [ "$(diff "$T/mutl.om" "$T/forged.om" | grep -c '^<')" = 1 ] && ok "coslist mutant: the old bound is back in one loop" || bad "coslist mutant: sed did not change exactly one loop"
131get json > /dev/null; rcwait
132check "$RC" 104 "NEG coslist with the old bound exits on the list object (trap 4)"
133unplant
134plant ctl
135start "$T/muto" 's/^if fovfany$/if false/' || { bad "overflow mutant did not start"; exit 1; }
136[ "$(grep -c '^if fovfany$' "$T/muto.om")" = 0 ] && [ "$(grep -c '^if fovfany$' "$T/forged.om")" = 1 ] && ok "overflow mutant: the check is out of fsend" || bad "overflow mutant: sed did not change exactly one line"
137c=$(get json)
138[ "$c" = 200 ] && [ "$(stat -c %s "$T/body")" = 8388608 ] && [ "$(isjson)" = invalid ] && ok "NEG without the check: 200 with 8388608 bytes that do not parse" || bad "NEG without the check: $c, $(stat -c %s "$T/body") bytes, $(isjson)"
139stop; unplant
140echo "collab bigstore: $PASS passed, $FAIL failed"
141[ "$FAIL" = 0 ]