interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / server / tests / translate.sh

207 lines · 15179 bytes Raw Blame History
1#!/usr/bin/env bash
2# server/tests/translate.sh: gate for "readers translate with their own tools" (server/tr.om).
3# bash server/tests/translate.sh (spark; FORGED_CPUS default 12-14; FORGE_TEST_PORT default 18495)
4# bash server/tests/translate.sh --live the same response checks against https://interplane.io
5# forged translates nothing and stores no translation. This gate proves the pages are safe to hand to
6# any translator (the browser's own, an extension, a model the reader runs):
7# 1. every response (HTML, JSON, plain text, raw files, errors, redirects) carries a
8# Content-Security-Policy with script-src 'none', and no page holds a script or event attribute;
9# 2. every HTML page: <html lang="en">, every pre/code/kbd/samp inside translate="no", no hash or
10# Radicle id in text a translator may change (oracle: server/tests/translate_check.py, Python's
11# own HTML parser, not the Omega code under test);
12# 3. a hostile translator that rewrites every translatable text node with commands, fake hashes
13# and markup leaves every protected string, every link target and every hash/id byte-identical;
14# 4. the hints change nothing else: each page's <main> minus the hints is byte-identical to the same
15# page from this head built with the hint pass taken out of fsend0 (the send path before server/tr.om);
16# 5. ?format=text answers text/plain with the same CSP, inline code in backticks, no markup;
17# Negative controls (each must FAIL for its reason): a fixture page with a bare hash; a forged built
18# without the translate="no" attribute (tftrno emptied); a forged whose page CSP lacks
19# script-src 'none'; a hostile run on the fixture page; a hint pass that adds visible text (check 4).
20# Positive control: a change to a page's words, made in both builds, passes check 4. Read-only on real storage; never 18480,
21# ~/forge-data or 18091.
22set -u
23cd "$(dirname "$0")/../.."
24ROOT=$(pwd)
25CORES=${FORGED_CPUS:-12-14}
26PORT=${FORGE_TEST_PORT:-18495}
27OMC=$HOME/work/flux/omega-mv/seed/out/omc
28PY=/usr/bin/python3
29CHK=$ROOT/server/tests/translate_check.py
30export GIT_ALLOW_PROTOCOL=file GIT_NO_LAZY_FETCH=1 GIT_TERMINAL_PROMPT=0
31T=$(mktemp -d)
32pid=
33pass=0; fail=0
34ok() { pass=$((pass+1)); echo "PASS $*"; }
35bad() { fail=$((fail+1)); echo "FAIL $*"; }
36cleanup() { [ -n "$pid" ] && kill "$pid" 2>/dev/null; wait 2>/dev/null; [ -n "${KEEP:-}" ] && echo "kept $T" || rm -rf "$T"; }
37trap cleanup EXIT
38# pages: owner/repo pages of real public repositories plus the shared pages; "raw" ones are files
39R1=drakestapleton/aienux
40R2=drakestapleton/interplane-forge
41PAGES="explore $R1 $R2 $R1/tree/main $R1/blob/main/README.md $R2/blob/main/README.md $R1/commits $R1/branches $R1/releases $R1/patches $R1/issues search?q=omega no-such-owner/no-such-repo"
42RAWS="$R1/raw/main/README.md $R1.json explore.json"
43
44# hdrcsp FILE: the Content-Security-Policy values of a saved header block, one per line
45hdrcsp() { tr -d '\r' < "$1" | sed -n 's/^[Cc]ontent-[Ss]ecurity-[Pp]olicy: //p'; }
46# cspok FILE WHAT: exactly one CSP header and it says script-src 'none'
47cspok() {
48 local n
49 n=$(hdrcsp "$1" | wc -l)
50 if [ "$n" -lt 1 ]; then bad "$2: no Content-Security-Policy"; return 1; fi
51 if hdrcsp "$1" | grep -qv "script-src 'none'"; then bad "$2: a CSP without script-src 'none': $(hdrcsp "$1" | head -1)"; return 1; fi
52 ok "$2: CSP has script-src 'none'"
53}
54# fetch URLBASE PATH OUT [curl args]: body OUT, headers OUT.h, prints the status
55fetch() { local u=$1 p=$2 o=$3; shift 3; curl -s --max-time 30 -o "$o" -D "$o.h" -w '%{http_code}' "$@" "$u/$p"; }
56
57# respchecks URLBASE TAG [curl args]: checks 1, 2, 3 and 5 on every page of PAGES and RAWS
58respchecks() {
59 local u=$1 tg=$2; shift 2
60 local i=0 p st f
61 for p in $PAGES; do
62 i=$((i+1)); f=$T/$tg-$i.html
63 st=$(fetch "$u" "$p" "$f" "$@")
64 case $st in 200|404) ;; *) bad "$tg $p: HTTP $st"; continue ;; esac
65 cspok "$f.h" "$tg $p ($st)"
66 if "$PY" "$CHK" check "$f" > "$f.chk"; then ok "$tg $p: hints ($(cut -d' ' -f3- "$f.chk"))"; else bad "$tg $p: $(head -3 "$f.chk" | tr '\n' ' ')"; fi
67 if "$PY" "$CHK" hostile "$f" "$f.hostile" > "$f.hos"; then ok "$tg $p: $(cut -d' ' -f3- "$f.hos")"; else bad "$tg $p: $(head -3 "$f.hos" | tr '\n' ' ')"; fi
68 echo "$p $f" >> "$T/$tg.list"
69 done
70 for p in $RAWS; do
71 i=$((i+1)); f=$T/$tg-$i.raw
72 st=$(fetch "$u" "$p" "$f" "$@")
73 [ "$st" = 200 ] || { bad "$tg $p: HTTP $st"; continue; }
74 cspok "$f.h" "$tg $p"
75 done
76 # a redirect (rad: shortcut) and a POST refusal carry the policy too
77 st=$(fetch "$u" "login" "$T/$tg-post" -X POST "$@"); cspok "$T/$tg-post.h" "$tg POST /login ($st)"
78 # 5. the plain-text form
79 f=$T/$tg-text.txt
80 st=$(fetch "$u" "$R1?format=text" "$f" "$@")
81 [ "$st" = 200 ] || bad "$tg $R1?format=text: HTTP $st"
82 cspok "$f.h" "$tg $R1?format=text"
83 ct=$(tr -d '\r' < "$f.h" | sed -n 's/^[Cc]ontent-[Tt]ype: //p')
84 [ "$ct" = "text/plain; charset=utf-8" ] && ok "$tg text: content type $ct" || bad "$tg text: content type '$ct'"
85 grep -qE '`[^`]+`' "$f" && ok "$tg text: inline code in backticks" || bad "$tg text: no inline code in backticks"
86 grep -qE '<(p|div|a|span|code|pre|table|section|main|h[1-6])[ >]' "$f" && bad "$tg text: HTML markup left in the text" || ok "$tg text: no HTML markup"
87 head -1 "$f" | grep -q '^Interplane page as plain text' && ok "$tg text: preface line" || bad "$tg text: no preface"
88 grep -q '^# ' "$f" && ok "$tg text: README headings as #" || bad "$tg text: no # heading"
89 # file names in the breadcrumb (bay 1 finding 1 at 099a4b0b) and the footer link keeps the query (finding 3)
90 f=$T/$tg-crumbs.html; fetch "$u" "$R1/blob/main/README.md" "$f" "$@" > /dev/null
91 grep -q '<p translate="no" class="crumbs">' "$f" && ok "$tg blob page: breadcrumb marked translate=no" || bad "$tg blob page: breadcrumb not marked"
92 f=$T/$tg-crumbs.txt; fetch "$u" "$R1/blob/main/README.md?format=text" "$f" "$@" > /dev/null
93 grep -qE '^`[^`]*README\.md[^`]*`$' "$f" && ok "$tg text form: breadcrumb in backticks" || bad "$tg text form: breadcrumb not in backticks"
94 f=$T/$tg-tree.txt; fetch "$u" "$R1/tree/main?format=text" "$f" "$@" > /dev/null
95 # one row per line inside the fence: name, link target, size, each apart
96 awk '/^```$/{fz=!fz; next} fz && /^README\.md <\/[^ >]*\/blob\/main\/README\.md> [0-9]+$/{f=1} fz && /^\.aien\/ <\/[^ >]*\/tree\/main\/\.aien>$/{d=1} END{exit !(f && d)}' "$f" && ok "$tg text form: file list fenced, one row per line" || bad "$tg text form: file list not fenced as rows"
97 f=$T/$tg-q.html; fetch "$u" "search?q=omega" "$f" "$@" > /dev/null
98 grep -q 'search?q=omega&amp;format=text" rel="alternate"' "$f" && ok "$tg footer: plain-text link keeps the query" || bad "$tg footer: plain-text link drops the query"
99 f=$T/$tg-q.txt; fetch "$u" "search?q=omega&format=text" "$f" "$@" > /dev/null
100 head -1 "$f" | grep -q '^Interplane page as plain text' && ok "$tg text form of a query page" || bad "$tg text form of a query page"
101 # script-free: the HTML and the text never name a script
102 if grep -l -i '<script' "$T/$tg"-*.html 2>/dev/null | grep -q .; then bad "$tg: a page holds <script"; else ok "$tg: no page holds <script"; fi
103}
104
105if [ "${1:-}" = --live ]; then
106 respchecks https://interplane.io live
107 echo "translate.sh --live: pass=$pass fail=$fail"
108 [ "$fail" = 0 ]; exit
109fi
110
111[ -x "$OMC" ] || { echo "NOT_RUN: no compiler at $OMC"; exit 3; }
112PIN=$(sed -n 's/^PIN=//p' run.sh)
113[ "$(sha256sum "$OMC" | cut -d' ' -f1)" = "$PIN" ] || { echo "FAIL compiler is not the run.sh pin"; exit 1; }
114# the comparison build: the same composed program with fsend0's six ftrrun calls and the streamed shelf's
115# one (fsend5, /open/ and /open.json in whole-row batches) put back to fsendr
116NOHINT='s/^n = ulong ftrrun cs io l lh 0$/n = get flay 1/;s/^n = add n ulong ftrrun cs io 0 l 0$/n = add n l/;s/^n = add n ulong ftrrun cs io lh lf 0$/n = add n get flay 2/;s/^r = hfe r ftrrun cs io l lh 1$/r = hfe r fsendr cs io l get flay 1/;s/^r = hfe r ftrrun cs io 0 l 1$/r = hfe r fsendr cs io 0 l/;s/^r = hfe r ftrrun cs io lh lf 1$/r = hfe r fsendr cs io lh get flay 2/;s/^r = ftrrun cs io 0 get fobl 0 1$/r = fsendr cs io 0 get fobl 0/'
117
118# compose DIR OUT PORT [sed expr]: build like run.sh, data dir and port moved into $T
119compose() {
120 local d=$1 o=$2 p=$3 m=${4:-}
121 mkdir -p "$T/data-$p/logs"
122 [ -f "$HOME/forge-data/logs/forged.log" ] && cp "$HOME/forge-data/logs/forged.log" "$T/data-$p/logs/forged.log"
123 (cd "$d" && { printf 'om2\n'; cat $(./run.sh units); }) |
124 sed -e "s|^text tfdata \".*\"|text tfdata \"$T/data-$p/\"|" -e "s/18480/$p/g" > "$o.om"
125 if [ -n "$m" ]; then
126 cp "$o.om" "$o.om.0"; sed -i -e "$m" "$o.om"
127 cmp -s "$o.om.0" "$o.om" && { echo "MUTANT did not apply: $m"; exit 3; }
128 fi
129 taskset -c "$CORES" nice -n 10 "$OMC" < "$o.om" > "$o" 2> "$o.err" || { echo "BUILD FAILED $d ${m:+mutant $m}: $(cat "$o.err")"; exit 1; }
130 chmod 755 "$o"
131}
132start() { taskset -c "$CORES" nice -n 10 "$1" 2>> "$T/forged.err" & pid=$!; for _ in $(seq 100); do curl -s -o /dev/null "http://127.0.0.1:$PORT/explore" && return 0; sleep 0.1; done; echo "server did not start"; cat "$T/forged.err"; exit 1; }
133stop() { kill "$pid" 2>/dev/null; wait "$pid" 2>/dev/null; pid=; }
134L=http://127.0.0.1:$PORT
135
136compose "$ROOT" "$T/forged" "$PORT"
137compose "$ROOT" "$T/forged.base" "$PORT" "$NOHINT"
138[ "$(grep -c 'ftrrun cs io' "$T/forged.base.om")" = 0 ] || { echo "FAIL the comparison build still calls ftrrun"; exit 1; }
139echo "built head $(sha256sum < "$T/forged" | cut -c1-16), head without hints $(sha256sum < "$T/forged.base" | cut -c1-16)"
140
141for b in / /forge/; do
142 tg=head$(echo "$b" | tr -d /)
143 start "$T/forged"
144 respchecks "$L" "$tg" -H "X-Forge-Base: $b" -H "X-Real-IP: 127.0.0.1"
145 stop
146done
147
148# 4. the hints change nothing else: <main> of every page equals the base build's minus the hints
149start "$T/forged.base"
150i=0
151while read -r p f; do
152 i=$((i+1))
153 fetch "$L" "$p" "$T/base-$i.html" -H "X-Forge-Base: /" -H "X-Real-IP: 127.0.0.1" > /dev/null
154 "$PY" "$CHK" strip "$f" "$f.strip"
155 mh=$(awk '/<main id="main"/{on=1} on{print} /<\/main>/{on=0}' "$f.strip" | sha256sum | cut -c1-16)
156 mb=$(awk '/<main id="main"/{on=1} on{print} /<\/main>/{on=0}' "$T/base-$i.html" | sha256sum | cut -c1-16)
157 # the counts lines move with the logs between the two reads; compare without them
158 mh2=$(awk '/<main id="main"/{on=1} on{print} /<\/main>/{on=0}' "$f.strip" | grep -v '^<p class="meta">\(Clones\|Downloads\|Seeding\)' | sha256sum | cut -c1-16)
159 mb2=$(awk '/<main id="main"/{on=1} on{print} /<\/main>/{on=0}' "$T/base-$i.html" | grep -v '^<p class="meta">\(Clones\|Downloads\|Seeding\)' | sha256sum | cut -c1-16)
160 if [ "$mh" = "$mb" ] || [ "$mh2" = "$mb2" ]; then ok "unchanged $p: <main> minus hints = base ($mh2)"; else bad "changed $p: <main> minus hints $mh2, base $mb2"; diff <(awk '/<main id="main"/{on=1} on{print} /<\/main>/{on=0}' "$f.strip") <(awk '/<main id="main"/{on=1} on{print} /<\/main>/{on=0}' "$T/base-$i.html") | head -6; fi
161done < "$T/head.list"
162stop
163
164# ---- negative controls
165fx=$T/fixture.html
166printf '<!doctype html>\n<html lang="en"><body><main><p>Head <code translate="no">95d5f03</code> and bare 95d5f03ed369f8e2 here.</p></main></body></html>\n' > "$fx"
167if "$PY" "$CHK" check "$fx" > "$T/neg1" 2>&1; then bad "NEG fixture with a bare hash passed the oracle"; else grep -q 'translatable text holds 95d5f03ed369f8e2' "$T/neg1" && ok "NEG fixture with a bare hash: refused ($(head -1 "$T/neg1"))" || bad "NEG fixture refused for another reason: $(head -1 "$T/neg1")"; fi
168if "$PY" "$CHK" hostile "$fx" "$fx.h" > "$T/neg2" 2>&1; then bad "NEG hostile on the fixture passed"; else grep -q 'hash or id lost' "$T/neg2" && ok "NEG hostile translator on the fixture changes the bare hash: caught" || bad "NEG hostile refused for another reason: $(head -1 "$T/neg2")"; fi
169printf '<!doctype html>\n<html lang="en"><body><pre>rad clone rad:z3KFmLGeYdvDAgwpCgdBd1ENW7nU5</pre></body></html>\n' > "$T/fx2.html"
170if "$PY" "$CHK" check "$T/fx2.html" > "$T/neg3" 2>&1; then bad "NEG fixture with an unmarked pre passed"; else grep -q '<pre> outside' "$T/neg3" && ok "NEG fixture with an unmarked pre: refused" || bad "NEG pre refused for another reason: $(head -1 "$T/neg3")"; fi
171
172compose "$ROOT" "$T/forged.m1" "$PORT" 's/^text tftrno ".*"$/text tftrno " data-x=\\"no\\""/'
173start "$T/forged.m1"
174fetch "$L" "$R1" "$T/m1.html" -H "X-Forge-Base: /" -H "X-Real-IP: 127.0.0.1" > /dev/null
175stop
176if "$PY" "$CHK" check "$T/m1.html" > "$T/neg4" 2>&1; then bad "NEG forged without translate=\"no\" passed"; else ok "NEG forged without translate=\"no\": refused ($(grep -c FAIL "$T/neg4") findings, first: $(head -1 "$T/neg4" | cut -d: -f2-))"; fi
177
178compose "$ROOT" "$T/forged.m2" "$PORT" "s/^text tfcsp \"default-src 'none'; style-src 'self'; img-src 'self'; script-src 'none'; /text tfcsp \"default-src 'none'; style-src 'self'; img-src 'self'; /"
179start "$T/forged.m2"
180fetch "$L" "$R1" "$T/m2.html" -H "X-Forge-Base: /" -H "X-Real-IP: 127.0.0.1" > /dev/null
181stop
182p0=$pass f0=$fail
183cspok "$T/m2.html.h" "mutant" > "$T/neg5"
184if [ "$fail" -gt "$f0" ]; then fail=$f0; pass=$p0; ok "NEG forged whose page CSP lacks script-src 'none': refused ($(cut -d: -f2- "$T/neg5"))"; else pass=$p0; bad "NEG forged without script-src 'none' passed the CSP check"; fi
185
186# check 4's controls, on the build under test and its build without the hint pass
187# main4 FILE: <main> of a page without the counts lines, which move with the logs between two reads
188main4() { awk '/<main id="main"/{on=1} on{print} /<\/main>/{on=0}' "$1" | grep -v '^<p class="meta">\(Clones\|Downloads\|Seeding\)'; }
189# same4 HINTED PLAIN: HINTED minus the hints has the same <main> as PLAIN
190same4() { "$PY" "$CHK" strip "$1" "$1.strip" && [ "$(main4 "$1.strip" | sha256sum)" = "$(main4 "$2" | sha256sum)" ]; }
191# page4 BIN OUT: R1 from that build
192page4() { start "$1"; fetch "$L" "$R1" "$2" -H "X-Forge-Base: /" -H "X-Real-IP: 127.0.0.1" > /dev/null; stop; }
193compose "$ROOT" "$T/forged.m3" "$PORT" 's|^text tftrsp "<span translate=\\"no\\">"$|text tftrsp "<span translate=\\"no\\">*"|'
194page4 "$T/forged.m3" "$T/m3.html"; page4 "$T/forged.base" "$T/b3.html"
195if ! grep -q '<span translate="no">\*' "$T/m3.html"; then bad "NEG hint pass that adds visible text: not run, $R1 holds no hint span"
196elif same4 "$T/m3.html" "$T/b3.html"; then bad "NEG hint pass that adds visible text (a * in every hint span) passed check 4"
197else ok "NEG hint pass that adds visible text (a * in every hint span): caught by check 4"; fi
198LEGIT='s|>For developers: proof and records</h2>|>Records and proof, for developers</h2>|'
199compose "$ROOT" "$T/forged.m4" "$PORT" "$LEGIT"
200compose "$ROOT" "$T/forged.m4b" "$PORT" "$NOHINT;$LEGIT"
201page4 "$T/forged.m4" "$T/m4.html"; page4 "$T/forged.m4b" "$T/m4b.html"
202if ! grep -q 'Records and proof, for developers' "$T/m4.html"; then bad "POS legitimate page change: not run, the changed words are not on $R1"
203elif same4 "$T/m4.html" "$T/m4b.html"; then ok "POS a legitimate change to a page's words (developer heading) passes check 4"
204else bad "POS a legitimate change to a page's words failed check 4"; fi
205
206echo "translate.sh: pass=$pass fail=$fail (head $(git rev-parse --short HEAD); comparison: the same head without the hint pass)"
207[ "$fail" = 0 ]