Files
cpanel-importer/tests/image-scope-test.sh
T
shadowdaoandClaude Sonnet 5.5 7da863cfd1 Scan only the WHP scan scope: whp-scan-scope + clamscan --file-list, scan attestation in report.json
- Dockerfile: multi-stage Go build of whp-scan-scope (CGO off, stdlib only) from a PINNED
  commit of the WHP repo (ARG WHP_COMMIT, currently the merge of WHP #331; bump it to the merge
  of the 2000 MiB limit PR before publishing an image meant to enforce it). Private repo: the
  BuildKit secret whp_token in CI; --build-context whp-src=<checkout> for local builds.
- scan-files.php: whp-scan-scope -> /tmp/scan.list + stats, then clamscan --file-list with
  --max-filesize=2000M --max-scansize=4000M --max-scantime=0 --alert-exceeds-max=yes --tempdir on
  the disk scratch. A missing/failing tool, clamscan exit >= 2 or 0 loaded signatures is FATAL
  (no fall back to an unscoped scan). Writes the attestation (the tool's stats + engine_version,
  signature_db_version, signatures_loaded, clamscan_exit).
- entrypoint.sh: passes --attestation/--scratch, removes the scratch, embeds the attestation as
  report.json "scan" (objects kept as objects).
- Tests: tests/run-tests.sh (stub clamscan, real tool, fail-closed cases), tests/build-scope-tree.sh
  (polyglot, web-root symlink, docroot under tmp, handler dir, uploads, EICAR, sparse 2001 MiB mp4),
  tests/image-scope-test.sh (end to end on the built image; CI runs it).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0185ukXi4YX5FxJGqhi6qab6
2026-11-22 07:05:11 -08:00

