interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / infra / test / edge-local.sh

261 lines · 19257 bytes Raw Blame History
1#!/usr/bin/env bash
2# Local functional test of the forge nginx edge (infra/nginx/*) WITHOUT touching the hub.
3# Runs a throwaway nginx (any host with nginx >= 1.25, python3, openssl, curl >= 8.2) on
4# loopback ports, with the real infra snippets (paths rewritten into a temp dir), a stand-in
5# upstream on 127.0.0.1:18480 that echoes what it received, and PROXY-protocol clients via
6# curl --haproxy-clientip. The stand-in is a test oracle only; it is not forged.
7# Usage: infra/test/edge-local.sh (exit 0 = every check passed)
8# Negative controls: EDGE_MUTANT=buffering (forged responses unbuffered), =base (interplane.io
9# X-Forge-Base not forced), =csp (forged's CSP hidden again, P2-3), =cspfallback (no edge CSP
10# when forged sends none or a weak one), and for the www.aienos.com/forge/ redirect =method (a POST
11# gets 301), =query (query string dropped), =headers (no security headers on the redirect) must
12# each make the run FAIL.
13set -u
14HERE=$(cd "$(dirname "$0")/.." && pwd) # infra/
15T=$(mktemp -d "${TMPDIR:-/tmp}/forge-edge.XXXXXX")
16P_IP=14444; P_IPPP=14448; P_AE=14446; UP=${EDGE_UP_PORT:-18480}
17PASS=0; FAIL=0
18ok() { PASS=$((PASS+1)); echo "PASS $*"; }
19bad() { FAIL=$((FAIL+1)); echo "FAIL $*"; }
20check() { local name=$1 want=$2 got=$3; [ "$got" = "$want" ] && ok "$name ($got)" || bad "$name: want '$want' got '$got'"; }
21has() { local name=$1 needle=$2 hay=$3; case "$hay" in *"$needle"*) ok "$name";; *) bad "$name: missing '$needle' in: $(printf %s "$hay" | head -c 300)";; esac; }
22hasnt() { local name=$1 needle=$2 hay=$3; case "$hay" in *"$needle"*) bad "$name: unexpected '$needle'";; *) ok "$name";; esac; }
23csps() { c -D - -o /dev/null "$1" | tr -d '\r' | sed -n 's/^[Cc]ontent-[Ss]ecurity-[Pp]olicy: //p'; }
24# exactly one CSP header, starting with $2
25csp1() { local name=$1 want=$2 got n; got=$(csps "$3"); n=$(printf '%s\n' "$got" | grep -c .)
26 if [ "$n" = 1 ] && case "$got" in "$want"*) true;; *) false;; esac; then ok "$name"
27 else bad "$name: want one CSP starting '$want', got $n: $(printf %s "$got" | tr '\n' '|')"; fi; }
28EDGE_PAGE="default-src 'self'; script-src 'none'"
29UP_PAGE="default-src 'none'; script-src 'none'; form-action 'self'"
30
31cleanup() { [ -f "$T/nginx.pid" ] && kill "$(cat "$T/nginx.pid")" 2>/dev/null; [ -n "${UPPID:-}" ] && kill "$UPPID" 2>/dev/null; rm -rf "$T"; }
32trap cleanup EXIT
33
34if (exec 3<>/dev/tcp/127.0.0.1/$UP) 2>/dev/null; then echo "port $UP busy (forged running here?) - refusing to test"; exit 2; fi
35
36mkdir -p "$T"/{conf.d,snippets,sites,certs,tmp,tmp/cache,www/ip/quick-start,www/ae}
37for n in interplane.io www.aienos.com; do
38 openssl req -x509 -newkey rsa:2048 -nodes -days 1 -subj "/CN=$n" -addext "subjectAltName=DNS:$n" \
39 -keyout "$T/certs/$n.key" -out "$T/certs/$n.pem" >/dev/null 2>&1 || { echo "openssl failed"; exit 2; }
40done
41echo STATIC-INDEX > "$T/www/ip/index.html"
42echo STATIC-QS > "$T/www/ip/quick-start/index.html"
43echo "User-agent: *" > "$T/www/ip/robots.txt"
44echo AE-INDEX > "$T/www/ae/index.html"; echo AE-404 > "$T/www/ae/404.html"; echo AE-MD > "$T/www/ae/x.md"
45
46rw() { sed -e "s#/etc/nginx/snippets/#$T/snippets/#g" -e "s#/var/cache/nginx/forge#$T/tmp/cache#g" -e "s#127\.0\.0\.1:18480#127.0.0.1:$UP#g" "$1"; }
47for f in "$HERE"/nginx/snippets/*.conf; do rw "$f" > "$T/snippets/$(basename "$f")"; done
48rw "$HERE/nginx/conf.d/forge.conf" > "$T/conf.d/forge.conf"
49rw "$HERE/nginx/sites-enabled/interplane.io" | sed \
50 -e "s#127.0.0.1:4444 ssl#127.0.0.1:$P_IP ssl#" -e "s#127.0.0.1:4448 ssl#127.0.0.1:$P_IPPP ssl#" \
51 -e "s#/etc/nginx/certs/interplane.io/fullchain.pem#$T/certs/interplane.io.pem#" \
52 -e "s#/etc/nginx/certs/interplane.io/key.pem#$T/certs/interplane.io.key#" \
53 -e "s#root /var/www/interplane.io;#root $T/www/ip;#" > "$T/sites/interplane.io"
54case "${EDGE_MUTANT:-}" in
55 buffering) sed -i.bak 's/proxy_buffering on;/proxy_buffering off;/' "$T/snippets/forge-proxy.conf" ;;
56 base) sed -i.bak '/X-Forge-Base/d' "$T/sites/interplane.io" ;;
57 csp) echo 'proxy_hide_header Content-Security-Policy;' >> "$T/snippets/forge-proxy.conf" ;;
58 cspfallback) sed -i.bak '/add_header Content-Security-Policy/d' "$T/snippets/forge-headers.conf" ;;
59 method) sed -i.bak 's/return 308 /return 301 /' "$T/snippets/aienos-forge.conf" ;;
60 query) sed -i.bak 's/\$is_args\$args//g' "$T/snippets/aienos-forge.conf" ;;
61 headers) sed -i.bak '/add_header/d' "$T/snippets/aienos-forge.conf" ;;
62 jsoncache) sed -i.bak 's/|\$forge_json//' "$T/sites/interplane.io" ;;
63 "") ;;
64 *) echo "unknown EDGE_MUTANT"; exit 2 ;;
65esac
66[ -n "${EDGE_MUTANT:-}" ] && echo "MUTANT ${EDGE_MUTANT} applied (this run must FAIL)"
67grep -q "$T/www/ip" "$T/sites/interplane.io" && grep -Eq ":$P_IPPP ssl( http2)? proxy_protocol" "$T/sites/interplane.io" || { echo "rewrite of interplane.io failed"; exit 2; }
68
69# Stand-in for the hub's www.aienos.com server: same root/404/regex shape as production.
70cat > "$T/sites/aienos" <<EOF
71server {
72 listen 127.0.0.1:$P_AE ssl proxy_protocol;
73 server_name www.aienos.com;
74 ssl_certificate $T/certs/www.aienos.com.pem; ssl_certificate_key $T/certs/www.aienos.com.key;
75 root $T/www/ae; absolute_redirect off; error_page 404 /404.html;
76 include $T/snippets/aienos-forge.conf;
77 location / { try_files \$uri \$uri/ \$uri/index.html =404; add_header Content-Security-Policy "frame-ancestors 'none'" always; }
78 location ~ \.md\$ { try_files \$uri =404; default_type text/markdown; }
79 location ~ \.om\$ { try_files \$uri =404; }
80}
81EOF
82cat > "$T/nginx.conf" <<EOF
83worker_processes 1; pid $T/nginx.pid; error_log $T/error.log notice;
84events { worker_connections 256; }
85http {
86 types { text/html html; text/plain txt; }
87 default_type application/octet-stream;
88 access_log off; gzip on;
89 client_body_temp_path $T/tmp/cb; proxy_temp_path $T/tmp/px; fastcgi_temp_path $T/tmp/fc;
90 uwsgi_temp_path $T/tmp/uw; scgi_temp_path $T/tmp/sc;
91 include $T/conf.d/forge.conf;
92 include $T/sites/interplane.io;
93 include $T/sites/aienos;
94}
95EOF
96
97cat > "$T/up.py" <<'EOF'
98import sys, time
99from http.server import BaseHTTPRequestHandler, HTTPServer
100class H(BaseHTTPRequestHandler):
101 protocol_version = "HTTP/1.0"
102 def log_message(self, *a): pass
103 def echo(self, code):
104 n = int(self.headers.get("Content-Length") or 0)
105 if n: self.rfile.read(n)
106 body = "UP method=%s path=%s base=%s realip=%s xff=%s host=%s proto=%s auth=%s accept=%s\n" % (
107 self.command, self.path, self.headers.get("X-Forge-Base"), self.headers.get("X-Real-IP"),
108 self.headers.get("X-Forwarded-For"), self.headers.get("Host"),
109 self.headers.get("X-Forwarded-Proto"), self.headers.get("Authorization"), self.headers.get("Accept"))
110 self.send_response(code)
111 self.send_header("Content-Type", "text/plain")
112 if "csp=none" in self.path: csp = None # edge must add its own
113 elif "csp=weak" in self.path: csp = "script-src *" # edge must add its own too
114 elif "/raw/" in self.path: csp = "sandbox; default-src 'none'; form-action 'none'"
115 else: csp = "default-src 'none'; script-src 'none'; form-action 'self'; frame-ancestors 'none'"
116 if csp: self.send_header("Content-Security-Policy", csp) # forged's, passed through (P2-3)
117 self.send_header("X-Powered-By", "standin")
118 if "/archive/" in self.path: # close-delimited, like forged
119 self.end_headers(); self.wfile.write(body.encode())
120 if "big=1" in self.path: # 32 MiB: more than any socket buffer
121 pad = b"x" * 65536
122 for i in range(512): self.wfile.write(pad)
123 self.wfile.write(b"END\n"); self.wfile.flush()
124 return
125 self.send_header("Content-Length", str(len(body))); self.end_headers(); self.wfile.write(body.encode())
126 def do_GET(self): self.echo(200)
127 def do_HEAD(self): self.echo(200)
128 def do_POST(self): self.echo(403)
129HTTPServer(("127.0.0.1", int(sys.argv[1])), H).serve_forever()
130EOF
131if [ -n "${EDGE_SYNTAX_ONLY:-}" ]; then # e.g. the hub's own nginx binary, nothing started
132 nginx -e "$T/error.log" -t -p "$T" -c "$T/nginx.conf"; exit $?
133fi
134python3 "$T/up.py" $UP & UPPID=$!
135nginx -e "$T/error.log" -t -p "$T" -c "$T/nginx.conf" 2>&1 | sed 's/^/nginx -t: /'
136nginx -e "$T/error.log" -p "$T" -c "$T/nginx.conf" || { echo "nginx failed to start"; cat "$T/error.log"; exit 2; }
137sleep 1
138
139IP="https://interplane.io:$P_IPPP"; AE="https://www.aienos.com:$P_AE"
140RES="--resolve interplane.io:$P_IPPP:127.0.0.1 --resolve interplane.io:$P_IP:127.0.0.1 --resolve www.aienos.com:$P_AE:127.0.0.1"
141c() { curl -sk --max-time 15 $RES --haproxy-clientip 203.0.113.7 "$@"; }
142code() { c -o /dev/null -w '%{http_code}' "$@"; }
143
144# --- interplane.io: static first ---
145check "ip GET / static" STATIC-INDEX "$(c "$IP/")"
146check "ip /robots.txt static" "User-agent: *" "$(c "$IP/robots.txt")"
147loc=$(c -o /dev/null -w '%{http_code} %{redirect_url}' "$IP/quick-start")
148has "ip /quick-start 301" "301" "$loc"
149h=$(c -D - -o /dev/null "$IP/quick-start")
150has "ip redirect is relative" "location: /quick-start/" "$(printf %s "$h" | tr 'A-Z' 'a-z')"
151hasnt "ip redirect leaks no port" ":$P_IPPP" "$h"
152h=$(c -D - -o /dev/null "$IP/")
153has "ip static CSP form-action self" "form-action 'self'" "$h"
154has "ip static CSP script-src none" "script-src 'none'" "$h"
155# --- interplane.io: everything else to forged ---
156b=$(c -H 'X-Forge-Base: /evil/' -H 'X-Real-IP: 1.2.3.4' -H 'X-Forwarded-For: 1.2.3.4' -H 'Authorization: Basic x' "$IP/drakestapleton/cuda-crack?tab=x")
157has "ip proxied path+query" "path=/drakestapleton/cuda-crack?tab=x" "$b"
158has "ip X-Forge-Base forced /" "base=/ " "$b"
159has "ip X-Real-IP from PROXY" "realip=203.0.113.7 " "$b"
160has "ip XFF from PROXY" "xff=203.0.113.7 " "$b"
161has "ip Authorization stripped" "auth=None" "$b"
162has "ip Host kept" "host=interplane.io" "$b"
163csp1 "ip forge page: forged's CSP passed through, alone" "$UP_PAGE" "$IP/drakestapleton"
164csp1 "ip raw: forged's sandbox CSP passed through, alone" "sandbox;" "$IP/drakestapleton/cuda-crack/raw/main/rows.txt"
165csp1 "ip page without forged CSP: edge policy" "$EDGE_PAGE" "$IP/drakestapleton?csp=none"
166csp1 "ip raw without forged CSP: edge sandbox" "sandbox; default-src 'none'; img-src" "$IP/drakestapleton/cuda-crack/raw/main/rows.txt?csp=none"
167h=$(csps "$IP/drakestapleton?csp=weak")
168has "ip weak forged CSP: edge policy added" "$EDGE_PAGE" "$h"
169has "ip weak forged CSP: forged's kept (both enforced)" "script-src *" "$h"
170h=$(c -D - -o /dev/null "$IP/drakestapleton")
171hasnt "ip X-Powered-By hidden" "standin" "$h"
172has "ip forge Referrer-Policy" "same-origin" "$(printf %s "$h" | tr 'A-Z' 'a-z')"
173check "ip PUT refused" 403 "$(code -X PUT "$IP/drakestapleton/cuda-crack")"
174check "ip POST >64k refused" 413 "$(head -c 70000 /dev/zero | c -o /dev/null -w '%{http_code}' --data-binary @- "$IP/drakestapleton/cuda-crack/star")"
175check "ip POST <64k reaches forged" 403 "$(code -d csrf=x "$IP/drakestapleton/cuda-crack/star")"
176# POST without a PROXY address (legacy 4444 path) is refused; GET still works there.
177check "ip POST w/o client addr -> 503" 503 "$(curl -sk --max-time 10 $RES -o /dev/null -w '%{http_code}' -d a=b "https://interplane.io:$P_IP/drakestapleton/cuda-crack/star")"
178check "ip GET on legacy listener" 200 "$(curl -sk --max-time 10 $RES -o /dev/null -w '%{http_code}' "https://interplane.io:$P_IP/drakestapleton")"
179# --- downloads: buffered (P2-1). The stand-in, like forged, serves one connection at a time. ---
180b=$(c "$IP/drakestapleton/cuda-crack/archive/refs/heads/main.zip")
181has "ip archive proxied" "path=/drakestapleton/cuda-crack/archive/refs/heads/main.zip" "$b"
182has "ip archive complete" "END" "$b"
183has "ip raw proxied" "path=/drakestapleton/cuda-crack/raw/main/README.md" "$(c "$IP/drakestapleton/cuda-crack/raw/main/README.md")"
184c --haproxy-clientip 192.0.2.50 --limit-rate 100k --max-time 8 -o /dev/null "$IP/drakestapleton/cuda-crack/archive/refs/heads/main.zip?big=1" & SLOW=$!
185sleep 2
186t=$(c --haproxy-clientip 192.0.2.51 -o /dev/null -w '%{http_code} %{time_total}' "$IP/drakestapleton")
187awk -v c="${t% *}" -v e="${t#* }" 'BEGIN{exit !(c == 200 && e < 2.0)}' && ok "ip page answers during a slow archive download ($t)" || bad "ip page stalled behind a slow download: $t"
188c --haproxy-clientip 192.0.2.50 --limit-rate 100k --max-time 8 -o /dev/null "$IP/drakestapleton/cuda-crack/archive/refs/heads/main.zip?big=1" & SLOW2=$!
189sleep 1
190check "ip 3rd concurrent download from one client" 429 "$(code --haproxy-clientip 192.0.2.50 "$IP/drakestapleton/cuda-crack/archive/refs/heads/main.zip")"
191check "ip download from another client" 200 "$(code --haproxy-clientip 192.0.2.52 "$IP/drakestapleton/cuda-crack/archive/refs/heads/main.zip")"
192kill $SLOW $SLOW2 2>/dev/null; wait $SLOW $SLOW2 2>/dev/null
193
194# --- www.aienos.com/forge/: a redirect to interplane.io (one-site, 2026-11-10); nothing reaches forged ---
195check "ae GET / static untouched" AE-INDEX "$(c "$AE/")"
196check "ae x.md static untouched" AE-MD "$(c "$AE/x.md")"
197check "ae /nope 404 page untouched" AE-404 "$(c "$AE/nope")"
198check "ae /forgex not redirected" AE-404 "$(c "$AE/forgex")"
199# rd URL [curl args] -> "<status> <Location as sent>"
200rd() { local u=$1; shift; c -o /dev/null -D - "$@" "$u" | tr -d '\r' | awk 'NR == 1 { s = $2 } tolower($1) == "location:" { l = $2 } END { print s, l }'; }
201IPL=https://interplane.io
202check "ae /forge" "301 $IPL/explore" "$(rd "$AE/forge")"
203check "ae /forge/" "301 $IPL/explore" "$(rd "$AE/forge/")"
204check "ae /forge?q=x keeps query" "301 $IPL/explore?q=x" "$(rd "$AE/forge?q=x")"
205check "ae /forge/?q=x keeps query" "301 $IPL/explore?q=x" "$(rd "$AE/forge/?q=x")"
206check "ae /forge/explore" "301 $IPL/explore" "$(rd "$AE/forge/explore")"
207check "ae /forge/search?q=omega" "301 $IPL/search?q=omega" "$(rd "$AE/forge/search?q=omega")"
208check "ae /forge/stars" "301 $IPL/stars" "$(rd "$AE/forge/stars")"
209check "ae /forge/login" "301 $IPL/login" "$(rd "$AE/forge/login")"
210check "ae profile" "301 $IPL/drakestapleton" "$(rd "$AE/forge/drakestapleton")"
211check "ae repo" "301 $IPL/drakestapleton/CUDApp" "$(rd "$AE/forge/drakestapleton/CUDApp")"
212check "ae releases" "301 $IPL/drakestapleton/hop/releases" "$(rd "$AE/forge/drakestapleton/hop/releases")"
213check "ae .md path not taken by the site" "301 $IPL/drakestapleton/cuda-crack/blob/main/README.md" "$(rd "$AE/forge/drakestapleton/cuda-crack/blob/main/README.md")"
214check "ae .om path not taken by the site" "301 $IPL/drakestapleton/omega/blob/main/x.om" "$(rd "$AE/forge/drakestapleton/omega/blob/main/x.om")"
215check "ae archive + query" "301 $IPL/drakestapleton/cuda-crack/archive/refs/heads/main.tar.gz?x=1" "$(rd "$AE/forge/drakestapleton/cuda-crack/archive/refs/heads/main.tar.gz?x=1")"
216check "ae rad:RID" "301 $IPL/rad:z631r2JJiVT8it4tST7RK24hpowi" "$(rd "$AE/forge/rad:z631r2JJiVT8it4tST7RK24hpowi")"
217check "ae percent-encoding kept" "301 $IPL/drakestapleton/a%20b/blob/main/%E2%9C%93.md?q=a%26b" "$(rd "$AE/forge/drakestapleton/a%20b/blob/main/%E2%9C%93.md?q=a%26b")"
218check "ae //forge/x -> explore" "301 $IPL/explore" "$(rd "$AE//forge/x" --path-as-is)"
219check "ae /forge//x" "301 $IPL/x" "$(rd "$AE/forge//x" --path-as-is)"
220check "ae encoded CRLF stays encoded" "301 $IPL/x%0d%0aSet-Cookie:%20a=b" "$(rd "$AE/forge/x%0d%0aSet-Cookie:%20a=b")"
221hasnt "ae no injected header" "set-cookie: a=b" "$(c -D - -o /dev/null "$AE/forge/x%0d%0aSet-Cookie:%20a=b" | tr 'A-Z' 'a-z')"
222check "ae HEAD 301" "301 $IPL/drakestapleton" "$(rd "$AE/forge/drakestapleton" -I)"
223check "ae POST 308 keeps the method" "308 $IPL/login" "$(rd "$AE/forge/login" -d user=x)"
224check "ae POST /forge 308" "308 $IPL/explore" "$(rd "$AE/forge" -d a=b)"
225check "ae PUT 308" "308 $IPL/drakestapleton/cuda-crack" "$(rd "$AE/forge/drakestapleton/cuda-crack" -X PUT)"
226for u in "$AE/forge" "$AE/forge/drakestapleton"; do
227 for m in GET POST; do
228 [ $m = POST ] && h=$(c -D - -o /dev/null -d a=b "$u") || h=$(c -D - -o /dev/null "$u")
229 l=$(printf %s "$h" | tr 'A-Z' 'a-z')
230 has "ae $m ${u#$AE} HSTS" "strict-transport-security: max-age=31536000; includesubdomains" "$l"
231 has "ae $m ${u#$AE} nosniff" "x-content-type-options: nosniff" "$l"
232 has "ae $m ${u#$AE} X-Frame-Options" "x-frame-options: deny" "$l"
233 has "ae $m ${u#$AE} Referrer-Policy" "referrer-policy: strict-origin-when-cross-origin" "$l"
234 has "ae $m ${u#$AE} CSP" "content-security-policy: frame-ancestors 'none'" "$l"
235 done
236done
237hasnt "ae redirect never proxied" "UP method=" "$(c "$AE/forge/drakestapleton/cuda-crack/archive/refs/heads/main.zip"; c -d a=b "$AE/forge/login")"
238
239# --- POST rate limit (20r/m, burst 10): one client is cut off, another is not ---
240codes=""; for i in $(seq 1 14); do codes="$codes $(c --haproxy-clientip 198.51.100.9 -o /dev/null -w '%{http_code}' -d a=b "$IP/drakestapleton/cuda-crack/star")"; done
241has "POST burst hits 429" "429" "$codes"
242check "first POST not limited" 403 "$(echo $codes | cut -d' ' -f1)"
243check "other client not limited" 403 "$(c --haproxy-clientip 198.51.100.10 -o /dev/null -w '%{http_code}' -d a=b "$IP/drakestapleton/cuda-crack/star")"
244codes=""; for i in $(seq 1 14); do codes="$codes $(c --haproxy-clientip 198.51.100.11 -o /dev/null -w '%{http_code}' "$IP/drakestapleton")"; done
245hasnt "GETs never POST-limited" "429" "$codes"
246
247# --- page cache + JSON cache key + crawler limit (L1058, L1139, L1142) ---
248h1=$(c -D - "$IP/open/")
249has "cache first anonymous HTML is MISS" "x-forge-cache: miss" "$(printf %s "$h1" | tr 'A-Z' 'a-z')"
250has "cache HTML body" "accept=*/*" "$h1"
251h2=$(c -D - "$IP/open/")
252has "cache second anonymous HTML hit" "x-forge-cache: hit" "$(printf %s "$h2" | tr 'A-Z' 'a-z')"
253hj=$(c -D - -H 'Accept: application/json' "$IP/open/")
254has "cache anonymous JSON is MISS" "x-forge-cache: miss" "$(printf %s "$hj" | tr 'A-Z' 'a-z')"
255has "cache anonymous JSON reaches forged" "accept=application/json" "$hj"
256hc=$(c -D - -H 'Cookie: forge_session=test' "$IP/open/")
257has "cookie bypasses page cache" "x-forge-cache: bypass" "$(printf %s "$hc" | tr 'A-Z' 'a-z')"
258check "L1057 overlong patch segment -> 404" 404 "$(code "$IP/drakestapleton/hop/patches/$(head -c 130 /dev/zero | tr '\0' a)")"
259
260echo "edge-local: $PASS passed, $FAIL failed"
261[ "$FAIL" = 0 ]