scorpiox-bot-plugin-files
←run.shbash
494 lines · 22 KBHEADBlameDownload
1#!/bin/sh
2# tests/run.sh - offline checks for plugin_files across all 3 modes.
3#
4# Every check runs against ./plugin_files with FILES_ALLOWED_ROOT pointing at a
5# throwaway fixture tree, so the suite never reads anything real. It proves the
6# public contract end to end: --meta, node auth on every action, each action's
7# JSON shape, the read-only promise, the 1 MiB preview cap, binary detection,
8# raw streaming checksums, directory ordering, hidden-file toggle, traversal
9# and symlink negatives -> 403/400, the generated UI (no CDN, no <script src=>),
10# the MinGW PE header when present, a _FORTIFY_SOURCE=2 build that must run
11# clean, and a full-history audit for secrets and LAN addresses.
12#
13# Exit code 0 = everything passed.
14
15set -u
16
17ROOT=$(cd "$(dirname "$0")/.." && pwd)
18cd "$ROOT" || exit 1
19
20BIN=./plugin_files
21PASS=0
22FAIL=0
23
24if [ -t 1 ]; then
25 C_OK=$(printf '\033[32m'); C_BAD=$(printf '\033[31m'); C_B=$(printf '\033[1m'); C_0=$(printf '\033[0m')
26else
27 C_OK=; C_BAD=; C_B=; C_0=
28fi
29pass() { PASS=$((PASS + 1)); printf ' %sok%s %s\n' "$C_OK" "$C_0" "$1"; }
30fail() { FAIL=$((FAIL + 1)); printf ' %sFAIL%s %s\n' "$C_BAD" "$C_0" "$1"; }
31
32check() { # check <desc> <expected-substring> <actual>
33 if printf '%s' "$3" | grep -F -q -- "$2"; then pass "$1"; else
34 fail "$1 (want: $2)"; printf ' got: %s\n' "$(printf '%s' "$3" | head -c 400)"
35 fi
36}
37
38check_not() { # check_not <desc> <forbidden-substring> <actual>
39 if printf '%s' "$3" | grep -F -q -- "$2"; then
40 fail "$1 (must NOT contain: $2)"; else pass "$1"
41 fi
42}
43
44section() { printf '\n%s== %s ==%s\n' "$C_B" "$1" "$C_0"; }
45
46# strip CGI headers if present, drop CRs, keep the payload
47body() {
48 printf '%s\n' "$1" | tr -d '\r' | awk '
49 BEGIN { inhdr = 0 }
50 NR == 1 && ($0 ~ /^(Content-Type|Status|Cache-Control|X-Content-Type-Options|X-Frame-Options|Content-Security-Policy|Content-Disposition):/) { inhdr = 1; next }
51 inhdr && $0 == "" { inhdr = 0; next }
52 inhdr { next }
53 { print }
54 '
55}
56
57json_ok() { # json_ok <desc> <json> [python-expression]
58 expr=${3:-True}
59 printf '%s' "$2" > "$WORK/j.json"
60 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
61 pass "$1"
62 else
63 fail "$1 (invalid JSON or assertion failed)"; printf ' got: %s\n' "$(printf '%s' "$2" | head -c 400)"
64 fi
65}
66
67api() { body "$(QUERY_STRING="$1" $BIN)"; }
68
69# raw output including CGI headers, for the Status: assertions
70apiraw() { QUERY_STRING="$1" $BIN; }
71
72status_of() { printf '%s' "$1" | tr -d '\r' | sed -n 's/^Status: \([0-9][0-9][0-9]\).*/\1/p' | head -1; }
73
74# ---------------------------------------------------------------- build
75section "build"
76if [ ! -x "$BIN" ]; then
77 echo " building native binary..."
78 cc -O2 -std=c99 -Wall -Wextra -Wno-overlength-strings -I src -o "$BIN" src/plugin_files.c \
79 || { echo " build failed"; exit 1; }
80 pass "native build"
81else
82 pass "native binary present"
83fi
84
85WORK=$(mktemp -d "${TMPDIR:-/tmp}/files-test.XXXXXX") || exit 1
86trap 'rm -rf "$WORK"' INT TERM EXIT
87FX="$WORK/root"
88mkdir -p "$FX/sub/deep/deeper" "$FX/emptydir" "$FX/.hiddendir" "$FX/pics" || exit 1
89
90printf 'hello world\nsecond line\n' > "$FX/a.txt"
91printf '# Title\n\nSome **bold** text and a [link](https://example.com).\n\n- one\n- two\n' > "$FX/readme.md"
92printf 'abc\000def\000ghi' > "$FX/bin.dat"
93printf 'hidden content\n' > "$FX/.hidden"
94: > "$FX/empty.txt"
95printf 'deep\n' > "$FX/sub/deep/deeper/leaf.txt"
96printf 'in sub\n' > "$FX/sub/inner.txt"
97python3 -c "open('$FX/big.txt','w').write('x'*(2*1024*1024))"
98python3 -c "
99import struct,zlib
100def chunk(t,d):
101 return struct.pack('>I',len(d))+t+d+struct.pack('>I',zlib.crc32(t+d)&0xffffffff)
102raw=b'\x00\xff\x00\xff'
103idat=zlib.compress(raw)
104png=b'\x89PNG\r\n\x1a\n'+chunk(b'IHDR',struct.pack('>IIBBBBB',2,1,8,2,0,0,0))+chunk(b'IDAT',idat)+chunk(b'IEND',b'')
105open('$FX/pics/px.png','wb').write(png)
106"
107ln -s /etc "$FX/etclink"
108ln -s "$FX/a.txt" "$FX/goodlink"
109
110export FILES_ALLOWED_ROOT="$FX"
111unset BOT_PASSWORD
112
113# ---------------------------------------------------------------- meta
114section "--meta / --version"
115M=$($BIN --meta)
116check "--meta emits name" '"name":"files"' "$M"
117check "--meta desc" '"desc":"File browser"' "$M"
118json_ok "--meta is valid JSON" "$M" "d['name']=='files' and d['desc']=='File browser'"
119M2=$(SX_PLUGIN_META=1 $BIN)
120check "SX_PLUGIN_META also emits meta" '"name":"files"' "$M2"
121V=$($BIN --version)
122check "--version" 'plugin_files 1.0.0' "$V"
123
124# ---------------------------------------------------------------- auth
125section "node auth"
126export BOT_PASSWORD=testpw
127R=$(apiraw "action=roots")
128check "no password -> 401" '401' "$(status_of "$R")"
129R=$(apiraw "action=ls&path=$FX")
130check "ls without password -> 401" '401' "$(status_of "$R")"
131R=$(apiraw "action=read&path=$FX/a.txt")
132check "read without password -> 401" '401' "$(status_of "$R")"
133R=$(apiraw "action=raw&path=$FX/a.txt")
134check "raw without password -> 401" '401' "$(status_of "$R")"
135R=$(apiraw "")
136check "HTML page without password -> 401" '401' "$(status_of "$R")"
137M=$($BIN --meta)
138check "--meta stays unauthenticated" '"name":"files"' "$M"
139R=$(HTTP_X_BOT_PASSWORD=testpw apiraw "action=roots")
140check "X-Bot-Password accepted" '"ok":true' "$(body "$R")"
141R=$(HTTP_AUTHORIZATION="Bearer testpw" apiraw "action=roots")
142check "Bearer token accepted" '"ok":true' "$(body "$R")"
143R=$(apiraw "action=roots&pwd=testpw")
144check "?pwd= accepted" '"ok":true' "$(body "$R")"
145R=$(apiraw "action=roots&pwd=wrong")
146check "wrong ?pwd= -> 401" '401' "$(status_of "$R")"
147unset BOT_PASSWORD
148
149# ---------------------------------------------------------------- roots
150section "?action=roots"
151R=$(api "action=roots")
152json_ok "roots ok" "$R" "d['ok'] is True"
153json_ok "roots has a list" "$R" "isinstance(d['roots'],list) and len(d['roots'])>=1"
154json_ok "fixture root is present" "$R" "any(r['path']=='$FX' for r in d['roots'])"
155json_ok "each root has name+path" "$R" "all('name' in r and 'path' in r for r in d['roots'])"
156json_ok "count matches list length" "$R" "d['count']==len(d['roots'])"
157json_ok "no /proc root" "$R" "all(r['path']!='/proc' for r in d['roots'])"
158json_ok "no /sys root" "$R" "all(r['path']!='/sys' for r in d['roots'])"
159json_ok "no /dev root" "$R" "all(r['path']!='/dev' for r in d['roots'])"
160R=$(api "format=json")
161json_ok "format=json falls back to roots" "$R" "d['ok'] is True and 'roots' in d"
162
163# ---------------------------------------------------------------- ls
164section "?action=ls"
165R=$(api "action=ls&path=$FX")
166json_ok "ls ok" "$R" "d['ok'] is True"
167json_ok "ls path echoed" "$R" "d['path']=='$FX'"
168json_ok "ls root is the fixture" "$R" "d['root']=='$FX'"
169json_ok "ls parent is null at a root" "$R" "d['parent'] is None"
170json_ok "ls count matches entries" "$R" "d['count']==len(d['entries'])"
171json_ok "ls not truncated" "$R" "d['truncated'] is False"
172json_ok "hidden off by default" "$R" "d['hidden']==0"
173json_ok "no dotfiles by default" "$R" "not any(e['name'].startswith('.') for e in d['entries'])"
174json_ok "dirs sort before files" "$R" "[e['type'] for e in d['entries']]==sorted([e['type'] for e in d['entries']], key=lambda t:(t!='dir'))"
175json_ok "names sorted dirs-first" "$R" "(lambda d:[e['name'] for e in d['entries'] if e['type']=='dir']==sorted([e['name'] for e in d['entries'] if e['type']=='dir'],key=str.lower) and [e['name'] for e in d['entries'] if e['type']!='dir']==sorted([e['name'] for e in d['entries'] if e['type']!='dir'],key=str.lower))(d)"
176json_ok "entry has size/mtime/mode" "$R" "all('size' in e and 'mtime' in e and 'mode' in e for e in d['entries'])"
177json_ok "dir entry flagged" "$R" "any(e['name']=='sub' and e['type']=='dir' for e in d['entries'])"
178json_ok "file entry flagged" "$R" "any(e['name']=='a.txt' and e['type']=='file' for e in d['entries'])"
179json_ok "a.txt size is 24" "$R" "any(e['name']=='a.txt' and e['size']==24 for e in d['entries'])"
180json_ok "mtime is ISO-8601" "$R" "all(len(e['mtime'])==20 and e['mtime'].endswith('Z') for e in d['entries'] if e['mtime'])"
181json_ok "mode is octal string" "$R" "all(e['mode'].isdigit() and len(e['mode'])==4 for e in d['entries'])"
182json_ok "empty file listed with size 0" "$R" "any(e['name']=='empty.txt' and e['size']==0 for e in d['entries'])"
183
184R=$(api "action=ls&path=$FX&hidden=1")
185json_ok "hidden=1 flags hidden" "$R" "d['hidden']==1"
186json_ok "hidden=1 shows dotfile" "$R" "any(e['name']=='.hidden' for e in d['entries'])"
187json_ok "hidden=1 shows dotdir" "$R" "any(e['name']=='.hiddendir' and e['type']=='dir' for e in d['entries'])"
188
189R=$(api "action=ls&path=$FX&hidden=true")
190json_ok "hidden=true also works" "$R" "any(e['name']=='.hidden' for e in d['entries'])"
191
192R=$(api "action=ls&path=$FX/sub")
193json_ok "nested parent is computed" "$R" "d['parent']=='$FX'"
194json_ok "nested root is still the fixture" "$R" "d['root']=='$FX'"
195json_ok "nested lists its own children" "$R" "any(e['name']=='inner.txt' for e in d['entries'])"
196
197R=$(api "action=ls&path=$FX/emptydir")
198json_ok "empty dir answers count 0" "$R" "d['count']==0 and d['entries']==[]"
199
200R=$(api "action=ls&path=$FX/goodlink")
201json_ok "symlink inside the root is browsable as a file" "$R" "'ok' in d"
202R=$(api "action=ls&path=$FX")
203json_ok "symlink entry is flagged link=true" "$R" "any(e['name']=='goodlink' and e['link'] is True for e in d['entries'])"
204json_ok "plain file is link=false" "$R" "any(e['name']=='a.txt' and e['link'] is False for e in d['entries'])"
205json_ok "every entry carries a link flag" "$R" "all('link' in e for e in d['entries'])"
206
207R=$(api "action=ls")
208json_ok "ls without path defaults to the first root" "$R" "d['ok'] is True and d['path'].startswith('/')"
209
210# ---------------------------------------------------------------- read
211section "?action=read"
212R=$(api "action=read&path=$FX/a.txt")
213json_ok "read ok" "$R" "d['ok'] is True"
214json_ok "read text" "$R" "d['text']=='hello world\\nsecond line\\n'"
215json_ok "read binary false" "$R" "d['binary'] is False"
216json_ok "read truncated false" "$R" "d['truncated'] is False"
217json_ok "read bytes_read matches file" "$R" "d['bytes_read']==24"
218json_ok "read name echoed" "$R" "d['name']=='a.txt'"
219json_ok "read size echoed" "$R" "d['size']==24"
220json_ok "read mode present" "$R" "len(d['mode'])==4"
221json_ok "read has no text key for binary" "$R" "True"
222
223R=$(api "action=read&path=$FX/bin.dat")
224json_ok "binary detected by NUL" "$R" "d['binary'] is True"
225json_ok "binary omits text" "$R" "'text' not in d"
226json_ok "binary still reports size" "$R" "d['size']==11"
227
228R=$(api "action=read&path=$FX/big.txt")
229json_ok "big file truncated" "$R" "d['truncated'] is True"
230json_ok "big file capped at 1 MiB" "$R" "d['bytes_read']==1048576"
231json_ok "big file still reports real size" "$R" "d['size']==2097152"
232
233R=$(api "action=read&path=$FX/empty.txt")
234json_ok "empty file reads as empty text" "$R" "d['text']=='' and d['bytes_read']==0"
235
236R=$(apiraw "action=read&path=$FX")
237check "read a directory -> 400" '400' "$(status_of "$R")"
238
239# ---------------------------------------------------------------- raw
240section "?action=raw"
241RAWR=$(apiraw "action=raw&path=$FX/a.txt")
242HDRS=$(printf '%s' "$RAWR" | tr -d '\r' | sed -n '1,/^$/p')
243check "raw content-type text/plain" 'Content-Type: text/plain; charset=utf-8' "$HDRS"
244check "raw attachment for text" 'Content-Disposition: attachment' "$HDRS"
245check "raw has no manual Content-Length" '0' "$(printf '%s' "$HDRS" | grep -c 'Content-Length')"
246check "raw nosniff" 'X-Content-Type-Options: nosniff' "$HDRS"
247check "raw sameorigin" 'X-Frame-Options: SAMEORIGIN' "$HDRS"
248
249# payload after the blank line must be byte-identical to the file
250python3 -c "
251import subprocess,sys,os,hashlib
252env=dict(os.environ); env['QUERY_STRING']='action=raw&path=$FX/a.txt'
253out=subprocess.run(['$BIN'],capture_output=True,env=env).stdout
254sep=out.find(b'\r\n\r\n')
255payload=out[sep+4:]
256want=open('$FX/a.txt','rb').read()
257sys.exit(0 if payload==want else 1)
258" && pass "raw payload equals the file byte-for-byte" || fail "raw payload equals the file byte-for-byte"
259
260python3 -c "
261import subprocess,sys,os,hashlib
262env=dict(os.environ); env['QUERY_STRING']='action=raw&path=$FX/pics/px.png'
263out=subprocess.run(['$BIN'],capture_output=True,env=env).stdout
264sep=out.find(b'\r\n\r\n')
265hdr=out[:sep].decode()
266payload=out[sep+4:]
267want=open('$FX/pics/px.png','rb').read()
268ok = payload==want and 'image/png' in hdr and 'inline' in hdr
269sys.exit(0 if ok else 1)
270" && pass "raw png is image/png, inline, byte-identical" || fail "raw png is image/png, inline, byte-identical"
271
272RAWR=$(apiraw "action=raw&path=$FX/pics/px.png&download=1")
273HDRS=$(printf '%s' "$RAWR" | tr -d '\r' | sed -n '1,/^$/p')
274check "download=1 forces attachment on an image" 'Content-Disposition: attachment' "$HDRS"
275
276RAWR=$(apiraw "action=raw&path=$FX/readme.md")
277HDRS=$(printf '%s' "$RAWR" | tr -d '\r' | sed -n '1,/^$/p')
278check "markdown served as text/plain (never text/html)" 'Content-Type: text/plain; charset=utf-8' "$HDRS"
279HDRS_HTML=$(printf '%s' "$RAWR" | tr -d '\r' | sed -n '1,/^$/p' | grep -c 'text/html')
280check "no text/html on raw markdown" '0' "$HDRS_HTML"
281
282printf 'PK\003\004xxxx' > "$FX/thing.zip"
283RAWR=$(apiraw "action=raw&path=$FX/thing.zip")
284HDRS=$(printf '%s' "$RAWR" | tr -d '\r' | sed -n '1,/^$/p')
285check "unknown type is octet-stream" 'application/octet-stream' "$HDRS"
286check "unknown type is attachment" 'Content-Disposition: attachment' "$HDRS"
287
288printf '<html><body><script>alert(1)</script></body></html>' > "$FX/evil.html"
289RAWR=$(apiraw "action=raw&path=$FX/evil.html")
290HDRS=$(printf '%s' "$RAWR" | tr -d '\r' | sed -n '1,/^$/p')
291check "html file is served as plain text" 'text/plain' "$HDRS"
292
293RAWR=$(apiraw "action=raw&path=$FX/big.txt")
294PLEN=$(printf '%s' "$RAWR" | wc -c | tr -d ' ')
295if [ "$PLEN" -gt 2000000 ]; then pass "raw streams a 2 MB file whole"; else fail "raw streams a 2 MB file whole ($PLEN)"; fi
296
297# ---------------------------------------------------------------- security
298section "security: traversal, symlink escape, denied roots"
299R=$(apiraw "action=ls&path=$FX/../..")
300check ".. in a path -> 400" '400' "$(status_of "$R")"
301R=$(apiraw "action=ls&path=$FX/sub/../../etc")
302check "nested .. -> 400" '400' "$(status_of "$R")"
303R=$(apiraw "action=ls&path=../../etc")
304check "relative .. path -> 400" '400' "$(status_of "$R")"
305R=$(apiraw "action=ls&path=$FX/%2e%2e/%2e%2e/etc")
306check "percent-encoded .. -> 400" '400' "$(status_of "$R")"
307R=$(apiraw "action=read&path=/etc/passwd")
308check "read outside every root -> 403" '403' "$(status_of "$R")"
309R=$(apiraw "action=ls&path=/etc")
310check "ls outside every root -> 403" '403' "$(status_of "$R")"
311R=$(apiraw "action=read&path=$FX/etclink/passwd")
312check "symlink pointing outside -> 403" '403' "$(status_of "$R")"
313R=$(apiraw "action=ls&path=$FX/etclink")
314check "ls a symlink that escapes -> 403" '403' "$(status_of "$R")"
315R=$(apiraw "action=raw&path=$FX/etclink/hostname")
316check "raw through an escaping symlink -> 403" '403' "$(status_of "$R")"
317R=$(apiraw "action=ls&path=/proc")
318check "/proc refused" '403' "$(status_of "$R")"
319R=$(apiraw "action=ls&path=/proc/self")
320check "/proc/self refused" '403' "$(status_of "$R")"
321R=$(apiraw "action=ls&path=/sys")
322check "/sys refused" '403' "$(status_of "$R")"
323R=$(apiraw "action=ls&path=/dev")
324check "/dev refused" '403' "$(status_of "$R")"
325R=$(apiraw "action=ls&path=")
326check "empty path falls back to a root, not the filesystem" '"ok":true' "$(body "$R")"
327R=$(apiraw "action=ls&path=$FX/missing")
328check "missing path -> 404" '404' "$(status_of "$R")"
329R=$(apiraw "action=read&path=$FX/missing.txt")
330check "missing file -> 404" '404' "$(status_of "$R")"
331R=$(apiraw "action=nope")
332check "unknown action -> 400" '400' "$(status_of "$R")"
333R=$(apiraw "action=ls&path=$FX/a.txt")
334check "ls a regular file -> 403 (cannot list)" '403' "$(status_of "$R")"
335
336# read-only by construction: no write-ish action exists
337for a in save write delete rename mkdir touch chmod upload put; do
338 R=$(apiraw "action=$a&path=$FX/a.txt")
339 check_not "no '$a' action" '"ok":true' "$(body "$R")"
340done
341SUM_BEFORE=$(python3 -c "
342import hashlib,os
343h=hashlib.sha256()
344for root,dirs,files in os.walk('$FX'):
345 for f in sorted(files):
346 p=os.path.join(root,f)
347 try: h.update(open(p,'rb').read())
348 except OSError: pass
349print(h.hexdigest())
350")
351api "action=read&path=$FX/a.txt" > /dev/null
352apiraw "action=raw&path=$FX/a.txt" > /dev/null
353api "action=ls&path=$FX" > /dev/null
354SUM_AFTER=$(python3 -c "
355import hashlib,os
356h=hashlib.sha256()
357for root,dirs,files in os.walk('$FX'):
358 for f in sorted(files):
359 p=os.path.join(root,f)
360 try: h.update(open(p,'rb').read())
361 except OSError: pass
362print(h.hexdigest())
363")
364check "fixture unchanged after every action" "$SUM_BEFORE" "$SUM_AFTER"
365
366# the source must contain no write syscall at all
367SRC=src/plugin_files.c
368W=$(grep -nE 'fopen\([^)]*"[wa]' "$SRC" | wc -l | tr -d ' ')
369check "no fopen for writing in the source" '0' "$W"
370for fn in fprintf fputc fwrite_never_used; do :; done
371check_not "no remove()" 'remove(' "$(grep -o 'remove(' "$SRC" | head -1)"
372check_not "no rmdir()" 'rmdir(' "$(grep -o 'rmdir(' "$SRC" | head -1)"
373check_not "no mkdir()" 'mkdir(' "$(grep -o 'mkdir(' "$SRC" | head -1)"
374check_not "no rename()" 'rename(' "$(grep -o 'rename(' "$SRC" | head -1)"
375check_not "no chmod()" 'chmod(' "$(grep -o 'chmod(' "$SRC" | head -1)"
376check_not "no popen()" 'popen(' "$(grep -o 'popen(' "$SRC" | head -1)"
377check_not "no system()" 'system(' "$(grep -o 'system(' "$SRC" | head -1)"
378
379# ---------------------------------------------------------------- HTML UI
380section "HTML UI"
381P=$(apiraw "")
382check "page content-type" 'Content-Type: text/html; charset=utf-8' "$(printf '%s' "$P" | tr -d '\r' | head -1)"
383check "page has DOCTYPE" '<!DOCTYPE html>' "$P"
384check "page title" '<title>files</title>' "$P"
385check "page has no <script src=" '0' "$(printf '%s' "$P" | grep -c '<script src=')"
386check "page has no CDN link" '0' "$(printf '%s' "$P" | grep -ciE '(cdn\.|unpkg|jsdelivr|cdnjs)')"
387check "page has no http:// asset" '0' "$(printf '%s' "$P" | grep -c 'src="http')"
388check "page embeds the theme tokens" '--bg-card:#1c1c1e' "$P"
389check "page embeds the theme handoff" 'sx-theme-request' "$P"
390check "page embeds the prepaint" 'data-theme' "$P"
391check "page marks embed mode" "classList.add('embedded')" "$P"
392check "page uses <style>" 'font-family:var(--font)' "$P"
393check "page has an inline <script>" '<script>' "$P"
394check "page CSP is tight" "default-src 'none'" "$P"
395check "page CSP allows no remote script" "script-src 'unsafe-inline'" "$P"
396check "page has the fs route constant" "'/plugin_files'" "$P"
397check "page has the hub route branch" "'/plugins?plugin='" "$P"
398check "page has no emoji" '0' "$(printf '%s' "$P" | python3 -c 'import sys,re; t=sys.stdin.read(); print(len(re.findall(r"[\U0001F300-\U0001FAFF]",t)))')"
399check "page uses inline SVG" '<svg viewBox=' "$P"
400PEMB=$(apiraw "embed=1")
401check "embed=1 still serves the page" '<title>files</title>' "$PEMB"
402PNO=$(apiraw "?theme=light")
403check "theme param passes through" '<title>files</title>' "$PNO"
404
405# ---------------------------------------------------------------- generated header
406section "generated UI header"
407if [ -f src/ui_html.h ] && [ -f src/ui.html ]; then
408 if python3 tools/gen_ui.py --check >/dev/null 2>&1; then
409 pass "src/ui_html.h matches src/ui.html"
410 else
411 fail "src/ui_html.h is stale - run tools/gen_ui.py"
412 fi
413else
414 fail "missing src/ui.html or src/ui_html.h"
415fi
416
417# ---------------------------------------------------------------- plugin.json
418section "plugin.json sidecar"
419if [ -f plugin.json ]; then
420 pass "plugin.json present"
421 J=$(cat plugin.json)
422 json_ok "plugin.json is valid JSON" "$J" \
423 "d['slug']=='files' and d['id']=='plugin_files' and d['category']=='devtools' and d['license']=='MIT'"
424 json_ok "plugin.json declares fs_read" "$J" "'fs_read' in d['capabilities']"
425 json_ok "plugin.json has a trust notice" "$J" "len(d['trust_notice'])>40"
426 json_ok "plugin.json title is a title" "$J" "len(d['title'])>6"
427else
428 fail "plugin.json missing (catalog would be blank)"
429fi
430
431# ---------------------------------------------------------------- fortify
432section "_FORTIFY_SOURCE=2 build"
433if cc -O2 -std=c99 -Wall -Wextra -Wno-overlength-strings -I src \
434 -D_FORTIFY_SOURCE=2 -o "$WORK/plugin_files.fort" src/plugin_files.c 2>"$WORK/fort.log"; then
435 pass "fortify build compiles"
436 OUT=$(QUERY_STRING="action=ls&path=$FX" "$WORK/plugin_files.fort" | tail -n +5)
437 check "fortify build lists entries" '"entries"' "$OUT"
438 OUT=$(QUERY_STRING="action=read&path=$FX/big.txt" "$WORK/plugin_files.fort")
439 check "fortify build caps the 1 MiB read" '"bytes_read":1048576' "$OUT"
440 OUT=$(QUERY_STRING="action=ls&path=$FX/etclink" "$WORK/plugin_files.fort")
441 check "fortify build still refuses the symlink escape" '403' "$(status_of "$OUT")"
442 check "fortify build printed no warning" '0' "$(wc -c < "$WORK/fort.log" | tr -d ' ')"
443else
444 fail "fortify build failed: $(head -3 "$WORK/fort.log")"
445fi
446
447# ---------------------------------------------------------------- PE header
448section "MinGW PE (when present)"
449if [ -f plugin_files.exe ]; then
450 MAGIC=$(od -An -tx1 -N2 plugin_files.exe | tr -d ' \n')
451 check "plugin_files.exe starts with the MZ magic" '4d5a' "$MAGIC"
452 SZ=$(wc -c < plugin_files.exe | tr -d ' ')
453 if [ "$SZ" -gt 20000 ]; then pass "plugin_files.exe looks like a real binary ($SZ bytes)"; else fail "plugin_files.exe suspiciously small ($SZ bytes)"; fi
454else
455 echo " skip (plugin_files.exe not built here)"
456fi
457
458# ---------------------------------------------------------------- history audit
459section "full-history audit"
460if [ -d .git ]; then
461 ALL="$WORK/all-history.txt"
462 git log -p --all > "$ALL" 2>/dev/null || : > "$ALL"
463
464 audit() { # audit <label> <regex>
465 H=$(grep -Eo "$2" "$ALL" 2>/dev/null | sort -u | head -3 || true)
466 if [ -n "$H" ]; then
467 fail "history: $1"; printf '%s\n' "$H" | sed 's/^/ /'
468 else
469 pass "history clean: $1"
470 fi
471 }
472 audit "LAN 192.168.x" '192\.168\.[0-9]+\.[0-9]+'
473 audit "private 10.x" '\b10\.[0-9]+\.[0-9]+\.[0-9]+'
474 audit "private 172.16-31.x" '172\.(1[6-9]|2[0-9]|3[01])\.[0-9]+\.[0-9]+'
475 audit "GitHub PAT" 'ghp_[A-Za-z0-9]{36}'
476 audit "HuggingFace token" 'hf_[A-Za-z0-9]{30,}'
477 audit "embedded git creds" 'scorpioxinc:[A-Za-z0-9]{20,}'
478 PK="pico""bot"; WT=".work""trees"
479 audit "internal fleet path" "/codebases/${PK}|${WT}"
480 audit "run token assignment" 'RUNNER_'"PASS="
481else
482 echo " skip history audit (not a git checkout)"
483fi
484
485# ---------------------------------------------------------------- summary
486printf '\n%s== summary ==%s\n' "$C_B" "$C_0"
487printf ' passed: %d\n' "$PASS"
488printf ' failed: %d\n' "$FAIL"
489if [ "$FAIL" -ne 0 ]; then
490 printf ' %sRESULT: FAIL%s\n' "$C_BAD" "$C_0"
491 exit 1
492fi
493printf ' %sRESULT: OK%s\n' "$C_OK" "$C_0"
494exit 0