Files
clean-architecture-backend-…/scripts/ci/trivy-scan.sh
T
DongHyeonka aeb6033990
pr / done (pull_request) Successful in 13m37s
fix(ci): 라이선스 판정을 배포하는 의존성에 두고 빌드한 뒤에 돈다
문제: PR 단계를 job 하나로 접자 Trivy filesystem 검사가 Gradle 빌드 뒤에 돌게
됐고, 처음으로 lockfile의 라이선스를 읽었다(run 95). Trivy는 같은 기계의 Gradle
캐시에서만 lockfile의 라이선스를 읽는데, 예전의 따로 된 검사 job에는 캐시가 없어
「High/Critical 라이선스는 막는다」는 검사가 라이선스를 한 번도 본 적이 없었다.
보이게 되자 lockfile마다 SpotBugs·Checkstyle(LGPL), Error Prone dataflow(GPL+CPE),
Jakarta API(EPL/GPL 이중), org.reflections(WTFPL, Trivy 분류 forbidden) 등이 걸렸다.
대부분 배포하지 않는 빌드 도구·시험 구성이다. 한편 릴리스 이미지 검사는 jar의
라이선스를 읽지 못한다(run 62의 Licenses 칸이 모든 jar에서 비어 있다). 그래서 앞
커밋의 「앱 의존성 금지 라이선스는 이미지에서 막는다」도 빈 검사였다.

고른 방법(2026-11-11 소유자 결정 「배포하는 의존성의 금지 라이선스만 실패」):
- filesystem 검사가 `src/bootstrap/app/gradle.lockfile`의 runtimeClasspath 항목
  (기본 조립 jar에 실리는 것)만 골라 forbidden(CRITICAL)이면 실패한다.
- Gradle 캐시가 없으면 건너뛴다고 적는다. 캐시가 있는데 라이선스를 하나도 읽지
  못하면 빈 검사이므로 실패한다.
- 나머지 라이선스 결과(OS, 도구, 시험, restricted)는 보고만 한다. 이미지 검사의
  빈 라이선스 gate는 지우고 보고만 남긴다. 정책 문서를 실제 동작에 맞춘다.

버린 대안: lockfile 전체에 High/Critical을 그대로 막고 예외를 다는 것. Trivy의
라이선스 예외는 라이선스 이름 단위라 도구용 LGPL을 빼면 배포 의존성의 LGPL도 빠진다.

위험·한계: main.yml의 vulnerabilities job은 빌드하지 않아 라이선스 판정을 건너뛴다.
판정은 병합을 막는 PR 단계에 있다. runtimeClasspath 밖에서 jar에 실리는 것(다른
조립)이 생기면 grep 조건을 넓혀야 한다.

검증: runner PR `pr / done`(이 PR 본문에 적는다).
2026-11-12 02:26:58 +09:00