73 lines
5.1 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# image-scope-test.sh <image> -- end-to-end test of a BUILT image: the real entrypoint, real ClamAV and the
# real whp-scan-scope, started with the panel's hardening flags (read-only rootfs, RAM /tmp, uid 999,
# --memory 2g), against the synthetic backup of tests/build-scope-tree.sh (including a sparse 2001 MiB mp4
# and an EICAR file). Needs only docker and tar on the host (the checks run inside the image, so it also
# works on CI runners without php). Uses named volumes and docker cp instead of bind mounts, so it works
# against a remote or sandboxed docker daemon. Takes about a minute (the sparse file is read once).
#
# Run: bash tests/image-scope-test.sh cpanel-importer:dev (exit 0 = all pass)
set -euo pipefail
IMAGE="${1:?usage: image-scope-test.sh <image>}"
REPO="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
WORK="$(mktemp -d)"
SUF="$$"
VOLS=(bk q s)
cleanup() {
docker rm -f "isc-helper-$SUF" >/dev/null 2>&1 || true
for v in "${VOLS[@]}"; do docker volume rm -f "isc-$v-$SUF" >/dev/null 2>&1 || true; done
rm -rf "$WORK"
}
trap cleanup EXIT
FAILS=0
pass() { printf ' PASS %s\n' "$1"; }
fail() { printf ' FAIL %s\n' "$1"; FAILS=$((FAILS + 1)); }
bash "$REPO/tests/build-scope-tree.sh" "$WORK/tree" --big
tar czf "$WORK/cpmove-scope.tar.gz" -C "$WORK/tree" --exclude='EXPECT_*' cpmove-scope
for v in "${VOLS[@]}"; do docker volume create "isc-$v-$SUF" >/dev/null; done
docker create --name "isc-helper-$SUF" --user 0 -v "isc-bk-$SUF:/b" --entrypoint sh "$IMAGE" -c true >/dev/null
docker cp "$WORK/cpmove-scope.tar.gz" "isc-helper-$SUF:/b/cpmove-scope.tar.gz"
docker rm "isc-helper-$SUF" >/dev/null
docker run --rm --user 0 -v "isc-q-$SUF:/q" -v "isc-s-$SUF:/s" --entrypoint sh "$IMAGE" -c 'chown 999:999 /q /s; chmod 750 /q /s'
echo "entrypoint run (panel-style flags)"
rc=0
docker run --rm --user 999:999 --cap-drop=ALL --security-opt=no-new-privileges --read-only \
--tmpfs /tmp:rw,nosuid,nodev,exec,size=4g --memory=2g --memory-swap=2g --cpus=2 \
-v "isc-bk-$SUF:/host/backup:ro" -v "isc-q-$SUF:/host/quarantine" -v "isc-s-$SUF:/host/sanitized" \
-e IMPORT_ID=t -e IMPORT_USERNAME=scope -e IMPORT_BACKUP_FILE=/host/backup/cpmove-scope.tar.gz \
-e CLAMAV_REFRESH=false -e IMPORT_INCLUDE_MAIL=true "$IMAGE" >"$WORK/run.log" 2>&1 || rc=$?
if [[ $rc -eq 0 ]]; then pass "entrypoint exits 0"; else fail "entrypoint exits 0 (rc=$rc)"; tail -20 "$WORK/run.log"; fi
# the checks run inside the image (php + find), on the finished volumes, as root so nothing is unreadable
CHECK='
$r = json_decode(file_get_contents("/s/t/report.json"));
$s = $r->scan ?? null;
$bad = [];
$need = function ($ok, $what) use (&$bad) { if (!$ok) $bad[] = $what; };
$need(is_object($s), "report.json has a scan object");
$need(($r->status ?? "") === "completed", "status completed");
$need($s->policy === "code-risky-v1" && $s->scope_version === 1, "policy code-risky-v1 / scope_version 1");
$need($s->files_total === $s->files_scanned + $s->files_skipped_by_policy + $s->in_scope_unscanned + $s->oversize_unscanned, "files_total accounts for every file");
$need(is_object($s->skipped_by_class) && array_sum((array) $s->skipped_by_class) === $s->files_skipped_by_policy, "skipped_by_class sums to files_skipped_by_policy");
$need($s->in_scope_unscanned === 0 && $s->in_scope_unscanned_files === [], "nothing in scope was left unscanned");
$need($s->oversize_unscanned === 1 && $s->oversize_unscanned_files === ["cpmove-scope/homedir/public_html/huge.mp4"], "the 2001 MiB mp4 is named in oversize_unscanned_files");
$need(is_string($s->engine_version) && is_string($s->signature_db_version) && is_int($s->signatures_loaded) && $s->signatures_loaded > 0, "engine_version, signature_db_version, signatures_loaded > 0");
$need($s->clamscan_exit === 1, "clamscan_exit 1 (the EICAR hit)");
$need(($s->skipped_by_class->mail ?? 0) >= 1 && ($s->mail_tree_skipped ?? false) === true, "the mail tree was skipped (include_mail=true)");
$need($r->files_scanned === $s->files_scanned, "top-level files_scanned = the scope files_scanned");
$need($r->files_quarantined === 1 && $r->actions[0]->path === "cpmove-scope/homedir/public_html/eicar.php", "only the EICAR file is quarantined");
$need(!file_exists("/s/t/extracted/cpmove-scope/homedir/public_html/eicar.php"), "the EICAR file is gone from the cleaned tree");
$need(file_exists("/s/t/extracted/cpmove-scope/homedir/public_html/polyglot.jpg") === true, "the polyglot (no ClamAV signature) is still in the tree: scanned, not removed");
$need(!is_dir("/s/t/work"), "no scratch left in the sanitized dir");
echo $bad ? "BAD: " . implode("; ", $bad) . "\n" : "OK\n";'
out="$(docker run --rm --user 0 -v "isc-s-$SUF:/s" -v "isc-q-$SUF:/q" --entrypoint php "$IMAGE" -r "$CHECK" 2>&1 || true)"
if [[ "$out" == "OK" ]]; then pass "report.json scan object: schema, invariants, hits, oversize, scratch"; else fail "report.json scan object: $out"; fi
grep -q 'scan-files: scope code-risky-v1' "$WORK/run.log" && pass "log shows the scope line" || fail "log shows the scope line"
echo
if [[ $FAILS -eq 0 ]]; then echo "ALL PASS"; else echo "$FAILS FAILURE(S)"; fi
exit $(( FAILS == 0 ? 0 : 1 ))