interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / server / tests / overflow.sh
| 1 | #!/usr/bin/env bash |
| 2 | # ORACLE: host test driver (bash + curl + git + python3), not product code. |
| 3 | # server/tests/overflow.sh: a response that does not fit its buffer is never sent cut (review of |
| 4 | # 3edf9201, finding N2). fputc drops a byte that does not fit fob and sets fobtr; fsend |
| 5 | # (server/main.om) reads the flag before the first header and sends a whole 500 instead. |
| 6 | # collab/tests/bigstore.sh shows this with 9 MB and 12 MB pages from stored data. This gate shows the |
| 7 | # parts that data cannot reach easily, on trial builds whose fob is made small with sed (the port, |
| 8 | # allow-list, storage, data dir and count sources are private, as in server/tests/title.sh): |
| 9 | # L fob = the size of the explore page less 100 bytes: the fragment fits, the layout appended |
| 10 | # after it does not. The answer must be a whole 500 page, not 200 cut 100 bytes short. |
| 11 | # J fob = the size of /explore.json less 50 bytes: a .json name with no Accept header must get a |
| 12 | # whole forge/error/1 document, and so must the HTML URL asked for with Accept: application/json. |
| 13 | # R the full-size build: a redirect whose Location would pass floc (4096 bytes) is a 500 with no |
| 14 | # Location; one that fits is the 302 it was. |
| 15 | # Negative control for each: the same build with the check taken out of fsend gives the cut answer. |
| 16 | # FORGE_OVERFLOW_PORT (default 18559), FORGED_CPUS (default 4,5), KEEP_TEST_TMP=1 keeps the temp dir |
| 17 | set -u |
| 18 | cd "$(dirname "$0")/../.." |
| 19 | OMC=$HOME/work/flux/omega-mv/seed/out/omc |
| 20 | PIN=3ad9f10f64309e7acf8a725082b28e23a2a4aeb07e98322fb4dc0fcd8fc93194 |
| 21 | PORT=${FORGE_OVERFLOW_PORT:-18559} |
| 22 | [ "$(sha256sum "$OMC" | cut -d' ' -f1)" = "$PIN" ] || { echo 'FAIL compiler pin'; exit 1; } |
| 23 | if ss -ltnH "sport = :$PORT" | grep -q .; then echo "NOT_RUN: private port $PORT busy"; exit 3; fi |
| 24 | T=$(mktemp -d) |
| 25 | pid=; pass=0; fail=0 |
| 26 | ok() { pass=$((pass+1)); echo "PASS $*"; } |
| 27 | bad() { fail=$((fail+1)); echo "FAIL $*"; } |
| 28 | want() { [ "$2" = "$3" ] && ok "$1 = $2" || bad "$1 = $2 (want $3)"; } |
| 29 | cleanup() { |
| 30 | [ -n "$pid" ] && { kill "$pid" 2>/dev/null; wait "$pid" 2>/dev/null; } |
| 31 | if [ "${KEEP_TEST_TMP:-0}" = 1 ]; then echo "Evidence: $T"; else rm -rf "$T"; fi |
| 32 | } |
| 33 | trap cleanup EXIT |
| 34 | run() { taskset -c "${FORGED_CPUS:-4,5}" nice -n 10 "$@"; } |
| 35 | # five repositories, so the explore fragment is well over the size of the notice |
| 36 | mkdir -p "$T/storage" "$T/data/logs" "$T/ng" "$T/bin" "$T/w" |
| 37 | cp server/tests/fake/rad server/tests/fake/getent "$T/bin/"; chmod 755 "$T/bin/rad" "$T/bin/getent" |
| 38 | : > "$T/ng/dl.log"; : > "$T/ng/access.log" |
| 39 | git init -q -b main "$T/w" && echo fixture > "$T/w/a.txt" && git -C "$T/w" add a.txt \ |
| 40 | && git -C "$T/w" -c user.name=Fixture -c user.email=fixture@example.invalid commit -qm fixture || { echo 'FAIL fixture repo'; exit 1; } |
| 41 | printf '| repo | RID | default branch |\n|---|---|---|\n' > "$T/RIDS.md" |
| 42 | # STORE_VARIANT=1 (with IPL_DIR): the same gates on the store-reading build; the fixture becomes a |
| 43 | # store (store/tests/fixstore.sh) and its ids are longer (the store's old-id rule wants 20..60 letters) |
| 44 | RIDP=zOverflowFixture |
| 45 | [ "${STORE_VARIANT:-0}" = 1 ] && RIDP=zOverflowFixtureStore |
| 46 | for n in a b c d e; do |
| 47 | git clone -q --bare "$T/w" "$T/storage/$RIDP$n" |
| 48 | printf '| overflow-fixture-%s | rad:%s%s | main |\n' "$n" "$RIDP" "$n" >> "$T/RIDS.md" |
| 49 | done |
| 50 | if [ "${STORE_VARIANT:-0}" = 1 ]; then |
| 51 | bash store/tests/fixstore.sh "$T/RIDS.md" "$T/storage" "$T/store" || { echo 'FAIL fixture store'; exit 1; } |
| 52 | export FORGE_STORE=$T/store |
| 53 | echo "STORE_VARIANT: store-reading build, store $T/store" |
| 54 | fi |
| 55 | { printf 'om2\n'; for u in $(./run.sh units); do cat "$u"; done; } > "$T/base.om" |
| 56 | sed -e "s|2130706433 18480|2130706433 $PORT|" \ |
| 57 | -e "s|/home/drakestapleton/handoffs/radicle/RIDS.md|$T/RIDS.md|g" \ |
| 58 | -e "s|/home/drakestapleton/.radicle/storage/|$T/storage/|g" \ |
| 59 | -e "s|/home/drakestapleton/forge-data/|$T/data/|g" \ |
| 60 | -e "s|^text tfzp0 \".*\"|text tfzp0 \"$T/ng/dl.log\"|" \ |
| 61 | -e "s|^text tfzp1 \".*\"|text tfzp1 \"$T/ng/access.log\"|" \ |
| 62 | -e "s|^text tfzwal \".*\"|text tfzwal \"$T/ng/wal\"|" \ |
| 63 | -e "s|^text tfzrad \".*\"|text tfzrad \"$T/bin/rad\"|" \ |
| 64 | -e "s|^text tfzspth \".*\"|text tfzspth \"$T/counts.snapshot\"|" \ |
| 65 | -e "s|^text tfzgent \".*\"|text tfzgent \"$T/bin/getent\"|" "$T/base.om" > "$T/a.om" |
| 66 | if grep -qE '2130706433 18480|/home/drakestapleton/(forge-data|\.radicle|handoffs/radicle)' "$T/a.om"; then echo 'FAIL live target remains'; exit 1; fi |
| 67 | [ "$(grep -c '^global fob uchar 8388608$' "$T/a.om")" = 1 ] || { echo 'FAIL fob is not declared as expected'; exit 1; } |
| 68 | [ "$(grep -c '^if fovfany$' "$T/a.om")" = 1 ] || { echo 'FAIL the check line is not unique'; exit 1; } |
| 69 | start() { # start NAME [sed expression...]: build $T/a.om with the expressions as $T/NAME and start it |
| 70 | local n=$1; shift |
| 71 | if [ $# -gt 0 ]; then local e=(); for x in "$@"; do e+=(-e "$x"); done; sed "${e[@]}" "$T/a.om" > "$T/$n.om"; fi |
| 72 | run "$OMC" < "$T/$n.om" > "$T/$n.bin" 2> "$T/$n.err" || { bad "build $n: $(cat "$T/$n.err")"; return 1; } |
| 73 | chmod 755 "$T/$n.bin" |
| 74 | FORGE_SOCIAL_DIR="$T/data/" taskset -c "${FORGED_CPUS:-4,5}" nice -n 10 "$T/$n.bin" > "$T/$n.log" 2>&1 & |
| 75 | pid=$! |
| 76 | for _ in $(seq 50); do curl --max-time 1 -s -o /dev/null "http://127.0.0.1:$PORT/login" && return 0; kill -0 "$pid" 2>/dev/null || break; sleep 0.1; done |
| 77 | bad "trial $n did not listen"; return 1 |
| 78 | } |
| 79 | stop() { [ -n "$pid" ] && { kill "$pid" 2>/dev/null; wait "$pid" 2>/dev/null; }; pid=; for _ in $(seq 50); do ss -ltnH "sport = :$PORT" | grep -q . || return 0; sleep 0.1; done; } |
| 80 | get() { # get PATH [curl args]: body in $T/b, headers in $T/h; prints the status |
| 81 | local p=$1; shift |
| 82 | curl --max-time 20 -s -o "$T/b" -D "$T/h" -w '%{http_code}' -H 'X-Forge-Base: /' "$@" "http://127.0.0.1:$PORT$p" |
| 83 | } |
| 84 | hdr() { grep -i "^$1:" "$T/h" | head -1 | tr -d '\r' | cut -d' ' -f2-; } |
| 85 | size() { stat -c %s "$T/b"; } |
| 86 | # fobsize: the bytes the page held in fob. forged adds translation hints while it sends a page |
| 87 | # (server/tr.om: translate="no" attributes and <span translate="no"> around hashes and ids), so the |
| 88 | # body is longer than fob by exactly those hints; server/tests/translate_check.py strip removes them. |
| 89 | fobsize() { python3 server/tests/translate_check.py strip "$T/b" "$T/b.fob" && stat -c %s "$T/b.fob"; } |
| 90 | whole() { tail -c 16 "$T/b" | grep -q '</html>' && echo whole || echo cut; } |
| 91 | errdoc() { 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 "too large" in d.get("message","") else "no")' "$T/b" 2> /dev/null || echo no; } |
| 92 | lenok() { [ "$(hdr content-length)" = "$(size)" ] && echo yes || echo "no (header $(hdr content-length), body $(size))"; } |
| 93 | |
| 94 | # ---- the full-size build: sizes, and the redirect |
| 95 | start a || exit 1 |
| 96 | want "GET /explore" "$(get /explore)" 200; TE=$(fobsize); want "explore page is whole" "$(whole)" whole |
| 97 | want "GET /explore.json" "$(get /explore.json)" 200; JE=$(size) |
| 98 | want "GET /no-such-page" "$(get /no-such-page)" 404; E4=$(fobsize) |
| 99 | echo "sizes: explore page $TE, explore.json $JE, 404 page $E4" |
| 100 | [ "$TE" -gt $((E4 + 700)) ] && [ "$JE" -gt 600 ] && ok "fixture: the pages are larger than the notice" || bad "fixture: explore $TE, json $JE, 404 $E4" |
| 101 | RID=${RIDP}a |
| 102 | short=/rad:$RID/tree/main |
| 103 | want "GET $short" "$(get "$short")" 302 |
| 104 | want "its Location" "$(hdr location)" /drakestapleton/overflow-fixture-a/tree/main |
| 105 | # the Location swaps /rad:<17 bytes> (22 bytes) for /drakestapleton/overflow-fixture-a (34 bytes): 12 longer |
| 106 | long=/rad:$RID/tree/$(printf '%*s' $((4095 - 11 - ${#RID})) '' | tr ' ' a) |
| 107 | want "fixture: the long path is 4095 bytes" "${#long}" 4095 |
| 108 | want "GET the 4095-byte /rad: path (its Location would be 4107 bytes, floc holds 4096)" "$(get "$long")" 500 |
| 109 | want "no Location on it" "$(grep -i -c '^location:' "$T/h")" 0 |
| 110 | want "a whole notice page" "$(whole)" whole |
| 111 | want "Content-Length equals the body" "$(lenok)" yes |
| 112 | want "server still answers" "$(get /explore)" 200 |
| 113 | stop |
| 114 | start am 's/^if fovfany$/if false/' || exit 1 |
| 115 | c=$(get "$long"); l=$(hdr location) |
| 116 | [ "$c" = 302 ] && [ "${#l}" = 4096 ] && ok "NEG without the check: 302 with a Location cut at 4096 bytes" || bad "NEG redirect without the check: $c, Location ${#l} bytes" |
| 117 | stop |
| 118 | # ---- L: the layout does not fit |
| 119 | SL=$((TE - 100)) |
| 120 | start l "s/^global fob uchar 8388608\$/global fob uchar $SL/" || exit 1 |
| 121 | want "[fob $SL] GET /explore, layout 100 bytes over" "$(get /explore)" 500 |
| 122 | want "[fob $SL] a whole notice page" "$(whole)" whole |
| 123 | grep -q 'too large' "$T/b" && ok "[fob $SL] the page says why" || bad "[fob $SL] the page does not say why" |
| 124 | want "[fob $SL] Content-Length equals the body" "$(lenok)" yes |
| 125 | want "[fob $SL] no ETag" "$(grep -i -c '^etag:' "$T/h")" 0 |
| 126 | want "[fob $SL] Cache-Control" "$(hdr cache-control)" no-store |
| 127 | want "[fob $SL] a page that fits is served" "$(get /no-such-page)" 404 |
| 128 | want "[fob $SL] that page is whole" "$(whole)" whole |
| 129 | sleep 0.3 |
| 130 | want "[fob $SL] the access log says 500" "$(grep -a -c ' /explore 500 ' "$T/l.log")" 1 |
| 131 | stop |
| 132 | start lm "s/^global fob uchar 8388608\$/global fob uchar $SL/" 's/^if fovfany$/if false/' || exit 1 |
| 133 | c=$(get /explore) |
| 134 | [ "$c" = 200 ] && [ "$(fobsize)" = "$SL" ] && [ "$(whole)" = cut ] && ok "NEG without the check: 200, $SL bytes of fob, cut" || bad "NEG layout without the check: $c, $(fobsize) bytes of fob, $(whole)" |
| 135 | stop |
| 136 | # ---- J: a JSON document does not fit |
| 137 | SJ=$((JE - 50)) |
| 138 | start j "s/^global fob uchar 8388608\$/global fob uchar $SJ/" || exit 1 |
| 139 | want "[fob $SJ] GET /explore.json with no Accept header" "$(get /explore.json)" 500 |
| 140 | want "[fob $SJ] a whole forge/error/1 document" "$(errdoc)" yes |
| 141 | case "$(hdr content-type)" in application/json*) ok "[fob $SJ] content type is JSON" ;; *) bad "[fob $SJ] content type $(hdr content-type)" ;; esac |
| 142 | want "[fob $SJ] Content-Length equals the body" "$(lenok)" yes |
| 143 | want "[fob $SJ] GET /explore with Accept: application/json" "$(get /explore -H 'Accept: application/json')" 500 |
| 144 | want "[fob $SJ] a whole forge/error/1 document again" "$(errdoc)" yes |
| 145 | c=$(get /explore) |
| 146 | [ "$c" = 500 ] && [ "$(lenok)" = yes ] && { [ "$(size)" = 0 ] || [ "$(whole)" = whole ]; } \ |
| 147 | && ok "[fob $SJ] the HTML page: 500, and the body is whole or empty ($(size) bytes), never cut" || bad "[fob $SJ] the HTML page: $c, $(size) bytes, $(whole)" |
| 148 | # The 404 HTML page (site header and footer, about 7 KB) no longer fits this small fob; the small |
| 149 | # forge/error/1 document does, so the JSON 404 shows the server still answers. |
| 150 | want "[fob $SJ] server still answers" "$(get /no-such-page -H 'Accept: application/json')" 404 |
| 151 | stop |
| 152 | start jm "s/^global fob uchar 8388608\$/global fob uchar $SJ/" 's/^if fovfany$/if false/' || exit 1 |
| 153 | c=$(get /explore.json) |
| 154 | [ "$c" = 200 ] && [ "$(size)" = "$SJ" ] && ! python3 -c 'import json,sys; json.load(open(sys.argv[1],"rb"))' "$T/b" 2> /dev/null \ |
| 155 | && ok "NEG without the check: 200, $SJ bytes, not JSON" || bad "NEG JSON without the check: $c, $(size) bytes" |
| 156 | stop |
| 157 | echo "server/tests/overflow.sh: $pass passed, $fail failed" |
| 158 | [ "$fail" = 0 ] |