interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / visits / tests / owner.sh
| 1 | #!/usr/bin/env bash |
| 2 | # visits/tests/owner.sh: gate for owner.om (the owner's pages inside the private network). |
| 3 | # OMV=/abs/omega-mv OWNER_TEST_PORT=18492 bash visits/tests/owner.sh |
| 4 | # Starts the server on 127.0.0.1 in a temp dir and checks the three files byte for byte, the |
| 5 | # headers, 404 and 405, and that it refuses every address that is not loopback or tailnet. |
| 6 | # Negative control: a mutant without the address rule does start on 0.0.0.0. |
| 7 | set -u |
| 8 | HERE=$(cd "$(dirname "$0")" && pwd) |
| 9 | V=$HERE/.. |
| 10 | OMV=${OMV:-$HOME/canon/omega-mv} |
| 11 | OMC=${OMC:-$OMV/seed/out/omc} |
| 12 | PIN=34d611ca0415eab1728d3234911d90e74af17ba9ab0c581d559f48274d7ca6d3 |
| 13 | PORT=${OWNER_TEST_PORT:-18492} |
| 14 | T=$(mktemp -d) || exit 2 |
| 15 | pid= |
| 16 | pass=0; fail=0 |
| 17 | ok() { pass=$((pass + 1)); echo "PASS $1"; } |
| 18 | no() { fail=$((fail + 1)); echo "FAIL $1"; } |
| 19 | eq() { if [ "$2" = "$3" ]; then ok "$1"; else no "$1: got '$2' want '$3'"; fi; } |
| 20 | cleanup() { [ -n "$pid" ] && kill "$pid" 2>/dev/null; wait 2>/dev/null; rm -rf "$T"; } |
| 21 | trap cleanup EXIT |
| 22 | got=$(sha256sum "$OMC" 2>/dev/null | cut -d' ' -f1) |
| 23 | if [ "$got" != "$PIN" ]; then echo "compiler pin mismatch: $got" >&2; exit 2; fi |
| 24 | if ss -ltn | grep -q ":$PORT "; then echo "NOT_RUN: port $PORT busy"; exit 3; fi |
| 25 | build() { { printf 'om2\n'; cat "$OMV/seed/net/http.om" "$OMV/seed/net/notls.om" "$1"; } | "$OMC" >"$2" 2>"$2.err" && chmod +x "$2"; } |
| 26 | build "$V/owner.om" "$T/owner" |
| 27 | eq "compile exit" "$?" 0 |
| 28 | |
| 29 | mkdir "$T/w" |
| 30 | printf '<!doctype html><title>t</title><p>owner page & more</p>\n' >"$T/w/visits.html" |
| 31 | cp "$V/visits.css" "$T/w/visits.css" |
| 32 | head -c 300000 /dev/urandom | base64 >"$T/w/tokens.css" |
| 33 | printf 'secret\n' >"$T/w/other.txt" |
| 34 | (cd "$T/w" && exec "$T/owner" 127 0 0 1 "$PORT") & |
| 35 | pid=$! |
| 36 | for _ in $(seq 50); do curl -s -o /dev/null "http://127.0.0.1:$PORT/" && break; sleep 0.1; done |
| 37 | get() { curl -s -o "$T/b" -D "$T/h" -w '%{http_code}' "$@"; } |
| 38 | hdr() { grep -i "^$1:" "$T/h" | tr -d '\r' | cut -d' ' -f2-; } |
| 39 | |
| 40 | eq "GET /" "$(get "http://127.0.0.1:$PORT/")" 200 |
| 41 | if cmp -s "$T/b" "$T/w/visits.html"; then ok "page body, byte for byte"; else no "page body, byte for byte"; fi |
| 42 | eq "page content type" "$(hdr content-type)" "text/html; charset=utf-8" |
| 43 | eq "never cached" "$(hdr cache-control)" "no-store" |
| 44 | eq "never indexed" "$(hdr x-robots-tag)" "noindex" |
| 45 | case "$(hdr content-security-policy)" in "default-src 'none'"*) ok "policy allows no script" ;; *) no "policy: $(hdr content-security-policy)" ;; esac |
| 46 | eq "GET /visits.css" "$(get "http://127.0.0.1:$PORT/visits.css")" 200 |
| 47 | if cmp -s "$T/b" "$T/w/visits.css"; then ok "stylesheet body"; else no "stylesheet body"; fi |
| 48 | eq "stylesheet content type" "$(hdr content-type)" "text/css; charset=utf-8" |
| 49 | eq "GET /tokens.css (405,000 bytes)" "$(get "http://127.0.0.1:$PORT/tokens.css")" 200 |
| 50 | if cmp -s "$T/b" "$T/w/tokens.css"; then ok "large file body, byte for byte"; else no "large file body, byte for byte"; fi |
| 51 | eq "a file that is not one of the three" "$(get "http://127.0.0.1:$PORT/other.txt")" 404 |
| 52 | eq "a path that climbs out" "$(get --path-as-is "http://127.0.0.1:$PORT/../w/other.txt")" 404 |
| 53 | eq "POST refused" "$(get -X POST --data x=1 "http://127.0.0.1:$PORT/")" 405 |
| 54 | eq "query string still serves the page" "$(get "http://127.0.0.1:$PORT/?x=1")" 200 |
| 55 | rm "$T/w/visits.html" |
| 56 | eq "page file missing" "$(get "http://127.0.0.1:$PORT/")" 404 |
| 57 | kill "$pid" 2>/dev/null; wait "$pid" 2>/dev/null; pid= |
| 58 | |
| 59 | # addresses: only loopback and tailnet may be used |
| 60 | for a in "0 0 0 0" "192 168 1 108" "8 8 8 8" "100 63 0 2" "100 128 0 2" "10 0 0 1"; do |
| 61 | (cd "$T/w" && timeout 3 "$T/owner" $a "$PORT") >/dev/null 2>&1 |
| 62 | eq "refuses to start on ${a// /.}" "$?" 1 |
| 63 | done |
| 64 | (cd "$T/w" && timeout 3 "$T/owner" 127 0 0 1) >/dev/null 2>&1; eq "missing port refused" "$?" 1 |
| 65 | (cd "$T/w" && timeout 3 "$T/owner" 127 0 0 1 70000) >/dev/null 2>&1; eq "port out of range refused" "$?" 1 |
| 66 | (cd "$T/w" && timeout 3 "$T/owner" 127 0 0 x "$PORT") >/dev/null 2>&1; eq "non-number refused" "$?" 1 |
| 67 | |
| 68 | # negative control: without the address rule the server starts on 0.0.0.0 (timeout ends it: 124) |
| 69 | sed 's/^if not oaddrok a b$/if false/' "$V/owner.om" >"$T/mutant.om" |
| 70 | if cmp -s "$V/owner.om" "$T/mutant.om"; then |
| 71 | no "mutant differs from source" |
| 72 | else |
| 73 | build "$T/mutant.om" "$T/mutant" |
| 74 | (cd "$T/w" && timeout 2 "$T/mutant" 0 0 0 0 "$PORT") >/dev/null 2>&1 |
| 75 | eq "mutant without the address rule starts on 0.0.0.0" "$?" 124 |
| 76 | fi |
| 77 | |
| 78 | echo "RESULT pass=$pass fail=$fail" |
| 79 | [ "$fail" -eq 0 ] |