| 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 | |
| 15 | set -u |
| 16 | |
| 17 | ROOT=$(cd "$(dirname "$0")/.." && pwd) |
| 18 | cd "$ROOT" || exit 1 |
| 19 | |
| 20 | BIN=./plugin_files |
| 21 | PASS=0 |
| 22 | FAIL=0 |
| 23 | |
| 24 | if [ -t 1 ]; then |
| 25 | C_OK=$(printf '\033[32m'); C_BAD=$(printf '\033[31m'); C_B=$(printf '\033[1m'); C_0=$(printf '\033[0m') |
| 26 | else |
| 27 | C_OK=; C_BAD=; C_B=; C_0= |
| 28 | fi |
| 29 | pass() { PASS=$((PASS + 1)); printf ' %sok%s %s\n' "$C_OK" "$C_0" "$1"; } |
| 30 | fail() { FAIL=$((FAIL + 1)); printf ' %sFAIL%s %s\n' "$C_BAD" "$C_0" "$1"; } |
| 31 | |
| 32 | check() { # 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 | |
| 38 | check_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 | |
| 44 | section() { printf '\n%s== %s ==%s\n' "$C_B" "$1" "$C_0"; } |
| 45 | |
| 46 | # strip CGI headers if present, drop CRs, keep the payload |
| 47 | body() { |
| 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 | |
| 57 | json_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 | |
| 67 | api() { body "$(QUERY_STRING="$1" $BIN)"; } |
| 68 | |
| 69 | # raw output including CGI headers, for the Status: assertions |
| 70 | apiraw() { QUERY_STRING="$1" $BIN; } |
| 71 | |
| 72 | status_of() { printf '%s' "$1" | tr -d '\r' | sed -n 's/^Status: \([0-9][0-9][0-9]\).*/\1/p' | head -1; } |
| 73 | |
| 74 | # ---------------------------------------------------------------- build |
| 75 | section "build" |
| 76 | if [ ! -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" |
| 81 | else |
| 82 | pass "native binary present" |
| 83 | fi |
| 84 | |
| 85 | WORK=$(mktemp -d "${TMPDIR:-/tmp}/files-test.XXXXXX") || exit 1 |
| 86 | trap 'rm -rf "$WORK"' INT TERM EXIT |
| 87 | FX="$WORK/root" |
| 88 | mkdir -p "$FX/sub/deep/deeper" "$FX/emptydir" "$FX/.hiddendir" "$FX/pics" || exit 1 |
| 89 | |
| 90 | printf 'hello world\nsecond line\n' > "$FX/a.txt" |
| 91 | printf '# Title\n\nSome **bold** text and a [link](https://example.com).\n\n- one\n- two\n' > "$FX/readme.md" |
| 92 | printf 'abc\000def\000ghi' > "$FX/bin.dat" |
| 93 | printf 'hidden content\n' > "$FX/.hidden" |
| 94 | : > "$FX/empty.txt" |
| 95 | printf 'deep\n' > "$FX/sub/deep/deeper/leaf.txt" |
| 96 | printf 'in sub\n' > "$FX/sub/inner.txt" |
| 97 | python3 -c "open('$FX/big.txt','w').write('x'*(2*1024*1024))" |
| 98 | python3 -c " |
| 99 | import struct,zlib |
| 100 | def chunk(t,d): |
| 101 | return struct.pack('>I',len(d))+t+d+struct.pack('>I',zlib.crc32(t+d)&0xffffffff) |
| 102 | raw=b'\x00\xff\x00\xff' |
| 103 | idat=zlib.compress(raw) |
| 104 | png=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'') |
| 105 | open('$FX/pics/px.png','wb').write(png) |
| 106 | " |
| 107 | ln -s /etc "$FX/etclink" |
| 108 | ln -s "$FX/a.txt" "$FX/goodlink" |
| 109 | |
| 110 | export FILES_ALLOWED_ROOT="$FX" |
| 111 | unset BOT_PASSWORD |
| 112 | |
| 113 | # ---------------------------------------------------------------- meta |
| 114 | section "--meta / --version" |
| 115 | M=$($BIN --meta) |
| 116 | check "--meta emits name" '"name":"files"' "$M" |
| 117 | check "--meta desc" '"desc":"File browser"' "$M" |
| 118 | json_ok "--meta is valid JSON" "$M" "d['name']=='files' and d['desc']=='File browser'" |
| 119 | M2=$(SX_PLUGIN_META=1 $BIN) |
| 120 | check "SX_PLUGIN_META also emits meta" '"name":"files"' "$M2" |
| 121 | V=$($BIN --version) |
| 122 | check "--version" 'plugin_files 1.0.0' "$V" |
| 123 | |
| 124 | # ---------------------------------------------------------------- auth |
| 125 | section "node auth" |
| 126 | export BOT_PASSWORD=testpw |
| 127 | R=$(apiraw "action=roots") |
| 128 | check "no password -> 401" '401' "$(status_of "$R")" |
| 129 | R=$(apiraw "action=ls&path=$FX") |
| 130 | check "ls without password -> 401" '401' "$(status_of "$R")" |
| 131 | R=$(apiraw "action=read&path=$FX/a.txt") |
| 132 | check "read without password -> 401" '401' "$(status_of "$R")" |
| 133 | R=$(apiraw "action=raw&path=$FX/a.txt") |
| 134 | check "raw without password -> 401" '401' "$(status_of "$R")" |
| 135 | R=$(apiraw "") |
| 136 | check "HTML page without password -> 401" '401' "$(status_of "$R")" |
| 137 | M=$($BIN --meta) |
| 138 | check "--meta stays unauthenticated" '"name":"files"' "$M" |
| 139 | R=$(HTTP_X_BOT_PASSWORD=testpw apiraw "action=roots") |
| 140 | check "X-Bot-Password accepted" '"ok":true' "$(body "$R")" |
| 141 | R=$(HTTP_AUTHORIZATION="Bearer testpw" apiraw "action=roots") |
| 142 | check "Bearer token accepted" '"ok":true' "$(body "$R")" |
| 143 | R=$(apiraw "action=roots&pwd=testpw") |
| 144 | check "?pwd= accepted" '"ok":true' "$(body "$R")" |
| 145 | R=$(apiraw "action=roots&pwd=wrong") |
| 146 | check "wrong ?pwd= -> 401" '401' "$(status_of "$R")" |
| 147 | unset BOT_PASSWORD |
| 148 | |
| 149 | # ---------------------------------------------------------------- roots |
| 150 | section "?action=roots" |
| 151 | R=$(api "action=roots") |
| 152 | json_ok "roots ok" "$R" "d['ok'] is True" |
| 153 | json_ok "roots has a list" "$R" "isinstance(d['roots'],list) and len(d['roots'])>=1" |
| 154 | json_ok "fixture root is present" "$R" "any(r['path']=='$FX' for r in d['roots'])" |
| 155 | json_ok "each root has name+path" "$R" "all('name' in r and 'path' in r for r in d['roots'])" |
| 156 | json_ok "count matches list length" "$R" "d['count']==len(d['roots'])" |
| 157 | json_ok "no /proc root" "$R" "all(r['path']!='/proc' for r in d['roots'])" |
| 158 | json_ok "no /sys root" "$R" "all(r['path']!='/sys' for r in d['roots'])" |
| 159 | json_ok "no /dev root" "$R" "all(r['path']!='/dev' for r in d['roots'])" |
| 160 | R=$(api "format=json") |
| 161 | json_ok "format=json falls back to roots" "$R" "d['ok'] is True and 'roots' in d" |
| 162 | |
| 163 | # ---------------------------------------------------------------- ls |
| 164 | section "?action=ls" |
| 165 | R=$(api "action=ls&path=$FX") |
| 166 | json_ok "ls ok" "$R" "d['ok'] is True" |
| 167 | json_ok "ls path echoed" "$R" "d['path']=='$FX'" |
| 168 | json_ok "ls root is the fixture" "$R" "d['root']=='$FX'" |
| 169 | json_ok "ls parent is null at a root" "$R" "d['parent'] is None" |
| 170 | json_ok "ls count matches entries" "$R" "d['count']==len(d['entries'])" |
| 171 | json_ok "ls not truncated" "$R" "d['truncated'] is False" |
| 172 | json_ok "hidden off by default" "$R" "d['hidden']==0" |
| 173 | json_ok "no dotfiles by default" "$R" "not any(e['name'].startswith('.') for e in d['entries'])" |
| 174 | json_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'))" |
| 175 | json_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)" |
| 176 | json_ok "entry has size/mtime/mode" "$R" "all('size' in e and 'mtime' in e and 'mode' in e for e in d['entries'])" |
| 177 | json_ok "dir entry flagged" "$R" "any(e['name']=='sub' and e['type']=='dir' for e in d['entries'])" |
| 178 | json_ok "file entry flagged" "$R" "any(e['name']=='a.txt' and e['type']=='file' for e in d['entries'])" |
| 179 | json_ok "a.txt size is 24" "$R" "any(e['name']=='a.txt' and e['size']==24 for e in d['entries'])" |
| 180 | json_ok "mtime is ISO-8601" "$R" "all(len(e['mtime'])==20 and e['mtime'].endswith('Z') for e in d['entries'] if e['mtime'])" |
| 181 | json_ok "mode is octal string" "$R" "all(e['mode'].isdigit() and len(e['mode'])==4 for e in d['entries'])" |
| 182 | json_ok "empty file listed with size 0" "$R" "any(e['name']=='empty.txt' and e['size']==0 for e in d['entries'])" |
| 183 | |
| 184 | R=$(api "action=ls&path=$FX&hidden=1") |
| 185 | json_ok "hidden=1 flags hidden" "$R" "d['hidden']==1" |
| 186 | json_ok "hidden=1 shows dotfile" "$R" "any(e['name']=='.hidden' for e in d['entries'])" |
| 187 | json_ok "hidden=1 shows dotdir" "$R" "any(e['name']=='.hiddendir' and e['type']=='dir' for e in d['entries'])" |
| 188 | |
| 189 | R=$(api "action=ls&path=$FX&hidden=true") |
| 190 | json_ok "hidden=true also works" "$R" "any(e['name']=='.hidden' for e in d['entries'])" |
| 191 | |
| 192 | R=$(api "action=ls&path=$FX/sub") |
| 193 | json_ok "nested parent is computed" "$R" "d['parent']=='$FX'" |
| 194 | json_ok "nested root is still the fixture" "$R" "d['root']=='$FX'" |
| 195 | json_ok "nested lists its own children" "$R" "any(e['name']=='inner.txt' for e in d['entries'])" |
| 196 | |
| 197 | R=$(api "action=ls&path=$FX/emptydir") |
| 198 | json_ok "empty dir answers count 0" "$R" "d['count']==0 and d['entries']==[]" |
| 199 | |
| 200 | R=$(api "action=ls&path=$FX/goodlink") |
| 201 | json_ok "symlink inside the root is browsable as a file" "$R" "'ok' in d" |
| 202 | R=$(api "action=ls&path=$FX") |
| 203 | json_ok "symlink entry is flagged link=true" "$R" "any(e['name']=='goodlink' and e['link'] is True for e in d['entries'])" |
| 204 | json_ok "plain file is link=false" "$R" "any(e['name']=='a.txt' and e['link'] is False for e in d['entries'])" |
| 205 | json_ok "every entry carries a link flag" "$R" "all('link' in e for e in d['entries'])" |
| 206 | |
| 207 | R=$(api "action=ls") |
| 208 | json_ok "ls without path defaults to the first root" "$R" "d['ok'] is True and d['path'].startswith('/')" |
| 209 | |
| 210 | # ---------------------------------------------------------------- read |
| 211 | section "?action=read" |
| 212 | R=$(api "action=read&path=$FX/a.txt") |
| 213 | json_ok "read ok" "$R" "d['ok'] is True" |
| 214 | json_ok "read text" "$R" "d['text']=='hello world\\nsecond line\\n'" |
| 215 | json_ok "read binary false" "$R" "d['binary'] is False" |
| 216 | json_ok "read truncated false" "$R" "d['truncated'] is False" |
| 217 | json_ok "read bytes_read matches file" "$R" "d['bytes_read']==24" |
| 218 | json_ok "read name echoed" "$R" "d['name']=='a.txt'" |
| 219 | json_ok "read size echoed" "$R" "d['size']==24" |
| 220 | json_ok "read mode present" "$R" "len(d['mode'])==4" |
| 221 | json_ok "read has no text key for binary" "$R" "True" |
| 222 | |
| 223 | R=$(api "action=read&path=$FX/bin.dat") |
| 224 | json_ok "binary detected by NUL" "$R" "d['binary'] is True" |
| 225 | json_ok "binary omits text" "$R" "'text' not in d" |
| 226 | json_ok "binary still reports size" "$R" "d['size']==11" |
| 227 | |
| 228 | R=$(api "action=read&path=$FX/big.txt") |
| 229 | json_ok "big file truncated" "$R" "d['truncated'] is True" |
| 230 | json_ok "big file capped at 1 MiB" "$R" "d['bytes_read']==1048576" |
| 231 | json_ok "big file still reports real size" "$R" "d['size']==2097152" |
| 232 | |
| 233 | R=$(api "action=read&path=$FX/empty.txt") |
| 234 | json_ok "empty file reads as empty text" "$R" "d['text']=='' and d['bytes_read']==0" |
| 235 | |
| 236 | R=$(apiraw "action=read&path=$FX") |
| 237 | check "read a directory -> 400" '400' "$(status_of "$R")" |
| 238 | |
| 239 | # ---------------------------------------------------------------- raw |
| 240 | section "?action=raw" |
| 241 | RAWR=$(apiraw "action=raw&path=$FX/a.txt") |
| 242 | HDRS=$(printf '%s' "$RAWR" | tr -d '\r' | sed -n '1,/^$/p') |
| 243 | check "raw content-type text/plain" 'Content-Type: text/plain; charset=utf-8' "$HDRS" |
| 244 | check "raw attachment for text" 'Content-Disposition: attachment' "$HDRS" |
| 245 | check "raw has no manual Content-Length" '0' "$(printf '%s' "$HDRS" | grep -c 'Content-Length')" |
| 246 | check "raw nosniff" 'X-Content-Type-Options: nosniff' "$HDRS" |
| 247 | check "raw sameorigin" 'X-Frame-Options: SAMEORIGIN' "$HDRS" |
| 248 | |
| 249 | # payload after the blank line must be byte-identical to the file |
| 250 | python3 -c " |
| 251 | import subprocess,sys,os,hashlib |
| 252 | env=dict(os.environ); env['QUERY_STRING']='action=raw&path=$FX/a.txt' |
| 253 | out=subprocess.run(['$BIN'],capture_output=True,env=env).stdout |
| 254 | sep=out.find(b'\r\n\r\n') |
| 255 | payload=out[sep+4:] |
| 256 | want=open('$FX/a.txt','rb').read() |
| 257 | sys.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 | |
| 260 | python3 -c " |
| 261 | import subprocess,sys,os,hashlib |
| 262 | env=dict(os.environ); env['QUERY_STRING']='action=raw&path=$FX/pics/px.png' |
| 263 | out=subprocess.run(['$BIN'],capture_output=True,env=env).stdout |
| 264 | sep=out.find(b'\r\n\r\n') |
| 265 | hdr=out[:sep].decode() |
| 266 | payload=out[sep+4:] |
| 267 | want=open('$FX/pics/px.png','rb').read() |
| 268 | ok = payload==want and 'image/png' in hdr and 'inline' in hdr |
| 269 | sys.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 | |
| 272 | RAWR=$(apiraw "action=raw&path=$FX/pics/px.png&download=1") |
| 273 | HDRS=$(printf '%s' "$RAWR" | tr -d '\r' | sed -n '1,/^$/p') |
| 274 | check "download=1 forces attachment on an image" 'Content-Disposition: attachment' "$HDRS" |
| 275 | |
| 276 | RAWR=$(apiraw "action=raw&path=$FX/readme.md") |
| 277 | HDRS=$(printf '%s' "$RAWR" | tr -d '\r' | sed -n '1,/^$/p') |
| 278 | check "markdown served as text/plain (never text/html)" 'Content-Type: text/plain; charset=utf-8' "$HDRS" |
| 279 | HDRS_HTML=$(printf '%s' "$RAWR" | tr -d '\r' | sed -n '1,/^$/p' | grep -c 'text/html') |
| 280 | check "no text/html on raw markdown" '0' "$HDRS_HTML" |
| 281 | |
| 282 | printf 'PK\003\004xxxx' > "$FX/thing.zip" |
| 283 | RAWR=$(apiraw "action=raw&path=$FX/thing.zip") |
| 284 | HDRS=$(printf '%s' "$RAWR" | tr -d '\r' | sed -n '1,/^$/p') |
| 285 | check "unknown type is octet-stream" 'application/octet-stream' "$HDRS" |
| 286 | check "unknown type is attachment" 'Content-Disposition: attachment' "$HDRS" |
| 287 | |
| 288 | printf '<html><body><script>alert(1)</script></body></html>' > "$FX/evil.html" |
| 289 | RAWR=$(apiraw "action=raw&path=$FX/evil.html") |
| 290 | HDRS=$(printf '%s' "$RAWR" | tr -d '\r' | sed -n '1,/^$/p') |
| 291 | check "html file is served as plain text" 'text/plain' "$HDRS" |
| 292 | |
| 293 | RAWR=$(apiraw "action=raw&path=$FX/big.txt") |
| 294 | PLEN=$(printf '%s' "$RAWR" | wc -c | tr -d ' ') |
| 295 | if [ "$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 |
| 298 | section "security: traversal, symlink escape, denied roots" |
| 299 | R=$(apiraw "action=ls&path=$FX/../..") |
| 300 | check ".. in a path -> 400" '400' "$(status_of "$R")" |
| 301 | R=$(apiraw "action=ls&path=$FX/sub/../../etc") |
| 302 | check "nested .. -> 400" '400' "$(status_of "$R")" |
| 303 | R=$(apiraw "action=ls&path=../../etc") |
| 304 | check "relative .. path -> 400" '400' "$(status_of "$R")" |
| 305 | R=$(apiraw "action=ls&path=$FX/%2e%2e/%2e%2e/etc") |
| 306 | check "percent-encoded .. -> 400" '400' "$(status_of "$R")" |
| 307 | R=$(apiraw "action=read&path=/etc/passwd") |
| 308 | check "read outside every root -> 403" '403' "$(status_of "$R")" |
| 309 | R=$(apiraw "action=ls&path=/etc") |
| 310 | check "ls outside every root -> 403" '403' "$(status_of "$R")" |
| 311 | R=$(apiraw "action=read&path=$FX/etclink/passwd") |
| 312 | check "symlink pointing outside -> 403" '403' "$(status_of "$R")" |
| 313 | R=$(apiraw "action=ls&path=$FX/etclink") |
| 314 | check "ls a symlink that escapes -> 403" '403' "$(status_of "$R")" |
| 315 | R=$(apiraw "action=raw&path=$FX/etclink/hostname") |
| 316 | check "raw through an escaping symlink -> 403" '403' "$(status_of "$R")" |
| 317 | R=$(apiraw "action=ls&path=/proc") |
| 318 | check "/proc refused" '403' "$(status_of "$R")" |
| 319 | R=$(apiraw "action=ls&path=/proc/self") |
| 320 | check "/proc/self refused" '403' "$(status_of "$R")" |
| 321 | R=$(apiraw "action=ls&path=/sys") |
| 322 | check "/sys refused" '403' "$(status_of "$R")" |
| 323 | R=$(apiraw "action=ls&path=/dev") |
| 324 | check "/dev refused" '403' "$(status_of "$R")" |
| 325 | R=$(apiraw "action=ls&path=") |
| 326 | check "empty path falls back to a root, not the filesystem" '"ok":true' "$(body "$R")" |
| 327 | R=$(apiraw "action=ls&path=$FX/missing") |
| 328 | check "missing path -> 404" '404' "$(status_of "$R")" |
| 329 | R=$(apiraw "action=read&path=$FX/missing.txt") |
| 330 | check "missing file -> 404" '404' "$(status_of "$R")" |
| 331 | R=$(apiraw "action=nope") |
| 332 | check "unknown action -> 400" '400' "$(status_of "$R")" |
| 333 | R=$(apiraw "action=ls&path=$FX/a.txt") |
| 334 | check "ls a regular file -> 403 (cannot list)" '403' "$(status_of "$R")" |
| 335 | |
| 336 | # read-only by construction: no write-ish action exists |
| 337 | for 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")" |
| 340 | done |
| 341 | SUM_BEFORE=$(python3 -c " |
| 342 | import hashlib,os |
| 343 | h=hashlib.sha256() |
| 344 | for 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 |
| 349 | print(h.hexdigest()) |
| 350 | ") |
| 351 | api "action=read&path=$FX/a.txt" > /dev/null |
| 352 | apiraw "action=raw&path=$FX/a.txt" > /dev/null |
| 353 | api "action=ls&path=$FX" > /dev/null |
| 354 | SUM_AFTER=$(python3 -c " |
| 355 | import hashlib,os |
| 356 | h=hashlib.sha256() |
| 357 | for 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 |
| 362 | print(h.hexdigest()) |
| 363 | ") |
| 364 | check "fixture unchanged after every action" "$SUM_BEFORE" "$SUM_AFTER" |
| 365 | |
| 366 | # the source must contain no write syscall at all |
| 367 | SRC=src/plugin_files.c |
| 368 | W=$(grep -nE 'fopen\([^)]*"[wa]' "$SRC" | wc -l | tr -d ' ') |
| 369 | check "no fopen for writing in the source" '0' "$W" |
| 370 | for fn in fprintf fputc fwrite_never_used; do :; done |
| 371 | check_not "no remove()" 'remove(' "$(grep -o 'remove(' "$SRC" | head -1)" |
| 372 | check_not "no rmdir()" 'rmdir(' "$(grep -o 'rmdir(' "$SRC" | head -1)" |
| 373 | check_not "no mkdir()" 'mkdir(' "$(grep -o 'mkdir(' "$SRC" | head -1)" |
| 374 | check_not "no rename()" 'rename(' "$(grep -o 'rename(' "$SRC" | head -1)" |
| 375 | check_not "no chmod()" 'chmod(' "$(grep -o 'chmod(' "$SRC" | head -1)" |
| 376 | check_not "no popen()" 'popen(' "$(grep -o 'popen(' "$SRC" | head -1)" |
| 377 | check_not "no system()" 'system(' "$(grep -o 'system(' "$SRC" | head -1)" |
| 378 | |
| 379 | # ---------------------------------------------------------------- HTML UI |
| 380 | section "HTML UI" |
| 381 | P=$(apiraw "") |
| 382 | check "page content-type" 'Content-Type: text/html; charset=utf-8' "$(printf '%s' "$P" | tr -d '\r' | head -1)" |
| 383 | check "page has DOCTYPE" '<!DOCTYPE html>' "$P" |
| 384 | check "page title" '<title>files</title>' "$P" |
| 385 | check "page has no <script src=" '0' "$(printf '%s' "$P" | grep -c '<script src=')" |
| 386 | check "page has no CDN link" '0' "$(printf '%s' "$P" | grep -ciE '(cdn\.|unpkg|jsdelivr|cdnjs)')" |
| 387 | check "page has no http:// asset" '0' "$(printf '%s' "$P" | grep -c 'src="http')" |
| 388 | check "page embeds the theme tokens" '--bg-card:#1c1c1e' "$P" |
| 389 | check "page embeds the theme handoff" 'sx-theme-request' "$P" |
| 390 | check "page embeds the prepaint" 'data-theme' "$P" |
| 391 | check "page marks embed mode" "classList.add('embedded')" "$P" |
| 392 | check "page uses <style>" 'font-family:var(--font)' "$P" |
| 393 | check "page has an inline <script>" '<script>' "$P" |
| 394 | check "page CSP is tight" "default-src 'none'" "$P" |
| 395 | check "page CSP allows no remote script" "script-src 'unsafe-inline'" "$P" |
| 396 | check "page has the fs route constant" "'/plugin_files'" "$P" |
| 397 | check "page has the hub route branch" "'/plugins?plugin='" "$P" |
| 398 | check "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)))')" |
| 399 | check "page uses inline SVG" '<svg viewBox=' "$P" |
| 400 | PEMB=$(apiraw "embed=1") |
| 401 | check "embed=1 still serves the page" '<title>files</title>' "$PEMB" |
| 402 | PNO=$(apiraw "?theme=light") |
| 403 | check "theme param passes through" '<title>files</title>' "$PNO" |
| 404 | |
| 405 | # ---------------------------------------------------------------- generated header |
| 406 | section "generated UI header" |
| 407 | if [ -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 |
| 413 | else |
| 414 | fail "missing src/ui.html or src/ui_html.h" |
| 415 | fi |
| 416 | |
| 417 | # ---------------------------------------------------------------- plugin.json |
| 418 | section "plugin.json sidecar" |
| 419 | if [ -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" |
| 427 | else |
| 428 | fail "plugin.json missing (catalog would be blank)" |
| 429 | fi |
| 430 | |
| 431 | # ---------------------------------------------------------------- fortify |
| 432 | section "_FORTIFY_SOURCE=2 build" |
| 433 | if 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 ' ')" |
| 443 | else |
| 444 | fail "fortify build failed: $(head -3 "$WORK/fort.log")" |
| 445 | fi |
| 446 | |
| 447 | # ---------------------------------------------------------------- PE header |
| 448 | section "MinGW PE (when present)" |
| 449 | if [ -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 |
| 454 | else |
| 455 | echo " skip (plugin_files.exe not built here)" |
| 456 | fi |
| 457 | |
| 458 | # ---------------------------------------------------------------- history audit |
| 459 | section "full-history audit" |
| 460 | if [ -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=" |
| 481 | else |
| 482 | echo " skip history audit (not a git checkout)" |
| 483 | fi |
| 484 | |
| 485 | # ---------------------------------------------------------------- summary |
| 486 | printf '\n%s== summary ==%s\n' "$C_B" "$C_0" |
| 487 | printf ' passed: %d\n' "$PASS" |
| 488 | printf ' failed: %d\n' "$FAIL" |
| 489 | if [ "$FAIL" -ne 0 ]; then |
| 490 | printf ' %sRESULT: FAIL%s\n' "$C_BAD" "$C_0" |
| 491 | exit 1 |
| 492 | fi |
| 493 | printf ' %sRESULT: OK%s\n' "$C_OK" "$C_0" |
| 494 | exit 0 |