diff --git a/action.yaml b/action.yaml index 2efe0f7..d6c94de 100644 --- a/action.yaml +++ b/action.yaml @@ -7,7 +7,8 @@ inputs: required: true target-image: description: Target baseline container image reference (e.g. host/server/gitea:latest) - required: true + required: false + default: '' target-branch: description: Target branch name for delta heading (e.g. main) required: false @@ -165,6 +166,14 @@ runs: echo "ℹ️ Strategy: Direct Trivy image scanning enabled (default). Inspects full container rootfs and package databases for maximum fidelity." fi + target_available=true + if [ -z "${{ inputs.target-image }}" ] || [ "${{ inputs.target-image }}" = "none" ] || [ "${{ inputs.target-image }}" = "null" ]; then + target_available=false + elif ! docker manifest inspect "${{ inputs.target-image }}" >/dev/null 2>&1; then + echo "::warning::Target baseline image '${{ inputs.target-image }}' not found in registry (MANIFEST_UNKNOWN or inaccessible). Falling back to standalone head scan." + target_available=false + fi + for platform in "${PLATFORMS[@]}"; do arch_tag=$(echo "$platform" | sed -e 's#^[a-zA-Z0-9_-]*/##' -e 's#/#-#g') @@ -186,20 +195,22 @@ runs: fi # Target image extraction & fallback - if [ "$use_inbuild" = "true" ]; then - echo "Attempting extraction of built-in SBOM for target image ($platform)..." - docker buildx imagetools inspect "${{ inputs.target-image }}" \ - --format "{{ json (index .SBOM \"${platform}\").SPDX }}" > "sboms/target/sbom-${arch_tag}.spdx.json" 2>/dev/null || true - cleanup_file "sboms/target/sbom-${arch_tag}.spdx.json" - [ -f "sboms/target/sbom-${arch_tag}.spdx.json" ] && cp "sboms/target/sbom-${arch_tag}.spdx.json" "sboms/target/inbuild-sbom-${arch_tag}.spdx.json" 2>/dev/null || true - fi + if [ "$target_available" = "true" ]; then + if [ "$use_inbuild" = "true" ]; then + echo "Attempting extraction of built-in SBOM for target image ($platform)..." + docker buildx imagetools inspect "${{ inputs.target-image }}" \ + --format "{{ json (index .SBOM \"${platform}\").SPDX }}" > "sboms/target/sbom-${arch_tag}.spdx.json" 2>/dev/null || true + cleanup_file "sboms/target/sbom-${arch_tag}.spdx.json" + [ -f "sboms/target/sbom-${arch_tag}.spdx.json" ] && cp "sboms/target/sbom-${arch_tag}.spdx.json" "sboms/target/inbuild-sbom-${arch_tag}.spdx.json" 2>/dev/null || true + fi - if [ ! -f "sboms/target/sbom-${arch_tag}.spdx.json" ]; then - echo "Generating SBOM for target image ($platform) with Trivy..." - if ! trivy image --skip-version-check --platform "$platform" --format spdx-json --output "sboms/target/sbom-${arch_tag}.spdx.json" "${{ inputs.target-image }}"; then - echo "::warning::Trivy image scan failed for target image ($platform)" + if [ ! -f "sboms/target/sbom-${arch_tag}.spdx.json" ]; then + echo "Generating SBOM for target image ($platform) with Trivy..." + if ! trivy image --skip-version-check --platform "$platform" --format spdx-json --output "sboms/target/sbom-${arch_tag}.spdx.json" "${{ inputs.target-image }}"; then + echo "::warning::Trivy image scan failed for target image ($platform)" + fi + cleanup_file "sboms/target/sbom-${arch_tag}.spdx.json" fi - cleanup_file "sboms/target/sbom-${arch_tag}.spdx.json" fi done @@ -221,7 +232,7 @@ runs: fi fi - if [ -z "$(find sboms/target -type f -name 'sbom-*.spdx.json' 2>/dev/null)" ]; then + if [ "$target_available" = "true" ] && [ -z "$(find sboms/target -type f -name 'sbom-*.spdx.json' 2>/dev/null)" ]; then if [ "$use_inbuild" = "true" ]; then docker buildx imagetools inspect "${{ inputs.target-image }}" \ --format '{{ json .SBOM.SPDX }}' > sboms/target/sbom-default.spdx.json 2>/dev/null || true @@ -246,7 +257,7 @@ runs: head_sbom_err="No SBOM generated and head image could not be inspected" fi - if [ ${#target_files[@]} -eq 0 ] && [ -z "$target_sbom_err" ]; then + if [ "$target_available" = "true" ] && [ ${#target_files[@]} -eq 0 ] && [ -z "$target_sbom_err" ]; then target_sbom_err="No SBOM generated and target baseline image could not be inspected" fi @@ -264,7 +275,9 @@ runs: done fi - if [ -n "$target_sbom_err" ]; then + if [ "$target_available" != "true" ]; then + echo "TARGET Baseline Image (${{ inputs.target-image }}): ⚠️ NOT AVAILABLE (Registry manifest missing or target empty)" + elif [ -n "$target_sbom_err" ]; then echo "TARGET Baseline Image (${{ inputs.target-image }}): ❌ FAILED - $target_sbom_err" else echo "TARGET Baseline Image (${{ inputs.target-image }}): ✅ SUCCESS" @@ -280,7 +293,7 @@ runs: echo "::error::Head SBOM extraction failed: $head_sbom_err" has_sbom_err=true fi - if [ -n "$target_sbom_err" ]; then + if [ "$target_available" = "true" ] && [ -n "$target_sbom_err" ]; then echo "::error::Target baseline SBOM extraction failed: $target_sbom_err" has_sbom_err=true fi @@ -416,9 +429,18 @@ runs: fi # 2. Scan Target SBOMs - if [ ${#target_sboms[@]} -eq 0 ]; then - target_ok=false - target_summary+=(" - ❌ No SBOM files found in sboms/target/") + target_available=true + if [ -z "${{ inputs.target-image }}" ] || [ "${{ inputs.target-image }}" = "none" ] || [ "${{ inputs.target-image }}" = "null" ]; then + target_available=false + fi + + if [ "$target_available" = "false" ] || [ ${#target_sboms[@]} -eq 0 ]; then + if [ "$target_available" = "true" ]; then + target_ok=false + target_summary+=(" - ⚠️ No baseline SBOM files found (target image manifest unavailable)") + else + target_summary+=(" - ℹ️ Target baseline not specified") + fi else for f in "${target_sboms[@]}"; do arch=$(basename "$f" .spdx.json | sed 's/sbom-//') @@ -512,31 +534,33 @@ runs: echo "================================================================================" echo "" - # 4. Enforce Gating: Both must have succeeded with valid reports + # 4. Enforce Gating has_error=false if [ "$head_ok" != "true" ] || [ "$head_report_count" -eq 0 ]; then echo "::error::Head image CVE analysis failed or produced no reports." has_error=true fi - if [ "$target_ok" != "true" ] || [ "$target_report_count" -eq 0 ]; then - echo "::error::Target baseline CVE analysis failed or produced no reports." - has_error=true - fi - if [ "$has_error" = "true" ]; then - echo "::error::Aborting differential analysis: both head and target scans must succeed before diffing." + echo "::error::Aborting analysis: head scan must succeed." exit 1 fi - # 5. Run multi-arch set-based diff and generate consolidated full summary report - echo "Both head and target scans succeeded. Computing set-based vulnerability delta..." + # 5. Run report generation (differential if target available, otherwise standalone summary) + if [ "$target_ok" = "true" ] && [ "$target_report_count" -gt 0 ]; then + echo "Both head and target scans succeeded. Computing set-based vulnerability delta..." + target_path="cve-reports/target" + else + echo "Target baseline report unavailable. Generating standalone full vulnerability report..." + target_path="--none" + fi + if ! node "${{ github.action_path }}/cve-diff.js" \ - "cve-reports/target" \ + "$target_path" \ "cve-reports/head" \ "${{ inputs.target-branch }}" \ "${{ inputs.output-summary }}" > "${{ inputs.output-delta }}"; then - echo "::error::Failed to generate CVE differential report" + echo "::error::Failed to generate CVE report" exit 1 fi diff --git a/cve-diff.js b/cve-diff.js index b0cac43..25dcda8 100755 --- a/cve-diff.js +++ b/cve-diff.js @@ -344,16 +344,30 @@ function main() { const baseName = args[2] || 'main'; const summaryOutputPath = args[3]; - const baseCves = loadReportsForArg(baseArg); const branchCves = loadReportsForArg(branchArg); - const output = renderMarkdownDiff(baseCves, branchCves, baseName); - console.log(output); - if (summaryOutputPath) { const summaryOutput = renderFullSummary(branchCves); fs.writeFileSync(summaryOutputPath, summaryOutput, 'utf8'); } + + if (baseArg === '--none' || baseArg === 'none' || baseArg === '') { + // No baseline provided: output full summary as diff report + const lines = [ + `### 🛡️ Vulnerability Report (\`${baseName}\` baseline unavailable)`, + '', + `> [!NOTE]`, + `> Baseline target image for \`${baseName}\` was not found in the registry. Showing all detected vulnerabilities.`, + '', + ]; + lines.push(renderFullSummary(branchCves).replace(/^### 🛡️ Security Scan Report\n*/, '')); + console.log(lines.join('\n')); + return; + } + + const baseCves = loadReportsForArg(baseArg); + const output = renderMarkdownDiff(baseCves, branchCves, baseName); + console.log(output); } if (require.main === module) { diff --git a/test/cve-diff.test.js b/test/cve-diff.test.js index 0b07766..5321011 100644 --- a/test/cve-diff.test.js +++ b/test/cve-diff.test.js @@ -109,3 +109,15 @@ test('formatVulnRow properly formats badge, architecture tag, and escape pipes', const rowWithoutArch = formatVulnRow(vuln, allArchs, false); assert.ok(!rowWithoutArch.includes('`amd64`')); }); + +test('cve-diff CLI handles --none baseline gracefully and prints full summary', () => { + const { execFileSync } = require('node:child_process'); + const headDir = path.join(FIXTURES_DIR, 'head'); + const scriptPath = path.join(__dirname, '..', 'cve-diff.js'); + + const stdout = execFileSync('node', [scriptPath, '--none', headDir, 'main'], { encoding: 'utf8' }); + assert.ok(stdout.includes('baseline unavailable')); + assert.ok(stdout.includes('Total Vulnerabilities')); + assert.ok(stdout.includes('CVE-2023-0002')); + assert.ok(stdout.includes('CVE-2023-0003')); +});