Skip to content

Commit 8503ad3

Browse files
committed
severity and cvss metrics support.
1 parent 090c04e commit 8503ad3

File tree

6 files changed

+91
-3
lines changed

6 files changed

+91
-3
lines changed

client/client_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ import (
1919

2020
var testVuln1 string = `[
2121
{"ID":"ID1","Package":{"Name":"golang.org/example/one","Ecosystem":"go"}, "Summary":"",
22-
"Severity":2,"Affects":{"Ranges":[{"Type":"SEMVER","Introduced":"","Fixed":"v2.2.0"}]},
22+
"Severity":"High","Affects":{"Ranges":[{"Type":"SEMVER","Introduced":"","Fixed":"v2.2.0"}]},
2323
"ecosystem_specific":{"Symbols":["some_symbol_1"]
2424
}}]`
2525

2626
var testVuln2 string = `[
2727
{"ID":"ID2","Package":{"Name":"golang.org/example/two","Ecosystem":"go"}, "Summary":"",
28-
"Severity":2,"Affects":{"Ranges":[{"Type":"SEMVER","Introduced":"","Fixed":"v2.1.0"}]},
28+
"Severity":"High","Affects":{"Ranges":[{"Type":"SEMVER","Introduced":"","Fixed":"v2.1.0"}]},
2929
"ecosystem_specific":{"Symbols":["some_symbol_2"]
3030
}}]`
3131

osv/json.go

+2
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ type Entry struct {
122122
ID string `json:"id"`
123123
Published time.Time `json:"published"`
124124
Modified time.Time `json:"modified"`
125+
Severity string `json:"severity,omitempty"`
125126
Withdrawn *time.Time `json:"withdrawn,omitempty"`
126127
Aliases []string `json:"aliases,omitempty"`
127128
Package Package `json:"package"`
@@ -144,6 +145,7 @@ func Generate(id string, url string, r report.Report) []Entry {
144145
ID: id,
145146
Published: r.Published,
146147
Modified: lastModified,
148+
Severity: report.CvssScoreToSeverity(r.CVEMetadata.CVSSMeta),
147149
Withdrawn: r.Withdrawn,
148150
Package: Package{
149151
Name: importPath,

osv/json_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func TestGenerate(t *testing.T) {
4343
Commit: "commit",
4444
Context: []string{"issue-a", "issue-b"},
4545
},
46+
CVEMetadata:&report.CVEMeta{ID: "CVE-2020-1234"},
4647
}
4748

4849
want := []Entry{

report/report.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@ type Links struct {
2727
type CVEMeta struct {
2828
ID string `yaml:",omitempty"`
2929
CWE string `yaml:",omitempty"`
30-
Description string `yaml:",omitempty"`
30+
Description string `yaml:",omitempty"`
31+
CVSSMeta *CVSS `yaml:",omitempty"`
32+
}
33+
type CVSS struct {
34+
Version string `yaml:",omitempty"`
35+
BaseScore float32 `yaml:",omitempty"`
36+
Vector string `yaml:",omitempty"`
3137
}
3238

3339
type Report struct {
@@ -59,3 +65,4 @@ type Report struct {
5965
Links Links `yaml:",omitempty"`
6066
CVEMetadata *CVEMeta `yaml:"cve_metadata,omitempty"`
6167
}
68+

report/utils.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package report
2+
3+
//CvssScoreToSeverity calculate severity by cvss version and score
4+
//accept cvss version and score , return severity
5+
func CvssScoreToSeverity(cvss *CVSS) string {
6+
if cvss == nil {
7+
return ""
8+
}
9+
switch cvss.Version {
10+
case "v2":
11+
return cvssV2SeverityByScore(cvss.BaseScore)
12+
case "v3":
13+
return cvssV3SeverityByScore(cvss.BaseScore)
14+
default:
15+
return ""
16+
}
17+
}
18+
19+
func cvssV3SeverityByScore(score float32) string {
20+
switch {
21+
case score == 0.0:
22+
return "None"
23+
case score >= 0.1 && score <= 3.9:
24+
return "Low"
25+
case score >= 4.0 && score <= 6.9:
26+
return "Medium"
27+
case score >= 7.0 && score <= 8.9:
28+
return "High"
29+
case score >= 9.0 && score <= 10.0:
30+
return "Critical"
31+
default:
32+
return ""
33+
}
34+
}
35+
36+
func cvssV2SeverityByScore(score float32) string {
37+
switch {
38+
case score >= 0.0 && score <= 3.9:
39+
return "Low"
40+
case score >= 4.0 && score <= 6.9:
41+
return "Medium"
42+
case score >= 7.0 && score <= 10.0:
43+
return "High"
44+
default:
45+
return "None"
46+
}
47+
}

report/utils_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package report
2+
3+
import "testing"
4+
5+
func TestReverseString1(t *testing.T) {
6+
tests := []struct {
7+
name string
8+
version string
9+
baseScore float32
10+
want string
11+
}{
12+
{name: "Low v2", version: "v2", baseScore: 1.0, want: "Low"},
13+
{name: "Medium v2", version: "v2", baseScore: 4.0, want: "Medium"},
14+
{name: "High v2", version: "v2", baseScore: 7.0, want: "High"},
15+
{name: "Non Existing score v2", version: "v2", baseScore: 12.0, want: ""},
16+
{name: "None v3", version: "v3", baseScore: 0.0, want: "None"},
17+
{name: "low v3", version: "v3", baseScore: 1.0, want: "Low"},
18+
{name: "Medium v3", version: "v3", baseScore: 4.0, want: "Medium"},
19+
{name: "High v3", version: "v3", baseScore: 7.0, want: "High"},
20+
{name: "Critical v3", version: "v3", baseScore: 9.0, want: "Critical"},
21+
{name: "Non Existing score v3", version: "v3", baseScore: 12.0, want: ""},
22+
{name: "Non existing version", version: "v1", baseScore: 9.0, want: ""},
23+
}
24+
for _, tt := range tests {
25+
t.Run(tt.name, func(t *testing.T) {
26+
if got := CvssScoreToSeverity(&CVSS{Version: tt.version, BaseScore: tt.baseScore}); got != tt.want {
27+
t.Errorf("CvssScoreToSeverity() = %v, want %v", got, tt.want)
28+
}
29+
})
30+
}
31+
}

0 commit comments

Comments
 (0)