146 lines
8.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# Vulnerability gate, shared by the stage workflows. Called by .gitea/workflows/pr.yml and main.yml
# (`filesystem`) and release.yml (`image <reference>`); not by Gradle and not on the desktop, because
# it downloads and installs its tools (docs/work/operate.md 4.1).
#
# bash scripts/ci/trivy-scan.sh filesystem dependency tree: block High/Critical
# vulnerabilities and a forbidden license on a
# shipped dependency (after a build), report the
# rest, fail on any CISA KEV match
# bash scripts/ci/trivy-scan.sh image <reference> release image: write image-sbom.cdx.json,
# block High/Critical vulnerabilities, report
# the rest and the licenses
#
# Tools are pinned release binaries with an asserted SHA-256, installed under RUNNER_TEMP (or a
# temporary directory): a checksum is a supply-chain claim that can be checked offline, an action
# pinned to a commit is not. .trivyignore.yaml is the one suppression source; each entry carries a
# reason and an expiry by policy (.github/dependency-vulnerability-policy.md).
# Optional mirrors: TRIVY_DOWNLOAD_BASE_URL, JQ_DOWNLOAD_BASE_URL, KEV_FEED_URL.
set -euo pipefail
readonly TRIVY_VERSION='0.71.2'
readonly TRIVY_SHA256_AMD64='0510e71e2fd39bf863856d499c8dc19feb4e7336546394c502a8f5cc7ab27460'
readonly TRIVY_SHA256_ARM64='fe1c7106e15a5365d485b098a8c338f91e3b7ba71cb0e4963b98a3a098763cfc'
readonly JQ_VERSION='1.8.1'
readonly JQ_SHA256_AMD64='020468de7539ce70ef1bceaf7cde2e8c4f2ca6c3afb84642aabc5c97d9fc2a0d'
readonly JQ_SHA256_ARM64='6bc62f25981328edd3cfcfe6fe51b073f2d7e7710d7ef7fcdac28d4e384fc3d4'
readonly DEFAULT_KEV_FEED_URL='https://www.cisa.gov/sites/default/files/feeds/known_exploited_vulnerabilities.json'
cd "$(dirname "${BASH_SOURCE[0]}")/../.."
tools="$(mktemp -d "${RUNNER_TEMP:-/tmp}/trivy-scan.XXXXXX")"
trap 'rm -rf "${tools}"' EXIT
download() { # url, sha256, destination
curl --fail --show-error --silent --location --retry 3 --proto '=https' --tlsv1.2 "$1" --output "$3"
printf '%s %s\n' "$2" "$3" | sha256sum -c -
}
case "$(uname -m)" in
x86_64 | amd64) trivy_arch='64bit'; trivy_sha="${TRIVY_SHA256_AMD64}"; jq_asset='jq-linux-amd64'; jq_sha="${JQ_SHA256_AMD64}" ;;
aarch64 | arm64) trivy_arch='ARM64'; trivy_sha="${TRIVY_SHA256_ARM64}"; jq_asset='jq-linux-arm64'; jq_sha="${JQ_SHA256_ARM64}" ;;
*) echo "trivy-scan: unsupported architecture $(uname -m)" >&2; exit 1 ;;
esac
download "${TRIVY_DOWNLOAD_BASE_URL:-https://github.com/aquasecurity/trivy/releases/download/v${TRIVY_VERSION}}/trivy_${TRIVY_VERSION}_Linux-${trivy_arch}.tar.gz" \
"${trivy_sha}" "${tools}/trivy.tar.gz"
tar -xzf "${tools}/trivy.tar.gz" -C "${tools}" trivy
download "${JQ_DOWNLOAD_BASE_URL:-https://github.com/jqlang/jq/releases/download/jq-${JQ_VERSION}}/${jq_asset}" \
"${jq_sha}" "${tools}/jq"
chmod 0755 "${tools}/trivy" "${tools}/jq"
export PATH="${tools}:${PATH}"
[ "$(jq --version)" = "jq-${JQ_VERSION}" ] || { echo "trivy-scan: unexpected $(jq --version)" >&2; exit 1; }
# A forbidden license on a dependency that ships fails (owner decision 2026-11-11). What ships is
# the default composition's jar, so the gate reads the bootstrap leaf's runtimeClasspath lockfile
# entries. Trivy finds a lockfile's licenses only in the Gradle cache of the same machine; the
# release image scan cannot read a jar's license at all (its Licenses column is empty for every
# jar), and the old separate scan job had no cache, so until the pull-request stage became one job
# that builds first no license gate here had ever seen a license. With no cache this says so and
# skips; with a cache and not one license read, the check would be empty, so it fails.
scan_shipped_licenses() {
local cache="${GRADLE_USER_HOME:-${HOME}/.gradle}/caches/modules-2"
if [ ! -d "${cache}" ]; then
echo "trivy-scan: no Gradle cache at ${cache}; the shipped-license gate runs after a build (pr.yml)"
return 0
fi
local shipped="${tools}/shipped"
mkdir -p "${shipped}"
grep -E '=([^=]*,)?runtimeClasspath(,|$)' src/bootstrap/app/gradle.lockfile > "${shipped}/gradle.lockfile" \
|| { echo "trivy-scan: src/bootstrap/app/gradle.lockfile has no runtimeClasspath entry" >&2; exit 1; }
trivy fs --scanners license --format json --output "${tools}/shipped-licenses.json" "${shipped}"
jq -e '[.Results[]?.Licenses[]?] | length > 0' "${tools}/shipped-licenses.json" > /dev/null \
|| { echo "trivy-scan: Trivy read no license for the shipped dependencies; the gate would be empty" >&2; exit 1; }
trivy fs --scanners license --severity CRITICAL --exit-code 1 --ignorefile .trivyignore.yaml "${shipped}"
}
scan_filesystem() {
trivy fs --scanners vuln --severity CRITICAL,HIGH --exit-code 1 --ignorefile .trivyignore.yaml .
trivy fs --scanners vuln --severity MEDIUM,LOW --exit-code 0 --ignorefile .trivyignore.yaml .
scan_shipped_licenses
# Every other license finding is reported only: the lockfiles also list build tools (SpotBugs,
# Checkstyle, Error Prone) and test libraries that never ship.
trivy fs --scanners license --exit-code 0 --ignorefile .trivyignore.yaml .
trivy fs --scanners vuln --severity CRITICAL,HIGH,MEDIUM,LOW,UNKNOWN --exit-code 0 \
--ignorefile .trivyignore.yaml --format json --output "${tools}/trivy-kev.json" .
# A KEV-listed vulnerability fails regardless of its CVSS score. The feed and the scan are both
# checked for shape first: an empty or malformed input would otherwise intersect to nothing and
# print "no catalog match" for a check that never ran.
if ! curl --fail --show-error --silent --location --retry 3 --proto '=https' --tlsv1.2 \
"${KEV_FEED_URL:-${DEFAULT_KEV_FEED_URL}}" --output "${tools}/kev.json"; then
echo "trivy-scan: KEV feed unavailable; set KEV_FEED_URL to an approved mirror" >&2
exit 1
fi
jq -e '
(.catalogVersion | type == "string" and test("\\S"))
and (.dateReleased | type == "string" and test("\\S"))
and (.count | type == "number")
and (.count > 0)
and (.count == (.count | floor))
and (.vulnerabilities | type == "array")
and ((.vulnerabilities | length) > 0)
and (.count == (.vulnerabilities | length))
and (all(.vulnerabilities[]; (.cveID | type == "string" and test("^CVE-[0-9]{4}-[0-9]{4,}$"))))
and (([.vulnerabilities[].cveID] | unique | length) == .count)
' "${tools}/kev.json" > /dev/null || { echo "trivy-scan: the KEV feed is malformed" >&2; exit 1; }
jq -e '
(type == "object")
and (.Results | type == "array")
and ((.Results | length) > 0)
and (all(.Results[]; type == "object"))
and (all(.Results[]; (.Vulnerabilities == null) or (.Vulnerabilities | type == "array")))
and (all(.Results[]; all(.Vulnerabilities[]?;
(type == "object") and (.VulnerabilityID | type == "string" and test("\\S")))))
' "${tools}/trivy-kev.json" > /dev/null || { echo "trivy-scan: the Trivy KEV input is malformed" >&2; exit 1; }
jq -r '[.Results[]?.Vulnerabilities[]?.VulnerabilityID] | unique[]' "${tools}/trivy-kev.json" | sort -u > "${tools}/found.txt"
jq -r '.vulnerabilities[].cveID' "${tools}/kev.json" | sort -u > "${tools}/kev.txt"
hits="$(comm -12 "${tools}/found.txt" "${tools}/kev.txt")"
if [ -n "${hits}" ]; then
printf 'trivy-scan: CISA KEV-listed vulnerability found regardless of CVSS:\n%s\n' "${hits}" >&2
exit 1
fi
echo "trivy-scan: no KEV catalog match."
}
# The image scan sees the base image's OS packages, which the filesystem scan cannot. The SBOM is
# written before the blocking scan, so the inventory survives the run that refuses to publish.
scan_image() {
trivy image --format cyclonedx --scanners license --output image-sbom.cdx.json "$1"
test -s image-sbom.cdx.json
# Vulnerabilities, OS and application packages alike: High or Critical fails the release. A
# finding with a fix is fixed (a newer base image digest or dependency); one with no fix yet is
# carried in .trivyignore.yaml with its expiry (owner decision 2026-11-11).
trivy image --scanners vuln --severity CRITICAL,HIGH --exit-code 1 --ignorefile .trivyignore.yaml "$1"
trivy image --scanners vuln --severity MEDIUM,LOW --exit-code 0 --ignorefile .trivyignore.yaml "$1"
# Licenses are reported only (same decision): the OS packages' are the distribution's terms, not
# this application's, and Trivy reads no license from a jar in an image. The shipped jars' gate is
# the filesystem scan's (scan_shipped_licenses), which pull requests run after building.
trivy image --scanners license --exit-code 0 --ignorefile .trivyignore.yaml "$1"
}
case "${1:-}" in
filesystem) scan_filesystem ;;
image) scan_image "${2:?image reference}" ;;
*) sed -n '2,13p' "$0" >&2; exit 2 ;;
esac