fix: handle missing OS field in Trivy report - #1353
Conversation
Signed-off-by: robert-cronin <robert.owen.cronin@gmail.com>
There was a problem hiding this comment.
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.Historybefore extracting Node.js and Yarn versions - Added nil check for
Metadata.OSbefore 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 |
|
Run after fix: |
| "ArtifactType": "container_image", | ||
| "Metadata": { | ||
| "OS": { | ||
| "Family": "alpine", |
Signed-off-by: robert-cronin <robert.owen.cronin@gmail.com>
| // Initialize OS metadata with safe defaults | ||
| osType := "" | ||
| osVersion := "" |
There was a problem hiding this comment.
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.
| // 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 |
Codecov Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
Signed-off-by: robert-cronin <robert.owen.cronin@gmail.com>
Signed-off-by: robert-cronin <robert.owen.cronin@gmail.com> Signed-off-by: ashnamehrotra <ashnamehrotra@gmail.com>
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