interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / browse / tests / hostile.sh

103 lines · 5924 bytes Raw Blame History
1#!/bin/sh
2# browse/tests/hostile.sh: the README safety policy of the project page (brought repositories are
3# untrusted), checked on the HTML the renderer (browse/b_md.om brmd) writes for every Markdown fixture
4# in tests/md, tests/md/hostile.md included, and four mutant renderers that must each break it.
5# The policy, for every tag the renderer writes:
6# only the Markdown tags (a p img h1-h6 ul ol li code pre em strong del blockquote table thead tbody
7# tr th td hr br input), so no script, style, frame, object, embed, svg, form, meta, link or base;
8# no inline event handler (on...=) and no style attribute;
9# an image loads only from this site (src starts with one "/");
10# a link stays on this site ("/..." or "#..."), or it leaves it (http:, https:, mailto:, "//") and
11# then carries class="ext" (the visible "leaves this site" marker) and rel="nofollow noopener
12# noreferrer"; any other scheme (javascript:, data:, vbscript:, file:, ...) is a failure;
13# an input is only the disabled task-list checkbox.
14# Negative controls: each mutant below lets one kind of hostile content through; the policy must fail it.
15# M1 remote image (brmimgout renders every kind but unsafe), M2 external link without its marker,
16# M3 unsafe link scheme kept (brmaout), M4 raw HTML (fescc writes "<" as itself).
17# Usage: sh browse/tests/hostile.sh [OUTDIR] (cores: taskset -c ${CORES:-6}, as tests/run.sh)
18set -u
19here=$(cd "$(dirname "$0")" && pwd)
20br=$(dirname "$here")
21omv=${OMV:-$HOME/work/flux/omega-mv}
22omc=$omv/seed/out/omc
23out=${1:-${OUT:-/tmp/forgebrowse}/hostile}
24mkdir -p "$out"
25want=3ad9f10f64309e7acf8a725082b28e23a2a4aeb07e98322fb4dc0fcd8fc93194
26got=$(sha256sum "$omc" | cut -d' ' -f1)
27[ "$got" = "$want" ] || { echo "omc sha256 $got != pinned $want" >&2; exit 2; }
28run() { taskset -c "${CORES:-6}" nice -n 10 "$@"; }
29pass=0; fail=0
30ok() { pass=$((pass+1)); echo "PASS $*"; }
31bad() { fail=$((fail+1)); echo "FAIL $*"; }
32
33# policy FILE: prints one line per violation; exit 1 when there is one
34policy() {
35 grep -o '<[A-Za-z][^>]*>' "$1" | awk '
36 function attr(t, n, r) {
37 if (match(t, " " n "=\"[^\"]*\"")) { r = substr(t, RSTART + length(n) + 3, RLENGTH - length(n) - 4); return r }
38 return "\001"
39 }
40 {
41 t = $0; name = tolower(t); sub(/^</, "", name); sub(/[ >\/].*$/, "", name)
42 if (name !~ /^(a|p|img|h[1-6]|ul|ol|li|code|pre|em|strong|del|blockquote|table|thead|tbody|tr|th|td|hr|br|input)$/) { print "tag " name ": " t; bad = 1; next }
43 low = tolower(t)
44 if (low ~ /[ \t\/]on[a-z]+[ \t]*=/) { print "event handler: " t; bad = 1 }
45 if (low ~ /[ \t]style[ \t]*=/) { print "style attribute: " t; bad = 1 }
46 if (name == "img") {
47 s = attr(t, "src")
48 if (s !~ /^\/[^\/]/) { print "image not from this site: " t; bad = 1 }
49 }
50 if (name == "a") {
51 h = attr(t, "href"); lh = tolower(h)
52 if (h == "\001") { print "link without href: " t; bad = 1 }
53 else if (h ~ /^\/[^\/]/ || h ~ /^#/ || h == "/") { }
54 else if (lh ~ /^(https?:|mailto:|\/\/)/) {
55 if (t !~ / class="ext"/ || t !~ / rel="nofollow noopener noreferrer"/) { print "external link not marked: " t; bad = 1 }
56 }
57 else { print "unsafe link: " t; bad = 1 }
58 }
59 if (name == "input" && t != "<input type=\"checkbox\" disabled>" && t != "<input type=\"checkbox\" disabled checked>") { print "input: " t; bad = 1 }
60 }
61 END { exit bad }'
62}
63
64# build NAME B_MD UTIL: the fixture driver of tests/run.sh from the given renderer and util units
65build() {
66 { printf 'om2\n'; cat "$3" "$(dirname "$br")"/nodes/head.om "$br"/a_core.om "$2" "$here/mdmain.om"; } | run "$omc" > "$out/$1" || return 1
67 chmod +x "$out/$1"
68}
69build mdtest "$br/b_md.om" "$here/stubutil.om" || { echo "compile mdtest failed" >&2; exit 1; }
70
71# 1. the policy on every fixture, as the real renderer writes it
72for md in "$here"/md/*.md; do
73 n=$(basename "${md%.md}")
74 run "$out/mdtest" < "$md" > "$out/$n.out" || { bad "$n.md: renderer exit $?"; continue; }
75 if v=$(policy "$out/$n.out"); then ok "policy $n.md"; else bad "policy $n.md"; echo "$v" | head -5; fi
76done
77# the hostile fixture really carries what the policy is about (its own control: a fixture that tried
78# nothing would pass trivially)
79h=$here/md/hostile.md
80for w in '<script' 'onerror=' '<iframe' 'https://evil.example/track.png' 'javascript:' '<svg onload'; do
81 grep -qF "$w" "$h" || bad "hostile.md does not try $w"
82done
83n=$(grep -c ' class="ext" rel="nofollow noopener noreferrer"' "$out/hostile.out")
84[ "$n" -ge 5 ] && ok "hostile.md: $n external links, each marked" || bad "hostile.md: $n marked external links, want at least 5"
85grep -q '<img src="/o/r/raw/main/docs/img/logo.png" alt="local image">' "$out/hostile.out" && ok "hostile.md: the local image is kept" || bad "hostile.md: the local image is missing"
86
87# 2. mutants: each must be caught by the policy on hostile.md
88mut() { # mut NAME DESCRIPTION SED-SCRIPT FILE(b_md|util)
89 cp "$br/b_md.om" "$out/b_md.$1.om"; cp "$here/stubutil.om" "$out/util.$1.om"
90 if [ "$4" = util ]; then f=$out/util.$1.om; else f=$out/b_md.$1.om; fi
91 sed -i "$3" "$f"
92 if cmp -s "$f" "$br/b_md.om" || cmp -s "$f" "$here/stubutil.om"; then bad "mutant $1 ($2): the sed script changed nothing"; return; fi
93 if ! build "md.$1" "$out/b_md.$1.om" "$out/util.$1.om"; then bad "mutant $1 ($2): does not compile"; return; fi
94 run "$out/md.$1" < "$h" > "$out/hostile.$1.out"
95 if v=$(policy "$out/hostile.$1.out"); then bad "mutant $1 ($2) survived: the policy passed it"
96 else ok "mutant $1 ($2) caught: $(echo "$v" | head -1 | cut -c1-120)"; fi
97}
98mut M1 "remote image rendered" '/^fn brmimgout /,/^end$/ s/^if eq k 3$/if ne k 0/' b_md
99mut M2 "external link without its marker" 's/^text tbmaext .*/text tbmaext "\\" rel=\\"nofollow noopener noreferrer\\">"/' b_md
100mut M3 "unsafe link scheme kept" '/^fn brmaout /,/^end$/ s/^if eq k 0$/if eq k 99/' b_md
101mut M4 "raw HTML kept" 's/^fput tflt$/fputc c/' util
102echo "hostile: $pass pass, $fail fail"
103[ "$fail" -eq 0 ]