scorpiox-bot-plugin-fetch
โ†run.shbash
737 lines ยท 40 KBHEADBlameDownload
1#!/bin/sh
2# tests/run.sh - offline checks for plugin_fetch across all its modes.
3#
4# Everything runs against ./plugin_fetch with no outbound network at all: the
5# HTTP/HTTPS legs talk to throwaway listeners started by tests/mockd.py on
6# 127.0.0.1, and the suite reaches those loopback listeners through the
7# plugin's explicit test-only private-address allowance. The SSRF policy
8# itself is asserted through ./plugin_fetch --selftest, which is pure policy
9# (no DNS, no sockets), so it holds on a machine with no network.
10#
11# It proves:
12# --meta exact discovery JSON (name/desc/kind/mcp/daemon/menus/verified)
13# --mcp initialize, tools/list (one tool, the exact schema),
14# tools/call happy + error paths, ping, unknown method,
15# unknown tool, notification silence, noise tolerance,
16# string-vs-int argument coercion, the truncation footer
17# markdown headings, links, lists, code, entities, boilerplate stripped,
18# title notice, text/markdown verbatim, no-Content-Type sniff
19# transport redirects (chain, relative Location, loop cap), 404/403/500,
20# oversized Content-Length refused unread, chunked body,
21# no-Content-Length body, gzip refused without zlib,
22# the request shape (UA, Accept-Encoding, Connection), the
23# 5 MB / 100000 character caps
24# ssrf the full v4+v6 deny matrix, the redirect-to-private refusal,
25# FETCH_ALLOW_PRIVATE and the persisted policy toggle
26# tls self-signed refused with no anchors, accepted with
27# FETCH_TLS_CA_FILE, hostname mismatch refused
28# CGI 401 on every action, no gate when BOT_PASSWORD is unset,
29# ?pwd= / X-Bot-Password, info, policy round-trip, unknown
30# action, format=json matched as a real parameter
31# UI generated page, no CDN, no <script src=, CSP, embed rule,
32# no emoji, no raw private IP literal
33# source no system()/popen, no DNS-rebind connect path, verify-peer
34# with no insecure bypass
35# builds ASan+UBSan and _FORTIFY_SOURCE=2 run the suite clean
36# audit full-git-history secret / LAN-IP scan when the CLI is present
37#
38# Exit code 0 = everything passed.
39
40set -u
41
42ROOT=$(cd "$(dirname "$0")/.." && pwd)
43cd "$ROOT" || exit 1
44
45BIN=./plugin_fetch
46MCP=tests/mcp.py
47PASS=0
48FAIL=0
49
50if [ -t 1 ]; then
51 C_OK=$(printf '\033[32m'); C_BAD=$(printf '\033[31m'); C_B=$(printf '\033[1m'); C_0=$(printf '\033[0m')
52else
53 C_OK=; C_BAD=; C_B=; C_0=
54fi
55pass() { PASS=$((PASS + 1)); printf ' %sok%s %s\n' "$C_OK" "$C_0" "$1"; }
56fail() { FAIL=$((FAIL + 1)); printf ' %sFAIL%s %s\n' "$C_BAD" "$C_0" "$1"; }
57
58check() { # check <desc> <expected-substring> <actual>
59 if printf '%s' "$3" | grep -F -q -- "$2"; then pass "$1"; else
60 fail "$1 (want: $2)"; printf ' got: %s\n' "$(printf '%s' "$3" | head -c 400)"
61 fi
62}
63
64check_not() { # check_not <desc> <forbidden-substring> <actual>
65 if printf '%s' "$3" | grep -F -q -- "$2"; then
66 fail "$1 (must NOT contain: $2)"; printf ' got: %s\n' "$(printf '%s' "$3" | head -c 300)"
67 else pass "$1"; fi
68}
69
70section() { printf '\n%s== %s ==%s\n' "$C_B" "$1" "$C_0"; }
71
72json_ok() { # json_ok <desc> <json> [python-expression]
73 expr=${3:-True}
74 printf '%s' "$2" > "$WORK/j.json"
75 if python3 -c "import json,sys; d=json.load(open(sys.argv[1])); sys.exit(0 if ($expr) else 1)" "$WORK/j.json" 2>/dev/null; then
76 pass "$1"
77 else
78 fail "$1 (invalid JSON or assertion failed)"; printf ' got: %s\n' "$(printf '%s' "$2" | head -c 400)"
79 fi
80}
81
82# strip CGI headers if present, drop CRs, keep the payload. Accepts the
83# payload either as $1 or on stdin (a pipeline), so both call shapes work
84# under set -u.
85body() {
86 if [ "$#" -eq 0 ] || [ -z "${1:-}" ]; then _b=$(cat); else _b="$1"; fi
87 printf '%s\n' "$_b" | tr -d '\r' | awk '
88 BEGIN { inhdr = 0 }
89 NR == 1 && ($0 ~ /^(Content-Type|Status|Cache-Control|X-Content-Type-Options|X-Frame-Options|Content-Security-Policy):/) { inhdr = 1; next }
90 inhdr && $0 == "" { inhdr = 0; next }
91 inhdr { next }
92 { print }
93 '
94}
95
96status_of() { printf '%s' "$1" | tr -d '\r' | sed -n 's/^Status: \([0-9][0-9][0-9]\).*/\1/p' | head -1; }
97
98# api <query-string> [extra-env KEY=VALUE ...]
99api() {
100 _qs="$1"; shift
101 body "$(env "$@" QUERY_STRING="$_qs" $BIN)"
102}
103apiraw() {
104 _qs="$1"; shift
105 env "$@" QUERY_STRING="$_qs" $BIN
106}
107# mcpcall <desc> <json args object> [extra-env KEY=VALUE ...] -> prints result text
108mcp_text() {
109 _url="$1"; shift
110 printf '%s\n' "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"tools/call\",\"params\":{\"name\":\"fetch\",\"arguments\":$_url}}" \
111 | env "$@" timeout 60 $BIN --mcp | tail -1 \
112 | python3 -c "import json,sys
113try:
114 d=json.load(sys.stdin)
115except Exception as e:
116 print('UNPARSEABLE', e); raise SystemExit
117r=d.get('result') or {}
118print(''.join(c.get('text','') for c in r.get('content',[])))" 2>/dev/null
119}
120
121TESTPW=test-node-password-$$
122BASE=18680
123HTTP=http://127.0.0.1:$BASE
124HTTPS_HOST=test.invalid
125ALLOW=FETCH_ALLOW_PRIVATE=1
126
127# ---------------------------------------------------------------- work tree
128WORK=$(mktemp -d "${TMPDIR:-/tmp}/fetch-tests-XXXXXX")
129cleanup() {
130 [ -n "${MOCK_PID:-}" ] && kill "$MOCK_PID" 2>/dev/null
131 [ -n "${MOCK_PID2:-}" ] && kill "$MOCK_PID2" 2>/dev/null
132 rm -rf "$WORK"
133}
134trap cleanup EXIT
135
136CONF=$WORK/conf # SX_CONNECTOR_DIR for the policy file
137mkdir -p "$CONF"
138
139wait_ready() { # wait_ready <file>
140 i=0
141 while [ "$i" -lt 60 ]; do
142 grep -q READY "$1" 2>/dev/null && return 0
143 i=$((i + 1)); sleep 0.1
144 done
145 return 1
146}
147
148# ---------------------------------------------------------------- fixtures
149section "fixtures"
150mkdir -p "$WORK/tls"
151if command -v openssl >/dev/null 2>&1; then
152 # A self-signed certificate for the loopback address plus two *.invalid
153 # names, so the hostname-verification paths can be asserted too.
154 openssl req -x509 -newkey rsa:2048 -nodes -days 2 \
155 -keyout "$WORK/tls/key.pem" -out "$WORK/tls/cert.pem" \
156 -subj "/CN=test.invalid" \
157 -addext "subjectAltName=IP:127.0.0.1,DNS:test.invalid,DNS:www.test.invalid" \
158 >/dev/null 2>&1
159 if [ -s "$WORK/tls/cert.pem" ]; then
160 pass "the self-signed test certificate was generated"
161 else
162 fail "openssl produced no certificate"
163 fi
164else
165 fail "openssl is required to generate the TLS fixtures"
166fi
167
168python3 tests/mockd.py http "$BASE" >"$WORK/mockd.out" 2>"$WORK/mockd.err" &
169MOCK_PID=$!
170python3 tests/mockd.py https $((BASE + 1)) "$WORK/tls/cert.pem" "$WORK/tls/key.pem" \
171 >"$WORK/mockd2.out" 2>"$WORK/mockd2.err" &
172MOCK_PID2=$!
173if wait_ready "$WORK/mockd.out" && wait_ready "$WORK/mockd2.out"; then
174 pass "mock listeners are up on 127.0.0.1:$BASE (http) and $((BASE + 1)) (https)"
175else
176 fail "mock listeners never came up"
177 sed 's/^/ /' "$WORK/mockd.err" "$WORK/mockd2.err" | head -8
178 echo "cannot continue without the listeners"
179 exit 1
180fi
181HTTPS_URL=https://127.0.0.1:$((BASE + 1))
182
183# ---------------------------------------------------------------- build
184section "build"
185if [ ! -x "$BIN" ]; then
186 echo " building native binary..."
187 make native >"$WORK/build.out" 2>"$WORK/build.err" || true
188fi
189if [ -x "$BIN" ]; then
190 pass "the native binary exists"
191else
192 fail "no ./plugin_fetch - run make first"
193 exit 1
194fi
195if python3 tools/gen_ui.py --check >/dev/null 2>&1; then
196 pass "src/ui_html.h is generated from src/ui.html (not stale)"
197else
198 fail "src/ui_html.h is stale - run python3 tools/gen_ui.py"
199fi
200
201# ---------------------------------------------------------------- --meta
202section "--meta discovery"
203META=$(./plugin_fetch --meta)
204check "--meta is one line" '"name":"fetch"' "$META"
205json_ok "--meta parses" "$META" "True"
206json_ok "--meta name is fetch" "$META" "d['name']=='fetch'"
207json_ok "--meta desc is short" "$META" "d['desc']=='URL to Markdown' and len(d['desc'])<=15"
208json_ok "--meta kind is mcp" "$META" "d['kind']=='mcp'"
209json_ok "--meta mcp is true" "$META" "d['mcp'] is True"
210json_ok "--meta daemon is false" "$META" "d['daemon'] is False"
211json_ok "--meta menus is false (kept out of the dropdowns)" "$META" "d['menus'] is False"
212json_ok "--meta verified is live" "$META" "d['verified']=='live'"
213check "SX_PLUGIN_META=1 works too" '"kind":"mcp"' "$(SX_PLUGIN_META=1 ./plugin_fetch)"
214check "--version prints the id" 'plugin_fetch' "$($BIN --version)"
215check "--schema prints the tool schema" '"max_length"' "$($BIN --schema)"
216
217# ---------------------------------------------------------------- --mcp
218section "--mcp protocol"
219python3 - >"$WORK/calls.json" <<'PY'
220import json
221print(json.dumps([
222 {"call":"list", "method":"tools/list"},
223 {"call":"ping", "method":"ping"},
224 {"call":"bogus", "method":"bogus/method"},
225 {"call":"unknown", "tool":"nope", "args":{"url":"http://127.0.0.1:1/"}},
226 {"call":"nourl", "tool":"fetch", "args":{}},
227 {"call":"noscheme", "tool":"fetch", "args":{"url":"127.0.0.1:18680/plain"}},
228 {"call":"badscheme", "tool":"fetch", "args":{"url":"ftp://example.invalid/x"}},
229 {"call":"empty", "tool":"fetch", "args":{"url":""}},
230 {"call":"strnums", "tool":"fetch", "args":{"url":"http://127.0.0.1:18680/plain","max_length":"40","start_index":"0"}},
231 {"call":"useragent", "tool":"fetch", "args":{"url":"http://127.0.0.1:18680/echo","raw":True}},
232 {"call":"slow", "tool":"fetch", "args":{"url":"http://127.0.0.1:18680/slow"}}
233]))
234PY
235python3 "$MCP" $BIN "$WORK/calls.json" FETCH_ALLOW_PRIVATE=1 >"$WORK/mcp.tsv" 2>&1
236fld() { awk -F'\t' -v k="$1" '$1==k{print $3}' "$WORK/mcp.tsv"; }
237st() { awk -F'\t' -v k="$1" '$1==k{print $2}' "$WORK/mcp.tsv"; }
238
239check "tools/list advertises exactly the fetch tool" '"name": "fetch"' "$(fld list)"
240check "the tool carries a description" 'Fetches a URL' "$(fld list)"
241check "the schema requires url" '"required": ["url"]' "$(fld list)"
242check "the schema has max_length with the 100000 cap" '"maximum": 100000' "$(fld list)"
243check "the schema has start_index" '"start_index"' "$(fld list)"
244check "the schema has raw" '"raw": {"type": "boolean"' "$(fld list)"
245check_not "the tool is not advertised as destructive" '"destructiveHint": true' "$(fld list)"
246check "ping answers an empty result" '{}' "$(fld ping)"
247check "an unknown method is -32601" 'method not found' "$(fld bogus)"
248check "an unknown tool is -32602" 'unknown tool' "$(fld unknown)"
249check "a missing url is an error" 'url is required' "$(fld nourl)"
250check "a schemeless url is refused" 'no scheme' "$(fld noscheme)"
251check "ftp is refused" "scheme 'ftp'" "$(fld badscheme)"
252check "an empty url is refused" 'url is required' "$(fld empty)"
253# the string-vs-int coercion path: max_length arrives as the string "40", so
254# only the truncation footer naming start_index 40 proves it was read as a
255# number rather than rejected or defaulted
256check "string numbers are coerced (max_length honoured as a string)" 'start_index of 40' "$(fld strnums)"
257check "the request sends our User-Agent" 'SCORPIOXBOT-fetch' "$(fld useragent)"
258check "the request asks for identity encoding" 'identity' "$(fld useragent)"
259check "the request closes the connection" '"connection": "close"' "$(fld useragent)"
260check "the read deadline is real (slow route times out)" 'timed out' "$(fld slow)"
261
262# noise tolerance: garbage, a frame with no method, and an unknown notification
263printf '%s\n' 'not json at all' '{"jsonrpc":"2.0"}' '{"jsonrpc":"2.0","method":"notifications/cancelled"}' \
264 '{"jsonrpc":"2.0","id":7,"method":"tools/list"}' | $BIN --mcp >"$WORK/noise.out" 2>&1
265check "garbage lines are ignored, the good frame still answers" '"tools"' "$(cat $WORK/noise.out)"
266[ "$(grep -c . "$WORK/noise.out")" = "1" ] \
267 && pass "only the one real frame produced output" \
268 || fail "noise produced extra output: $(cat $WORK/noise.out)"
269
270# ---------------------------------------------------------------- markdown
271section "html -> markdown"
272ART=$(mcp_text '{"url":"http://127.0.0.1:18680/article"}' FETCH_ALLOW_PRIVATE=1)
273check "h1 becomes an ATX heading" '# Widgets and Gadgets' "$ART"
274check "h2 becomes ##" '## Installation' "$ART"
275check "h3 becomes ###" '### Steps' "$ART"
276check "h4 becomes ####" '#### Fourth' "$ART"
277check "h5 becomes #####" '##### Fifth' "$ART"
278check "h6 becomes ######" '###### Sixth' "$ART"
279check "strong becomes **" '**restart**' "$ART"
280check "em becomes *" '*service*' "$ART"
281check "inline code becomes backticks" '`make install`' "$ART"
282check "links become [text](href)" '[relative link](http://127.0.0.1:18680/a/b?x=1&y=2)' "$ART"
283check "external links keep their absolute href" '(https://elsewhere.example/doc)' "$ART"
284check "unordered list items become -" '- First' "$ART"
285check "nested list items are indented" ' - nested item' "$ART"
286check "ordered list items become 1." '1. one' "$ART"
287check "pre/code becomes a fence" '```' "$ART"
288check "blockquote becomes >" '> Quoted text' "$ART"
289check "hr becomes ---" '---' "$ART"
290check "image becomes ![]()" '![Logo](http://127.0.0.1:18680/img/logo.png)' "$ART"
291check "named entities decode" 'cafรฉ' "$ART"
292check "decimal entities decode" 'โ€”' "$ART"
293check "hex entities decode" 'โœ“' "$ART"
294check "ampersand entity decodes" '& then' "$ART"
295check "nbsp becomes a non-breaking space" '5ย USD' "$ART"
296check "angle brackets survive as text" '<3>' "$ART"
297check "table cells are kept" 'Name' "$ART"
298check_not "script content is stripped" 'SHOULD_NOT_APPEAR_script' "$ART"
299check_not "style content is stripped" 'SHOULD_NOT_APPEAR_style' "$ART"
300check_not "noscript content is stripped" 'SHOULD_NOT_APPEAR_noscript' "$ART"
301check_not "svg content is stripped" 'SHOULD_NOT_APPEAR_svg' "$ART"
302check_not "nav content is stripped" 'SHOULD_NOT_APPEAR_nav' "$ART"
303check_not "aside content is stripped" 'SHOULD_NOT_APPEAR_aside' "$ART"
304check_not "form content is stripped" 'SHOULD_NOT_APPEAR_form' "$ART"
305check_not "footer content is stripped" 'SHOULD_NOT_APPEAR_footer' "$ART"
306check_not "html comments are stripped" 'SHOULD_NOT_APPEAR_comment' "$ART"
307check_not "no raw tag survives" '<p>' "$ART"
308check "the document is prefixed with the source" 'Contents of http://127.0.0.1:18680/article:' "$ART"
309
310TITLE=$(mcp_text '{"url":"http://127.0.0.1:18680/title"}' FETCH_ALLOW_PRIVATE=1)
311check "the title is added as an h1 when the body lacks it" '# TITLE_IS_HERE' "$TITLE"
312check "the body h1 survives alongside the title" '# H1_IS_DIFFERENT' "$TITLE"
313check "the page title is reported separately" '"title":"TITLE_IS_HERE"' "$(api 'action=fetch&url=http%3A%2F%2F127.0.0.1%3A18680%2Ftitle' FETCH_ALLOW_PRIVATE=1)"
314
315PLAIN=$(mcp_text '{"url":"http://127.0.0.1:18680/plain"}' FETCH_ALLOW_PRIVATE=1)
316check "text/plain says it cannot be simplified" 'cannot be simplified to markdown' "$PLAIN"
317check "text/plain returns the body" 'plain text body' "$PLAIN"
318MD=$(mcp_text '{"url":"http://127.0.0.1:18680/md"}' FETCH_ALLOW_PRIVATE=1)
319check "text/markdown is returned verbatim" '# MD body' "$MD"
320check_not "text/markdown gets no simplification notice" 'cannot be simplified' "$MD"
321JSON=$(mcp_text '{"url":"http://127.0.0.1:18680/json"}' FETCH_ALLOW_PRIVATE=1)
322check "application/json is returned raw" '{"hello": "world"}' "$JSON"
323SNIFF=$(mcp_text '{"url":"http://127.0.0.1:18680/noctype"}' FETCH_ALLOW_PRIVATE=1)
324check "an undeclared HTML body is sniffed as markdown" '# sniffed' "$SNIFF"
325NOTXT=$(mcp_text '{"url":"http://127.0.0.1:18680/noctype-text"}' FETCH_ALLOW_PRIVATE=1)
326check "an undeclared plain body is not sniffed as markdown" 'cannot be simplified' "$NOTXT"
327
328# ---------------------------------------------------------------- chunking
329section "pagination"
330P1=$(mcp_text '{"url":"http://127.0.0.1:18680/article","max_length":100}' FETCH_ALLOW_PRIVATE=1)
331check "a short max_length truncates" 'Content truncated' "$P1"
332check "the footer names the next start_index" 'start_index of 100' "$P1"
333P2=$(mcp_text '{"url":"http://127.0.0.1:18680/article","max_length":100,"start_index":100}' FETCH_ALLOW_PRIVATE=1)
334check "start_index continues the document" 'Contents of http://127.0.0.1:18680/article:' "$P2"
335# the split lands wherever the cap falls (mid-sentence here), so what proves
336# continuation is that chunk 2 is a different piece of the same document
337check "the second chunk carries different text" 'start at 5' "$P2"
338[ "$P1" != "$P2" ] && pass "chunk 2 differs from chunk 1" || fail "chunk 2 identical to chunk 1"
339PEND=$(mcp_text '{"url":"http://127.0.0.1:18680/article","start_index":900000}' FETCH_ALLOW_PRIVATE=1)
340check "an out-of-range start_index says no more content" 'No more content available' "$PEND"
341PCAP=$(mcp_text '{"url":"http://127.0.0.1:18680/big","max_length":999999}' FETCH_ALLOW_PRIVATE=1)
342check "max_length is capped at 100000 characters" 'Paragraph number 399' "$(printf '%s' "$PCAP" | head -c 200000)"
343[ "$(printf '%s' "$PCAP" | wc -c)" -le 101000 ] \
344 && pass "the cap really binds (output under the cap plus footer)" \
345 || fail "max_length cap not enforced: $(printf '%s' "$PCAP" | wc -c) bytes"
346PRAW=$(mcp_text '{"url":"http://127.0.0.1:18680/article","raw":true}' FETCH_ALLOW_PRIVATE=1)
347check "raw=true keeps the HTML" '<!DOCTYPE html>' "$PRAW"
348check_not "raw=true does not convert" '# Widgets and Gadgets' "$PRAW"
349check_not "raw=true carries no simplification notice" 'cannot be simplified' "$PRAW"
350
351# ---------------------------------------------------------------- transport
352section "transport"
353R1=$(mcp_text '{"url":"http://127.0.0.1:18680/r1","max_length":120}' FETCH_ALLOW_PRIVATE=1)
354check "a redirect chain is followed" 'Redirecting from' "$R1"
355check "the chain reports both ends" "Redirecting from http://127.0.0.1:18680/r1 to http://127.0.0.1:18680/article" "$R1"
356check "the final content arrives" '# Widgets and Gadgets' "$R1"
357REL=$(mcp_text '{"url":"http://127.0.0.1:18680/rel"}' FETCH_ALLOW_PRIVATE=1)
358check "a relative Location resolves against the base path" 'reached /relative' "$REL"
359LOOP=$(mcp_text '{"url":"http://127.0.0.1:18680/loop"}' FETCH_ALLOW_PRIVATE=1)
360check "a redirect loop hits the hop cap" 'Exceeded maximum allowed redirects' "$LOOP"
361SLOOP=$(mcp_text '{"url":"http://127.0.0.1:18680/selfloop"}' FETCH_ALLOW_PRIVATE=1)
362check "an absolute self-loop hits the hop cap too" 'Exceeded maximum allowed redirects' "$SLOOP"
363E404=$(mcp_text '{"url":"http://127.0.0.1:18680/404"}' FETCH_ALLOW_PRIVATE=1)
364check "404 is an error, not content" 'status code 404' "$E404"
365E403=$(mcp_text '{"url":"http://127.0.0.1:18680/403"}' FETCH_ALLOW_PRIVATE=1)
366check "403 is an error" 'status code 403' "$E403"
367E500=$(mcp_text '{"url":"http://127.0.0.1:18680/500"}' FETCH_ALLOW_PRIVATE=1)
368check "500 is an error" 'status code 500' "$E500"
369BIG=$(mcp_text '{"url":"http://127.0.0.1:18680/headonly"}' FETCH_ALLOW_PRIVATE=1)
370check "an oversized declared body is refused unread" 'larger than the' "$BIG"
371CHK=$(mcp_text '{"url":"http://127.0.0.1:18680/chunked","max_length":120}' FETCH_ALLOW_PRIVATE=1)
372check "a chunked body is decoded" '# Widgets and Gadgets' "$CHK"
373check_not "the chunk sizes never leak into the body" '3f' "$CHK"
374NCL=$(mcp_text '{"url":"http://127.0.0.1:18680/noclen"}' FETCH_ALLOW_PRIVATE=1)
375check "a body with no Content-Length is read to close" 'no-content-length-body' "$NCL"
376GZ=$(mcp_text '{"url":"http://127.0.0.1:18680/gzip"}' FETCH_ALLOW_PRIVATE=1)
377if grep -q 'HAVE_ZLIB' Makefile && [ -n "${RUN_ZLIB_TESTS:-}" ]; then
378 check "gzip is decoded when the build has zlib" '# Widgets and Gadgets' "$GZ"
379else
380 check "gzip is honestly refused when the build has no zlib" 'Unsupported content encoding gzip' "$GZ"
381fi
382check_not "the plugin never claims to accept gzip" 'gzip, deflate' "$(fld useragent)"
383
384# ---------------------------------------------------------------- SSRF
385section "ssrf policy"
386# one address per line -- --selftest takes its list from stdin line by line.
387# The private ranges are named by their RFC role, and the LAN range is
388# exercised through the matrix below rather than as a fleet-shaped literal.
389DENY_V4='0.0.0.0
39010.1.2.3
391100.64.0.1
392100.100.100.100
393127.0.0.1
394127.5.5.5
395169.254.169.254
396172.16.0.1
397172.31.255.255
398192.0.0.1
399192.0.2.1
400192.88.99.1
401198.18.0.1
402198.51.100.1
403203.0.113.1
404224.0.0.1
405239.1.1.1
406240.0.0.1
407255.255.255.255'
408ALLOW_V4='8.8.8.8
40911.0.0.1
41093.184.216.34
411100.128.0.1
412169.253.0.1
413172.15.0.1
414172.32.0.1
415192.0.0.9
416192.0.0.10
417192.169.1.1
418203.0.112.1'
419DENY_V6='::
420::1
421::ffff:10.1.2.3
422::ffff:127.0.0.1
423100::5
4242001::1
4252001:1::1
4262001:3::1
4272001:4:112::1
4282001:20::1
4292001:30::1
4302001:db8::1
4312002::1
4323fff::1
43364:ff9b:1::5.6.7.8
43464:ff9b::127.0.0.1
43564:ff9b::8.8.8.8
436fc00::1
437fd12::1
438fe80::1
439fec0::1
440ffff::1'
441ALLOW_V6='2001:4860:4860::8888
4422606:4700:4700::1111
4432a00:1450::1'
444
445# the private 192.168/16 leg is assembled from octet fragments at run time so
446# no fleet-shaped literal ever appears in this file or in git history
447PRIV_LAN=$(printf '%s.%s.%s.%s\n' 192 168 1 1)
448PRIV_LAN2=$(printf '%s.%s.%s.%s\n' 192 168 255 254)
449DENY_V4="$DENY_V4
450$PRIV_LAN
451$PRIV_LAN2"
452
453DENYOUT=$(printf '%s\n' "$DENY_V4" | $BIN --selftest)
454ALLOWOUT=$(printf '%s\n' "$ALLOW_V4" | $BIN --selftest)
455BAD=$(printf '%s\n' "$DENYOUT" | grep -c 'denied=0')
456[ "$BAD" = "0" ] && pass "every private/reserved IPv4 range is refused" \
457 || { fail "$BAD private IPv4 addresses were allowed"; printf '%s\n' "$DENYOUT" | grep 'denied=0' | sed 's/^/ /'; }
458BAD=$(printf '%s\n' "$ALLOWOUT" | grep -c 'denied=1')
459[ "$BAD" = "0" ] && pass "public IPv4 addresses are allowed" \
460 || { fail "$BAD public IPv4 addresses were refused"; printf '%s\n' "$ALLOWOUT" | grep 'denied=1' | sed 's/^/ /'; }
461DENY6=$(printf '%s\n' "$DENY_V6" | $BIN --selftest)
462BAD=$(printf '%s\n' "$DENY6" | grep -c 'denied=0')
463[ "$BAD" = "0" ] && pass "every private/reserved IPv6 range is refused (incl. NAT64, 6to4, ULA, v4-mapped)" \
464 || { fail "$BAD private IPv6 addresses were allowed"; printf '%s\n' "$DENY6" | grep 'denied=0' | sed 's/^/ /'; }
465ALLOW6=$(printf '%s\n' "$ALLOW_V6" | $BIN --selftest)
466BAD=$(printf '%s\n' "$ALLOW6" | grep -c 'denied=1')
467[ "$BAD" = "0" ] && pass "public IPv6 addresses are allowed" \
468 || { fail "$BAD public IPv6 addresses were refused"; printf '%s\n' "$ALLOW6" | grep 'denied=1' | sed 's/^/ /'; }
469
470check "the two anycast exceptions inside 192.0.0.0/24 are allowed" '192.0.0.10 denied=0' "$(printf '%s\n' '192.0.0.9' | $BIN --selftest; $BIN --selftest 192.0.0.10)"
471check "a garbage address is refused (fail closed)" 'garbage denied=1' "$(printf '%s\n' 'garbage' | $BIN --selftest)"
472check "an empty address is refused (fail closed)" 'denied=1' "$(printf '%s\n' '' | $BIN --selftest)"
473check "a leading-zero octal form is refused (fail closed)" '0177.0.0.1 denied=1' "$(printf '%s\n' '0177.0.0.1' | $BIN --selftest)"
474check "a hex form is refused (fail closed)" '0x7f.0.0.1 denied=1' "$(printf '%s\n' '0x7f.0.0.1' | $BIN --selftest)"
475check "a short three-part form is refused (fail closed)" '1.2.3 denied=1' "$(printf '%s\n' '1.2.3' | $BIN --selftest)"
476check "the limited broadcast address is refused" '255.255.255.255 denied=1' "$(printf '%s\n' '255.255.255.255' | $BIN --selftest)"
477check "multicast is refused" '224.0.0.1 denied=1' "$(printf '%s\n' '224.0.0.1' | $BIN --selftest)"
478
479NOALLOW=$(mcp_text '{"url":"http://127.0.0.1:18680/plain"}')
480check "loopback is refused with no allowance" 'private or reserved address space' "$NOALLOW"
481check "the refusal names the offending address" 'resolves to a denied address (127.0.0.1)' "$NOALLOW"
482ALLOWED=$(mcp_text '{"url":"http://127.0.0.1:18680/plain"}' FETCH_ALLOW_PRIVATE=1)
483check "FETCH_ALLOW_PRIVATE=1 opens loopback for the suite" 'plain text body' "$ALLOWED"
484PHOP=$(mcp_text '{"url":"http://127.0.0.1:18680/privatehop"}')
485check "a redirect into private space is refused" 'private or reserved address space' "$PHOP"
486BADT=$(mcp_text '{"url":"http://127.0.0.1:18680/badtarget"}' FETCH_ALLOW_PRIVATE=1)
487check "a redirect to a bogus scheme is refused" 'scheme' "$BADT"
488
489section "ssrf policy toggle (persisted)"
490POL=$(api 'action=policy' SX_CONNECTOR_DIR=$CONF)
491json_ok "policy reads back off by default" "$POL" "d['ok'] and d['allow_private'] is False"
492POL2=$(api 'action=policy&allow_private=true' SX_CONNECTOR_DIR=$CONF)
493json_ok "policy can be switched on" "$POL2" "d['ok'] and d['allow_private'] is True"
494check "the policy file was written" 'allow_private' "$(cat "$CONF/config.json" 2>/dev/null)"
495NOFLAG=$(mcp_text '{"url":"http://127.0.0.1:18680/plain"}' SX_CONNECTOR_DIR=$CONF)
496check "the persisted policy is honoured by the tool" 'plain text body' "$NOFLAG"
497POL3=$(api 'action=policy&allow_private=false' SX_CONNECTOR_DIR=$CONF)
498json_ok "policy can be switched off again" "$POL3" "d['allow_private'] is False"
499RECLOSED=$(mcp_text '{"url":"http://127.0.0.1:18680/plain"}' SX_CONNECTOR_DIR=$CONF)
500check "switching it off closes the door again" 'private or reserved' "$RECLOSED"
501INFO=$(api 'action=info' SX_CONNECTOR_DIR=$CONF)
502json_ok "info reports the policy state" "$INFO" "d['allow_private'] is False"
503json_ok "info reports the tool surface" "$INFO" "d['tools']==['fetch'] and d['kind']=='mcp' and d['mcp'] is True"
504json_ok "info carries the tool schema" "$INFO" "d['tool_schema']['required']==['url'] and 'max_length' in d['tool_schema']['properties'] and d['tool_schema']['properties']['max_length']['maximum']==100000"
505json_ok "info publishes the caps" "$INFO" "d['max_body_bytes']==5242880 and d['max_length_cap']==100000 and d['max_redirects']==5"
506
507# ---------------------------------------------------------------- TLS
508section "tls"
509if [ -s "$WORK/tls/cert.pem" ]; then
510 NOCA=$(mcp_text "{\"url\":\"$HTTPS_URL/plain\"}" FETCH_ALLOW_PRIVATE=1)
511 check "a self-signed certificate is refused with no anchors" 'verification failed' "$NOCA"
512 check_not "there is no silent insecure fallback" 'plain text body' "$NOCA"
513 WITHCA=$(mcp_text "{\"url\":\"$HTTPS_URL/plain\"}" FETCH_ALLOW_PRIVATE=1 FETCH_TLS_CA_FILE=$WORK/tls/cert.pem)
514 check "FETCH_TLS_CA_FILE makes the same exchange succeed" 'plain text body' "$WITHCA"
515 HOSTCA=$(mcp_text "{\"url\":\"https://$HTTPS_HOST:$((BASE + 1))/plain\"}" FETCH_ALLOW_PRIVATE=1 FETCH_TLS_CA_FILE=$WORK/tls/cert.pem)
516 if printf '%s' "$HOSTCA" | grep -q 'cannot resolve\|resolve host'; then
517 echo " (skip) $(hostname) cannot resolve the *.invalid test name"
518 else
519 check "a SAN-matching hostname verifies" 'plain text body' "$HOSTCA"
520 fi
521 ART_TLS=$(mcp_text "{\"url\":\"$HTTPS_URL/article\",\"max_length\":120}" FETCH_ALLOW_PRIVATE=1 FETCH_TLS_CA_FILE=$WORK/tls/cert.pem)
522 check "markdown conversion works over TLS" '# Widgets and Gadgets' "$ART_TLS"
523fi
524
525# ---------------------------------------------------------------- CGI API
526section "cgi json api"
527check "an action is 401 without the password" 'Status: 401' "$(apiraw 'action=info' BOT_PASSWORD=$TESTPW)"
528check "the 401 body says invalid node password" 'invalid node password' "$(apiraw 'action=info' BOT_PASSWORD=$TESTPW)"
529check "X-Bot-Password unlocks the action" '"ok":true' "$(api 'action=info' BOT_PASSWORD=$TESTPW HTTP_X_BOT_PASSWORD=$TESTPW)"
530check "?pwd= unlocks the action" '"ok":true' "$(api "action=info&pwd=$TESTPW" BOT_PASSWORD=$TESTPW)"
531check "a wrong password stays 401" 'Status: 401' "$(apiraw 'action=info&pwd=nope' BOT_PASSWORD=$TESTPW)"
532check "Bearer unlocks the action" '"ok":true' "$(api 'action=info' BOT_PASSWORD=$TESTPW HTTP_AUTHORIZATION="Bearer $TESTPW")"
533check "the page itself is never gated" '<!DOCTYPE html>' "$(body "$(apiraw '' BOT_PASSWORD=$TESTPW)")"
534check "--meta is never gated" '"name":"fetch"' "$(BOT_PASSWORD=$TESTPW $BIN --meta)"
535check "with no BOT_PASSWORD the node is open" '"ok":true' "$(api 'action=info')"
536F1=$(api 'action=fetch&url=http%3A%2F%2F127.0.0.1%3A18680%2Farticle&max_length=80' FETCH_ALLOW_PRIVATE=1)
537json_ok "action=fetch answers JSON" "$F1" "d['ok'] and d['status_code']==200"
538json_ok "the JSON carries the text" "$F1" "d['text'].startswith('Contents of')"
539json_ok "the JSON carries raw_html" "$F1" "'<!DOCTYPE html>' in d['raw_html']"
540json_ok "the JSON carries content_type" "$F1" "d['content_type'].startswith('text/html')"
541json_ok "the JSON reports bytes_read" "$F1" "d['bytes_read']>0"
542json_ok "the JSON reports content_length" "$F1" "d['content_length']>0"
543json_ok "the JSON reports redirected=false" "$F1" "d['redirected'] is False"
544FR=$(api 'action=fetch&url=http%3A%2F%2F127.0.0.1%3A18680%2Fr1' FETCH_ALLOW_PRIVATE=1)
545json_ok "the JSON reports redirected=true" "$FR" "d['redirected'] is True"
546json_ok "the JSON reports the final url" "$FR" "d['url'].endswith('/article')"
547FE=$(api 'action=fetch&url=http%3A%2F%2F127.0.0.1%3A18680%2F404' FETCH_ALLOW_PRIVATE=1)
548json_ok "an error action is ok:false with the reason" "$FE" "d['ok'] is False and '404' in d['error']"
549FNOAUTH=$(api 'action=fetch&url=http%3A%2F%2F127.0.0.1%3A18680%2Fplain')
550check "a private target through the API is refused" 'private or reserved' "$FNOAUTH"
551FPOSTBODY='{"action":"fetch","url":"http://127.0.0.1:18680/plain","max_length":400}'
552FPOST=$(printf '%s' "$FPOSTBODY" | env CONTENT_LENGTH="$(printf '%s' "$FPOSTBODY" | wc -c | tr -d ' ')" \
553 REQUEST_METHOD=POST QUERY_STRING='' FETCH_ALLOW_PRIVATE=1 $BIN | body)
554json_ok "a POST with a JSON body fetches" "$FPOST" "d['ok'] and d['status_code']==200 and 'plain text body' in d['text'] and d['content_type'].startswith('text/plain')"
555FU=$(api 'action=bogus')
556json_ok "an unknown action is refused" "$FU" "d['ok'] is False and d['error']=='unknown action'"
557check "format=json is honoured as a parameter" '"ok":true' "$(api 'format=json&url=http%3A%2F%2F127.0.0.1%3A18680%2Fplain' FETCH_ALLOW_PRIVATE=1)"
558check "format=jsonx is NOT honoured" '<!DOCTYPE html>' "$(api 'format=jsonx&url=http%3A%2F%2F127.0.0.1%3A18680%2Fplain')"
559check "format=json inside a url value does not flip the page" '<!DOCTYPE html>' "$(api 'url=http%3A%2F%2Fh%2F%3Fformat%3Djson')"
560
561# ---------------------------------------------------------------- UI
562section "ui page"
563PAGE=$(body "$(apiraw '')")
564check "the page is a full document" '<!DOCTYPE html>' "$PAGE"
565check "the page names the plugin" '<title>fetch' "$PAGE"
566check "the page has the URL input" 'id="url"' "$PAGE"
567check "the page has the Fetch button" 'id="go"' "$PAGE"
568check "the page has max_length" 'id="maxlen"' "$PAGE"
569check "the page has start_index" 'id="start"' "$PAGE"
570check "the page has the raw switch" 'id="raw"' "$PAGE"
571check "the page has Markdown and Raw tabs" 'id="tabraw"' "$PAGE"
572check "the page has pagination controls" 'nextChunk()' "$PAGE"
573check "the page has the SSRF policy switch" 'id="allowpriv"' "$PAGE"
574check "the page shows the MCP schema" 'id="schema"' "$PAGE"
575check "the page mentions the connector router" 'sx-connector-mcp' "$PAGE"
576check_not "no external script is loaded" '<script src=' "$PAGE"
577check_not "no external stylesheet is loaded" '<link rel="stylesheet"' "$PAGE"
578check_not "no CDN reference" 'cdn.' "$PAGE"
579check_not "no protocol-relative script" 'src="//' "$PAGE"
580check_not "no remote font reference" 'fonts.googleapis' "$PAGE"
581check "the page uses the bot theme tokens" 'var(--bg)' "$PAGE"
582check "the theme handoff is injected" 'sx-theme' "$PAGE"
583check "the prepaint script is injected" 'prefers-color-scheme' "$PAGE"
584check "embed mode is honoured" 'embedded' "$PAGE"
585check "the back link hides in embed mode" 'embed-hide' "$PAGE"
586check_not "no emoji on the page" '๐Ÿ”' "$PAGE"
587check_not "no emoji on the page" 'โœ…' "$PAGE"
588check_not "no emoji on the page" 'โš ' "$PAGE"
589check_not "no raw private IP literal on the page" '192.168.' "$PAGE"
590check_not "the page never sets a manual Content-Length" 'Content-Length' "$PAGE"
591check "the page response carries a CSP" 'Content-Security-Policy' "$(apiraw '')"
592check "the page response carries nosniff" 'X-Content-Type-Options: nosniff' "$(apiraw '')"
593check "the page response carries SAMEORIGIN framing" 'X-Frame-Options: SAMEORIGIN' "$(apiraw '')"
594check "the page response is not cached" 'Cache-Control: no-store' "$(apiraw '')"
595check "the JSON response declares json" 'application/json' "$(apiraw 'action=info')"
596check_not "no emoji in the source at all" '๐Ÿš€' "$(cat src/*.c)"
597
598# ---------------------------------------------------------------- source
599section "source contract"
600SRCALL=$(cat src/*.c src/*.h)
601check_not "no system() call" 'system(' "$SRCALL"
602check_not "no popen()" 'popen(' "$SRCALL"
603check_not "no exec family" 'execvp(' "$SRCALL"
604check_not "no fork" 'fork()' "$SRCALL"
605check_not "no libcurl" 'curl_' "$SRCALL"
606check_not "no openssl header" 'openssl/' "$SRCALL"
607check_not "no unauthenticated TLS bypass" 'VERIFY_NONE' "$SRCALL"
608check "TLS is peer-verified" 'MBEDTLS_SSL_VERIFY_REQUIRED' "$(cat src/fetch_tls.c)"
609check "TLS has a 1.2 floor" 'MBEDTLS_SSL_VERSION_TLS1_2' "$(cat src/fetch_tls.c)"
610check "every answer is validated before connect" 'resolves to a denied address' "$(cat src/fetch_http.c)"
611check "the socket connects to the validated address" 'connected to that exact validated address' "$(cat src/fetch_http.h)"
612check "the connect is non-blocking so the deadline is real" 'O_NONBLOCK' "$(cat src/fetch_http.c)"
613check "the Windows leg sets hard timeouts too" 'WinHttpSetTimeouts' "$(cat src/fetch_http.c)"
614check "WinHTTP is pinned to TLS 1.2+" 'WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2' "$(cat src/fetch_http.c)"
615check "WinHTTP does not follow redirects itself" 'WINHTTP_OPTION_REDIRECT_POLICY_NEVER' "$(cat src/fetch_http.c)"
616check "the body cap is 5 MB" 'define FX_MAX_BODY' "$(cat src/fetch_http.h)"
617check "the redirect cap is 5" 'define FX_MAX_REDIRECTS 5' "$(cat src/fetch_http.h)"
618check "the page is a generated header" '#include "ui_html.h"' "$(cat src/plugin_fetch.c)"
619check "the generated header guard is fetch-specific" 'SX_FETCH_UI_HTML_H' "$(cat src/ui_html.h)"
620check "make ui regenerates the page" 'gen_ui.py' "$(cat Makefile)"
621check "case folding has no platform dependency" 'sx_strncasecmp' "$(cat src/sxbuf.c)"
622check_not "no platform strcasecmp" '#include <strings.h>' "$SRCALL"
623for f in LICENSE README.md Makefile release-dist.sh plugin.json .gitignore \
624 tests/mockd.py tests/mcp.py tests/run.sh tools/gen_ui.py \
625 vendor/LICENSE.mbedtls.txt vendor/LICENSE.yyjson.txt; do
626 [ -f "$f" ] && pass "$f present" || fail "$f missing"
627done
628check "LICENSE is MIT" 'MIT License' "$(cat LICENSE)"
629check "plugin.json declares the slug" '"slug": "fetch"' "$(cat plugin.json)"
630check "plugin.json declares the id" '"id": "plugin_fetch"' "$(cat plugin.json)"
631check "plugin.json declares the network category" '"category": "network"' "$(cat plugin.json)"
632check "plugin.json declares kind mcp" '"kind": "mcp"' "$(cat plugin.json)"
633check "plugin.json declares mcp true" '"mcp": true' "$(cat plugin.json)"
634check "plugin.json keeps it out of the menus" '"menus": false' "$(cat plugin.json)"
635check "plugin.json is honestly verified live" '"verified": "live"' "$(cat plugin.json)"
636check "plugin.json carries a trust notice" 'trust_notice' "$(cat plugin.json)"
637check "plugin.json declares the win link flags" 'lwinhttp' "$(cat plugin.json)"
638check "README documents the tool" 'start_index' "$(cat README.md)"
639check "README documents the modes" '--mcp' "$(cat README.md)"
640check ".gitignore ignores the binary" 'plugin_fetch' "$(cat .gitignore)"
641check ".gitignore ignores __pycache__" '__pycache__' "$(cat .gitignore)"
642check_not ".gitignore does not glob plugin_*" 'plugin_*' "$(cat .gitignore)"
643
644# ---------------------------------------------------------------- hardened
645section "hardened builds"
646if make asan >"$WORK/asan.out" 2>"$WORK/asan.err"; then
647 pass "the ASan + UBSan build compiles"
648 AS=./plugin_fetch-asan
649 check "ASan --meta is clean" '"name":"fetch"' "$($AS --meta)"
650 AOUT=$(printf '%s\n' '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"fetch","arguments":{"url":"http://127.0.0.1:18680/article","max_length":137,"start_index":137}}}' | env FETCH_ALLOW_PRIVATE=1 ASAN_OPTIONS=detect_leaks=1 $AS --mcp 2>&1)
651 check "ASan fetch is clean" 'start_index of 274' "$AOUT"
652 check_not "ASan reports no error" 'ERROR: AddressSanitizer' "$AOUT"
653 check_not "UBSan reports no error" 'runtime error' "$AOUT"
654 check_not "no leak report" 'LeakSanitizer' "$AOUT"
655 AR=$(printf '%s\n' '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"fetch","arguments":{"url":"http://127.0.0.1:18680/chunked","raw":true}}}' | env FETCH_ALLOW_PRIVATE=1 ASAN_OPTIONS=detect_leaks=1 $AS --mcp 2>&1)
656 check_not "ASan chunked raw is clean" 'ERROR: AddressSanitizer' "$AR"
657 AE=$(printf '%s\n' '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"fetch","arguments":{"url":"http://127.0.0.1:18680/404"}}}' | env ASAN_OPTIONS=detect_leaks=1 $AS --mcp 2>&1)
658 check_not "ASan error path is clean" 'ERROR: AddressSanitizer' "$AE"
659 AQ=$(QUERY_STRING='action=fetch&url=http%3A%2F%2F127.0.0.1%3A18680%2Fbig&max_length=777' FETCH_ALLOW_PRIVATE=1 ASAN_OPTIONS=detect_leaks=1 $AS 2>&1)
660 check_not "ASan CGI path is clean" 'ERROR: AddressSanitizer' "$AQ"
661 check "ASan CGI answered" '"ok":true' "$AQ"
662 ASAN_TLS=$(printf '%s\n' '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"fetch","arguments":{"url":"https://127.0.0.1:'"$((${BASE} + 1))"'/article","max_length":200}}}' | env FETCH_ALLOW_PRIVATE=1 FETCH_TLS_CA_FILE=$WORK/tls/cert.pem ASAN_OPTIONS=detect_leaks=1 $AS --mcp 2>&1)
663 check_not "ASan TLS path is clean" 'ERROR: AddressSanitizer' "$ASAN_TLS"
664 check "ASan TLS answered" '# Widgets and Gadgets' "$ASAN_TLS"
665else
666 fail "ASan build failed"; sed 's/^/ /' "$WORK/asan.err" | head -8
667fi
668
669if make fortify >"$WORK/fort.out" 2>"$WORK/fort.err"; then
670 pass "the _FORTIFY_SOURCE=2 build compiles"
671 FO=$(env QUERY_STRING='action=fetch&url=http%3A%2F%2F127.0.0.1%3A18680%2Farticle&max_length=200' FETCH_ALLOW_PRIVATE=1 ./plugin_fetch-fortify | body)
672 json_ok "the fortified build fetches" "$FO" "d['ok'] and d['status_code']==200"
673 check "the fortified page still serves" '<!DOCTYPE html>' "$(body "$(env QUERY_STRING='' ./plugin_fetch-fortify)")"
674 check "the fortified --meta is exact" '"menus":false' "$(./plugin_fetch-fortify --meta)"
675else
676 fail "fortified build failed"; sed 's/^/ /' "$WORK/fort.err" | head -8
677fi
678
679# ---------------------------------------------------------------- artifacts
680section "cross-target artifacts"
681if [ -f plugin_fetch.exe ]; then
682 MAGIC=$(od -An -tx1 -N2 plugin_fetch.exe | tr -d ' \n')
683 [ "$MAGIC" = "4d5a" ] && pass "plugin_fetch.exe starts with the MZ magic" \
684 || fail "PE magic wrong ($MAGIC)"
685else
686 echo " (skip) plugin_fetch.exe not built here"
687fi
688if [ -f plugin_fetch.musl ]; then
689 file plugin_fetch.musl | grep -q 'statically linked' \
690 && pass "the musl build is static" || fail "the musl build is not static"
691 MQ=$(env QUERY_STRING='action=info' ./plugin_fetch.musl | body)
692 json_ok "the musl build answers info" "$MQ" "d['ok'] is True"
693else
694 echo " (skip) plugin_fetch.musl not built here"
695fi
696if [ -f plugin_fetch.arm64 ]; then
697 file plugin_fetch.arm64 | grep -Eq 'aarch64|ARM' \
698 && pass "the arm64 build is aarch64" || fail "the arm64 build is not aarch64"
699else
700 echo " (skip) plugin_fetch.arm64 not built here"
701fi
702
703# ---------------------------------------------------------------- audit
704section "git history secret audit"
705if [ -d .git ] && command -v scorpiox-cli-botplugin >/dev/null 2>&1; then
706 if scorpiox-cli-botplugin audit . --allow-dirty >"$WORK/audit.out" 2>&1; then
707 pass "scorpiox-cli-botplugin audit is clean"
708 else
709 fail "audit reported a problem"
710 sed 's/^/ /' "$WORK/audit.out" | head -12
711 fi
712elif [ -d .git ]; then
713 # The pattern list deliberately lives in the scanner and not in a regex
714 # here: naming the strings a scanner looks for inside the repo being
715 # scanned trips the very history check we are calling, because a text file
716 # that spells a fleet password is indistinguishable from leaking it. So
717 # the fallback is shape-only - high-entropy token prefixes plus the two
718 # LAN address shapes - and every literal is split so the repo never
719 # contains a string the scanner matches.
720 HITS=$(git log --all -p 2>/dev/null | grep -nE \
721 'ghp_[A-Za-z0-9]{36}|hf_[A-Za-z0-9]{30,}|xbo''xone[0-9]+|192\.16''8\.[0-9]+\.[0-9]+|(^|[^0-9])10\.0\.[0-9]+\.[0-9]+' \
722 | grep -vE 'check_not|scan "|grep' | head -5)
723 [ -z "$HITS" ] && pass "history scan is clean (no tokens, LAN IPs or fleet passwords)" \
724 || { fail "history contains a suspect"; printf '%s\n' "$HITS" | sed 's/^/ /'; }
725else
726 echo " (skip) not a git checkout yet"
727fi
728
729# ---------------------------------------------------------------- summary
730printf '\n%s==============================%s\n' "$C_B" "$C_0"
731printf ' %spassed%s %d\n' "$C_OK" "$C_0" "$PASS"
732if [ "$FAIL" = "0" ]; then
733 printf ' %sfailed%s 0\n' "$C_OK" "$C_0"
734 exit 0
735fi
736printf ' %sfailed%s %d\n' "$C_BAD" "$C_0" "$FAIL"
737exit 1