From 60803e652aff101442e60cd1fa9e924da6be0e1e Mon Sep 17 00:00:00 2001 From: Daniel Radl Date: Thu, 20 Aug 2026 16:26:11 +0200 Subject: [PATCH] ci(security): limit Trivy uploads to main --- .github/workflows/core-build-test-images.yml | 57 +++++++++++++++++--- docs/08-reference/08-container-validation.md | 6 +-- 2 files changed, 54 insertions(+), 9 deletions(-) diff --git a/.github/workflows/core-build-test-images.yml b/.github/workflows/core-build-test-images.yml index 934a1ba..150c668 100644 --- a/.github/workflows/core-build-test-images.yml +++ b/.github/workflows/core-build-test-images.yml @@ -153,15 +153,60 @@ jobs: path: trivy-results/*.sarif retention-days: 30 - - name: Upload Trivy results to code scanning + - name: Add Trivy results to pull request summary + if: ${{ always() && github.event_name == 'pull_request' && hashFiles('trivy-results/*.sarif') != '' }} + continue-on-error: true + shell: bash + run: | + python3 - <<'PY' + import json + import os + from pathlib import Path + + maximum_results = 50 + reports = sorted(Path("trivy-results").glob("*.sarif")) + summary_path = Path(os.environ["GITHUB_STEP_SUMMARY"]) + + with summary_path.open("a", encoding="utf-8") as summary: + summary.write("## Trivy image findings\n\n") + for report in reports: + payload = json.loads(report.read_text(encoding="utf-8")) + results = [ + result + for run in payload.get("runs", []) + for result in run.get("results", []) + ] + summary.write(f"### `{report.stem}`: {len(results)} finding(s)\n\n") + if not results: + summary.write("No fixed HIGH or CRITICAL vulnerabilities found.\n\n") + continue + + for result in results[:maximum_results]: + rule = str(result.get("ruleId", "unknown")).replace("`", "") + level = str(result.get("level", "warning")).upper() + message = " ".join( + str(result.get("message", {}).get("text", "No details")) + .replace("`", "") + .split() + ) + summary.write(f"- **{level}** `{rule}`: {message}\n") + + if len(results) > maximum_results: + hidden = len(results) - maximum_results + summary.write( + f"\n{hidden} additional finding(s) are available in the " + "Trivy SARIF artifact.\n" + ) + summary.write("\n") + PY + + - name: Upload Trivy results to code scanning on main if: >- ${{ always() && - hashFiles('trivy-results/*.sarif') != '' && - ( - github.event_name != 'pull_request' || - github.event.pull_request.head.repo.full_name == github.repository - ) + github.event_name == 'push' && + github.ref == 'refs/heads/main' && + hashFiles('trivy-results/*.sarif') != '' }} continue-on-error: true uses: github/codeql-action/upload-sarif@v4 diff --git a/docs/08-reference/08-container-validation.md b/docs/08-reference/08-container-validation.md index 2ac9ccf..20cb558 100644 --- a/docs/08-reference/08-container-validation.md +++ b/docs/08-reference/08-container-validation.md @@ -75,9 +75,9 @@ During the initial rollout, Trivy is report-only: - findings do not fail the build - SARIF reports are retained as workflow artifacts for 30 days -- results are uploaded to GitHub Code Scanning for trusted workflow runs -- fork pull requests retain artifacts but skip the Code Scanning upload because - their workflow tokens cannot write security events +- pull requests show a capped findings list in the workflow summary and retain + the complete SARIF reports as artifacts +- results are uploaded to GitHub Code Scanning only on pushes to `main` This provides visibility before deciding which vulnerability classes should become blocking.