interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / collab / tests / bigstore.sh
| 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) |
| 20 | set -u |
| 21 | H=$(cd "$(dirname "$0")" && pwd) |
| 22 | export PORT=${BIGSTORE_PORT:-18502} CORES=${CORES:-14} |
| 23 | T=$(mktemp -d /tmp/cpbig.XXXXXX); W=$T/w |
| 24 | PASS=0; FAIL=0; PID= |
| 25 | ok() { PASS=$((PASS+1)); echo "PASS $*"; } |
| 26 | bad() { FAIL=$((FAIL+1)); echo "FAIL $*"; } |
| 27 | check() { if [ "$1" = "$2" ]; then ok "$3 ($1)"; else bad "$3: want $2, got $1"; fi; } |
| 28 | trap '[ -n "$PID" ] && kill $PID 2>/dev/null; wait 2>/dev/null; [ -n "${KEEP:-}" ] || rm -rf "$T"' EXIT |
| 29 | if ss -ltn | grep -q "127.0.0.1:$PORT "; then echo "NOT_RUN: port $PORT busy"; exit 3; fi |
| 30 | RID=zBigStoreFixture1 |
| 31 | S=$T/storage/$RID |
| 32 | mkdir -p "$T/storage" "$T/data" "$T/social" |
| 33 | git 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; } |
| 36 | printf '# test allow-list\n\n| repo | RID | default branch |\n|---|---|---|\n| bigstore | rad:%s | main |\n' "$RID" > "$T/rids.md" |
| 37 | python3 - "$T" <<'PY' |
| 38 | import sys, json |
| 39 | t = sys.argv[1] |
| 40 | open(t + "/keys.json", "wb").write(b'{' + b'"":0,' * 1048577 + b'"type":"edit","title":"large"}') |
| 41 | open(t + "/list.json", "wb").write(b'{"type":"label","labels":[' + b'"",' * 1048577 + b'"last"]}') |
| 42 | open(t + "/ctl.json", "wb").write(json.dumps(dict(type="comment", body="\x01" * 1500000), separators=(",", ":")).encode()) |
| 43 | open(t + "/lt.json", "wb").write(json.dumps(dict(type="comment", body="<" * 3000000), separators=(",", ":")).encode()) |
| 44 | PY |
| 45 | check "$(stat -c %s "$T/keys.json")" 5242916 "fixture: keys object bytes" |
| 46 | check "$(stat -c %s "$T/list.json")" 3145765 "fixture: list object bytes" |
| 47 | check "$(stat -c %s "$T/ctl.json")" 9000028 "fixture: control-byte comment bytes" |
| 48 | check "$(stat -c %s "$T/lt.json")" 3000028 "fixture: 3,000,000 < comment bytes" |
| 49 | REF= |
| 50 | plant() { # 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 | } |
| 57 | unplant() { [ -n "$REF" ] && git -C "$S" update-ref -d "$REF"; REF=; } |
| 58 | start() { # 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 | } |
| 65 | stop() { [ -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) |
| 67 | rcwait() { |
| 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) |
| 72 | get() { |
| 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 | } |
| 76 | isjson() { python3 -c 'import json,sys; json.load(open(sys.argv[1],"rb"))' "$T/body" 2> /dev/null && echo valid || echo invalid; } |
| 77 | iserr() { 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; } |
| 78 | ends() { 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 |
| 80 | alive() { |
| 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 | } |
| 90 | start "$T/forged" || { bad "test forged did not start"; exit 1; } |
| 91 | for 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 |
| 100 | done |
| 101 | plant ctl || { bad "fixture ctl"; exit 1; } |
| 102 | c=$(get json); check "$c" 500 "ctl: a JSON form over 8 MiB is refused" |
| 103 | check "$(iserr)" yes "ctl: the answer is a whole forge/error/1 document" |
| 104 | check "$(grep -i -c '^content-type: application/json' "$T/hdr")" 1 "ctl: content type is JSON" |
| 105 | check "$(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" |
| 107 | alive "ctl as JSON" |
| 108 | unplant |
| 109 | plant lt || { bad "fixture lt"; exit 1; } |
| 110 | c=$(get html); check "$c" 500 "lt: an HTML form over 8 MiB is refused" |
| 111 | check "$(ends)" whole "lt: the answer is a whole error page" |
| 112 | grep -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" |
| 114 | alive "lt as HTML" |
| 115 | c=$(get json); check "$c" 500 "lt: its JSON form (18 MB) is refused too" |
| 116 | check "$(iserr)" yes "lt: that answer is a whole forge/error/1 document" |
| 117 | unplant |
| 118 | stop |
| 119 | check "$(grep -a -c "issues/[0-9a-f]* 500 " "$T/forged.log")" 3 "access log records the three refusals as 500" |
| 120 | # ---- negative controls |
| 121 | plant keys |
| 122 | start "$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" |
| 125 | get json > /dev/null; rcwait |
| 126 | check "$RC" 104 "NEG cokey with the old bound exits on the keys object (trap 4)" |
| 127 | unplant |
| 128 | plant list |
| 129 | start "$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" |
| 131 | get json > /dev/null; rcwait |
| 132 | check "$RC" 104 "NEG coslist with the old bound exits on the list object (trap 4)" |
| 133 | unplant |
| 134 | plant ctl |
| 135 | start "$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" |
| 137 | c=$(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)" |
| 139 | stop; unplant |
| 140 | echo "collab bigstore: $PASS passed, $FAIL failed" |
| 141 | [ "$FAIL" = 0 ] |