Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit c1ce655

Browse files
committed
Better CVE rendering
Signed-off-by: Christian Dupuis <[email protected]>
1 parent b6c861d commit c1ce655

File tree

2 files changed

+84
-65
lines changed

2 files changed

+84
-65
lines changed

sbom/diff.go

Lines changed: 1 addition & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -234,67 +234,6 @@ type CveEntry struct {
234234

235235
type CveMap map[string]CveEntry
236236

237-
func colorizeSeverity(severity string) string {
238-
switch severity {
239-
case "CRITICAL":
240-
return defaultColors.critical.Sprintf(severity)
241-
case "HIGH":
242-
return defaultColors.high.Sprintf(severity)
243-
case "MEDIUM":
244-
return defaultColors.medium.Sprintf(severity)
245-
case "LOW":
246-
return defaultColors.low.Sprintf(severity)
247-
default:
248-
return severity
249-
}
250-
}
251-
252-
func toSeverity(cve types.Cve) string {
253-
findSeverity := func(adv *types.Advisory) (string, bool) {
254-
if adv == nil {
255-
return "", false
256-
}
257-
for _, r := range (*adv).References {
258-
if r.Source == "atomist" {
259-
for _, s := range r.Scores {
260-
if s.Type == "atm_severity" {
261-
v := s.Value
262-
if v != "SEVERITY_UNSPECIFIED" {
263-
return v, true
264-
}
265-
}
266-
}
267-
}
268-
}
269-
return "", false
270-
}
271-
272-
if severity, ok := findSeverity(cve.Cve); ok {
273-
return severity
274-
}
275-
if severity, ok := findSeverity(cve.Advisory); ok {
276-
return severity
277-
}
278-
279-
return "IN TRIAGE"
280-
}
281-
282-
func toSeverityInt(cve types.Cve) int {
283-
severity := toSeverity(cve)
284-
switch severity {
285-
case "CRITICAL":
286-
return 4
287-
case "HIGH":
288-
return 3
289-
case "MEDIUM":
290-
return 2
291-
case "LOW":
292-
return 1
293-
default:
294-
return 0
295-
}
296-
}
297-
298237
func diffCves(result1, result2 ImageIndexResult) {
299238
dc := 0
300239
cves := make(CveMap)
@@ -371,7 +310,7 @@ func diffCves(result1, result2 ImageIndexResult) {
371310
} else if len(c1) == 0 {
372311
cl = defaultColors.added.Sprintf(k)
373312
}
374-
t.AppendRow(table.Row{k, toSeverityInt(cve), cl, colorizeSeverity(toSeverity(cve)), strings.Join(c1, "\n"), strings.Join(c2, "\n")})
313+
t.AppendRow(table.Row{k, types.ToSeverityInt(cve), cl, types.ColorizeSeverity(types.ToSeverity(cve)), strings.Join(c1, "\n"), strings.Join(c2, "\n")})
375314
dc++
376315
}
377316
}

types/format.go

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,13 @@ func RenderVulnerabilities(image *Image) string {
204204
}
205205

206206
func FormatCve(sb *Sbom, c *Cve) {
207+
sourceId := c.SourceId
208+
if c.Cve != nil {
209+
sourceId = c.Cve.SourceId
210+
}
207211
fmt.Println("")
208-
fmt.Println(fmt.Sprintf("More information https://dso.docker.com/cve/%s", c.SourceId))
209-
fmt.Println("")
210-
fmt.Println(defaultColors.underline.Sprintf(fmt.Sprintf("Detected %s in", c.SourceId)))
212+
fmt.Println(defaultColors.underline.Sprintf(fmt.Sprintf("Detected %s %s", sourceId, ColorizeSeverity(ToSeverity(*c)))))
213+
fmt.Println(fmt.Sprintf("https://dso.docker.com/cve/%s", sourceId))
211214
fmt.Println("")
212215
purl := c.Purl
213216
for _, p := range sb.Artifacts {
@@ -263,3 +266,80 @@ RUN apt-get update && apt-get install -y \\
263266
}
264267
return ""
265268
}
269+
270+
func ColorizeStringBySeverity(value string, severity string) string {
271+
switch severity {
272+
case "CRITICAL":
273+
return defaultColors.critical.Sprintf(value)
274+
case "HIGH":
275+
return defaultColors.high.Sprintf(value)
276+
case "MEDIUM":
277+
return defaultColors.medium.Sprintf(value)
278+
case "LOW":
279+
return defaultColors.low.Sprintf(value)
280+
default:
281+
return value
282+
}
283+
}
284+
285+
func ColorizeSeverity(severity string) string {
286+
label := fmt.Sprintf(" %s ", strings.TrimSpace(severity))
287+
switch severity {
288+
case "CRITICAL":
289+
return defaultColors.critical.Sprintf(label)
290+
case "HIGH":
291+
return defaultColors.high.Sprintf(label)
292+
case "MEDIUM":
293+
return defaultColors.medium.Sprintf(label)
294+
case "LOW":
295+
return defaultColors.low.Sprintf(label)
296+
default:
297+
return severity
298+
}
299+
}
300+
301+
func ToSeverity(cve Cve) string {
302+
findSeverity := func(adv *Advisory) (string, bool) {
303+
if adv == nil {
304+
return "", false
305+
}
306+
for _, r := range (*adv).References {
307+
if r.Source == "atomist" {
308+
for _, s := range r.Scores {
309+
if s.Type == "atm_severity" {
310+
v := s.Value
311+
if v != "SEVERITY_UNSPECIFIED" {
312+
return v, true
313+
}
314+
}
315+
}
316+
}
317+
}
318+
return "", false
319+
}
320+
321+
if severity, ok := findSeverity(cve.Cve); ok {
322+
return severity
323+
}
324+
if severity, ok := findSeverity(cve.Advisory); ok {
325+
return severity
326+
}
327+
328+
return "IN TRIAGE"
329+
}
330+
331+
func ToSeverityInt(cve Cve) int {
332+
severity := ToSeverity(cve)
333+
switch severity {
334+
case "CRITICAL":
335+
return 4
336+
case "HIGH":
337+
return 3
338+
case "MEDIUM":
339+
return 2
340+
case "LOW":
341+
return 1
342+
default:
343+
return 0
344+
}
345+
}

0 commit comments

Comments
 (0)