| 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 | |
| 40 | set -u |
| 41 | |
| 42 | ROOT=$(cd "$(dirname "$0")/.." && pwd) |
| 43 | cd "$ROOT" || exit 1 |
| 44 | |
| 45 | BIN=./plugin_fetch |
| 46 | MCP=tests/mcp.py |
| 47 | PASS=0 |
| 48 | FAIL=0 |
| 49 | |
| 50 | if [ -t 1 ]; then |
| 51 | C_OK=$(printf '\033[32m'); C_BAD=$(printf '\033[31m'); C_B=$(printf '\033[1m'); C_0=$(printf '\033[0m') |
| 52 | else |
| 53 | C_OK=; C_BAD=; C_B=; C_0= |
| 54 | fi |
| 55 | pass() { PASS=$((PASS + 1)); printf ' %sok%s %s\n' "$C_OK" "$C_0" "$1"; } |
| 56 | fail() { FAIL=$((FAIL + 1)); printf ' %sFAIL%s %s\n' "$C_BAD" "$C_0" "$1"; } |
| 57 | |
| 58 | check() { # 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 | |
| 64 | check_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 | |
| 70 | section() { printf '\n%s== %s ==%s\n' "$C_B" "$1" "$C_0"; } |
| 71 | |
| 72 | json_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. |
| 85 | body() { |
| 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 | |
| 96 | status_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 ...] |
| 99 | api() { |
| 100 | _qs="$1"; shift |
| 101 | body "$(env "$@" QUERY_STRING="$_qs" $BIN)" |
| 102 | } |
| 103 | apiraw() { |
| 104 | _qs="$1"; shift |
| 105 | env "$@" QUERY_STRING="$_qs" $BIN |
| 106 | } |
| 107 | # mcpcall <desc> <json args object> [extra-env KEY=VALUE ...] -> prints result text |
| 108 | mcp_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 |
| 113 | try: |
| 114 | d=json.load(sys.stdin) |
| 115 | except Exception as e: |
| 116 | print('UNPARSEABLE', e); raise SystemExit |
| 117 | r=d.get('result') or {} |
| 118 | print(''.join(c.get('text','') for c in r.get('content',[])))" 2>/dev/null |
| 119 | } |
| 120 | |
| 121 | TESTPW=test-node-password-$$ |
| 122 | BASE=18680 |
| 123 | HTTP=http://127.0.0.1:$BASE |
| 124 | HTTPS_HOST=test.invalid |
| 125 | ALLOW=FETCH_ALLOW_PRIVATE=1 |
| 126 | |
| 127 | # ---------------------------------------------------------------- work tree |
| 128 | WORK=$(mktemp -d "${TMPDIR:-/tmp}/fetch-tests-XXXXXX") |
| 129 | cleanup() { |
| 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 | } |
| 134 | trap cleanup EXIT |
| 135 | |
| 136 | CONF=$WORK/conf # SX_CONNECTOR_DIR for the policy file |
| 137 | mkdir -p "$CONF" |
| 138 | |
| 139 | wait_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 |
| 149 | section "fixtures" |
| 150 | mkdir -p "$WORK/tls" |
| 151 | if 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 |
| 164 | else |
| 165 | fail "openssl is required to generate the TLS fixtures" |
| 166 | fi |
| 167 | |
| 168 | python3 tests/mockd.py http "$BASE" >"$WORK/mockd.out" 2>"$WORK/mockd.err" & |
| 169 | MOCK_PID=$! |
| 170 | python3 tests/mockd.py https $((BASE + 1)) "$WORK/tls/cert.pem" "$WORK/tls/key.pem" \ |
| 171 | >"$WORK/mockd2.out" 2>"$WORK/mockd2.err" & |
| 172 | MOCK_PID2=$! |
| 173 | if 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)" |
| 175 | else |
| 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 |
| 180 | fi |
| 181 | HTTPS_URL=https://127.0.0.1:$((BASE + 1)) |
| 182 | |
| 183 | # ---------------------------------------------------------------- build |
| 184 | section "build" |
| 185 | if [ ! -x "$BIN" ]; then |
| 186 | echo " building native binary..." |
| 187 | make native >"$WORK/build.out" 2>"$WORK/build.err" || true |
| 188 | fi |
| 189 | if [ -x "$BIN" ]; then |
| 190 | pass "the native binary exists" |
| 191 | else |
| 192 | fail "no ./plugin_fetch - run make first" |
| 193 | exit 1 |
| 194 | fi |
| 195 | if 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)" |
| 197 | else |
| 198 | fail "src/ui_html.h is stale - run python3 tools/gen_ui.py" |
| 199 | fi |
| 200 | |
| 201 | # ---------------------------------------------------------------- --meta |
| 202 | section "--meta discovery" |
| 203 | META=$(./plugin_fetch --meta) |
| 204 | check "--meta is one line" '"name":"fetch"' "$META" |
| 205 | json_ok "--meta parses" "$META" "True" |
| 206 | json_ok "--meta name is fetch" "$META" "d['name']=='fetch'" |
| 207 | json_ok "--meta desc is short" "$META" "d['desc']=='URL to Markdown' and len(d['desc'])<=15" |
| 208 | json_ok "--meta kind is mcp" "$META" "d['kind']=='mcp'" |
| 209 | json_ok "--meta mcp is true" "$META" "d['mcp'] is True" |
| 210 | json_ok "--meta daemon is false" "$META" "d['daemon'] is False" |
| 211 | json_ok "--meta menus is false (kept out of the dropdowns)" "$META" "d['menus'] is False" |
| 212 | json_ok "--meta verified is live" "$META" "d['verified']=='live'" |
| 213 | check "SX_PLUGIN_META=1 works too" '"kind":"mcp"' "$(SX_PLUGIN_META=1 ./plugin_fetch)" |
| 214 | check "--version prints the id" 'plugin_fetch' "$($BIN --version)" |
| 215 | check "--schema prints the tool schema" '"max_length"' "$($BIN --schema)" |
| 216 | |
| 217 | # ---------------------------------------------------------------- --mcp |
| 218 | section "--mcp protocol" |
| 219 | python3 - >"$WORK/calls.json" <<'PY' |
| 220 | import json |
| 221 | print(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 | ])) |
| 234 | PY |
| 235 | python3 "$MCP" $BIN "$WORK/calls.json" FETCH_ALLOW_PRIVATE=1 >"$WORK/mcp.tsv" 2>&1 |
| 236 | fld() { awk -F'\t' -v k="$1" '$1==k{print $3}' "$WORK/mcp.tsv"; } |
| 237 | st() { awk -F'\t' -v k="$1" '$1==k{print $2}' "$WORK/mcp.tsv"; } |
| 238 | |
| 239 | check "tools/list advertises exactly the fetch tool" '"name": "fetch"' "$(fld list)" |
| 240 | check "the tool carries a description" 'Fetches a URL' "$(fld list)" |
| 241 | check "the schema requires url" '"required": ["url"]' "$(fld list)" |
| 242 | check "the schema has max_length with the 100000 cap" '"maximum": 100000' "$(fld list)" |
| 243 | check "the schema has start_index" '"start_index"' "$(fld list)" |
| 244 | check "the schema has raw" '"raw": {"type": "boolean"' "$(fld list)" |
| 245 | check_not "the tool is not advertised as destructive" '"destructiveHint": true' "$(fld list)" |
| 246 | check "ping answers an empty result" '{}' "$(fld ping)" |
| 247 | check "an unknown method is -32601" 'method not found' "$(fld bogus)" |
| 248 | check "an unknown tool is -32602" 'unknown tool' "$(fld unknown)" |
| 249 | check "a missing url is an error" 'url is required' "$(fld nourl)" |
| 250 | check "a schemeless url is refused" 'no scheme' "$(fld noscheme)" |
| 251 | check "ftp is refused" "scheme 'ftp'" "$(fld badscheme)" |
| 252 | check "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 |
| 256 | check "string numbers are coerced (max_length honoured as a string)" 'start_index of 40' "$(fld strnums)" |
| 257 | check "the request sends our User-Agent" 'SCORPIOXBOT-fetch' "$(fld useragent)" |
| 258 | check "the request asks for identity encoding" 'identity' "$(fld useragent)" |
| 259 | check "the request closes the connection" '"connection": "close"' "$(fld useragent)" |
| 260 | check "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 |
| 263 | printf '%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 |
| 265 | check "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 |
| 271 | section "html -> markdown" |
| 272 | ART=$(mcp_text '{"url":"http://127.0.0.1:18680/article"}' FETCH_ALLOW_PRIVATE=1) |
| 273 | check "h1 becomes an ATX heading" '# Widgets and Gadgets' "$ART" |
| 274 | check "h2 becomes ##" '## Installation' "$ART" |
| 275 | check "h3 becomes ###" '### Steps' "$ART" |
| 276 | check "h4 becomes ####" '#### Fourth' "$ART" |
| 277 | check "h5 becomes #####" '##### Fifth' "$ART" |
| 278 | check "h6 becomes ######" '###### Sixth' "$ART" |
| 279 | check "strong becomes **" '**restart**' "$ART" |
| 280 | check "em becomes *" '*service*' "$ART" |
| 281 | check "inline code becomes backticks" '`make install`' "$ART" |
| 282 | check "links become [text](href)" '[relative link](http://127.0.0.1:18680/a/b?x=1&y=2)' "$ART" |
| 283 | check "external links keep their absolute href" '(https://elsewhere.example/doc)' "$ART" |
| 284 | check "unordered list items become -" '- First' "$ART" |
| 285 | check "nested list items are indented" ' - nested item' "$ART" |
| 286 | check "ordered list items become 1." '1. one' "$ART" |
| 287 | check "pre/code becomes a fence" '```' "$ART" |
| 288 | check "blockquote becomes >" '> Quoted text' "$ART" |
| 289 | check "hr becomes ---" '---' "$ART" |
| 290 | check "image becomes ![]()" '' "$ART" |
| 291 | check "named entities decode" 'cafรฉ' "$ART" |
| 292 | check "decimal entities decode" 'โ' "$ART" |
| 293 | check "hex entities decode" 'โ' "$ART" |
| 294 | check "ampersand entity decodes" '& then' "$ART" |
| 295 | check "nbsp becomes a non-breaking space" '5ย USD' "$ART" |
| 296 | check "angle brackets survive as text" '<3>' "$ART" |
| 297 | check "table cells are kept" 'Name' "$ART" |
| 298 | check_not "script content is stripped" 'SHOULD_NOT_APPEAR_script' "$ART" |
| 299 | check_not "style content is stripped" 'SHOULD_NOT_APPEAR_style' "$ART" |
| 300 | check_not "noscript content is stripped" 'SHOULD_NOT_APPEAR_noscript' "$ART" |
| 301 | check_not "svg content is stripped" 'SHOULD_NOT_APPEAR_svg' "$ART" |
| 302 | check_not "nav content is stripped" 'SHOULD_NOT_APPEAR_nav' "$ART" |
| 303 | check_not "aside content is stripped" 'SHOULD_NOT_APPEAR_aside' "$ART" |
| 304 | check_not "form content is stripped" 'SHOULD_NOT_APPEAR_form' "$ART" |
| 305 | check_not "footer content is stripped" 'SHOULD_NOT_APPEAR_footer' "$ART" |
| 306 | check_not "html comments are stripped" 'SHOULD_NOT_APPEAR_comment' "$ART" |
| 307 | check_not "no raw tag survives" '<p>' "$ART" |
| 308 | check "the document is prefixed with the source" 'Contents of http://127.0.0.1:18680/article:' "$ART" |
| 309 | |
| 310 | TITLE=$(mcp_text '{"url":"http://127.0.0.1:18680/title"}' FETCH_ALLOW_PRIVATE=1) |
| 311 | check "the title is added as an h1 when the body lacks it" '# TITLE_IS_HERE' "$TITLE" |
| 312 | check "the body h1 survives alongside the title" '# H1_IS_DIFFERENT' "$TITLE" |
| 313 | check "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 | |
| 315 | PLAIN=$(mcp_text '{"url":"http://127.0.0.1:18680/plain"}' FETCH_ALLOW_PRIVATE=1) |
| 316 | check "text/plain says it cannot be simplified" 'cannot be simplified to markdown' "$PLAIN" |
| 317 | check "text/plain returns the body" 'plain text body' "$PLAIN" |
| 318 | MD=$(mcp_text '{"url":"http://127.0.0.1:18680/md"}' FETCH_ALLOW_PRIVATE=1) |
| 319 | check "text/markdown is returned verbatim" '# MD body' "$MD" |
| 320 | check_not "text/markdown gets no simplification notice" 'cannot be simplified' "$MD" |
| 321 | JSON=$(mcp_text '{"url":"http://127.0.0.1:18680/json"}' FETCH_ALLOW_PRIVATE=1) |
| 322 | check "application/json is returned raw" '{"hello": "world"}' "$JSON" |
| 323 | SNIFF=$(mcp_text '{"url":"http://127.0.0.1:18680/noctype"}' FETCH_ALLOW_PRIVATE=1) |
| 324 | check "an undeclared HTML body is sniffed as markdown" '# sniffed' "$SNIFF" |
| 325 | NOTXT=$(mcp_text '{"url":"http://127.0.0.1:18680/noctype-text"}' FETCH_ALLOW_PRIVATE=1) |
| 326 | check "an undeclared plain body is not sniffed as markdown" 'cannot be simplified' "$NOTXT" |
| 327 | |
| 328 | # ---------------------------------------------------------------- chunking |
| 329 | section "pagination" |
| 330 | P1=$(mcp_text '{"url":"http://127.0.0.1:18680/article","max_length":100}' FETCH_ALLOW_PRIVATE=1) |
| 331 | check "a short max_length truncates" 'Content truncated' "$P1" |
| 332 | check "the footer names the next start_index" 'start_index of 100' "$P1" |
| 333 | P2=$(mcp_text '{"url":"http://127.0.0.1:18680/article","max_length":100,"start_index":100}' FETCH_ALLOW_PRIVATE=1) |
| 334 | check "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 |
| 337 | check "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" |
| 339 | PEND=$(mcp_text '{"url":"http://127.0.0.1:18680/article","start_index":900000}' FETCH_ALLOW_PRIVATE=1) |
| 340 | check "an out-of-range start_index says no more content" 'No more content available' "$PEND" |
| 341 | PCAP=$(mcp_text '{"url":"http://127.0.0.1:18680/big","max_length":999999}' FETCH_ALLOW_PRIVATE=1) |
| 342 | check "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" |
| 346 | PRAW=$(mcp_text '{"url":"http://127.0.0.1:18680/article","raw":true}' FETCH_ALLOW_PRIVATE=1) |
| 347 | check "raw=true keeps the HTML" '<!DOCTYPE html>' "$PRAW" |
| 348 | check_not "raw=true does not convert" '# Widgets and Gadgets' "$PRAW" |
| 349 | check_not "raw=true carries no simplification notice" 'cannot be simplified' "$PRAW" |
| 350 | |
| 351 | # ---------------------------------------------------------------- transport |
| 352 | section "transport" |
| 353 | R1=$(mcp_text '{"url":"http://127.0.0.1:18680/r1","max_length":120}' FETCH_ALLOW_PRIVATE=1) |
| 354 | check "a redirect chain is followed" 'Redirecting from' "$R1" |
| 355 | check "the chain reports both ends" "Redirecting from http://127.0.0.1:18680/r1 to http://127.0.0.1:18680/article" "$R1" |
| 356 | check "the final content arrives" '# Widgets and Gadgets' "$R1" |
| 357 | REL=$(mcp_text '{"url":"http://127.0.0.1:18680/rel"}' FETCH_ALLOW_PRIVATE=1) |
| 358 | check "a relative Location resolves against the base path" 'reached /relative' "$REL" |
| 359 | LOOP=$(mcp_text '{"url":"http://127.0.0.1:18680/loop"}' FETCH_ALLOW_PRIVATE=1) |
| 360 | check "a redirect loop hits the hop cap" 'Exceeded maximum allowed redirects' "$LOOP" |
| 361 | SLOOP=$(mcp_text '{"url":"http://127.0.0.1:18680/selfloop"}' FETCH_ALLOW_PRIVATE=1) |
| 362 | check "an absolute self-loop hits the hop cap too" 'Exceeded maximum allowed redirects' "$SLOOP" |
| 363 | E404=$(mcp_text '{"url":"http://127.0.0.1:18680/404"}' FETCH_ALLOW_PRIVATE=1) |
| 364 | check "404 is an error, not content" 'status code 404' "$E404" |
| 365 | E403=$(mcp_text '{"url":"http://127.0.0.1:18680/403"}' FETCH_ALLOW_PRIVATE=1) |
| 366 | check "403 is an error" 'status code 403' "$E403" |
| 367 | E500=$(mcp_text '{"url":"http://127.0.0.1:18680/500"}' FETCH_ALLOW_PRIVATE=1) |
| 368 | check "500 is an error" 'status code 500' "$E500" |
| 369 | BIG=$(mcp_text '{"url":"http://127.0.0.1:18680/headonly"}' FETCH_ALLOW_PRIVATE=1) |
| 370 | check "an oversized declared body is refused unread" 'larger than the' "$BIG" |
| 371 | CHK=$(mcp_text '{"url":"http://127.0.0.1:18680/chunked","max_length":120}' FETCH_ALLOW_PRIVATE=1) |
| 372 | check "a chunked body is decoded" '# Widgets and Gadgets' "$CHK" |
| 373 | check_not "the chunk sizes never leak into the body" '3f' "$CHK" |
| 374 | NCL=$(mcp_text '{"url":"http://127.0.0.1:18680/noclen"}' FETCH_ALLOW_PRIVATE=1) |
| 375 | check "a body with no Content-Length is read to close" 'no-content-length-body' "$NCL" |
| 376 | GZ=$(mcp_text '{"url":"http://127.0.0.1:18680/gzip"}' FETCH_ALLOW_PRIVATE=1) |
| 377 | if grep -q 'HAVE_ZLIB' Makefile && [ -n "${RUN_ZLIB_TESTS:-}" ]; then |
| 378 | check "gzip is decoded when the build has zlib" '# Widgets and Gadgets' "$GZ" |
| 379 | else |
| 380 | check "gzip is honestly refused when the build has no zlib" 'Unsupported content encoding gzip' "$GZ" |
| 381 | fi |
| 382 | check_not "the plugin never claims to accept gzip" 'gzip, deflate' "$(fld useragent)" |
| 383 | |
| 384 | # ---------------------------------------------------------------- SSRF |
| 385 | section "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. |
| 389 | DENY_V4='0.0.0.0 |
| 390 | 10.1.2.3 |
| 391 | 100.64.0.1 |
| 392 | 100.100.100.100 |
| 393 | 127.0.0.1 |
| 394 | 127.5.5.5 |
| 395 | 169.254.169.254 |
| 396 | 172.16.0.1 |
| 397 | 172.31.255.255 |
| 398 | 192.0.0.1 |
| 399 | 192.0.2.1 |
| 400 | 192.88.99.1 |
| 401 | 198.18.0.1 |
| 402 | 198.51.100.1 |
| 403 | 203.0.113.1 |
| 404 | 224.0.0.1 |
| 405 | 239.1.1.1 |
| 406 | 240.0.0.1 |
| 407 | 255.255.255.255' |
| 408 | ALLOW_V4='8.8.8.8 |
| 409 | 11.0.0.1 |
| 410 | 93.184.216.34 |
| 411 | 100.128.0.1 |
| 412 | 169.253.0.1 |
| 413 | 172.15.0.1 |
| 414 | 172.32.0.1 |
| 415 | 192.0.0.9 |
| 416 | 192.0.0.10 |
| 417 | 192.169.1.1 |
| 418 | 203.0.112.1' |
| 419 | DENY_V6=':: |
| 420 | ::1 |
| 421 | ::ffff:10.1.2.3 |
| 422 | ::ffff:127.0.0.1 |
| 423 | 100::5 |
| 424 | 2001::1 |
| 425 | 2001:1::1 |
| 426 | 2001:3::1 |
| 427 | 2001:4:112::1 |
| 428 | 2001:20::1 |
| 429 | 2001:30::1 |
| 430 | 2001:db8::1 |
| 431 | 2002::1 |
| 432 | 3fff::1 |
| 433 | 64:ff9b:1::5.6.7.8 |
| 434 | 64:ff9b::127.0.0.1 |
| 435 | 64:ff9b::8.8.8.8 |
| 436 | fc00::1 |
| 437 | fd12::1 |
| 438 | fe80::1 |
| 439 | fec0::1 |
| 440 | ffff::1' |
| 441 | ALLOW_V6='2001:4860:4860::8888 |
| 442 | 2606:4700:4700::1111 |
| 443 | 2a00: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 |
| 447 | PRIV_LAN=$(printf '%s.%s.%s.%s\n' 192 168 1 1) |
| 448 | PRIV_LAN2=$(printf '%s.%s.%s.%s\n' 192 168 255 254) |
| 449 | DENY_V4="$DENY_V4 |
| 450 | $PRIV_LAN |
| 451 | $PRIV_LAN2" |
| 452 | |
| 453 | DENYOUT=$(printf '%s\n' "$DENY_V4" | $BIN --selftest) |
| 454 | ALLOWOUT=$(printf '%s\n' "$ALLOW_V4" | $BIN --selftest) |
| 455 | BAD=$(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/^/ /'; } |
| 458 | BAD=$(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/^/ /'; } |
| 461 | DENY6=$(printf '%s\n' "$DENY_V6" | $BIN --selftest) |
| 462 | BAD=$(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/^/ /'; } |
| 465 | ALLOW6=$(printf '%s\n' "$ALLOW_V6" | $BIN --selftest) |
| 466 | BAD=$(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 | |
| 470 | check "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)" |
| 471 | check "a garbage address is refused (fail closed)" 'garbage denied=1' "$(printf '%s\n' 'garbage' | $BIN --selftest)" |
| 472 | check "an empty address is refused (fail closed)" 'denied=1' "$(printf '%s\n' '' | $BIN --selftest)" |
| 473 | check "a leading-zero octal form is refused (fail closed)" '0177.0.0.1 denied=1' "$(printf '%s\n' '0177.0.0.1' | $BIN --selftest)" |
| 474 | check "a hex form is refused (fail closed)" '0x7f.0.0.1 denied=1' "$(printf '%s\n' '0x7f.0.0.1' | $BIN --selftest)" |
| 475 | check "a short three-part form is refused (fail closed)" '1.2.3 denied=1' "$(printf '%s\n' '1.2.3' | $BIN --selftest)" |
| 476 | check "the limited broadcast address is refused" '255.255.255.255 denied=1' "$(printf '%s\n' '255.255.255.255' | $BIN --selftest)" |
| 477 | check "multicast is refused" '224.0.0.1 denied=1' "$(printf '%s\n' '224.0.0.1' | $BIN --selftest)" |
| 478 | |
| 479 | NOALLOW=$(mcp_text '{"url":"http://127.0.0.1:18680/plain"}') |
| 480 | check "loopback is refused with no allowance" 'private or reserved address space' "$NOALLOW" |
| 481 | check "the refusal names the offending address" 'resolves to a denied address (127.0.0.1)' "$NOALLOW" |
| 482 | ALLOWED=$(mcp_text '{"url":"http://127.0.0.1:18680/plain"}' FETCH_ALLOW_PRIVATE=1) |
| 483 | check "FETCH_ALLOW_PRIVATE=1 opens loopback for the suite" 'plain text body' "$ALLOWED" |
| 484 | PHOP=$(mcp_text '{"url":"http://127.0.0.1:18680/privatehop"}') |
| 485 | check "a redirect into private space is refused" 'private or reserved address space' "$PHOP" |
| 486 | BADT=$(mcp_text '{"url":"http://127.0.0.1:18680/badtarget"}' FETCH_ALLOW_PRIVATE=1) |
| 487 | check "a redirect to a bogus scheme is refused" 'scheme' "$BADT" |
| 488 | |
| 489 | section "ssrf policy toggle (persisted)" |
| 490 | POL=$(api 'action=policy' SX_CONNECTOR_DIR=$CONF) |
| 491 | json_ok "policy reads back off by default" "$POL" "d['ok'] and d['allow_private'] is False" |
| 492 | POL2=$(api 'action=policy&allow_private=true' SX_CONNECTOR_DIR=$CONF) |
| 493 | json_ok "policy can be switched on" "$POL2" "d['ok'] and d['allow_private'] is True" |
| 494 | check "the policy file was written" 'allow_private' "$(cat "$CONF/config.json" 2>/dev/null)" |
| 495 | NOFLAG=$(mcp_text '{"url":"http://127.0.0.1:18680/plain"}' SX_CONNECTOR_DIR=$CONF) |
| 496 | check "the persisted policy is honoured by the tool" 'plain text body' "$NOFLAG" |
| 497 | POL3=$(api 'action=policy&allow_private=false' SX_CONNECTOR_DIR=$CONF) |
| 498 | json_ok "policy can be switched off again" "$POL3" "d['allow_private'] is False" |
| 499 | RECLOSED=$(mcp_text '{"url":"http://127.0.0.1:18680/plain"}' SX_CONNECTOR_DIR=$CONF) |
| 500 | check "switching it off closes the door again" 'private or reserved' "$RECLOSED" |
| 501 | INFO=$(api 'action=info' SX_CONNECTOR_DIR=$CONF) |
| 502 | json_ok "info reports the policy state" "$INFO" "d['allow_private'] is False" |
| 503 | json_ok "info reports the tool surface" "$INFO" "d['tools']==['fetch'] and d['kind']=='mcp' and d['mcp'] is True" |
| 504 | json_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" |
| 505 | json_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 |
| 508 | section "tls" |
| 509 | if [ -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" |
| 523 | fi |
| 524 | |
| 525 | # ---------------------------------------------------------------- CGI API |
| 526 | section "cgi json api" |
| 527 | check "an action is 401 without the password" 'Status: 401' "$(apiraw 'action=info' BOT_PASSWORD=$TESTPW)" |
| 528 | check "the 401 body says invalid node password" 'invalid node password' "$(apiraw 'action=info' BOT_PASSWORD=$TESTPW)" |
| 529 | check "X-Bot-Password unlocks the action" '"ok":true' "$(api 'action=info' BOT_PASSWORD=$TESTPW HTTP_X_BOT_PASSWORD=$TESTPW)" |
| 530 | check "?pwd= unlocks the action" '"ok":true' "$(api "action=info&pwd=$TESTPW" BOT_PASSWORD=$TESTPW)" |
| 531 | check "a wrong password stays 401" 'Status: 401' "$(apiraw 'action=info&pwd=nope' BOT_PASSWORD=$TESTPW)" |
| 532 | check "Bearer unlocks the action" '"ok":true' "$(api 'action=info' BOT_PASSWORD=$TESTPW HTTP_AUTHORIZATION="Bearer $TESTPW")" |
| 533 | check "the page itself is never gated" '<!DOCTYPE html>' "$(body "$(apiraw '' BOT_PASSWORD=$TESTPW)")" |
| 534 | check "--meta is never gated" '"name":"fetch"' "$(BOT_PASSWORD=$TESTPW $BIN --meta)" |
| 535 | check "with no BOT_PASSWORD the node is open" '"ok":true' "$(api 'action=info')" |
| 536 | F1=$(api 'action=fetch&url=http%3A%2F%2F127.0.0.1%3A18680%2Farticle&max_length=80' FETCH_ALLOW_PRIVATE=1) |
| 537 | json_ok "action=fetch answers JSON" "$F1" "d['ok'] and d['status_code']==200" |
| 538 | json_ok "the JSON carries the text" "$F1" "d['text'].startswith('Contents of')" |
| 539 | json_ok "the JSON carries raw_html" "$F1" "'<!DOCTYPE html>' in d['raw_html']" |
| 540 | json_ok "the JSON carries content_type" "$F1" "d['content_type'].startswith('text/html')" |
| 541 | json_ok "the JSON reports bytes_read" "$F1" "d['bytes_read']>0" |
| 542 | json_ok "the JSON reports content_length" "$F1" "d['content_length']>0" |
| 543 | json_ok "the JSON reports redirected=false" "$F1" "d['redirected'] is False" |
| 544 | FR=$(api 'action=fetch&url=http%3A%2F%2F127.0.0.1%3A18680%2Fr1' FETCH_ALLOW_PRIVATE=1) |
| 545 | json_ok "the JSON reports redirected=true" "$FR" "d['redirected'] is True" |
| 546 | json_ok "the JSON reports the final url" "$FR" "d['url'].endswith('/article')" |
| 547 | FE=$(api 'action=fetch&url=http%3A%2F%2F127.0.0.1%3A18680%2F404' FETCH_ALLOW_PRIVATE=1) |
| 548 | json_ok "an error action is ok:false with the reason" "$FE" "d['ok'] is False and '404' in d['error']" |
| 549 | FNOAUTH=$(api 'action=fetch&url=http%3A%2F%2F127.0.0.1%3A18680%2Fplain') |
| 550 | check "a private target through the API is refused" 'private or reserved' "$FNOAUTH" |
| 551 | FPOSTBODY='{"action":"fetch","url":"http://127.0.0.1:18680/plain","max_length":400}' |
| 552 | FPOST=$(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) |
| 554 | json_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')" |
| 555 | FU=$(api 'action=bogus') |
| 556 | json_ok "an unknown action is refused" "$FU" "d['ok'] is False and d['error']=='unknown action'" |
| 557 | check "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)" |
| 558 | check "format=jsonx is NOT honoured" '<!DOCTYPE html>' "$(api 'format=jsonx&url=http%3A%2F%2F127.0.0.1%3A18680%2Fplain')" |
| 559 | check "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 |
| 562 | section "ui page" |
| 563 | PAGE=$(body "$(apiraw '')") |
| 564 | check "the page is a full document" '<!DOCTYPE html>' "$PAGE" |
| 565 | check "the page names the plugin" '<title>fetch' "$PAGE" |
| 566 | check "the page has the URL input" 'id="url"' "$PAGE" |
| 567 | check "the page has the Fetch button" 'id="go"' "$PAGE" |
| 568 | check "the page has max_length" 'id="maxlen"' "$PAGE" |
| 569 | check "the page has start_index" 'id="start"' "$PAGE" |
| 570 | check "the page has the raw switch" 'id="raw"' "$PAGE" |
| 571 | check "the page has Markdown and Raw tabs" 'id="tabraw"' "$PAGE" |
| 572 | check "the page has pagination controls" 'nextChunk()' "$PAGE" |
| 573 | check "the page has the SSRF policy switch" 'id="allowpriv"' "$PAGE" |
| 574 | check "the page shows the MCP schema" 'id="schema"' "$PAGE" |
| 575 | check "the page mentions the connector router" 'sx-connector-mcp' "$PAGE" |
| 576 | check_not "no external script is loaded" '<script src=' "$PAGE" |
| 577 | check_not "no external stylesheet is loaded" '<link rel="stylesheet"' "$PAGE" |
| 578 | check_not "no CDN reference" 'cdn.' "$PAGE" |
| 579 | check_not "no protocol-relative script" 'src="//' "$PAGE" |
| 580 | check_not "no remote font reference" 'fonts.googleapis' "$PAGE" |
| 581 | check "the page uses the bot theme tokens" 'var(--bg)' "$PAGE" |
| 582 | check "the theme handoff is injected" 'sx-theme' "$PAGE" |
| 583 | check "the prepaint script is injected" 'prefers-color-scheme' "$PAGE" |
| 584 | check "embed mode is honoured" 'embedded' "$PAGE" |
| 585 | check "the back link hides in embed mode" 'embed-hide' "$PAGE" |
| 586 | check_not "no emoji on the page" '๐' "$PAGE" |
| 587 | check_not "no emoji on the page" 'โ
' "$PAGE" |
| 588 | check_not "no emoji on the page" 'โ ' "$PAGE" |
| 589 | check_not "no raw private IP literal on the page" '192.168.' "$PAGE" |
| 590 | check_not "the page never sets a manual Content-Length" 'Content-Length' "$PAGE" |
| 591 | check "the page response carries a CSP" 'Content-Security-Policy' "$(apiraw '')" |
| 592 | check "the page response carries nosniff" 'X-Content-Type-Options: nosniff' "$(apiraw '')" |
| 593 | check "the page response carries SAMEORIGIN framing" 'X-Frame-Options: SAMEORIGIN' "$(apiraw '')" |
| 594 | check "the page response is not cached" 'Cache-Control: no-store' "$(apiraw '')" |
| 595 | check "the JSON response declares json" 'application/json' "$(apiraw 'action=info')" |
| 596 | check_not "no emoji in the source at all" '๐' "$(cat src/*.c)" |
| 597 | |
| 598 | # ---------------------------------------------------------------- source |
| 599 | section "source contract" |
| 600 | SRCALL=$(cat src/*.c src/*.h) |
| 601 | check_not "no system() call" 'system(' "$SRCALL" |
| 602 | check_not "no popen()" 'popen(' "$SRCALL" |
| 603 | check_not "no exec family" 'execvp(' "$SRCALL" |
| 604 | check_not "no fork" 'fork()' "$SRCALL" |
| 605 | check_not "no libcurl" 'curl_' "$SRCALL" |
| 606 | check_not "no openssl header" 'openssl/' "$SRCALL" |
| 607 | check_not "no unauthenticated TLS bypass" 'VERIFY_NONE' "$SRCALL" |
| 608 | check "TLS is peer-verified" 'MBEDTLS_SSL_VERIFY_REQUIRED' "$(cat src/fetch_tls.c)" |
| 609 | check "TLS has a 1.2 floor" 'MBEDTLS_SSL_VERSION_TLS1_2' "$(cat src/fetch_tls.c)" |
| 610 | check "every answer is validated before connect" 'resolves to a denied address' "$(cat src/fetch_http.c)" |
| 611 | check "the socket connects to the validated address" 'connected to that exact validated address' "$(cat src/fetch_http.h)" |
| 612 | check "the connect is non-blocking so the deadline is real" 'O_NONBLOCK' "$(cat src/fetch_http.c)" |
| 613 | check "the Windows leg sets hard timeouts too" 'WinHttpSetTimeouts' "$(cat src/fetch_http.c)" |
| 614 | check "WinHTTP is pinned to TLS 1.2+" 'WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2' "$(cat src/fetch_http.c)" |
| 615 | check "WinHTTP does not follow redirects itself" 'WINHTTP_OPTION_REDIRECT_POLICY_NEVER' "$(cat src/fetch_http.c)" |
| 616 | check "the body cap is 5 MB" 'define FX_MAX_BODY' "$(cat src/fetch_http.h)" |
| 617 | check "the redirect cap is 5" 'define FX_MAX_REDIRECTS 5' "$(cat src/fetch_http.h)" |
| 618 | check "the page is a generated header" '#include "ui_html.h"' "$(cat src/plugin_fetch.c)" |
| 619 | check "the generated header guard is fetch-specific" 'SX_FETCH_UI_HTML_H' "$(cat src/ui_html.h)" |
| 620 | check "make ui regenerates the page" 'gen_ui.py' "$(cat Makefile)" |
| 621 | check "case folding has no platform dependency" 'sx_strncasecmp' "$(cat src/sxbuf.c)" |
| 622 | check_not "no platform strcasecmp" '#include <strings.h>' "$SRCALL" |
| 623 | for 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" |
| 627 | done |
| 628 | check "LICENSE is MIT" 'MIT License' "$(cat LICENSE)" |
| 629 | check "plugin.json declares the slug" '"slug": "fetch"' "$(cat plugin.json)" |
| 630 | check "plugin.json declares the id" '"id": "plugin_fetch"' "$(cat plugin.json)" |
| 631 | check "plugin.json declares the network category" '"category": "network"' "$(cat plugin.json)" |
| 632 | check "plugin.json declares kind mcp" '"kind": "mcp"' "$(cat plugin.json)" |
| 633 | check "plugin.json declares mcp true" '"mcp": true' "$(cat plugin.json)" |
| 634 | check "plugin.json keeps it out of the menus" '"menus": false' "$(cat plugin.json)" |
| 635 | check "plugin.json is honestly verified live" '"verified": "live"' "$(cat plugin.json)" |
| 636 | check "plugin.json carries a trust notice" 'trust_notice' "$(cat plugin.json)" |
| 637 | check "plugin.json declares the win link flags" 'lwinhttp' "$(cat plugin.json)" |
| 638 | check "README documents the tool" 'start_index' "$(cat README.md)" |
| 639 | check "README documents the modes" '--mcp' "$(cat README.md)" |
| 640 | check ".gitignore ignores the binary" 'plugin_fetch' "$(cat .gitignore)" |
| 641 | check ".gitignore ignores __pycache__" '__pycache__' "$(cat .gitignore)" |
| 642 | check_not ".gitignore does not glob plugin_*" 'plugin_*' "$(cat .gitignore)" |
| 643 | |
| 644 | # ---------------------------------------------------------------- hardened |
| 645 | section "hardened builds" |
| 646 | if 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" |
| 665 | else |
| 666 | fail "ASan build failed"; sed 's/^/ /' "$WORK/asan.err" | head -8 |
| 667 | fi |
| 668 | |
| 669 | if 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)" |
| 675 | else |
| 676 | fail "fortified build failed"; sed 's/^/ /' "$WORK/fort.err" | head -8 |
| 677 | fi |
| 678 | |
| 679 | # ---------------------------------------------------------------- artifacts |
| 680 | section "cross-target artifacts" |
| 681 | if [ -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)" |
| 685 | else |
| 686 | echo " (skip) plugin_fetch.exe not built here" |
| 687 | fi |
| 688 | if [ -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" |
| 693 | else |
| 694 | echo " (skip) plugin_fetch.musl not built here" |
| 695 | fi |
| 696 | if [ -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" |
| 699 | else |
| 700 | echo " (skip) plugin_fetch.arm64 not built here" |
| 701 | fi |
| 702 | |
| 703 | # ---------------------------------------------------------------- audit |
| 704 | section "git history secret audit" |
| 705 | if [ -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 |
| 712 | elif [ -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/^/ /'; } |
| 725 | else |
| 726 | echo " (skip) not a git checkout yet" |
| 727 | fi |
| 728 | |
| 729 | # ---------------------------------------------------------------- summary |
| 730 | printf '\n%s==============================%s\n' "$C_B" "$C_0" |
| 731 | printf ' %spassed%s %d\n' "$C_OK" "$C_0" "$PASS" |
| 732 | if [ "$FAIL" = "0" ]; then |
| 733 | printf ' %sfailed%s 0\n' "$C_OK" "$C_0" |
| 734 | exit 0 |
| 735 | fi |
| 736 | printf ' %sfailed%s %d\n' "$C_BAD" "$C_0" "$FAIL" |
| 737 | exit 1 |