#!/usr/bin/env bash # Vulnerability scan of the Docker image with Trivy, at a fixed version (strumento.sh), on the archive produced by # the build (docker save or buildx --output type=docker): no daemon is needed, and the very image that was tested is # scanned. Debian packages and Febe's .NET dependencies (the *.deps.json files). # # .gitea/scripts/scansione-immagine.sh [cartella dei rapporti] # # The rule (docs/processo.md, «Rilievi di sicurezza»): # - CRITICAL with a fix available: the job fails and the image is not published; # - HIGH, and CRITICAL still without a fix: reported in the log and in trivy.json, without stopping anything. # The exceptions live only in .trivyignore, at the repository root: one identifier per line, with the expiry # (exp:AAAA-MM-GG) and, on the line above, the written reason and who decided it. set -euo pipefail radice="$(git rev-parse --show-toplevel)" archivio="${1:?Manca il file .tar da scansionare.}" rapporti="${2:-$radice/out/sicurezza}" lavoro="${RUNNER_TEMP:-/tmp}" strumenti="$lavoro/febe-strumenti" [[ -s "$archivio" ]] || { echo "Manca l'archivio dell'immagine: $archivio" >&2; exit 2; } bash "$radice/.gitea/scripts/strumento.sh" trivy "$strumenti" mkdir -p "$rapporti" # No progress bars, no check for new versions, no usage data. export TRIVY_NO_PROGRESS=true TRIVY_SKIP_VERSION_CHECK=true TRIVY_DISABLE_TELEMETRY=true export TRIVY_CACHE_DIR="$lavoro/febe-trivy-cache" comuni=(image --input "$archivio" --scanners vuln --timeout 20m) if [[ -f "$radice/.trivyignore" ]]; then comuni+=(--ignorefile "$radice/.trivyignore") echo "Eccezioni in .trivyignore: $(grep -c -E '^[^#[:space:]]' "$radice/.trivyignore" || true)" fi echo "== Vulnerabilità HIGH e CRITICAL (rapporto: non fermano il lavoro)" "$strumenti/trivy" "${comuni[@]}" --severity HIGH,CRITICAL --format json --output "$rapporti/trivy.json" --exit-code 0 "$strumenti/trivy" convert --format table "$rapporti/trivy.json" # The count with our own JSON reader (json.sh, awk): no jq. One line per vulnerability, severity and the version that # fixes it (empty or null if there is none). An unreadable report stops nothing: it just says so. if vulnerabilita=$(bash "$radice/.gitea/scripts/json.sh" campi "Results/*/Vulnerabilities/*" Severity,FixedVersion "$rapporti/trivy.json"); then conteggio=$(awk -F '\t' ' $1 == "HIGH" { alte++ } $1 == "CRITICAL" { critiche++; if ($2 != "" && $2 != "null") correggibili++ } END { printf "HIGH %d · CRITICAL %d, di cui correggibili %d", alte, critiche, correggibili }' <<< "$vulnerabilita") echo "Trovate: $conteggio." if [[ -n "${GITHUB_STEP_SUMMARY:-}" ]]; then printf "### Sicurezza dell'immagine (Trivy)\n\n%s.\n" "$conteggio" >> "$GITHUB_STEP_SUMMARY" || true fi else echo "::warning::Il rapporto di Trivy non si legge come JSON: niente conteggio (la scansione che ferma il lavoro segue)." fi echo "== CRITICAL con una correzione disponibile (fermano il lavoro)" if "$strumenti/trivy" "${comuni[@]}" --skip-db-update --skip-java-db-update --severity CRITICAL --ignore-unfixed \ --format table --exit-code 1; then echo "Nessuna vulnerabilità CRITICAL con una correzione disponibile." else echo "Vulnerabilità CRITICAL con una correzione disponibile: l'immagine non passa. Aggiorna il pacchetto o la dipendenza (docs/processo.md, «Rilievi di sicurezza»)." >&2 exit 1 fi