scorpiox-bot-plugin-docker
←run.shbash
403 lines · 19 KBHEADBlameDownload
1#!/bin/sh
2# tests/run.sh — 60+ offline checks for plugin_docker across all 3 modes.
3#
4# Runs against ./plugin_docker (native build) using a mock DOCKER_BIN script, so
5# nothing here needs a real container runtime. If a musl static build or a
6# Windows PE (plugin_docker.exe) is present, they are checked too. The whole git
7# history is finally audited for secrets, PATs and LAN IPs.
8#
9# Exit code 0 = everything passed.
10
11set -u
12
13ROOT=$(cd "$(dirname "$0")/.." && pwd)
14cd "$ROOT" || exit 1
15
16BIN=./plugin_docker
17PASS=0
18FAIL=0
19
20if [ -t 1 ]; then
21 C_OK=$(printf '\033[32m'); C_BAD=$(printf '\033[31m'); C_B=$(printf '\033[1m'); C_0=$(printf '\033[0m')
22else
23 C_OK=; C_BAD=; C_B=; C_0=
24fi
25pass() { PASS=$((PASS + 1)); printf ' %sok%s %s\n' "$C_OK" "$C_0" "$1"; }
26fail() { FAIL=$((FAIL + 1)); printf ' %sFAIL%s %s\n' "$C_BAD" "$C_0" "$1"; }
27
28check() { # check <desc> <expected-substring> <actual>
29 if printf '%s' "$3" | grep -F -q -- "$2"; then pass "$1"; else
30 fail "$1 (want: $2)"; printf ' got: %s\n' "$(printf '%s' "$3" | head -c 300)"
31 fi
32}
33
34check_not() { # check_not <desc> <forbidden-substring> <actual>
35 if printf '%s' "$3" | grep -F -q -- "$2"; then
36 fail "$1 (must NOT contain: $2)"; else pass "$1"
37 fi
38}
39
40section() { printf '\n%s== %s ==%s\n' "$C_B" "$1" "$C_0"; }
41
42body() { printf '%s' "$1" | sed '1,/^\r$/d'; }
43
44# ---------------------------------------------------------------- setup
45section "build"
46if [ ! -x "$BIN" ]; then
47 echo " building native binary..."
48 cc -O2 -std=c99 -Wall -Wextra -o "$BIN" src/plugin_docker.c || { echo " build failed"; exit 1; }
49fi
50[ -x "$BIN" ] && pass "native binary exists and is executable" || fail "native binary missing"
51
52# mock runtime
53MOCKDIR=$(mktemp -d 2>/dev/null || echo /tmp/pd_mock_$$)
54mkdir -p "$MOCKDIR"
55MOCK="$MOCKDIR/mockdocker"
56cat > "$MOCK" <<'MOCKEOF'
57#!/bin/sh
58case "$1" in
59 ps)
60 echo "a1b2c3d4e5f6|web|nginx:1.27|running|Up 3 hours|0.0.0.0:8080->80/tcp"
61 echo "b2c3d4e5f6a1|db|postgres:16|running|Up 3 hours|127.0.0.1:5432->5432/tcp"
62 echo "c3d4e5f6a1b2|old-job|alpine:3.20|exited|Exited (0) 2 days ago|"
63 exit 0 ;;
64 stats)
65 echo "a1b2c3d4e5f6|1.25%|12.5MiB / 1GiB"
66 echo "b2c3d4e5f6a1|0.40%|88.1MiB / 1GiB"
67 exit 0 ;;
68 logs)
69 echo "line one"
70 echo 'line two with "quotes" and a <tab>'
71 printf 'line three\n'
72 exit 0 ;;
73 start|stop|restart)
74 exit 0 ;;
75 *) echo "unknown command: $1" >&2; exit 1 ;;
76esac
77MOCKEOF
78chmod +x "$MOCK"
79
80# a "no runtime" path: empty DOCKER_BIN, PATH with no docker/podman
81# (no runtime helper: assignments are inlined at each call site)
82
83# ---------------------------------------------------------------- meta mode
84section "mode 1 --meta (must work without auth — discovery)"
85META=$("$BIN" --meta)
86check "meta emits name docker" '"name":"docker"' "$META"
87check "meta emits desc Containers" '"desc":"Containers"' "$META"
88METAENV=$(SX_PLUGIN_META=1 "$BIN")
89check "SX_PLUGIN_META=1 yields same meta" '"name":"docker"' "$METAENV"
90LEN=$(printf '%s' "$META" | wc -c | tr -d ' ')
91[ "$LEN" -lt 60 ] && pass "meta payload is compact ($LEN bytes)" || fail "meta payload too long ($LEN)"
92printf '%s' "$META" | python3 -c 'import json,sys; json.load(sys.stdin)' 2>/dev/null \
93 && pass "meta is valid JSON" || fail "meta is not valid JSON"
94DESC=$(printf '%s' "$META" | sed 's/.*"desc":"\([^"]*\)".*/\1/')
95[ ${#DESC} -le 15 ] && pass "desc length ${#DESC} <= 15 chars" || fail "desc too long (${#DESC})"
96NAME=$(printf '%s' "$META" | sed 's/.*"name":"\([^"]*\)".*/\1/')
97[ "$(printf '%s' "$NAME" | wc -w | tr -d ' ')" -le 1 ] && pass "name is a single word" || fail "name has spaces"
98check "--version prints version" '1.1.3' "$("$BIN" --version)"
99check "--help is available" 'usage:' "$("$BIN" --help)"
100
101# ---------------------------------------------------------------- list mode
102section "node auth"
103AUTHNONE=$(BOT_PASSWORD=testpw QUERY_STRING='action=list' "$BIN")
104check "no credentials -> 401" 'Status: 401' "$AUTHNONE"
105check "401 body is json error" 'invalid node password' "$AUTHNONE"
106check_not "401 leaks no container data" '"containers"' "$AUTHNONE"
107AUTHBAD=$(BOT_PASSWORD=testpw HTTP_X_BOT_PASSWORD=wrong QUERY_STRING='action=list' "$BIN")
108check "wrong header password -> 401" 'Status: 401' "$AUTHBAD"
109AUTHHDR=$(BOT_PASSWORD=testpw HTTP_X_BOT_PASSWORD=testpw QUERY_STRING='action=list' "$BIN")
110check "X-Bot-Password accepted" '"ok":true' "$(body "$AUTHHDR")"
111AUTHBEAR=$(BOT_PASSWORD=testpw HTTP_AUTHORIZATION='Bearer testpw' QUERY_STRING='action=list' "$BIN")
112check "Authorization Bearer accepted" '"ok":true' "$(body "$AUTHBEAR")"
113AUTHQ=$(BOT_PASSWORD=testpw QUERY_STRING='action=list&pwd=testpw' "$BIN")
114check "?pwd= accepted" '"ok":true' "$(body "$AUTHQ")"
115AUTHHTML=$(BOT_PASSWORD=testpw QUERY_STRING='' "$BIN")
116check "html page requires auth too" 'Status: 401' "$AUTHHTML"
117AUTHMETA=$(BOT_PASSWORD=testpw "$BIN" --meta)
118check "--meta succeeds without auth" '"name":"docker"' "$AUTHMETA"
119AUTHOPEN=$(QUERY_STRING='action=list' "$BIN")
120check "unset BOT_PASSWORD = open node" 'Content-Type: application/json' "$AUTHOPEN"
121
122# All remaining checks run with BOT_PASSWORD unset (open mode) so they stay readable.
123section "mode 2 JSON list (mock runtime)"
124export DOCKER_BIN="$MOCK"
125LIST=$(QUERY_STRING='action=list' "$BIN")
126LBODY=$(body "$LIST")
127check "list header content-type" 'Content-Type: application/json; charset=utf-8' "$LIST"
128check_not "list header has no Content-Length" 'Content-Length' "$LIST"
129check "list ok:true" '"ok":true' "$LBODY"
130check "list plugin id" '"plugin":"plugin_docker"' "$LBODY"
131check "list slug docker" '"slug":"docker"' "$LBODY"
132check "list version" '"version":"1.1.3"' "$LBODY"
133check "list runtime available" '"available":true' "$LBODY"
134check "list daemon available" '"daemon_available":true' "$LBODY"
135check "list count 3" '"count":3' "$LBODY"
136check "summary total 3" '"total":3' "$LBODY"
137check "summary running 2" '"running":2' "$LBODY"
138check "summary exited 1" '"exited":1' "$LBODY"
139check "container web present" '"name":"web"' "$LBODY"
140check "container db present" '"name":"db"' "$LBODY"
141check "container old-job present" '"name":"old-job"' "$LBODY"
142check "image field present" '"image":"nginx:1.27"' "$LBODY"
143check "state running present" '"state":"running"' "$LBODY"
144check "state exited present" '"state":"exited"' "$LBODY"
145check "ports field present" '0.0.0.0:8080->80/tcp' "$LBODY"
146check "cpu stat merged" '"cpu":"1.25%"' "$LBODY"
147check "mem stat merged" '12.5MiB / 1GiB' "$LBODY"
148printf '%s' "$LBODY" | python3 -c 'import json,sys; d=json.load(sys.stdin); assert len(d["containers"])==3' 2>/dev/null \
149 && pass "list body is valid JSON with 3 containers" || fail "list body JSON invalid"
150
151section "mode 2 JSON info"
152INFO=$(QUERY_STRING='action=info' "$BIN")
153IBODY=$(body "$INFO")
154check "info ok:true" '"ok":true' "$IBODY"
155check "info action info" '"action":"info"' "$IBODY"
156check "info runtime label" '"label":"custom"' "$IBODY"
157check "info pid present" '"pid":' "$IBODY"
158check "info utc present" '"utc":"20' "$IBODY"
159check "info supported list" '"supported":["list","info","logs","control"]' "$IBODY"
160printf '%s' "$IBODY" | python3 -c 'import json,sys; json.load(sys.stdin)' 2>/dev/null \
161 && pass "info body is valid JSON" || fail "info body JSON invalid"
162
163section "mode 2 JSON logs"
164LOGS=$(QUERY_STRING='action=logs&cid=web' "$BIN")
165LOG_BODY=$(body "$LOGS")
166check "logs ok:true" '"ok":true' "$LOG_BODY"
167check "logs cid echoed" '"cid":"web"' "$LOG_BODY"
168check "logs tail 100" '"tail":100' "$LOG_BODY"
169check "logs exit_code 0" '"exit_code":0' "$LOG_BODY"
170check "logs first line" 'line one' "$LOG_BODY"
171check "logs escapes quotes" 'line two with \"quotes\"' "$LOG_BODY"
172check_not "logs has no Content-Length" 'Content-Length' "$LOGS"
173printf '%s' "$LOG_BODY" | python3 -c 'import json,sys; d=json.load(sys.stdin); assert d["lines"][0]=="line one"' 2>/dev/null \
174 && pass "logs body is valid JSON with parsed lines" || fail "logs body JSON invalid"
175
176section "mode 2 unknown action"
177UNK=$(QUERY_STRING='action=bogus' "$BIN")
178check "unknown action rejected" 'unknown_action' "$(body "$UNK")"
179check "unknown action lists supported" '"supported":["list","info","logs","control"]' "$(body "$UNK")"
180
181section "mode 2 no runtime"
182LIST2=$(DOCKER_BIN= PATH=/nonexistent QUERY_STRING='action=list' "$BIN")
183NRT=$(body "$LIST2")
184check "no-runtime available false" '"available":false' "$NRT"
185check "no-runtime graceful message" 'Neither docker nor podman' "$NRT"
186check "no-runtime count 0" '"count":0' "$NRT"
187LOGS2=$(DOCKER_BIN= PATH=/nonexistent QUERY_STRING='action=logs&cid=web' "$BIN")
188check "no-runtime logs explains why" 'runtime_available' "$(body "$LOGS2")"
189
190section "podman state normalisation"
191PODMOCK="$MOCKDIR/podmock"
192cat > "$PODMOCK" <<'PODEOF'
193#!/bin/sh
194case "$1" in
195 ps)
196 echo "8890ba09c621|registry|registry:2|Up 4 months ago|Up 4 months ago|"
197 echo "aa11bb22cc33|job|alpine:3.20|Exited (1) 2 days ago|Exited (1) 2 days ago|"
198 echo "dd44ee55ff66|paused-one|redis:7|Paused|Paused|"
199 exit 0 ;;
200 stats) exit 0 ;;
201 *) exit 0 ;;
202esac
203PODEOF
204chmod +x "$PODMOCK"
205POD=$(DOCKER_BIN="$PODMOCK" QUERY_STRING='action=list' "$BIN")
206PBODY=$(body "$POD")
207check "podman: Up becomes running" '"state":"running"' "$PBODY"
208check "podman: Exited becomes exited" '"state":"exited"' "$PBODY"
209check "podman: Paused becomes paused" '"state":"paused"' "$PBODY"
210check "podman: running counted" '"running":1' "$PBODY"
211check "podman: exited counted" '"exited":1' "$PBODY"
212check "podman: paused counted" '"paused":1' "$PBODY"
213check_not "podman: raw status not used as state" '"state":"Up 4 months ago"' "$PBODY"
214
215section "injection & validation"
216section "injection & validation"
217INJ1=$(QUERY_STRING='action=logs&cid=a;id' "$BIN")
218check "semicolon cid rejected" 'invalid_cid' "$(body "$INJ1")"
219INJ2=$(QUERY_STRING='action=logs&cid=--help' "$BIN")
220check "leading-dash cid rejected" 'invalid_cid' "$(body "$INJ2")"
221INJ3=$(QUERY_STRING='action=logs&cid=a%24(id)' "$BIN")
222check "command-substitution cid rejected" 'invalid_cid' "$(body "$INJ3")"
223INJ4=$(QUERY_STRING='action=logs&cid=8a7b6c5d4e3f' "$BIN")
224check "valid hex cid accepted" '"ok":true' "$(body "$INJ4")"
225INJ5=$(QUERY_STRING='action=logs&cid=my_app.v2-x' "$BIN")
226check "valid dotted/dashed cid accepted" '"ok":true' "$(body "$INJ5")"
227INJ6=$(QUERY_STRING="action=logs&cid=$(printf 'a%.0s' $(seq 1 200))" "$BIN")
228check "over-long cid rejected" 'invalid_cid' "$(body "$INJ6")"
229
230section "control (POST-only, op allowlist)"
231CG=$(QUERY_STRING='action=control&op=start&cid=web' "$BIN")
232check "control GET refused" 'control_requires_post' "$(body "$CG")"
233check "control GET status 405" 'Status: 405' "$CG"
234CP=$(REQUEST_METHOD=POST CONTENT_LENGTH=17 QUERY_STRING='action=control' \
235 sh -c 'printf "op=start&cid=web" | ./plugin_docker')
236check "control POST start ok" '"ok":true' "$(body "$CP")"
237check "control POST op echoed" '"op":"start"' "$(body "$CP")"
238CP2=$(REQUEST_METHOD=POST CONTENT_LENGTH=17 QUERY_STRING='action=control' \
239 sh -c 'printf "op=pwn&cid=web " | ./plugin_docker')
240check "invalid op rejected" 'invalid_op' "$(body "$CP2")"
241CP3=$(REQUEST_METHOD=POST CONTENT_LENGTH=22 QUERY_STRING='action=control' \
242 sh -c 'printf "op=stop&cid=a;id " | ./plugin_docker')
243check "control POST injection rejected" 'invalid_cid' "$(body "$CP3")"
244CP4=$(printf 'op=start&cid=web' | DOCKER_BIN= PATH=/nonexistent \
245 REQUEST_METHOD=POST CONTENT_LENGTH=16 QUERY_STRING='action=control' ./plugin_docker)
246check "control POST without runtime degrades" 'runtime_available' "$(body "$CP4")"
247
248section "mode 3 HTML"
249HTML=$(QUERY_STRING='' "$BIN")
250check "html content-type" 'Content-Type: text/html; charset=utf-8' "$HTML"
251check_not "html has no Content-Length" 'Content-Length' "$HTML"
252check "html has viewport meta" 'width=device-width' "$HTML"
253check "html title docker" '<title>docker</title>' "$HTML"
254check "html back-link present" 'Back to Bot' "$HTML"
255check "html KPI total" "k-total" "$HTML"
256check "html KPI running" "k-run" "$HTML"
257check "html table rows host" "id='rows'" "$HTML"
258check "html log viewer card" "logCard" "$HTML"
259check "html uses /plugins?plugin= route" "/plugins?plugin=" "$HTML"
260check "html uses POST for control" "method:'POST'" "$HTML"
261check "html has theme switch" "bot_theme" "$HTML"
262
263# ---- theme handoff (bot_theme_plugin.h) ----
264# the bot owns the theme: ?theme= before first paint, sx-theme after it
265check "html reads ?theme= before paint" 'theme=(light|dark)' "$HTML"
266check "html listens for sx-theme" "d.type==='sx-theme'" "$HTML"
267check "html asks the host for the theme" "sx-theme-request" "$HTML"
268check "html uses bot token --bg-card" "--bg-card" "$HTML"
269check "html uses bot token --surface-2" "--surface-2" "$HTML"
270check "html uses bot token --text-2" "--text-2" "$HTML"
271check "html hides its toggle when embedded" "embed-hide" "$HTML"
272check_not "html no retired brand casing" 'Scorpio'"X Bot" "$HTML"
273check_not "html no private palette 0d0d10" '0d0d10' "$HTML"
274check_not "html no private palette 16161a" '16161a' "$HTML"
275check_not "html no private palette f4f4f7" 'f4f4f7' "$HTML"
276THEMEQ=$(QUERY_STRING='theme=light' "$BIN")
277check "?theme=light still renders the UI" 'prefers-color-scheme' "$THEMEQ"
278# ---- end theme handoff ----
279
280check "html closes document" '</html>' "$HTML"
281EMB=$(QUERY_STRING='embed=1' "$BIN")
282check_not "embed=1 strips the back-link" 'Back to Bot' "$EMB"
283check "embed=1 still renders UI" "k-total" "$EMB"
284ZEROEMOJI=$(printf '%s' "$HTML" | python3 -c '
285import sys
286s = sys.stdin.read()
287bad = [c for c in s if ord(c) > 0x2600 and ord(c) not in (0x2014, 0x2013, 0x2026)]
288print(len(bad))
289')
290[ "$ZEROEMOJI" = "0" ] && pass "HTML contains no emoji glyphs" || fail "HTML has $ZEROEMOJI emoji glyph(s)"
291PK="pico""bot"
292check_not "html has no internal fleet paths" "$PK" "$HTML"
293check_not "html has no private IPs" "192.168." "$HTML"
294
295section "format=json alias"
296FJ=$(QUERY_STRING='format=json' "$BIN")
297check "format=json routes to JSON" 'Content-Type: application/json' "$FJ"
298
299# ---------------------------------------------------------------- optional targets
300section "optional cross builds"
301if [ -f plugin_docker.musl ]; then
302 M=$(./plugin_docker.musl --meta)
303 check "musl build --meta" '"name":"docker"' "$M"
304else
305 echo " skip musl (not built)"
306fi
307if [ -f plugin_docker.arm64 ]; then
308 case $(head -c 4 plugin_docker.arm64 | od -An -tx1 | tr -d ' \n') in
309 7f454c46) pass "arm64 artifact is ELF" ;;
310 *) fail "arm64 artifact is not ELF" ;;
311 esac
312 # e_machine sits at offset 18, little endian: b7 00 == EM_AARCH64
313 EM=$(od -An -tx1 -j 18 -N 2 plugin_docker.arm64 | tr -d ' \n')
314 [ "$EM" = "b700" ] && pass "arm64 artifact declares EM_AARCH64" \
315 || fail "arm64 artifact e_machine is $EM, expected b700"
316elif command -v aarch64-linux-musl-gcc >/dev/null 2>&1 || command -v aarch64-linux-gnu-gcc >/dev/null 2>&1; then
317 A64=$(command -v aarch64-linux-musl-gcc 2>/dev/null || command -v aarch64-linux-gnu-gcc)
318 if "$A64" -O2 -std=c99 -static -o /tmp/plugin_docker_arm64_test src/plugin_docker.c 2>/dev/null; then
319 pass "aarch64 cross build succeeds"
320 EM=$(od -An -tx1 -j 18 -N 2 /tmp/plugin_docker_arm64_test | tr -d ' \n')
321 [ "$EM" = "b700" ] && pass "cross-built arm64 declares EM_AARCH64" \
322 || fail "cross-built arm64 e_machine is $EM, expected b700"
323 rm -f /tmp/plugin_docker_arm64_test
324 else
325 fail "aarch64 cross build failed"
326 fi
327else
328 echo " skip arm64 (no aarch64 cross compiler)"
329fi
330for slice in arm64 x86_64; do
331 f="plugin_docker.macos-$slice"
332 if [ -f "$f" ]; then
333 MF=$(head -c 4 "$f" | od -An -tx1 | tr -d ' \n')
334 case "$MF" in
335 cffaedfe|feedfacf|cefaedfe|feedface) pass "macos-$slice artifact is Mach-O" ;;
336 *) fail "macos-$slice artifact is not Mach-O ($MF)" ;;
337 esac
338 else
339 echo " skip macos-$slice (not built)"
340 fi
341done
342if [ -f plugin_docker.exe ]; then
343 MAGIC=$(head -c 2 plugin_docker.exe)
344 [ "$MAGIC" = "MZ" ] && pass "windows artifact has MZ header" || fail "windows artifact missing MZ"
345else
346 echo " skip windows PE (not built)"
347fi
348
349[ -f plugin.json ] && pass "file present: plugin.json" || fail "file missing: plugin.json"
350if [ -f plugin.json ]; then
351 python3 - <<'PY' >/dev/null 2>&1 && pass "plugin.json is valid JSON with the contract keys" || fail "plugin.json invalid or missing contract keys"
352import json
353d = json.load(open("plugin.json"))
354for k in ("slug", "id", "title", "summary", "category", "license", "capabilities", "trust_notice"):
355 assert k in d, k
356assert d["slug"] == "docker" and d["id"] == "plugin_docker", d
357assert isinstance(d["capabilities"], list) and d["capabilities"], d
358PY
359fi
360
361# ---------------------------------------------------------------- history audit
362section "git history secret & LAN audit"
363if git rev-parse --git-dir >/dev/null 2>&1; then
364 ALL="$MOCKDIR/history.txt"
365 # Bookkeeping lines of this audit are excluded so the detector cannot flag itself.
366 git log -p --all 2>/dev/null \
367 | grep -v '^Author: ' \
368 | grep -vE 'scan_hist|HITS=|grep -E|suspects=|audit "|check_not "html has no internal fleet' \
369 > "$ALL" || : > "$ALL"
370
371 audit() { # audit <label> <regex>
372 H=$(grep -Eo "$2" "$ALL" 2>/dev/null | sort -u | head -3 || true)
373 if [ -n "$H" ]; then
374 fail "history: $1"; printf '%s\n' "$H" | sed 's/^/ /'
375 else
376 pass "history clean: $1"
377 fi
378 }
379 audit "LAN 192.168.x" '192\.168\.[0-9]+\.[0-9]+'
380 audit "private 10.x" '\b10\.[0-9]+\.[0-9]+\.[0-9]+'
381 audit "private 172.16-31.x" '172\.(1[6-9]|2[0-9]|3[01])\.[0-9]+\.[0-9]+'
382 audit "GitHub PAT" 'ghp_[A-Za-z0-9]{36}'
383 audit "HuggingFace token" 'hf_[A-Za-z0-9]{30,}'
384 audit "embedded git creds" 'scorpioxinc:[A-Za-z0-9]{20,}'
385 # the literal is split so this file never contains the string it hunts
386 audit "bare host password" 'Xbo''xone0909|xbo''xone'
387 PK="pico""bot"; WT=".work""trees"
388 audit "fleet path" "/codebases/${PK}|${WT}/"
389else
390 echo " skip history audit (not a git checkout)"
391fi
392
393# ---------------------------------------------------------------- summary
394rm -rf "$MOCKDIR"
395printf '\n%s== summary ==%s\n' "$C_B" "$C_0"
396printf ' passed: %d\n' "$PASS"
397printf ' failed: %d\n' "$FAIL"
398if [ "$FAIL" -ne 0 ]; then
399 printf ' %sRESULT: FAILED%s\n' "$C_BAD" "$C_0"
400 exit 1
401fi
402printf ' %sRESULT: ALL PASSED%s\n' "$C_OK" "$C_0"
403exit 0