scorpiox-bot-plugin-gitstat
←run.shbash
559 lines · 25 KBHEADBlameDownload
1#!/bin/sh
2# tests/run.sh - offline checks for plugin_gitstat across all 3 modes.
3#
4# Everything runs against ./plugin_gitstat (native build). The repository trees
5# are real git repos built in a temp directory, so the status parser, the
6# ahead/behind counter, the stash counter, the detached and unborn states and
7# the credential redaction are all exercised against git's own output rather
8# than our idea of it. GITSTAT_ONLY_ROOTS=1 pins the allowed roots to that temp
9# directory so a developer machine full of checkouts cannot affect the result.
10#
11# Optional cross-target artifacts (musl, aarch64, PE) are checked when present.
12# The last section audits the whole git history for secrets and LAN addresses.
13#
14# Exit code 0 = everything passed.
15
16set -u
17
18ROOT=$(cd "$(dirname "$0")/.." && pwd)
19cd "$ROOT" || exit 1
20
21BIN=./plugin_gitstat
22PASS=0
23FAIL=0
24
25if [ -t 1 ]; then
26 C_OK=$(printf '\033[32m'); C_BAD=$(printf '\033[31m'); C_B=$(printf '\033[1m'); C_0=$(printf '\033[0m')
27else
28 C_OK=; C_BAD=; C_B=; C_0=
29fi
30pass() { PASS=$((PASS + 1)); printf ' %sok%s %s\n' "$C_OK" "$C_0" "$1"; }
31fail() { FAIL=$((FAIL + 1)); printf ' %sFAIL%s %s\n' "$C_BAD" "$C_0" "$1"; }
32
33check() { # check <desc> <expected-substring> <actual>
34 if printf '%s' "$3" | grep -F -q -- "$2"; then pass "$1"; else
35 fail "$1 (want: $2)"; printf ' got: %s\n' "$(printf '%s' "$3" | head -c 500)"
36 fi
37}
38
39check_not() { # check_not <desc> <forbidden-substring> <actual>
40 if printf '%s' "$3" | grep -F -q -- "$2"; then
41 fail "$1 (must NOT contain: $2)"; else pass "$1"
42 fi
43}
44
45section() { printf '\n%s== %s ==%s\n' "$C_B" "$1" "$C_0"; }
46
47body() { # strip CGI headers if present, drop CRs, keep the payload
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|Content-Security-Policy):/) { inhdr = 1; next }
51 inhdr && $0 == "" { inhdr = 0; next }
52 inhdr { next }
53 { print }
54 '
55}
56
57hdr() { printf '%s\n' "$1" | tr -d '\r' | sed -n '1p'; }
58
59json_ok() { # json_ok <desc> <json> [python-expression on d]
60 expr=${3:-True}
61 if printf '%s' "$2" | python3 -c "import json,sys; d=json.load(sys.stdin); sys.exit(0 if ($expr) else 1)" 2>/dev/null; then
62 pass "$1"
63 else
64 fail "$1 (invalid JSON or assertion failed)"; printf ' got: %s\n' "$(printf '%s' "$2" | head -c 500)"
65 fi
66}
67
68run() { # run <query-string> [extra env assignments...] -> raw output
69 qs="$1"; shift
70 env QUERY_STRING="$qs" "$@" $BIN 2>&1
71}
72
73api() { body "$(run "$1")"; }
74
75# ---------------------------------------------------------------- setup
76section "build"
77if [ ! -x "$BIN" ]; then
78 echo " building native binary..."
79 cc -O2 -std=c99 -Wall -Wextra -pedantic -Wno-overlength-strings -I src \
80 -o "$BIN" src/plugin_gitstat.c || { echo " build failed"; exit 1; }
81fi
82[ -x "$BIN" ] && pass "native binary exists and is executable" || fail "native binary missing"
83
84WORK=$(mktemp -d "${TMPDIR:-/tmp}/gitstat-tests-XXXXXX") || exit 1
85trap 'rm -rf "$WORK"' EXIT
86
87GIT_AUTHOR_NAME="Gitstat Test"; export GIT_AUTHOR_NAME
88GIT_AUTHOR_EMAIL="gitstat@example.invalid"; export GIT_AUTHOR_EMAIL
89GIT_COMMITTER_NAME="Gitstat Test"; export GIT_COMMITTER_NAME
90GIT_COMMITTER_EMAIL="gitstat@example.invalid"; export GIT_COMMITTER_EMAIL
91GIT_CONFIG_GLOBAL=/dev/null; export GIT_CONFIG_GLOBAL
92GIT_CONFIG_NOSYSTEM=1; export GIT_CONFIG_NOSYSTEM
93GIT_TERMINAL_PROMPT=0; export GIT_TERMINAL_PROMPT
94
95ROOTS="$WORK/roots"
96mkdir -p "$ROOTS"
97
98mkrepo() { # mkrepo <dir> <branch>
99 mkdir -p "$1"
100 git -C "$1" init -q -b "$2"
101 git -C "$1" config user.name "Gitstat Test"
102 git -C "$1" config user.email "gitstat@example.invalid"
103}
104
105# A bare upstream with one commit in it, cloned into <dir>. Using a real clone
106# rather than init+remote means the upstream refs actually exist, which is what
107# the ahead/behind counters read.
108mkclone() { # mkclone <dir> <branch>
109 up="$WORK/up-$(basename "$1").git"
110 seed="$WORK/seed-$(basename "$1")"
111 git init -q --bare "$up"
112 git -C "$up" symbolic-ref HEAD "refs/heads/$2"
113 mkrepo "$seed" "$2"
114 printf 'base\n' > "$seed/base.txt"
115 git -C "$seed" add base.txt
116 git -C "$seed" commit -q -m "base commit"
117 git -C "$seed" remote add origin "$up"
118 git -C "$seed" push -q -u origin "$2"
119 git clone -q "$up" "$1"
120 git -C "$1" config user.name "Gitstat Test"
121 git -C "$1" config user.email "gitstat@example.invalid"
122}
123
124# 1. clean repo with an upstream, fully in sync
125mkclone "$ROOTS/cleanrepo" main
126
127# 2. dirty repo: one modified file, one untracked file, one staged file
128mkrepo "$ROOTS/dirtyrepo" main
129printf 'one\n' > "$ROOTS/dirtyrepo/keep.txt"
130printf 'two\n' > "$ROOTS/dirtyrepo/mod.txt"
131printf 'three\n' > "$ROOTS/dirtyrepo/add.txt"
132git -C "$ROOTS/dirtyrepo" add keep.txt mod.txt add.txt
133git -C "$ROOTS/dirtyrepo" commit -q -m "seed three files"
134printf 'modified\n' >> "$ROOTS/dirtyrepo/mod.txt"
135printf 'staged\n' >> "$ROOTS/dirtyrepo/add.txt"
136git -C "$ROOTS/dirtyrepo" add add.txt
137printf 'untracked\n' > "$ROOTS/dirtyrepo/new.txt"
138
139# 3. ahead repo: two local commits past its upstream, plus a stash
140mkclone "$ROOTS/aheadrepo" main
141printf 'a\n' >> "$ROOTS/aheadrepo/base.txt"
142git -C "$ROOTS/aheadrepo" commit -q -am "local commit one"
143printf 'b\n' >> "$ROOTS/aheadrepo/base.txt"
144git -C "$ROOTS/aheadrepo" commit -q -am "local commit two"
145printf 'stashed\n' >> "$ROOTS/aheadrepo/base.txt"
146git -C "$ROOTS/aheadrepo" stash push -q -m "work in progress"
147
148# 3b. behind repo: its upstream has moved on
149mkclone "$ROOTS/behindrepo" main
150other="$WORK/other-behindrepo"
151git clone -q "$WORK/up-behindrepo.git" "$other"
152git -C "$other" config user.name "Gitstat Test"
153git -C "$other" config user.email "gitstat@example.invalid"
154printf 'upstream\n' >> "$other/base.txt"
155git -C "$other" commit -q -am "upstream moved on"
156git -C "$other" push -q origin main
157git -C "$ROOTS/behindrepo" fetch -q origin
158
159# 4. detached repo
160mkrepo "$ROOTS/detachedrepo" main
161printf 'd\n' > "$ROOTS/detachedrepo/d.txt"
162git -C "$ROOTS/detachedrepo" add d.txt
163git -C "$ROOTS/detachedrepo" commit -q -m "detach me"
164printf 'd2\n' >> "$ROOTS/detachedrepo/d.txt"
165git -C "$ROOTS/detachedrepo" commit -q -am "second commit"
166git -C "$ROOTS/detachedrepo" checkout -q HEAD~1
167
168# 5. unborn repo: initialised, nothing committed
169mkrepo "$ROOTS/unbornrepo" main
170
171# 6. repo whose origin URL carries userinfo (must be redacted)
172mkclone "$ROOTS/secretremote" main
173git -C "$ROOTS/secretremote" remote set-url origin \
174 "https://plantuser:planttoken123@example.invalid/x.git"
175
176# 7. a nested repo two levels deep, to prove the walk descends
177mkdir -p "$ROOTS/group/nested"
178mkrepo "$ROOTS/group/nested/deeprepo" main
179printf 'n\n' > "$ROOTS/group/nested/deeprepo/n.txt"
180git -C "$ROOTS/group/nested/deeprepo" add n.txt
181git -C "$ROOTS/group/nested/deeprepo" commit -q -m "nested seed"
182
183# 8. a directory with no .git at all (must be ignored)
184mkdir -p "$ROOTS/notarepo"
185
186ROOTENV="GITSTAT_ONLY_ROOTS=1"
187ROOTVAR="GITSTAT_ROOTS=$ROOTS"
188
189# ---------------------------------------------------------------- --meta
190section "--meta / discovery"
191META=$($BIN --meta)
192check "--meta prints name git" '"name":"git"' "$META"
193check "--meta prints desc Repo status" '"desc":"Repo status"' "$META"
194json_ok "--meta is valid JSON with exactly 2 keys" "$META" "sorted(d.keys())==['desc','name']"
195SXMETA=$(env SX_PLUGIN_META=1 $BIN)
196check "SX_PLUGIN_META=1 returns the metadata" '"name":"git"' "$SXMETA"
197METAQ=$(env SX_PLUGIN_META=1 QUERY_STRING="action=repos" $BIN)
198check "--meta wins over the query string" '"name":"git"' "$METAQ"
199DESCLEN=$(printf '%s' "$META" | python3 -c "import json,sys;print(len(json.load(sys.stdin)['desc']))")
200[ "$DESCLEN" -le 15 ] && pass "desc is <= 15 chars ($DESCLEN)" || fail "desc too long ($DESCLEN)"
201
202# ---------------------------------------------------------------- auth
203section "node authentication"
204UNAUTH=$(env BOT_PASSWORD=testpw QUERY_STRING="action=repos" \
205 GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN)
206check "unauthenticated action=repos is 401" "Status: 401 Unauthorized" "$UNAUTH"
207check "401 body carries the error" '"error":"invalid node password"' "$UNAUTH"
208
209OKPW=$(env BOT_PASSWORD=testpw HTTP_X_BOT_PASSWORD=testpw QUERY_STRING="action=repos" \
210 GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN)
211check "X-Bot-Password is accepted" '"ok":true' "$OKPW"
212
213OKBR=$(env BOT_PASSWORD=testpw HTTP_AUTHORIZATION="Bearer testpw" QUERY_STRING="action=repos" \
214 GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN)
215check "Authorization: Bearer is accepted" '"ok":true' "$OKBR"
216
217OKQP=$(env BOT_PASSWORD=testpw QUERY_STRING="action=repos&pwd=testpw" \
218 GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN)
219check "?pwd= is accepted" '"ok":true' "$OKQP"
220
221BADPW=$(env BOT_PASSWORD=testpw QUERY_STRING="action=repos&pwd=nope" \
222 GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN)
223check "wrong ?pwd= is 401" "Status: 401 Unauthorized" "$BADPW"
224
225UNAUTHH=$(env BOT_PASSWORD=testpw QUERY_STRING="" \
226 GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN)
227check "unauthenticated HTML page is 401 too" "Status: 401 Unauthorized" "$UNAUTHH"
228
229METAOK=$(env BOT_PASSWORD=testpw SX_PLUGIN_META=1 $BIN)
230check "--meta stays unauthenticated so discovery works" '"name":"git"' "$METAOK"
231
232NOAUTH=$(env QUERY_STRING="action=repos" GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN)
233check "no BOT_PASSWORD means auth is a no-op" '"ok":true' "$NOAUTH"
234
235# ---------------------------------------------------------------- repos
236section "?action=repos"
237REPOS=$(env QUERY_STRING="action=repos" GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN)
238REPOSB=$(body "$REPOS")
239check "repos answers JSON" "Content-Type: application/json" "$REPOS"
240json_ok "repos doc is valid" "$REPOSB"
241json_ok "repos reports ok" "$REPOSB" "d['ok'] is True"
242json_ok "repos has a summary" "$REPOSB" "'summary' in d and 'dirty' in d['summary']"
243json_ok "repos has a roots array with per-root counts" "$REPOSB" \
244 "len(d['roots'])==1 and d['roots'][0]['count']==8 and d['roots'][0]['exists']"
245json_ok "repos found the 8 fixture repos" "$REPOSB" "d['count']==8"
246json_ok "the plain directory is not a repo" "$REPOSB" \
247 "not any('notarepo' in r['path'] for r in d['repos'])"
248json_ok "the nested repo was found at depth 3" "$REPOSB" \
249 "any(r['name']=='deeprepo' for r in d['repos'])"
250json_ok "the first repo is a dirty one (dirty-first sort)" "$REPOSB" "d['repos'][0]['dirty']>0"
251json_ok "every repo has the documented field set" "$REPOSB" \
252 "all(set(r)>=set(['path','name','root','branch','upstream','ahead','behind','dirty','untracked','staged','conflicted','stash','detached','no_upstream','unborn','last_commit']) for r in d['repos'])"
253json_ok "last_commit has hash/ct/at/subject" "$REPOSB" \
254 "all(set(r['last_commit'])>=set(['hash','short','ct','at','subject']) for r in d['repos'])"
255json_ok "checked_at is an ISO-Z stamp" "$REPOSB" "d['checked_at'].endswith('Z')"
256
257# per-repo assertions
258DIRTY=$(printf '%s' "$REPOSB" | python3 -c "
259import json,sys
260d=json.load(sys.stdin)
261print(json.dumps([r for r in d['repos'] if r['name']=='dirtyrepo'][0]))")
262json_ok "dirtyrepo counts 3 changed entries" "$DIRTY" "d['dirty']==3"
263json_ok "dirtyrepo counts 1 untracked" "$DIRTY" "d['untracked']==1"
264json_ok "dirtyrepo is not detached and has a branch" "$DIRTY" \
265 "d['detached'] is False and d['branch']=='main'"
266
267CLEAN=$(printf '%s' "$REPOSB" | python3 -c "
268import json,sys
269d=json.load(sys.stdin)
270print(json.dumps([r for r in d['repos'] if r['name']=='cleanrepo'][0]))")
271json_ok "cleanrepo is clean" "$CLEAN" "d['dirty']==0"
272json_ok "cleanrepo is in sync with its upstream" "$CLEAN" "d['ahead']==0 and d['behind']==0"
273json_ok "cleanrepo has an upstream" "$CLEAN" "d['no_upstream'] is False and d['upstream']"
274json_ok "cleanrepo carried its last commit" "$CLEAN" "d['last_commit']['subject']=='base commit'"
275
276AHEAD=$(printf '%s' "$REPOSB" | python3 -c "
277import json,sys
278d=json.load(sys.stdin)
279print(json.dumps([r for r in d['repos'] if r['name']=='aheadrepo'][0]))")
280json_ok "aheadrepo is 2 ahead" "$AHEAD" "d['ahead']==2"
281json_ok "aheadrepo is not behind" "$AHEAD" "d['behind']==0"
282json_ok "aheadrepo counted its stash" "$AHEAD" "d['stash']==1"
283json_ok "aheadrepo last subject is the newest commit" "$AHEAD" \
284 "d['last_commit']['subject']=='local commit two'"
285
286DET=$(printf '%s' "$REPOSB" | python3 -c "
287import json,sys
288d=json.load(sys.stdin)
289print(json.dumps([r for r in d['repos'] if r['name']=='detachedrepo'][0]))")
290json_ok "detachedrepo is flagged detached" "$DET" "d['detached'] is True"
291json_ok "detachedrepo branch reads detached" "$DET" "d['branch']=='detached'"
292
293UNB=$(printf '%s' "$REPOSB" | python3 -c "
294import json,sys
295d=json.load(sys.stdin)
296print(json.dumps([r for r in d['repos'] if r['name']=='unbornrepo'][0]))")
297json_ok "unbornrepo is flagged unborn" "$UNB" "d['unborn'] is True"
298json_ok "unbornrepo has no upstream" "$UNB" "d['no_upstream'] is True"
299
300SEC=$(printf '%s' "$REPOSB" | python3 -c "
301import json,sys
302d=json.load(sys.stdin)
303print(json.dumps([r for r in d['repos'] if r['name']=='secretremote'][0]))")
304check_not "credential in the origin URL is redacted (token)" "planttoken123" "$SEC"
305check_not "credential in the origin URL is redacted (user)" "plantuser" "$SEC"
306SECUREMOTE=$(body "$(env QUERY_STRING="action=repo&path=$ROOTS/secretremote" \
307 GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN)")
308check "the board row does not pay for the origin URL" '"remote": ""' "$SEC"
309json_ok "the detail reads the origin URL" "$SECUREMOTE" \
310 "d['repo']['remote']=='https://example.invalid/x.git'"
311check_not "the detail remote carries no userinfo" "plantuser" "$SECUREMOTE"
312check_not "the detail remote carries no token" "planttoken123" "$SECUREMOTE"
313
314check_not "no path outside the root is reported" "$WORK/upstream.git" "$REPOSB"
315
316# summary counters
317json_ok "summary counts the dirty repos" "$REPOSB" "d['summary']['dirty']>=1"
318json_ok "summary counts the ahead repo" "$REPOSB" "d['summary']['ahead']>=1"
319json_ok "summary counts the behind repo" "$REPOSB" "d['summary']['behind']>=1"
320BEHIND=$(printf '%s' "$REPOSB" | python3 -c "
321import json,sys
322d=json.load(sys.stdin)
323print(json.dumps([r for r in d['repos'] if r['name']=='behindrepo'][0]))")
324json_ok "behindrepo is 1 behind" "$BEHIND" "d['behind']==1"
325json_ok "behindrepo is not ahead" "$BEHIND" "d['ahead']==0"
326json_ok "behindrepo still has an upstream" "$BEHIND" "d['no_upstream'] is False"
327json_ok "summary counts the stashes" "$REPOSB" "d['summary']['stashes']>=1"
328json_ok "summary counts the detached repo" "$REPOSB" "d['summary']['detached']==1"
329
330# empty root
331EMPTY=$(env QUERY_STRING="action=repos" GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$WORK/nowhere" $BIN)
332json_ok "a missing root reports exists:false" "$(body "$EMPTY")" \
333 "d['roots'][0]['exists'] is False and d['count']==0"
334
335# per-root budget: two roots are counted independently
336mkdir -p "$WORK/r1" "$WORK/r2"
337cp -a "$ROOTS/cleanrepo" "$WORK/r1/one"
338cp -a "$ROOTS/cleanrepo" "$WORK/r1/two"
339cp -a "$ROOTS/cleanrepo" "$WORK/r2/three"
340TWOROOT=$(env QUERY_STRING="action=repos" GITSTAT_ONLY_ROOTS=1 \
341 GITSTAT_ROOTS="$WORK/r1:$WORK/r2" $BIN)
342json_ok "two roots keep independent counts" "$(body "$TWOROOT")" \
343 "len(d['roots'])==2 and d['roots'][0]['count']==2 and d['roots'][1]['count']==1"
344json_ok "the global table is that of both roots" "$(body "$TWOROOT")" "d['count']==3"
345
346# a duplicate root listing is de-duplicated, not double counted
347DUPR=$(env QUERY_STRING="action=repos" GITSTAT_ONLY_ROOTS=1 \
348 GITSTAT_ROOTS="$WORK/r1:$WORK/r1" $BIN)
349json_ok "a repeated root is de-duplicated" "$(body "$DUPR")" \
350 "len(d['roots'])==1 and d['count']==2"
351
352# ---------------------------------------------------------------- repo detail
353section "?action=repo&path="
354RP="env QUERY_STRING=action=repo\&path=$ROOTS/dirtyrepo GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS=$ROOTS"
355DETAIL=$(env QUERY_STRING="action=repo&path=$ROOTS/dirtyrepo" \
356 GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN)
357DETB=$(body "$DETAIL")
358check "repo detail answers JSON" "Content-Type: application/json" "$DETAIL"
359json_ok "repo detail is valid" "$DETB"
360json_ok "repo detail reports ok" "$DETB" "d['ok'] is True"
361json_ok "repo detail has the repo block" "$DETB" "d['repo']['name']=='dirtyrepo'"
362json_ok "repo detail lists 3 changed files" "$DETB" "len(d['changed'])==3"
363json_ok "repo detail marks the untracked file" "$DETB" \
364 "any(c['code']=='??' and c['path']=='new.txt' for c in d['changed'])"
365json_ok "repo detail marks the modified file" "$DETB" \
366 "any(c['path']=='mod.txt' and c['code'][1]=='M' for c in d['changed'])"
367json_ok "repo detail marks the staged file" "$DETB" \
368 "any(c['path']=='add.txt' and c['code'][0]=='M' for c in d['changed'])"
369json_ok "repo detail has recent commits" "$DETB" "len(d['commits'])>=1"
370json_ok "each commit has hash/ct/author/subject" "$DETB" \
371 "all(set(c)>=set(['hash','ct','author','subject']) for c in d['commits'])"
372json_ok "changed_count is reported" "$DETB" "d['changed_count']==3"
373json_ok "a discovered repo keeps its root attribution" "$DETB" "d['repo']['root']!=''"
374
375# a repo the walk deliberately skipped (it lives inside a node_modules tree) is
376# still served when asked for by path, and is attributed to its root
377mkdir -p "$ROOTS/node_modules"
378cp -a "$ROOTS/cleanrepo" "$ROOTS/node_modules/skipped"
379UNSEEN=$(body "$(env QUERY_STRING="action=repo&path=$ROOTS/node_modules/skipped" \
380 GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN)")
381json_ok "the walk skips node_modules" "$REPOSB" \
382 "not any('node_modules' in r['path'] for r in d['repos'])"
383json_ok "an undiscovered repo is read on demand" "$UNSEEN" "d['ok'] is True"
384json_ok "a skipped repo is attributed to its root" "$UNSEEN" \
385 "d['repo']['root']=='$ROOTS'"
386
387AHEADD=$(env QUERY_STRING="action=repo&path=$ROOTS/aheadrepo" \
388 GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN)
389json_ok "detail of a repo with commits lists newest first" "$(body "$AHEADD")" \
390 "d['commits'][0]['subject']=='local commit two'"
391json_ok "detail caps commits at 20" "$(body "$AHEADD")" "len(d['commits'])<=20"
392
393# path allowlisting
394OUTSIDE=$(env QUERY_STRING="action=repo&path=/etc" \
395 GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN)
396check "a path outside every root is 403" "Status: 403 Forbidden" "$OUTSIDE"
397check "403 explains why" "not in an allowlisted root" "$OUTSIDE"
398
399DOTDOT=$(env QUERY_STRING="action=repo&path=$ROOTS/../etc" \
400 GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN)
401check "a .. traversal is refused" "Status: 403 Forbidden" "$DOTDOT"
402
403NOTREPO=$(env QUERY_STRING="action=repo&path=$ROOTS/notarepo" \
404 GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN)
405check "a directory without .git is refused" "Status: 403 Forbidden" "$NOTREPO"
406
407EMPTYP=$(env QUERY_STRING="action=repo&path=" \
408 GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN)
409check "a missing path argument is refused" "Status: 403 Forbidden" "$EMPTYP"
410
411# ---------------------------------------------------------------- fetch
412section "?action=fetch"
413NOFETCH=$(env QUERY_STRING="action=fetch&path=$ROOTS/cleanrepo" REQUEST_METHOD=POST \
414 GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN)
415check "fetch is disabled by default" '"ok":false' "$NOFETCH"
416check "fetch says how to enable it" "GITSTAT_ALLOW_FETCH" "$NOFETCH"
417
418GETFETCH=$(env QUERY_STRING="action=fetch&path=$ROOTS/cleanrepo" \
419 GITSTAT_ALLOW_FETCH=1 GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN)
420check "fetch is POST only" "fetch is POST only" "$GETFETCH"
421
422FETCHOUT=$(env QUERY_STRING="action=fetch&path=$ROOTS/cleanrepo" REQUEST_METHOD=POST \
423 GITSTAT_ALLOW_FETCH=1 GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN)
424FB=$(body "$FETCHOUT")
425json_ok "enabled fetch returns a JSON document" "$FB"
426json_ok "enabled fetch of a clean repo succeeds" "$FB" "d['ok'] is True and d['exit']==0"
427json_ok "fetch echoes the branch and sync state" "$FB" "'branch' in d and 'ahead' in d"
428
429FETCHBAD=$(env QUERY_STRING="action=fetch&path=/etc" REQUEST_METHOD=POST \
430 GITSTAT_ALLOW_FETCH=1 GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN)
431check "fetch of a path outside the roots is 403" "Status: 403 Forbidden" "$FETCHBAD"
432
433# ---------------------------------------------------------------- other actions
434UNKNOWN=$(env QUERY_STRING="action=bogus" GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN)
435check "an unknown action answers an error document" '"error":"unknown action"' "$UNKNOWN"
436
437FMT=$(env QUERY_STRING="format=json" GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN)
438check "format=json is served as JSON" "Content-Type: application/json" "$FMT"
439json_ok "format=json returns the repos document" "$(body "$FMT")" "d['action']=='repos'"
440
441# ---------------------------------------------------------------- HTML
442section "HTML UI"
443HTML=$(env QUERY_STRING="" GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN)
444check "GET answers HTML" "Content-Type: text/html" "$HTML"
445check "the page has a doctype" "<!DOCTYPE html>" "$HTML"
446check "the page names the product" "SCORPIOX BOT" "$HTML"
447check "the page sets the token block" "--bg:#000000" "$HTML"
448check "the page carries the light token block" "data-theme=light" "$HTML"
449check "the page carries the prepaint script" "sx-theme-request" "$HTML"
450check "the page carries the handoff listener" "sx-theme" "$HTML"
451check_not "no external script is referenced" "<script src=" "$HTML"
452check_not "no CDN is referenced" "cdn." "$HTML"
453check_not "no http URL is referenced in the page" "http://" "$HTML"
454check "the page has the rows table" 'id="rows"' "$HTML"
455check "the page has the filter chips" 'class="chip' "$HTML"
456check "the page has the detail drawer" 'id="drawer"' "$HTML"
457check "the page has inline SVG icons" "<svg viewBox" "$HTML"
458check "the page has a CSP" "Content-Security-Policy" "$HTML"
459check "the page declares the theme toggle hidden in embed" "embed-hide" "$HTML"
460
461EMBED=$(env QUERY_STRING="embed=1" GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN)
462check "embed=1 still serves the page" "<!DOCTYPE html>" "$EMBED"
463check "embed=1 marks the document embedded" "embed" "$EMBED"
464
465# no emoji anywhere in the page
466if printf '%s' "$HTML" | python3 -c "
467import sys
468s=sys.stdin.buffer.read().decode('utf-8','replace')
469bad=[c for c in s if ord(c)>0x2100 and not (0x2010<=ord(c)<=0x2027)]
470sys.exit(1 if bad else 0)" 2>/dev/null; then
471 pass "no emoji in the page"
472else
473 fail "the page contains emoji or exotic glyphs"
474fi
475
476# the hub-vs-direct route branch must be present, got wrong is the classic bug
477check "the page branches on ?plugin= for the hub route" \
478 "HUB ? '/plugins?plugin='" "$HTML"
479check "the page falls back to the direct node route" "'/plugin_gitstat'" "$HTML"
480
481# ---------------------------------------------------------------- cross targets
482section "cross-target artifacts"
483if [ -f plugin_gitstat.musl ]; then
484 file plugin_gitstat.musl | grep -q 'ELF' && pass "the musl build is an ELF" || fail "musl build is not ELF"
485 file plugin_gitstat.musl | grep -qi 'static' && pass "the musl build is statically linked" || fail "musl build is dynamic"
486else
487 echo " (skip) plugin_gitstat.musl not built here"
488fi
489if [ -f plugin_gitstat.arm64 ]; then
490 file plugin_gitstat.arm64 | grep -Eq 'aarch64' && pass "the arm64 build is aarch64" || fail "arm64 build is not aarch64"
491else
492 echo " (skip) plugin_gitstat.arm64 not built here"
493fi
494if [ -f plugin_gitstat.exe ]; then
495 if head -c 2 plugin_gitstat.exe | od -An -c | grep -q 'M Z'; then
496 pass "the Windows build starts with an MZ header"
497 else
498 fail "the Windows build is not a PE (no MZ header)"
499 fi
500else
501 echo " (skip) plugin_gitstat.exe not built here"
502fi
503
504# ---------------------------------------------------------------- generated UI
505section "generated page"
506if command -v python3 >/dev/null 2>&1; then
507 if python3 tools/gen_ui.py --check >/dev/null 2>&1; then
508 pass "src/ui_html.h matches src/ui.html"
509 else
510 fail "src/ui_html.h is stale - run tools/gen_ui.py"
511 fi
512else
513 echo " (skip) python3 not available"
514fi
515
516# ---------------------------------------------------------------- history audit
517section "git history secret audit"
518if git rev-parse --git-dir >/dev/null 2>&1; then
519 ALL="$WORK/history.txt"
520 git log -p --all 2>/dev/null \
521 | grep -v '^Author: ' \
522 | grep -vE 'scan_hist|HITS=|grep -Eo|suspects=|audit "|check_not "|scan "|audit\(\)' \
523 > "$ALL" || : > "$ALL"
524
525 audit() { # audit <label> <regex>
526 H=$(grep -Eo "$2" "$ALL" 2>/dev/null | sort -u | head -3 || true)
527 if [ -n "$H" ]; then
528 fail "history: $1"; printf '%s\n' "$H" | sed 's/^/ /'
529 else
530 pass "history: $1"
531 fi
532 }
533 audit "no LAN addresses" '192\.168\.[0-9]+\.[0-9]+'
534 audit "no private 10.x range" '\b10\.[0-9]+\.[0-9]+\.[0-9]+'
535 audit "no 172.16-31 range" '172\.(1[6-9]|2[0-9]|3[01])\.[0-9]+\.[0-9]+'
536 audit "no GitHub PAT" 'ghp_[A-Za-z0-9]{36}'
537 audit "no HF token" 'hf_[A-Za-z0-9]{30,}'
538 audit "no embedded git creds" 'scorpioxinc:[A-Za-z0-9]{20,}'
539 if command -v scorpiox-cli-botplugin >/dev/null 2>&1; then
540 if scorpiox-cli-botplugin audit . >"$WORK/audit.out" 2>&1; then
541 pass "scorpiox-cli-botplugin audit passes"
542 else
543 fail "scorpiox-cli-botplugin audit failed"; sed 's/^/ /' "$WORK/audit.out" | head -6
544 fi
545 else
546 echo " (skip) scorpiox-cli-botplugin not installed"
547 fi
548else
549 echo " (skip) not a git checkout"
550fi
551
552# ---------------------------------------------------------------- summary
553printf '\n%s=================================%s\n' "$C_B" "$C_0"
554printf ' passed %s%d%s failed %s%d%s\n' "$C_OK" "$PASS" "$C_0" \
555 "$([ "$FAIL" -eq 0 ] && echo "$C_OK" || echo "$C_BAD")" "$FAIL" "$C_0"
556printf '%s=================================%s\n' "$C_B" "$C_0"
557
558[ "$FAIL" -eq 0 ] || exit 1
559exit 0