fix(931): a missing semgrep fails the security-scan release gate instead of warning and exiting 0

The SAST half is now required: require_semgrep exits 2 naming the tool before the
image pull; ETV_SCAN_SKIP_SEMGREP=1 stays the only skip. --selftest pins absent,
opt-out and present. docs/ci-cd.md states the invariant, not the stale host claim.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: f9ce6bc6-5b36-491a-8d72-89c1f70dd3e1
This commit is contained in:
2026-10-30 00:03:24 +01:00
co-authored by Claude Opus 5.5
parent bcf6f7e04d
commit f21dc121d3
2 changed files with 38 additions and 6 deletions
+32 -3
View File
@@ -21,7 +21,7 @@
# PORT host port for the throwaway app (default: 8411)
# Env:
# ETV_SCAN_OUT host dir for reports (default: /tmp/etv-scan-out)
# ETV_SCAN_SKIP_SEMGREP=1 skip the SAST pass (DAST only)
# ETV_SCAN_SKIP_SEMGREP=1 skip the SAST pass (DAST only) — the ONLY skip; a missing semgrep otherwise exits 2
#
# Reports land in $ETV_SCAN_OUT: zap-api-report.{html,md,json}. zap-api-scan.py's exit code is NOT a
# simple pass/fail — its wrapper contract (ersatztv#338, found via the #335 exact-image release scan
@@ -50,6 +50,16 @@ classify_zap_exit() {
esac
}
# require_semgrep — the SAST half is part of the gate: a missing semgrep is an ERROR naming the tool, never a
# warning that still exits 0 (ersatztv#931 — a v26.16.0 release gate could pass with its SAST half never run).
# The only skip is the explicit opt-out ETV_SCAN_SKIP_SEMGREP=1. Standalone so `--selftest` can exercise it.
require_semgrep() {
[ "${ETV_SCAN_SKIP_SEMGREP:-0}" = "1" ] && return 0
command -v semgrep >/dev/null && return 0
echo "error: semgrep not found on PATH — the SAST half of the release gate cannot run. Install semgrep, or set ETV_SCAN_SKIP_SEMGREP=1 to skip it explicitly (DAST only)." >&2
return 2
}
if [ "${1:-}" = "--selftest" ]; then
# Lightweight regression check for the classification contract above (ersatztv#338 Done-when) — no
# docker/ZAP required, so it can run anywhere (incl. off bumblebee, incl. in per-PR CI if ever added).
@@ -70,6 +80,24 @@ if [ "${1:-}" = "--selftest" ]; then
selftest_check 124 1 "TIMEOUT"
selftest_check 3 1 "TOOL ERROR"
selftest_check 77 1 "TOOL ERROR"
# semgrep gate (ersatztv#931): absent -> non-zero naming the tool; opt-out -> 0; present -> 0.
semgrep_check() {
local label="$1" want_status="$2" want_substr="$3" got_status err
err="$(require_semgrep 2>&1 >/dev/null)" && got_status=0 || got_status=$?
if [ "$got_status" != "$want_status" ] || [[ "$err" != *"$want_substr"* ]]; then
echo "SELFTEST FAIL: semgrep gate ($label) -> status=$got_status msg='$err' (want status=$want_status, msg containing '$want_substr')" >&2
return 1
else
echo "selftest ok: semgrep gate ($label) -> status=$got_status"
fi
}
sg_dir="$(mktemp -d)"; sg_empty="$(mktemp -d)"
printf '#!/bin/sh\n' > "$sg_dir/semgrep"; chmod +x "$sg_dir/semgrep"
(PATH="$sg_empty"; unset ETV_SCAN_SKIP_SEMGREP; semgrep_check "absent" 2 "semgrep") || selftest_failed=1
(PATH="$sg_empty"; ETV_SCAN_SKIP_SEMGREP=0; semgrep_check "absent, skip=0" 2 "semgrep") || selftest_failed=1
(PATH="$sg_empty"; ETV_SCAN_SKIP_SEMGREP=1; semgrep_check "absent, explicit opt-out" 0 "") || selftest_failed=1
(PATH="$sg_dir"; unset ETV_SCAN_SKIP_SEMGREP; semgrep_check "present" 0 "") || selftest_failed=1
rm -rf "$sg_dir" "$sg_empty"
if [ "$selftest_failed" = 0 ]; then
echo "selftest: all ZAP exit classifications OK"; exit 0
else
@@ -83,6 +111,9 @@ OUT="${ETV_SCAN_OUT:-/tmp/etv-scan-out}"
TARGET_NAME="etv-secscan-target"
ZAP_IMAGE="ghcr.io/zaproxy/zaproxy:stable"
# Fail fast, before the image pull and the multi-minute ZAP run, if the SAST half cannot run.
require_semgrep || exit $?
command -v docker >/dev/null || { echo "error: docker not found — run this on the docker host (bumblebee)" >&2; exit 2; }
CFG="$(mktemp -d /tmp/etv-secscan-cfg-XXXXXX)"
@@ -145,8 +176,6 @@ echo "==> ZAP result: $ZAP_CLASS_MSG (raw exit $zap_rc)"
if [ "${ETV_SCAN_SKIP_SEMGREP:-0}" = "1" ]; then
echo "==> semgrep SAST skipped (ETV_SCAN_SKIP_SEMGREP=1)"
elif ! command -v semgrep >/dev/null; then
echo "WARN: semgrep not installed — SAST cross-check SKIPPED (install semgrep for full coverage)" >&2
else
echo "==> semgrep SAST cross-check (repo root)"
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"