interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / social / tests / run.sh

183 lines · 12756 bytes Raw Blame History
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"
10echo "test repo $R"
11echo "throwaway DID $D1"
12start
13
14login_get
15[ ${#PRE} = 32 ] && ok "pre cookie issued" || bad "pre cookie: '$PRE'"
16check "$PREN" "__Host-forge_pre" "pre cookie has the __Host- prefix"
17grep -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
20sign "$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
22sign "$T/k1"; c=$(login_post "$D1"); check "$c" 403 "NEG challenge retired by one failed attempt"
23grep -q "already used" "$T/body" && ok "retired challenge says already used" || bad "retired challenge message"
24# NEGATIVE: garbage signature body -> 400
25login_get; echo garbage > "$T/sig"; c=$(login_post "$D1"); check "$c" 400 "NEG malformed signature refused"
26# NEGATIVE: tampered form MAC -> 403
27login_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
29login_get; sign "$T/k1"; PRE=; c=$(login_post "$D1"); check "$c" 403 "NEG missing pre cookie refused"
30
31# POSITIVE: real sign-in
32login_get; sign "$T/k1"; c=$(login_post "$D1"); check "$c" 303 "POST /login with valid signature"
33SES=$(hdr set-cookie | sed -n 's/^__Host-forge_session=\([0-9a-f]*\);.*/\1/p')
34SC=$(hdr set-cookie)
35[ ${#SES} = 64 ] && ok "session cookie 256 bits" || bad "session cookie '$SC'"
36case "$SC" in *HttpOnly*Secure*SameSite=Lax*"Path=/") ok "cookie HttpOnly Secure SameSite=Lax Path=/";; *) bad "cookie attrs: $SC";; esac
37case "$SC" in *[Dd]omain*) bad "cookie has a Domain: $SC";; *) ok "cookie has no Domain";; esac
38check "$(hdr location)" "/settings" "first sign-in goes to settings"
39# NEGATIVE: replay of the same nonce -> 403
40c=$(login_post "$D1"); check "$c" 403 "NEG replayed nonce refused"
41CK="Cookie: __Host-forge_session=$SES"
42# NEGATIVE: the old cookie name is not a session
43c=$(code GET /settings -H "Cookie: forge_session=$SES"); check "$c" 303 "NEG old cookie name forge_session is not a session"
44
45c=$(code GET /settings -H "$CK"); check "$c" 200 "GET /settings signed in"
46TOK=$(field csrf)
47[ ${#TOK} = 32 ] && ok "CSRF token in settings form" || bad "csrf token '$TOK'"
48grep -q "${D1#did:key:}" "$T/body" && ok "settings shows the DID" || bad "DID not on settings"
49c=$(code POST /settings/name -H "$CK" --data-urlencode "csrf=$TOK" --data-urlencode "name=tester-one"); check "$c" 303 "set display name"
50c=$(code GET /tester-one); check "$c" 200 "profile /tester-one"
51grep -q "${D1#did:key:}" "$T/body" && ok "profile shows the DID" || bad "profile lacks DID"
52# NEGATIVE: reserved name
53c=$(code POST /settings/name -H "$CK" --data-urlencode "csrf=$TOK" --data-urlencode "name=settings"); check "$c" 400 "NEG reserved name refused"
54c=$(code POST /settings/name -H "$CK" --data-urlencode "csrf=$TOK" --data-urlencode "name=drakestapleton"); check "$c" 409 "NEG owner's name cannot be taken"
55c=$(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
58check "$(stars)" 0 "stars start at 0 (no seeded stars)"
59# NEGATIVE: not signed in -> 303 to login, count unchanged
60c=$(code POST "$R/star" --data-urlencode "csrf=$TOK"); check "$c" 303 "NEG unsigned star redirects"
61case "$(hdr location)" in */login) ok "NEG unsigned star -> /login";; *) bad "unsigned star location $(hdr location)";; esac
62# NEGATIVE: missing CSRF -> 403
63c=$(code POST "$R/star" -H "$CK"); check "$c" 403 "NEG star without CSRF"
64c=$(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
66code GET "$R/star" -H "$CK" >/dev/null
67check "$(stars)" 0 "negatives left the count at 0"
68c=$(code POST "$R/star" -H "$CK" --data-urlencode "csrf=$TOK" --data-urlencode "back=$R"); check "$c" 303 "star"
69check "$(hdr location)" "$R" "star redirects back"
70check "$(stars)" 1 "star -> 1"
71c=$(code POST "$R/star" -H "$CK" --data-urlencode "csrf=$TOK"); check "$c" 303 "double star"
72check "$(stars)" 1 "double star stays 1"
73grep -q "tester-one" "$T/body" && ok "stargazers lists tester-one" || bad "stargazer missing"
74c=$(code POST "$R/unstar" -H "$CK" --data-urlencode "csrf=$TOK"); check "$c" 303 "unstar"
75check "$(stars)" 0 "unstar -> 0"
76check "$(jstars)" "forge/stargazers/1 0 0" "JSON stargazers after unstar: count 0, no users"
77c=$(code POST "$R/star" -H "$CK" --data-urlencode "csrf=$TOK"); check "$(stars)" 1 "star again -> 1"
78check "$(jstars)" "forge/stargazers/1 1 1" "JSON stargazers: count 1, one user"
79# NEGATIVE: open redirect
80c=$(code POST "$R/star" -H "$CK" --data-urlencode "csrf=$TOK" --data-urlencode "back=//evil.example/x")
81check "$(hdr location)" "$R" "NEG back=//evil refused"
82
83# ---- favorites (private) and watch
84c=$(code POST "$R/fav" -H "$CK" --data-urlencode "csrf=$TOK"); check "$c" 303 "favorite"
85c=$(code GET "/stars?tab=favorites" -H "$CK"); check "$c" 200 "GET /stars?tab=favorites"
86grep -q "$R\"" "$T/body" && ok "favorites lists the repo" || bad "favorite missing"
87c=$(code GET "/stars?tab=favorites"); check "$c" 303 "NEG favorites need a session"
88c=$(code GET "/stars?tab=favorites" -H 'Accept: application/json'); check "$c" 303 "NEG JSON favorites need a session"
89code GET "/stars?tab=favorites" -H "$CK" -H 'Accept: application/json' >/dev/null
90check "$(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"
92c=$(code POST "$R/watch" -H "$CK" --data-urlencode "csrf=$TOK"); check "$c" 303 "watch"
93code GET "$R/watchers" >/dev/null; grep -q "tester-one" "$T/body" && ok "watchers lists tester-one" || bad "watcher missing"
94
95# ---- follow the owner
96c=$(code POST /follow -H "$CK" --data-urlencode "csrf=$TOK" --data-urlencode "user=drakestapleton"); check "$c" 303 "follow drakestapleton"
97c=$(code GET "/drakestapleton?tab=followers"); check "$c" 200 "owner followers tab"
98grep -q "tester-one" "$T/body" && ok "followers lists tester-one" || bad "follower missing"
99c=$(code GET "/tester-one?tab=following"); grep -q "drakestapleton" "$T/body" && ok "following lists drakestapleton" || bad "following missing"
100code GET "/tester-one/following" -H 'Accept: application/json' >/dev/null
101check "$(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"
103c=$(code POST /follow -H "$CK" --data-urlencode "csrf=$TOK" --data-urlencode "user=tester-one"); check "$c" 400 "NEG self-follow refused"
104c=$(code POST /unfollow -H "$CK" --data-urlencode "csrf=$TOK" --data-urlencode "user=drakestapleton"); check "$c" 303 "unfollow"
105code GET "/drakestapleton?tab=followers" >/dev/null
106grep -q "tester-one" "$T/body" && bad "unfollow left the follower" || ok "unfollow removes the follower"
107
108# ---- base path: aienos.com/forge/
109c=$(code GET /login -H "X-Forge-Base: /forge/"); check "$c" 200 "GET /login under /forge/"
110grep -q 'action="/forge/login"' "$T/body" && ok "form action under /forge/" || bad "form action not based"
111check "$(hdr set-cookie | cut -d= -f1)" "__Host-forge_pre" "pre cookie name under /forge/"
112case "$(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/...)
114login_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/"
115check "$(hdr location)" "/forge/" "sign-in under /forge/ returns to /forge/"
116SES2=$(hdr set-cookie | sed -n 's/^__Host-forge_session=\([0-9a-f]*\);.*/\1/p')
117case "$(hdr set-cookie)" in *"Path=/") ok "session cookie Path=/ under /forge/";; *) bad "session cookie $(hdr set-cookie)";; esac
118c=$(code GET /settings -H "X-Forge-Base: /forge/" -H "Cookie: __Host-forge_session=$SES2"); check "$c" 200 "session works under /forge/"
119grep -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
122EV=$(wc -l < "$T/social/social.log")
123STARS=$(grep -c '"action":"star"' "$T/social/social.log")
124check "$STARS" 2 "log holds exactly 2 star events (double star wrote nothing)"
125stop; start
126check "$(stars)" 1 "count rebuilt from the log after restart"
127c=$(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
129touch "$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")"
131check "$(stars)" 1 "count still 1 after in-process reload"
132c=$(code GET /settings -H "$CK"); check "$c" 200 "session survives in-process reload"
133stop
134# independent oracle: each line's prev = sha256 of the previous line (no newline); seq counts up
135python3 - "$T/social/social.log" <<'EOF' && ok "chain verified by an independent sha256 oracle" || bad "chain oracle"
136import sys, hashlib, json
137L = open(sys.argv[1], 'rb').read().split(b'\n')
138assert L[-1] == b'', 'log must end in a newline'
139L = L[:-1]; prev = '0' * 64
140for 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()
144EOF
145cp "$T/social/social.log" "$T/log.good"
146sed -i '0,/"action":"star"/s//"action":"fav"/' "$T/social/social.log"
147start
148grep -q "SOCIAL-LOG TAMPER line" "$T/forged.err" && ok "NEG tampered log line detected at start" || bad "tamper not detected"
149c=$(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"
150c=$(code GET /login); check "$c" 503 "NEG sign-in off after tamper"
151c=$(code GET "$R/stargazers"); [ "$c" != 500 ] && ok "pages still serve after tamper ($c)" || bad "500 after tamper"
152check "$(wc -l < "$T/social/social.log")" "$EV" "tampered log was not appended to"
153stop
154# NEGATIVE: a truncated log (last event dropped) no longer matches social.head
155head -n -1 "$T/log.good" > "$T/social/social.log"
156: > "$T/forged.err"
157start
158grep -q "social.head does not match" "$T/forged.err" && ok "NEG truncated log detected at start" || bad "truncation not detected: $(cat "$T/forged.err")"
159c=$(code GET /login); check "$c" 503 "NEG sign-in off after truncation"
160stop
161# recovery control: the untouched log loads again
162cp "$T/log.good" "$T/social/social.log"; : > "$T/forged.err"
163start
164grep -q "SOCIAL-LOG" "$T/forged.err" && bad "good log refused: $(cat "$T/forged.err")" || ok "untouched log loads cleanly"
165check "$(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)
167N0=$(wc -l < "$T/social/social.log"); L429=0
168for 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
173done
174N1=$(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)
178L429=0
179for 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
182echo "---- $PASS passed, $FAIL failed"
183[ "$FAIL" = 0 ]