Skip to content

Commit 3d388d8

Browse files
authoredMay 14, 2024··
fix(report): hide empty tables if all vulns has been filtered (#6352)
1 parent fa3cf99 commit 3d388d8

File tree

2 files changed

+115
-4
lines changed

2 files changed

+115
-4
lines changed
 

‎pkg/report/table/vulnerability.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,16 @@ func NewVulnerabilityRenderer(result types.Result, isTerminal, tree, suppressed
5252
}
5353

5454
func (r *vulnerabilityRenderer) Render() string {
55-
r.renderDetectedVulnerabilities()
56-
57-
if r.tree {
58-
r.renderDependencyTree()
55+
// There are 3 cases when we show the vulnerability table (or only target and `Total: 0...`):
56+
// When Result contains vulnerabilities;
57+
// When Result target is OS packages even if no vulnerabilities are found;
58+
// When we show non-empty `Suppressed Vulnerabilities` table.
59+
if len(r.result.Vulnerabilities) > 0 || r.result.Class == types.ClassOSPkg || (r.showSuppressed && len(r.result.ModifiedFindings) > 0) {
60+
r.renderDetectedVulnerabilities()
61+
62+
if r.tree {
63+
r.renderDependencyTree()
64+
}
5965
}
6066

6167
if r.showSuppressed {

‎pkg/report/table/vulnerability_test.go

+105
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,111 @@ Suppressed Vulnerabilities (Total: 1)
396396
├─────────┼───────────────┼──────────┼─────────┼─────────────────┼───────────────────┤
397397
│ bar │ CVE-2020-0002 │ MEDIUM │ ignored │ Not exploitable │ .trivyignore.yaml │
398398
└─────────┴───────────────┴──────────┴─────────┴─────────────────┴───────────────────┘
399+
`,
400+
},
401+
{
402+
name: "suppressed all OS package vulnerabilities without `showSuppressed` flag",
403+
result: types.Result{
404+
Target: "test",
405+
Class: types.ClassOSPkg,
406+
Type: ftypes.Alpine,
407+
ModifiedFindings: []types.ModifiedFinding{
408+
{
409+
Type: types.FindingTypeVulnerability,
410+
Status: types.FindingStatusIgnored,
411+
Statement: "Not exploitable",
412+
Source: ".trivyignore.yaml",
413+
Finding: types.DetectedVulnerability{
414+
VulnerabilityID: "CVE-2020-0001",
415+
PkgName: "foo",
416+
InstalledVersion: "1.2.3",
417+
Status: dbTypes.StatusWillNotFix,
418+
Vulnerability: dbTypes.Vulnerability{
419+
Title: "title1",
420+
Description: "desc1",
421+
Severity: "MEDIUM",
422+
},
423+
},
424+
},
425+
},
426+
},
427+
showSuppressed: false,
428+
want: `
429+
test
430+
====
431+
Total: 0 (MEDIUM: 0, HIGH: 0)
432+
433+
`,
434+
},
435+
{
436+
name: "suppressed all language package vulnerabilities without `showSuppressed` flag",
437+
result: types.Result{
438+
Target: "test",
439+
Class: types.ClassLangPkg,
440+
Type: ftypes.Jar,
441+
ModifiedFindings: []types.ModifiedFinding{
442+
{
443+
Type: types.FindingTypeVulnerability,
444+
Status: types.FindingStatusIgnored,
445+
Statement: "Not exploitable",
446+
Source: ".trivyignore.yaml",
447+
Finding: types.DetectedVulnerability{
448+
VulnerabilityID: "CVE-2020-0001",
449+
PkgName: "foo",
450+
InstalledVersion: "1.2.3",
451+
Status: dbTypes.StatusWillNotFix,
452+
Vulnerability: dbTypes.Vulnerability{
453+
Title: "title1",
454+
Description: "desc1",
455+
Severity: "MEDIUM",
456+
},
457+
},
458+
},
459+
},
460+
},
461+
showSuppressed: false,
462+
want: ``,
463+
},
464+
{
465+
name: "suppressed all language package vulnerabilities with `showSuppressed` flag",
466+
result: types.Result{
467+
Target: "test",
468+
Class: types.ClassLangPkg,
469+
Type: ftypes.Jar,
470+
ModifiedFindings: []types.ModifiedFinding{
471+
{
472+
Type: types.FindingTypeVulnerability,
473+
Status: types.FindingStatusIgnored,
474+
Statement: "Not exploitable",
475+
Source: ".trivyignore.yaml",
476+
Finding: types.DetectedVulnerability{
477+
VulnerabilityID: "CVE-2020-0001",
478+
PkgName: "foo",
479+
InstalledVersion: "1.2.3",
480+
Status: dbTypes.StatusWillNotFix,
481+
Vulnerability: dbTypes.Vulnerability{
482+
Title: "title1",
483+
Description: "desc1",
484+
Severity: "MEDIUM",
485+
},
486+
},
487+
},
488+
},
489+
},
490+
showSuppressed: true,
491+
want: `
492+
test (jar)
493+
==========
494+
Total: 0 (MEDIUM: 0, HIGH: 0)
495+
496+
497+
Suppressed Vulnerabilities (Total: 1)
498+
=====================================
499+
┌─────────┬───────────────┬──────────┬─────────┬─────────────────┬───────────────────┐
500+
│ Library │ Vulnerability │ Severity │ Status │ Statement │ Source │
501+
├─────────┼───────────────┼──────────┼─────────┼─────────────────┼───────────────────┤
502+
│ foo │ CVE-2020-0001 │ MEDIUM │ ignored │ Not exploitable │ .trivyignore.yaml │
503+
└─────────┴───────────────┴──────────┴─────────┴─────────────────┴───────────────────┘
399504
`,
400505
},
401506
}

0 commit comments

Comments
 (0)
Please sign in to comment.