Skip to content

fix: handle missing OS field in Trivy report - #1353

Merged
ashnamehrotra merged 2 commits into
project-copacetic:mainfrom
robert-cronin:fix/imageconfig-nil-dereference
Oct 24, 2025
Merged

fix: handle missing OS field in Trivy report#1353
ashnamehrotra merged 2 commits into
project-copacetic:mainfrom
robert-cronin:fix/imageconfig-nil-dereference

Conversation

@robert-cronin

Copy link
Copy Markdown
Contributor

Fixes panic when parsing Trivy reports that don't include ImageConfig history data. Added nil check before accessing History field to prevent segmentation violation.

Closes #1352

Signed-off-by: robert-cronin <robert.owen.cronin@gmail.com>
@robert-cronin robert-cronin self-assigned this Oct 23, 2025
Copilot AI review requested due to automatic review settings October 23, 2025 23:24
@robert-cronin robert-cronin added the bug Something isn't working label Oct 23, 2025

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a nil pointer dereference panic that occurs when parsing Trivy vulnerability reports that lack ImageConfig.History data. The fix adds defensive nil checks before accessing the History and OS fields.

Key changes:

  • Added nil check for ImageConfig.History before extracting Node.js and Yarn versions
  • Added nil check for Metadata.OS before accessing OS family and name
  • Added test case with corresponding test data to verify graceful handling of reports without history

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
pkg/report/trivy.go Added nil checks for ImageConfig.History and Metadata.OS fields to prevent panics
pkg/report/trivy_test.go Added test case to verify parser handles reports without ImageConfig.History
pkg/report/testdata/trivy_no_history.json Added test data representing a Trivy report without ImageConfig.History field

@robert-cronin

Copy link
Copy Markdown
Contributor Author

Run after fix:

❯ export IMAGE=quay.io/prometheus/alertmanager:v0.28.1
❯ trivy image --pkg-types os,library --ignore-unfixed -f json -o alertmanager-report.json $IMAGE
2025-10-23T23:25:20Z    INFO    [vuln] Vulnerability scanning is enabled
2025-10-23T23:25:20Z    INFO    [secret] Secret scanning is enabled
2025-10-23T23:25:20Z    INFO    [secret] If your scanning is slow, please try '--scanners vuln' to disable secret scanning
2025-10-23T23:25:20Z    INFO    [secret] Please see also https://trivy.dev/v0.60/docs/scanner/secret#recommendation for faster secret detection
2025-10-23T23:25:22Z    INFO    Number of language-specific files       num=2
2025-10-23T23:25:22Z    INFO    [gobinary] Detecting vulnerabilities...
2025-10-23T23:25:22Z    WARN    Using severities from other vendors for some vulnerabilities. Read https://trivy.dev/v0.60/docs/scanner/vulnerability#severity-selection for details.
❯ copa patch -r alertmanager-report.json -i $IMAGE
INFO[0000] Patched image name: quay.io/prometheus/alertmanager:v0.28.1-patched 
Error: no patchable vulnerabilities found in provided report for selected pkg-types (os)

"ArtifactType": "container_image",
"Metadata": {
"OS": {
"Family": "alpine",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you remove family too

Signed-off-by: robert-cronin <robert.owen.cronin@gmail.com>
@sozercan
sozercan requested a review from Copilot October 23, 2025 23:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

Comment thread pkg/report/trivy.go
Comment on lines +356 to +358
// Initialize OS metadata with safe defaults
osType := ""
osVersion := ""

Copilot AI Oct 23, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment mentions 'safe defaults' but empty strings may not be safe defaults for OS metadata. Consider clarifying whether empty values are intentionally treated as valid or if this represents a degraded state that should be handled differently downstream.

Suggested change
// Initialize OS metadata with safe defaults
osType := ""
osVersion := ""
// Initialize OS metadata with explicit "unknown" values to indicate missing or degraded state
const UnknownOSType = "unknown"
const UnknownOSVersion = "unknown"
osType := UnknownOSType
osVersion := UnknownOSVersion

Copilot uses AI. Check for mistakes.
@robert-cronin robert-cronin changed the title fix: handle nil ImageConfig.History in Trivy report parser fix: handle nil OS field in Trivy report parser Oct 23, 2025
@robert-cronin robert-cronin changed the title fix: handle nil OS field in Trivy report parser fix: handle missing OS field in Trivy report Oct 23, 2025
@codecov

codecov Bot commented Oct 24, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 80.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 39.47%. Comparing base (ff6330d) to head (15f2f99).
⚠️ Report is 5 commits behind head on main.

Files with missing lines Patch % Lines
pkg/report/trivy.go 80.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1353      +/-   ##
==========================================
+ Coverage   38.19%   39.47%   +1.27%     
==========================================
  Files          42       42              
  Lines        7673     5877    -1796     
==========================================
- Hits         2931     2320     -611     
+ Misses       4546     3361    -1185     
  Partials      196      196              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ashnamehrotra ashnamehrotra left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ashnamehrotra
ashnamehrotra merged commit eb4f9e4 into project-copacetic:main Oct 24, 2025
37 checks passed
@github-project-automation github-project-automation Bot moved this from 🆕 New to ✅ Done in Copacetic Workboard Oct 24, 2025
ashnamehrotra pushed a commit that referenced this pull request Oct 24, 2025
Signed-off-by: robert-cronin <robert.owen.cronin@gmail.com>
ashnamehrotra pushed a commit to ashnamehrotra/copacetic that referenced this pull request Oct 24, 2025
Signed-off-by: robert-cronin <robert.owen.cronin@gmail.com>
Signed-off-by: ashnamehrotra <ashnamehrotra@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

[BUG] panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0xcffebf]

4 participants