interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / social / tests / run.sh
| 1 | #!/bin/bash |
| 2 | # ForgeSocial end-to-end test (HOST_TESTED on spark). Build, keys and sign-in helpers: tests/lib.sh. |
| 3 | # Checks sign-in, display name, star 1 / unstar 0 / double star 1, favorites, watch, follow, the JSON |
| 4 | # forms (forge/stargazers/1, forge/stars/1, forge/user/1), the |
| 5 | # __Host- cookies under both bases, and the negative controls: forged signature, wrong DID, replayed |
| 6 | # nonce, a challenge retired by one failed attempt, missing CSRF, unsigned write, open redirect, |
| 7 | # tampered log (detected at start, social off). The sign-in lockout test is tests/lockout.sh. |
| 8 | # Usage: social/tests/run.sh (from ~/work/forge/web; cores 2) |
| 9 | . "$(dirname "$0")/lib.sh" |
| 10 | echo "test repo $R" |
| 11 | echo "throwaway DID $D1" |
| 12 | start |
| 13 | |
| 14 | login_get |
| 15 | [ ${#PRE} = 32 ] && ok "pre cookie issued" || bad "pre cookie: '$PRE'" |
| 16 | check "$PREN" "__Host-forge_pre" "pre cookie has the __Host- prefix" |
| 17 | grep -q "interplane-forge-login:$NONCE:$TS" "$T/body" && ok "challenge shown" || bad "challenge not in page" |
| 18 | |
| 19 | # NEGATIVE: signature from another key for D1 -> 403 |
| 20 | sign "$T/k2"; c=$(login_post "$D1"); check "$c" 403 "NEG forged signature (k2 signs for D1) refused" |
| 21 | # NEGATIVE: that failed attempt retired the challenge: the right key on it now is refused |
| 22 | sign "$T/k1"; c=$(login_post "$D1"); check "$c" 403 "NEG challenge retired by one failed attempt" |
| 23 | grep -q "already used" "$T/body" && ok "retired challenge says already used" || bad "retired challenge message" |
| 24 | # NEGATIVE: garbage signature body -> 400 |
| 25 | login_get; echo garbage > "$T/sig"; c=$(login_post "$D1"); check "$c" 400 "NEG malformed signature refused" |
| 26 | # NEGATIVE: tampered form MAC -> 403 |
| 27 | login_get; sign "$T/k1"; CSRF=0000${CSRF:4}; c=$(login_post "$D1"); check "$c" 403 "NEG tampered form token refused" |
| 28 | # NEGATIVE: missing pre cookie -> 403 |
| 29 | login_get; sign "$T/k1"; PRE=; c=$(login_post "$D1"); check "$c" 403 "NEG missing pre cookie refused" |
| 30 | |
| 31 | # POSITIVE: real sign-in |
| 32 | login_get; sign "$T/k1"; c=$(login_post "$D1"); check "$c" 303 "POST /login with valid signature" |
| 33 | SES=$(hdr set-cookie | sed -n 's/^__Host-forge_session=\([0-9a-f]*\);.*/\1/p') |
| 34 | SC=$(hdr set-cookie) |
| 35 | [ ${#SES} = 64 ] && ok "session cookie 256 bits" || bad "session cookie '$SC'" |
| 36 | case "$SC" in *HttpOnly*Secure*SameSite=Lax*"Path=/") ok "cookie HttpOnly Secure SameSite=Lax Path=/";; *) bad "cookie attrs: $SC";; esac |
| 37 | case "$SC" in *[Dd]omain*) bad "cookie has a Domain: $SC";; *) ok "cookie has no Domain";; esac |
| 38 | check "$(hdr location)" "/settings" "first sign-in goes to settings" |
| 39 | # NEGATIVE: replay of the same nonce -> 403 |
| 40 | c=$(login_post "$D1"); check "$c" 403 "NEG replayed nonce refused" |
| 41 | CK="Cookie: __Host-forge_session=$SES" |
| 42 | # NEGATIVE: the old cookie name is not a session |
| 43 | c=$(code GET /settings -H "Cookie: forge_session=$SES"); check "$c" 303 "NEG old cookie name forge_session is not a session" |
| 44 | |
| 45 | c=$(code GET /settings -H "$CK"); check "$c" 200 "GET /settings signed in" |
| 46 | TOK=$(field csrf) |
| 47 | [ ${#TOK} = 32 ] && ok "CSRF token in settings form" || bad "csrf token '$TOK'" |
| 48 | grep -q "${D1#did:key:}" "$T/body" && ok "settings shows the DID" || bad "DID not on settings" |
| 49 | c=$(code POST /settings/name -H "$CK" --data-urlencode "csrf=$TOK" --data-urlencode "name=tester-one"); check "$c" 303 "set display name" |
| 50 | c=$(code GET /tester-one); check "$c" 200 "profile /tester-one" |
| 51 | grep -q "${D1#did:key:}" "$T/body" && ok "profile shows the DID" || bad "profile lacks DID" |
| 52 | # NEGATIVE: reserved name |
| 53 | c=$(code POST /settings/name -H "$CK" --data-urlencode "csrf=$TOK" --data-urlencode "name=settings"); check "$c" 400 "NEG reserved name refused" |
| 54 | c=$(code POST /settings/name -H "$CK" --data-urlencode "csrf=$TOK" --data-urlencode "name=drakestapleton"); check "$c" 409 "NEG owner's name cannot be taken" |
| 55 | c=$(code POST /settings/name -H "$CK" --data-urlencode "csrf=$TOK" --data-urlencode "name=Bad Name"); check "$c" 400 "NEG invalid name refused" |
| 56 | |
| 57 | # ---- stars |
| 58 | check "$(stars)" 0 "stars start at 0 (no seeded stars)" |
| 59 | # NEGATIVE: not signed in -> 303 to login, count unchanged |
| 60 | c=$(code POST "$R/star" --data-urlencode "csrf=$TOK"); check "$c" 303 "NEG unsigned star redirects" |
| 61 | case "$(hdr location)" in */login) ok "NEG unsigned star -> /login";; *) bad "unsigned star location $(hdr location)";; esac |
| 62 | # NEGATIVE: missing CSRF -> 403 |
| 63 | c=$(code POST "$R/star" -H "$CK"); check "$c" 403 "NEG star without CSRF" |
| 64 | c=$(code POST "$R/star" -H "$CK" --data-urlencode "csrf=${TOK%?}x"); check "$c" 403 "NEG star with wrong CSRF" |
| 65 | # NEGATIVE: GET on a write route is not a write |
| 66 | code GET "$R/star" -H "$CK" >/dev/null |
| 67 | check "$(stars)" 0 "negatives left the count at 0" |
| 68 | c=$(code POST "$R/star" -H "$CK" --data-urlencode "csrf=$TOK" --data-urlencode "back=$R"); check "$c" 303 "star" |
| 69 | check "$(hdr location)" "$R" "star redirects back" |
| 70 | check "$(stars)" 1 "star -> 1" |
| 71 | c=$(code POST "$R/star" -H "$CK" --data-urlencode "csrf=$TOK"); check "$c" 303 "double star" |
| 72 | check "$(stars)" 1 "double star stays 1" |
| 73 | grep -q "tester-one" "$T/body" && ok "stargazers lists tester-one" || bad "stargazer missing" |
| 74 | c=$(code POST "$R/unstar" -H "$CK" --data-urlencode "csrf=$TOK"); check "$c" 303 "unstar" |
| 75 | check "$(stars)" 0 "unstar -> 0" |
| 76 | check "$(jstars)" "forge/stargazers/1 0 0" "JSON stargazers after unstar: count 0, no users" |
| 77 | c=$(code POST "$R/star" -H "$CK" --data-urlencode "csrf=$TOK"); check "$(stars)" 1 "star again -> 1" |
| 78 | check "$(jstars)" "forge/stargazers/1 1 1" "JSON stargazers: count 1, one user" |
| 79 | # NEGATIVE: open redirect |
| 80 | c=$(code POST "$R/star" -H "$CK" --data-urlencode "csrf=$TOK" --data-urlencode "back=//evil.example/x") |
| 81 | check "$(hdr location)" "$R" "NEG back=//evil refused" |
| 82 | |
| 83 | # ---- favorites (private) and watch |
| 84 | c=$(code POST "$R/fav" -H "$CK" --data-urlencode "csrf=$TOK"); check "$c" 303 "favorite" |
| 85 | c=$(code GET "/stars?tab=favorites" -H "$CK"); check "$c" 200 "GET /stars?tab=favorites" |
| 86 | grep -q "$R\"" "$T/body" && ok "favorites lists the repo" || bad "favorite missing" |
| 87 | c=$(code GET "/stars?tab=favorites"); check "$c" 303 "NEG favorites need a session" |
| 88 | c=$(code GET "/stars?tab=favorites" -H 'Accept: application/json'); check "$c" 303 "NEG JSON favorites need a session" |
| 89 | code GET "/stars?tab=favorites" -H "$CK" -H 'Accept: application/json' >/dev/null |
| 90 | check "$(python3 -c 'import json,sys; d=json.load(open(sys.argv[1])); print(d["schema"], d["user"], d["tab"], [r["path"] for r in d["repos"]])' "$T/body")" \ |
| 91 | "forge/stars/1 tester-one favorites ['$R']" "JSON /stars?tab=favorites lists the repo" |
| 92 | c=$(code POST "$R/watch" -H "$CK" --data-urlencode "csrf=$TOK"); check "$c" 303 "watch" |
| 93 | code GET "$R/watchers" >/dev/null; grep -q "tester-one" "$T/body" && ok "watchers lists tester-one" || bad "watcher missing" |
| 94 | |
| 95 | # ---- follow the owner |
| 96 | c=$(code POST /follow -H "$CK" --data-urlencode "csrf=$TOK" --data-urlencode "user=drakestapleton"); check "$c" 303 "follow drakestapleton" |
| 97 | c=$(code GET "/drakestapleton?tab=followers"); check "$c" 200 "owner followers tab" |
| 98 | grep -q "tester-one" "$T/body" && ok "followers lists tester-one" || bad "follower missing" |
| 99 | c=$(code GET "/tester-one?tab=following"); grep -q "drakestapleton" "$T/body" && ok "following lists drakestapleton" || bad "following missing" |
| 100 | code GET "/tester-one/following" -H 'Accept: application/json' >/dev/null |
| 101 | check "$(python3 -c 'import json,sys; d=json.load(open(sys.argv[1])); print(d["schema"], d["user"], d["tab"], d["starred"], [u["path"] for u in d["users"]])' "$T/body")" \ |
| 102 | "forge/user/1 tester-one following None ['/drakestapleton']" "JSON /tester-one/following lists drakestapleton" |
| 103 | c=$(code POST /follow -H "$CK" --data-urlencode "csrf=$TOK" --data-urlencode "user=tester-one"); check "$c" 400 "NEG self-follow refused" |
| 104 | c=$(code POST /unfollow -H "$CK" --data-urlencode "csrf=$TOK" --data-urlencode "user=drakestapleton"); check "$c" 303 "unfollow" |
| 105 | code GET "/drakestapleton?tab=followers" >/dev/null |
| 106 | grep -q "tester-one" "$T/body" && bad "unfollow left the follower" || ok "unfollow removes the follower" |
| 107 | |
| 108 | # ---- base path: aienos.com/forge/ |
| 109 | c=$(code GET /login -H "X-Forge-Base: /forge/"); check "$c" 200 "GET /login under /forge/" |
| 110 | grep -q 'action="/forge/login"' "$T/body" && ok "form action under /forge/" || bad "form action not based" |
| 111 | check "$(hdr set-cookie | cut -d= -f1)" "__Host-forge_pre" "pre cookie name under /forge/" |
| 112 | case "$(hdr set-cookie)" in *"Path=/") ok "pre cookie Path=/ under /forge/ (__Host-)";; *) bad "cookie path $(hdr set-cookie)";; esac |
| 113 | # full sign-in under /forge/ (nginx strips /forge/; the browser sends a Path=/ cookie to /forge/...) |
| 114 | login_get -H "X-Forge-Base: /forge/"; sign "$T/k1"; c=$(login_post "$D1" -H "X-Forge-Base: /forge/"); check "$c" 303 "sign-in under /forge/" |
| 115 | check "$(hdr location)" "/forge/" "sign-in under /forge/ returns to /forge/" |
| 116 | SES2=$(hdr set-cookie | sed -n 's/^__Host-forge_session=\([0-9a-f]*\);.*/\1/p') |
| 117 | case "$(hdr set-cookie)" in *"Path=/") ok "session cookie Path=/ under /forge/";; *) bad "session cookie $(hdr set-cookie)";; esac |
| 118 | c=$(code GET /settings -H "X-Forge-Base: /forge/" -H "Cookie: __Host-forge_session=$SES2"); check "$c" 200 "session works under /forge/" |
| 119 | grep -q 'action="/forge/settings/name"' "$T/body" && ok "settings forms under /forge/" || bad "settings form base" |
| 120 | |
| 121 | # ---- log: append-only chain survives restart; then tamper |
| 122 | EV=$(wc -l < "$T/social/social.log") |
| 123 | STARS=$(grep -c '"action":"star"' "$T/social/social.log") |
| 124 | check "$STARS" 2 "log holds exactly 2 star events (double star wrote nothing)" |
| 125 | stop; start |
| 126 | check "$(stars)" 1 "count rebuilt from the log after restart" |
| 127 | c=$(code GET /settings -H "$CK"); check "$c" 200 "session survives restart" |
| 128 | # in-process reload (SIGHUP path: the flag file makes main.om call soload again) must not double counts |
| 129 | touch "$T/data/reload"; sleep 2; curl -s -o /dev/null "$B/"; sleep 1 |
| 130 | [ "$(grep -c 'reloaded' "$T/forged.err")" -ge 3 ] && ok "flag-file reload ran soload again" || bad "reload not seen: $(grep -c reloaded "$T/forged.err")" |
| 131 | check "$(stars)" 1 "count still 1 after in-process reload" |
| 132 | c=$(code GET /settings -H "$CK"); check "$c" 200 "session survives in-process reload" |
| 133 | stop |
| 134 | # independent oracle: each line's prev = sha256 of the previous line (no newline); seq counts up |
| 135 | python3 - "$T/social/social.log" <<'EOF' && ok "chain verified by an independent sha256 oracle" || bad "chain oracle" |
| 136 | import sys, hashlib, json |
| 137 | L = open(sys.argv[1], 'rb').read().split(b'\n') |
| 138 | assert L[-1] == b'', 'log must end in a newline' |
| 139 | L = L[:-1]; prev = '0' * 64 |
| 140 | for i, l in enumerate(L): |
| 141 | e = json.loads(l) |
| 142 | assert e['seq'] == i + 1 and e['prev'] == prev, (i, e) |
| 143 | prev = hashlib.sha256(l).hexdigest() |
| 144 | EOF |
| 145 | cp "$T/social/social.log" "$T/log.good" |
| 146 | sed -i '0,/"action":"star"/s//"action":"fav"/' "$T/social/social.log" |
| 147 | start |
| 148 | grep -q "SOCIAL-LOG TAMPER line" "$T/forged.err" && ok "NEG tampered log line detected at start" || bad "tamper not detected" |
| 149 | c=$(code POST "$R/star" -H "$CK" --data-urlencode "csrf=$TOK"); [ "$c" != 200 ] && [ "$c" != 303 -o "$(hdr location)" = /login ] && ok "NEG writes refused after tamper ($c)" || bad "write after tamper: $c" |
| 150 | c=$(code GET /login); check "$c" 503 "NEG sign-in off after tamper" |
| 151 | c=$(code GET "$R/stargazers"); [ "$c" != 500 ] && ok "pages still serve after tamper ($c)" || bad "500 after tamper" |
| 152 | check "$(wc -l < "$T/social/social.log")" "$EV" "tampered log was not appended to" |
| 153 | stop |
| 154 | # NEGATIVE: a truncated log (last event dropped) no longer matches social.head |
| 155 | head -n -1 "$T/log.good" > "$T/social/social.log" |
| 156 | : > "$T/forged.err" |
| 157 | start |
| 158 | grep -q "social.head does not match" "$T/forged.err" && ok "NEG truncated log detected at start" || bad "truncation not detected: $(cat "$T/forged.err")" |
| 159 | c=$(code GET /login); check "$c" 503 "NEG sign-in off after truncation" |
| 160 | stop |
| 161 | # recovery control: the untouched log loads again |
| 162 | cp "$T/log.good" "$T/social/social.log"; : > "$T/forged.err" |
| 163 | start |
| 164 | grep -q "SOCIAL-LOG" "$T/forged.err" && bad "good log refused: $(cat "$T/forged.err")" || ok "untouched log loads cleanly" |
| 165 | check "$(stars)" 1 "count back to 1 from the untouched log" |
| 166 | # NEGATIVE: write limits (per IP 30/min in the server, per DID 30/min in social) |
| 167 | N0=$(wc -l < "$T/social/social.log"); L429=0 |
| 168 | for i in $(seq 1 20); do |
| 169 | for a in unstar star; do |
| 170 | c=$(code POST "$R/$a" -H "$CK" --data-urlencode "csrf=$TOK") |
| 171 | [ "$c" = 429 ] && L429=$((L429+1)) |
| 172 | done |
| 173 | done |
| 174 | N1=$(wc -l < "$T/social/social.log") |
| 175 | [ "$L429" -gt 0 ] && ok "NEG write flood hits 429 ($L429 of 40 refused)" || bad "no 429 in 40 writes" |
| 176 | [ $((N1-N0)) -le 30 ] && ok "flood appended at most 30 events ($((N1-N0)))" || bad "flood appended $((N1-N0))" |
| 177 | # NEGATIVE: sign-in attempts per IP (10/min) |
| 178 | L429=0 |
| 179 | for i in $(seq 1 12); do c=$(code POST /login --data-urlencode "nonce=x"); [ "$c" = 429 ] && L429=$((L429+1)); done |
| 180 | [ "$L429" -gt 0 ] && ok "NEG sign-in flood hits 429 ($L429 of 12)" || bad "no 429 in 12 sign-in posts" |
| 181 | |
| 182 | echo "---- $PASS passed, $FAIL failed" |
| 183 | [ "$FAIL" = 0 ] |