Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/interplane-forge/blob/main/collab/tests/compose.sh # interplane-forge INTERPLANE Forge Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `interplane-forge / collab / tests / compose.sh` 242 lines ยท 19418 bytes Raw Blame History 1 #!/bin/bash 2 # collab/tests/compose.sh: end-to-end HOST test of Phase A (docs/collab-write.md): the web composes and 3 # checks and says publishing is closed (no command shown); the author runs commands this test writes, 4 # and the result shows on the forge. Everything is private 5 # to a temp dir: a throwaway Radicle profile (RAD_HOME) with a private repo, a test forged on PORT 6 # (default 18493) whose store and allow-list point at that profile, throwaway ssh keys for sign-in. 7 # Nothing touches ~/.radicle, the live forged or the network. Every gate has a negative control. 8 # Usage: collab/tests/compose.sh (from anywhere; CORES default 14; KEEP=1 keeps the temp dir) 9 # untr FILE: forged adds translation hints while it sends an HTML page (server/tr.om); the checks here 10 # read pages without them (server/tests/translate.sh gates the hints). Other bodies are left as sent. 11 untr() { case "$(head -c 15 "$1" 2>/dev/null)" in '\([^<]*\)<\/span>/\1/g; s/ translate="no"//g' "$1" ;; esac; } 12 set -u 13 H=$(cd "$(dirname "$0")" && pwd) 14 export PORT=${COMPOSE_PORT:-18493} CORES=${CORES:-14} 15 T=$(mktemp -d /tmp/cpcompose.XXXXXX) 16 W=$T/w 17 PASS=0; FAIL=0 18 ok() { PASS=$((PASS+1)); echo "PASS $*"; } 19 bad() { FAIL=$((FAIL+1)); echo "FAIL $*"; } 20 check() { if [ "$1" = "$2" ]; then ok "$3 ($1)"; else bad "$3: want $2, got $1"; fi; } 21 has() { grep -qF -- "$1" "$T/body"; } 22 23 # ---- throwaway Radicle profile and private repo (the "author's clone") 24 export RAD_HOME=$T/rad RAD_PASSPHRASE= 25 rad auth --alias cptest < /dev/null > /dev/null 2>&1 || { echo "rad auth failed"; exit 1; } 26 git init -q -b main "$W" && cd "$W" && echo hello > a.txt && git add a.txt \ 27 && git -c user.name=t -c user.email=t@t.invalid commit -qm init \ 28 && rad init --private --name cpscratch --description "compose test" --default-branch main --no-confirm --no-seed > /dev/null 2>&1 \ 29 || { echo "rad init failed"; exit 1; } 30 git config user.name t; git config user.email t@t.invalid 31 RID=$(rad . | sed 's/^rad://') 32 echo "scratch repo rad:$RID in $RAD_HOME" 33 printf '# test allow-list\n\n| repo | RID | default branch |\n|---|---|---|\n| cpscratch | rad:%s | main |\n' "$RID" > "$T/rids.md" 34 35 # ---- test forged 36 mkdir -p "$T/data" "$T/social" 37 DATA=$T/data/ STORE=$RAD_HOME/storage/ RIDS=$T/rids.md "$H/tbuild.sh" "$T/forged" || exit 1 38 FORGE_SOCIAL_DIR=$T/social/ taskset -c "$CORES" nice -n 10 "$T/forged" 2>> "$T/forged.err" & 39 PID=$! 40 trap 'kill $PID 2>/dev/null; wait $PID 2>/dev/null; [ -n "${KEEP:-}" ] || rm -rf "$T"' EXIT 41 U="http://127.0.0.1:$PORT" 42 for i in $(seq 50); do curl -s -o /dev/null "$U/" && break; sleep 0.1; done 43 kill -0 $PID 2> /dev/null || { echo "test forged did not start: $(cat "$T/forged.err")"; exit 1; } 44 R=/drakestapleton/cpscratch 45 BASE=/ 46 IP=10.9.0.1 47 48 # code METHOD PATH [curl args] -> status; body in $T/body, headers in $T/hdr 49 code() { m=$1; p=$2; shift 2; c=$(curl -s -o "$T/body" -D "$T/hdr" -X "$m" -H "X-Forge-Base: $BASE" -H "X-Real-IP: $IP" "$@" "$U$p" -w '%{http_code}'); untr "$T/body"; printf %s "$c"; } 50 hdr() { grep -i "^$1:" "$T/hdr" | head -1 | cut -d' ' -f2- | tr -d '\r'; } 51 field() { grep -o "name=\"$1\" value=\"[^\"]*\"" "$T/body" | head -1 | sed 's/.*value="//; s/"$//'; } 52 cmds() { python3 - "$T/body" <<'EOF' 53 import sys, re, html 54 for m in re.findall(r'
(.*?)', open(sys.argv[1]).read(), re.S): 55 print(html.unescape(m)) 56 EOF 57 } 58 run_() { [ -n "$1" ] && (cd "$W" && sh -c "$1"); } 59 # The compose page shows no command: publishing is closed for now. The author's own commands below 60 # are written by this test (not read from the page) so the fixture repo still gets its COBs. 61 closed() { has 'Publishing it is closed for now: Interplane does not accept comments, reviews, issues or patches yet.' && [ -z "$(cmds)" ] && ok "$1: publishing closed, no command shown" || bad "$1: closed note missing or a command shown"; } 62 dl() { curl -s -o "$2" -D "$T/hdr" -H "X-Forge-Base: $BASE" -H "$CK" "$U$1" -w '%{http_code}'; } 63 cidof() { hdr location | sed -n 's|.*/compose/\([0-9a-f]\{32\}\)$|\1|p'; } 64 65 # ---- sign-in with throwaway ssh keys (the forge accepts any did:key it can verify) 66 ssh-keygen -q -t `ed25519` -N '' -C t1 -f "$T/k1"; ssh-keygen -q -t `ed25519` -N '' -C t2 -f "$T/k2" 67 did() { python3 - "$1" <<'EOF' 68 import sys, base64 69 raw = b'\xed\x01' + base64.b64decode(open(sys.argv[1]).read().split()[1])[-32:] 70 A = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' 71 n = int.from_bytes(raw, 'big'); s = '' 72 while n: n, r = divmod(n, 58); s = A[r] + s 73 print('did:key:z' + s) 74 EOF 75 } 76 login() { # key -> SES, TOK 77 code GET /login > /dev/null 78 local pn=$(hdr set-cookie | sed -n 's/^\([A-Za-z_-]*forge_pre\)=.*/\1/p') 79 local pre=$(hdr set-cookie | sed -n 's/^[A-Za-z_-]*forge_pre=\([0-9a-f]*\);.*/\1/p') 80 local cs=$(field csrf) no=$(field nonce) ts=$(field ts) 81 printf '%s' "interplane-forge-login:$no:$ts" | ssh-keygen -Y sign -f "$1" -n interplane-forge > "$T/sig" 2> /dev/null 82 code POST /login -H "Cookie: $pn=$pre" --data-urlencode "csrf=$cs" --data-urlencode "nonce=$no" \ 83 --data-urlencode "ts=$ts" --data-urlencode "did=$(did "$1.pub")" --data-urlencode "sig@$T/sig" > /dev/null 84 SES=$(grep -i "^set-cookie:" "$T/hdr" | cut -d" " -f2- | tr -d "\r" | sed -n 's/^\([A-Za-z_-]*forge_session=[0-9a-f]*\);.*/\1/p') 85 code GET /settings -H "Cookie: $SES" > /dev/null 86 TOK=$(field csrf) 87 } 88 login "$T/k2"; SES2=$SES; TOK2=$TOK 89 login "$T/k1" 90 [ ${#TOK} = 32 ] && [ ${#TOK2} = 32 ] && [ "$TOK" != "$TOK2" ] && ok "two sessions signed in" || { bad "sign-in failed ($SES / $TOK)"; exit 1; } 91 CK="Cookie: $SES" 92 93 # ---- forms 94 c=$(code GET "$R/issues/new"); check "$c" 200 "GET issues/new signed out" 95 has 'Sign in with your key' && ! has 'name="title"' && ok "NEG signed out: sign-in link, no form" || bad "signed-out new-issue page" 96 c=$(code GET "$R/issues/new" -H "$CK"); check "$c" 200 "GET issues/new signed in" 97 has "action=\"$R/issues/new\"" && has 'name="title"' && has "value=\"$TOK\"" && ok "new-issue form with this session's CSRF token" || bad "new-issue form" 98 c=$(code GET "$R/patches/new" -H "$CK"); has 'name="draft"' && ok "new-patch form has the draft box" || bad "new-patch form ($c)" 99 c=$(code GET "$R/issues" -H "$CK"); has "href=\"$R/issues/new\"" && ok "issue list links the new-issue form" || bad "issue list link" 100 101 # ---- write gate (contract section 5): signed out, CSRF, then the checks 102 printf 'Title one\n' > "$T/t" 103 c=$(code POST "$R/issues/new" --data-urlencode "csrf=$TOK" --data-urlencode "title=x" --data-urlencode "body=y") 104 check "$c" 303 "NEG signed-out POST redirects"; check "$(hdr location)" "/login" "NEG signed-out POST goes to login" 105 c=$(code POST "$R/issues/new" -H "$CK" --data-urlencode "csrf=$TOK2" --data-urlencode "title=x"); check "$c" 403 "NEG other session's CSRF token" 106 c=$(code POST "$R/issues/new" -H "$CK" --data-urlencode "title=x"); check "$c" 403 "NEG missing CSRF token" 107 c=$(code POST "$R/issues/new" -H "$CK" --data-urlencode "csrf=$TOK" --data-urlencode "title="); check "$c" 400 "NEG empty title" 108 has 'The title must be one line' && ok "empty title message" || bad "empty title message" 109 c=$(code POST "$R/issues/new" -H "$CK" --data-urlencode "csrf=$TOK" --data-urlencode "title=a 110 b"); check "$c" 400 "NEG two-line title" 111 c=$(code POST "$R/issues/new" -H "$CK" --data-urlencode "csrf=$TOK" --data-urlencode "title=$(printf 'x%.0s' $(seq 257))"); check "$c" 400 "NEG 257-byte title" 112 c=$(code POST "$R/issues/new" -H "$CK" --data "csrf=$TOK&title=ok&body=%FF%FE"); check "$c" 400 "NEG invalid UTF-8 body" 113 has 'must be UTF-8' && ok "UTF-8 message" || bad "UTF-8 message" 114 c=$(code POST "$R/issues/new" -H "$CK" --data "csrf=$TOK&title=ok&body=a%01b"); check "$c" 400 "NEG control byte in body" 115 c=$(code POST "$R/issues/new" -H "$CK" --data "csrf=$TOK&title=ok&body=%ED%A0%80"); check "$c" 400 "NEG UTF-16 surrogate in body" 116 head -c 60001 /dev/zero | tr '\0' a > "$T/big" 117 c=$(code POST "$R/issues/new" -H "$CK" --data-urlencode "csrf=$TOK" --data-urlencode "title=ok" --data-urlencode "body@$T/big"); check "$c" 400 "NEG 60001-byte body" 118 has 'longer than 60000' && ok "length message" || bad "length message" 119 head -c 60000 /dev/zero | tr '\0' a > "$T/big" 120 c=$(code POST "$R/issues/new" -H "$CK" --data-urlencode "csrf=$TOK" --data-urlencode "title=ok" --data-urlencode "body@$T/big"); check "$c" 303 "60000-byte body accepted" 121 c=$(code POST "$R/issues/`0000000000000000000000000000000000000000`/compose" -H "$CK" --data-urlencode "csrf=$TOK" --data-urlencode "body=hi"); check "$c" 404 "NEG comment on an unknown issue" 122 c=$(code POST "$R/issues/new/x" -H "$CK" --data-urlencode "csrf=$TOK"); check "$c" 405 "NEG POST on an unknown route" 123 124 # ---- new issue, with an XSS body; the published issue must render it inert 125 IP=10.9.0.2 126 printf 'Hello there\r\n\r\n\n[x](javascript:alert(1))