Files
shadowdaoandClaude Sonnet 5.5 2ebb05a6d6 scan-files: fail closed on scan errors masked by hits, unmapped hits and quarantine failures
Review of the scan-scope change (measured on ClamAV 1.4.6 in this image):
- clamscan exits 1, not 2, when one listed file is infected and another
  cannot be read: only the summary says "Total errors: 1". A mode-000
  webshell next to any detected file went unscanned and the run passed
  (privd accepted the attestation, require_scan_scope=true included).
  Now: a summary with Total errors > 0, or no summary at all, fails the run.
- Every hit line must name an EXACT entry of the scan list (the last ": "
  ends the path; signature names never contain one); anything else fails
  the run instead of being skipped.
- The list itself is checked before clamscan sees it: each entry a plain
  absolute path below the extract, <= 1022 bytes, no control character,
  and exactly files_scanned entries.
- A hit that cannot be moved to quarantine fails the run (it used to log a
  WARN and leave the infected file in the tree to be imported). Duplicate
  hits (a link target listed twice) are one action.
- Tests: names with ': ' + ' FOUND', leading '-', \n, \r, >= 1023 bytes;
  rogue hit, Total errors, no summary, unquarantinable hit, list longer
  than files_scanned, entry outside the extract (red on the old script);
  image-scope-test.sh runs a mode-000 file next to EICAR end to end.

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

95 lines
6.9 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"
# Review of 2026-11-22: clamscan exits 1, not 2, when one listed file is infected and another cannot be read, so a
# mode-000 webshell next to any hit went unscanned with in_scope_unscanned 0. The tarball is made inside the image
# as root (GNU tar keeps mode 000; root can read the file to pack it).
echo "entrypoint run: mode-000 file next to an EICAR hit"
docker run --rm --user 0 -v "isc-bk-$SUF:/b" --entrypoint sh "$IMAGE" -c '
set -e; d=/tmp/m/cpmove-mask/homedir/public_html; mkdir -p "$d" /tmp/m/cpmove-mask/userdata
printf "documentroot: /home/mask/public_html\n" > /tmp/m/cpmove-mask/userdata/mask.test
printf "%s" "X5O!P%@AP[4\\PZX54(P^)7CC)7}\$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!\$H+H*" > "$d/eicar.php"
cp "$d/eicar.php" "$d/hidden.php"; chmod 000 "$d/hidden.php"
tar czf /b/cpmove-mask.tar.gz -C /tmp/m cpmove-mask'
docker run --rm --user 0 -v "isc-q-$SUF:/q" -v "isc-s-$SUF:/s" --entrypoint sh "$IMAGE" -c 'rm -rf /q/m /s/m'
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=1g --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=m -e IMPORT_USERNAME=mask -e IMPORT_BACKUP_FILE=/host/backup/cpmove-mask.tar.gz \
-e CLAMAV_REFRESH=false "$IMAGE" >"$WORK/run-mask.log" 2>&1 || rc=$?
out="$(docker run --rm --user 0 -v "isc-s-$SUF:/s" --entrypoint php "$IMAGE" -r '
$s = json_decode(file_get_contents("/s/m/report.json"))->scan ?? null;
echo (is_object($s) && $s->in_scope_unscanned === 1 && $s->in_scope_unscanned_files === ["cpmove-mask/homedir/public_html/hidden.php"]) ? "OK" : json_encode($s);' 2>&1 || true)"
if [[ $rc -eq 0 && "$out" == "OK" ]]; then pass "mode-000 file is in_scope_unscanned by name (privd refuses the run)"; else fail "mode-000 file (rc=$rc): $out"; fi
echo
if [[ $FAILS -eq 0 ]]; then echo "ALL PASS"; else echo "$FAILS FAILURE(S)"; fi
exit $(( FAILS == 0 ? 0 : 1 ))