Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

severity and cvss metrics support #6

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ import (

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

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

Expand Down
2 changes: 2 additions & 0 deletions osv/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ type Entry struct {
ID string `json:"id"`
Published time.Time `json:"published"`
Modified time.Time `json:"modified"`
Severity string `json:"severity,omitempty"`
Withdrawn *time.Time `json:"withdrawn,omitempty"`
Aliases []string `json:"aliases,omitempty"`
Package Package `json:"package"`
Expand All @@ -144,6 +145,7 @@ func Generate(id string, url string, r report.Report) []Entry {
ID: id,
Published: r.Published,
Modified: lastModified,
Severity: report.CvssScoreToSeverity(r.CVEMetadata.CVSSMeta),
Withdrawn: r.Withdrawn,
Package: Package{
Name: importPath,
Expand Down
1 change: 1 addition & 0 deletions osv/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TestGenerate(t *testing.T) {
Commit: "commit",
Context: []string{"issue-a", "issue-b"},
},
CVEMetadata: &report.CVEMeta{ID: "CVE-2020-1234"},
}

want := []Entry{
Expand Down
6 changes: 6 additions & 0 deletions report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ type CVEMeta struct {
ID string `yaml:",omitempty"`
CWE string `yaml:",omitempty"`
Description string `yaml:",omitempty"`
CVSSMeta *CVSS `yaml:",omitempty"`
}
type CVSS struct {
Version string `yaml:",omitempty"`
Score float32 `yaml:",omitempty"`
Vector string `yaml:",omitempty"`
}

type Report struct {
Expand Down
47 changes: 47 additions & 0 deletions report/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package report

//CvssScoreToSeverity calculate severity by cvss version and score
//accept cvss version and score , return severity
func CvssScoreToSeverity(cvss *CVSS) string {
if cvss == nil {
return ""
}
switch cvss.Version {
case "v2":
return cvssV2SeverityByScore(cvss.Score)
case "v3":
return cvssV3SeverityByScore(cvss.Score)
default:
return ""
}
}

func cvssV3SeverityByScore(score float32) string {
switch {
case score == 0.0:
return "None"
case score >= 0.1 && score <= 3.9:
return "Low"
case score >= 4.0 && score <= 6.9:
return "Medium"
case score >= 7.0 && score <= 8.9:
return "High"
case score >= 9.0 && score <= 10.0:
return "Critical"
default:
return ""
}
}

func cvssV2SeverityByScore(score float32) string {
switch {
case score >= 0.0 && score <= 3.9:
return "Low"
case score >= 4.0 && score <= 6.9:
return "Medium"
case score >= 7.0 && score <= 10.0:
return "High"
default:
return ""
}
}
31 changes: 31 additions & 0 deletions report/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package report

import "testing"

func TestReverseString1(t *testing.T) {
tests := []struct {
name string
version string
baseScore float32
want string
}{
{name: "Low v2", version: "v2", baseScore: 1.0, want: "Low"},
{name: "Medium v2", version: "v2", baseScore: 4.0, want: "Medium"},
{name: "High v2", version: "v2", baseScore: 7.0, want: "High"},
{name: "Non Existing score v2", version: "v2", baseScore: 12.0, want: ""},
{name: "None v3", version: "v3", baseScore: 0.0, want: "None"},
{name: "low v3", version: "v3", baseScore: 1.0, want: "Low"},
{name: "Medium v3", version: "v3", baseScore: 4.0, want: "Medium"},
{name: "High v3", version: "v3", baseScore: 7.0, want: "High"},
{name: "Critical v3", version: "v3", baseScore: 9.0, want: "Critical"},
{name: "Non Existing score v3", version: "v3", baseScore: 12.0, want: ""},
{name: "Non existing version", version: "v1", baseScore: 9.0, want: ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := CvssScoreToSeverity(&CVSS{Version: tt.version, Score: tt.baseScore}); got != tt.want {
t.Errorf("CvssScoreToSeverity() = %v, want %v", got, tt.want)
}
})
}
}