fix: extract osv package string literals to constants
goconst counts occurrences across the whole package (including test files) when deciding whether to flag a non-test file — the exclusion only suppresses reports FROM test files. Add unexported constants for severity levels, CVSS types, ecosystem, and reference types in client.go, and update client_test.go to use them so the total raw-string count per literal drops below threshold.
This commit is contained in:
parent
25671b4f9d
commit
7d69339413
|
|
@ -43,6 +43,16 @@ const (
|
||||||
vulnEndpoint = "https://api.osv.dev/v1/vulns/"
|
vulnEndpoint = "https://api.osv.dev/v1/vulns/"
|
||||||
userAgent = "angela/0.1.0 (https://github.com/CarterPerez-dev/angela)"
|
userAgent = "angela/0.1.0 (https://github.com/CarterPerez-dev/angela)"
|
||||||
maxHydrate = 15
|
maxHydrate = 15
|
||||||
|
|
||||||
|
ecosystemPyPI = "PyPI"
|
||||||
|
cvssTypeV3 = "CVSS_V3"
|
||||||
|
cvssTypeV4 = "CVSS_V4"
|
||||||
|
sevCritical = "CRITICAL"
|
||||||
|
sevHigh = "HIGH"
|
||||||
|
sevModerate = "MODERATE"
|
||||||
|
sevLow = "LOW"
|
||||||
|
refTypeWeb = "WEB"
|
||||||
|
refTypeReport = "REPORT"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Client queries the OSV.dev API for known vulnerabilities
|
// Client queries the OSV.dev API for known vulnerabilities
|
||||||
|
|
@ -161,7 +171,7 @@ func (c *Client) queryBatch(
|
||||||
queries := make([]query, len(packages))
|
queries := make([]query, len(packages))
|
||||||
for i, p := range packages {
|
for i, p := range packages {
|
||||||
queries[i] = query{
|
queries[i] = query{
|
||||||
Package: pkg{Name: p.Name, Ecosystem: "PyPI"},
|
Package: pkg{Name: p.Name, Ecosystem: ecosystemPyPI},
|
||||||
Version: p.Version,
|
Version: p.Version,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -346,7 +356,7 @@ func toVulnerability(v *osvVuln) types.Vulnerability {
|
||||||
|
|
||||||
func extractSeverity(v *osvVuln) string {
|
func extractSeverity(v *osvVuln) string {
|
||||||
for _, s := range v.Severity {
|
for _, s := range v.Severity {
|
||||||
if s.Type != "CVSS_V3" && s.Type != "CVSS_V4" {
|
if s.Type != cvssTypeV3 && s.Type != cvssTypeV4 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -374,13 +384,13 @@ func extractSeverity(v *osvVuln) string {
|
||||||
func classifyScore(score float64) string {
|
func classifyScore(score float64) string {
|
||||||
switch {
|
switch {
|
||||||
case score >= 9.0:
|
case score >= 9.0:
|
||||||
return "CRITICAL"
|
return sevCritical
|
||||||
case score >= 7.0:
|
case score >= 7.0:
|
||||||
return "HIGH"
|
return sevHigh
|
||||||
case score >= 4.0:
|
case score >= 4.0:
|
||||||
return "MODERATE"
|
return sevModerate
|
||||||
case score > 0:
|
case score > 0:
|
||||||
return "LOW"
|
return sevLow
|
||||||
default:
|
default:
|
||||||
return "NONE"
|
return "NONE"
|
||||||
}
|
}
|
||||||
|
|
@ -388,7 +398,7 @@ func classifyScore(score float64) string {
|
||||||
|
|
||||||
func extractFixed(affected []affected) string {
|
func extractFixed(affected []affected) string {
|
||||||
for _, a := range affected {
|
for _, a := range affected {
|
||||||
if !strings.EqualFold(a.Package.Ecosystem, "PyPI") {
|
if !strings.EqualFold(a.Package.Ecosystem, ecosystemPyPI) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for _, r := range a.Ranges {
|
for _, r := range a.Ranges {
|
||||||
|
|
@ -409,7 +419,7 @@ func extractLink(refs []reference) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, r := range refs {
|
for _, r := range refs {
|
||||||
if r.Type == "WEB" || r.Type == "REPORT" {
|
if r.Type == refTypeWeb || r.Type == refTypeReport {
|
||||||
return r.URL
|
return r.URL
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,11 +24,11 @@ func TestExtractSeverityCVSSV3(t *testing.T) {
|
||||||
|
|
||||||
v := &osvVuln{
|
v := &osvVuln{
|
||||||
Severity: []severity{
|
Severity: []severity{
|
||||||
{Type: "CVSS_V3", Score: "9.8"},
|
{Type: cvssTypeV3, Score: "9.8"},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
got := extractSeverity(v)
|
got := extractSeverity(v)
|
||||||
if got != "CRITICAL" { //nolint:goconst
|
if got != sevCritical {
|
||||||
t.Errorf("extractSeverity = %q, want CRITICAL", got)
|
t.Errorf("extractSeverity = %q, want CRITICAL", got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -38,11 +38,11 @@ func TestExtractSeverityCVSSV4(t *testing.T) {
|
||||||
|
|
||||||
v := &osvVuln{
|
v := &osvVuln{
|
||||||
Severity: []severity{
|
Severity: []severity{
|
||||||
{Type: "CVSS_V4", Score: "7.5"},
|
{Type: cvssTypeV4, Score: "7.5"},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
got := extractSeverity(v)
|
got := extractSeverity(v)
|
||||||
if got != "HIGH" {
|
if got != sevHigh {
|
||||||
t.Errorf("extractSeverity = %q, want HIGH", got)
|
t.Errorf("extractSeverity = %q, want HIGH", got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -53,13 +53,13 @@ func TestExtractSeverityCVSSVector(t *testing.T) {
|
||||||
v := &osvVuln{
|
v := &osvVuln{
|
||||||
Severity: []severity{
|
Severity: []severity{
|
||||||
{
|
{
|
||||||
Type: "CVSS_V3",
|
Type: cvssTypeV3,
|
||||||
Score: "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H/9.8",
|
Score: "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H/9.8",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
got := extractSeverity(v)
|
got := extractSeverity(v)
|
||||||
if got != "CRITICAL" {
|
if got != sevCritical {
|
||||||
t.Errorf("extractSeverity = %q, want CRITICAL", got)
|
t.Errorf("extractSeverity = %q, want CRITICAL", got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -69,11 +69,11 @@ func TestExtractSeverityDatabaseSpecific(t *testing.T) {
|
||||||
|
|
||||||
v := &osvVuln{
|
v := &osvVuln{
|
||||||
DatabaseSpecific: map[string]any{
|
DatabaseSpecific: map[string]any{
|
||||||
"severity": "MODERATE",
|
"severity": sevModerate,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
got := extractSeverity(v)
|
got := extractSeverity(v)
|
||||||
if got != "MODERATE" {
|
if got != sevModerate {
|
||||||
t.Errorf("extractSeverity = %q, want MODERATE", got)
|
t.Errorf("extractSeverity = %q, want MODERATE", got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -95,14 +95,14 @@ func TestClassifyScore(t *testing.T) {
|
||||||
score float64
|
score float64
|
||||||
want string
|
want string
|
||||||
}{
|
}{
|
||||||
{9.8, "CRITICAL"},
|
{9.8, sevCritical},
|
||||||
{9.0, "CRITICAL"},
|
{9.0, sevCritical},
|
||||||
{8.5, "HIGH"},
|
{8.5, sevHigh},
|
||||||
{7.0, "HIGH"},
|
{7.0, sevHigh},
|
||||||
{6.9, "MODERATE"},
|
{6.9, sevModerate},
|
||||||
{4.0, "MODERATE"},
|
{4.0, sevModerate},
|
||||||
{3.9, "LOW"},
|
{3.9, sevLow},
|
||||||
{0.1, "LOW"},
|
{0.1, sevLow},
|
||||||
{0.0, "NONE"},
|
{0.0, "NONE"},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -154,7 +154,7 @@ func TestExtractFixed(t *testing.T) {
|
||||||
|
|
||||||
aff := []affected{
|
aff := []affected{
|
||||||
{
|
{
|
||||||
Package: pkg{Name: "django", Ecosystem: "PyPI"},
|
Package: pkg{Name: "django", Ecosystem: ecosystemPyPI},
|
||||||
Ranges: []rng{
|
Ranges: []rng{
|
||||||
{
|
{
|
||||||
Type: "ECOSYSTEM",
|
Type: "ECOSYSTEM",
|
||||||
|
|
@ -178,7 +178,7 @@ func TestExtractFixedNoFix(t *testing.T) {
|
||||||
|
|
||||||
aff := []affected{
|
aff := []affected{
|
||||||
{
|
{
|
||||||
Package: pkg{Name: "django", Ecosystem: "PyPI"},
|
Package: pkg{Name: "django", Ecosystem: ecosystemPyPI},
|
||||||
Ranges: []rng{
|
Ranges: []rng{
|
||||||
{
|
{
|
||||||
Type: "ECOSYSTEM",
|
Type: "ECOSYSTEM",
|
||||||
|
|
@ -198,7 +198,7 @@ func TestExtractLink(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
refs := []reference{
|
refs := []reference{
|
||||||
{Type: "WEB", URL: "https://example.com"},
|
{Type: refTypeWeb, URL: "https://example.com"},
|
||||||
{
|
{
|
||||||
Type: "ADVISORY",
|
Type: "ADVISORY",
|
||||||
URL: "https://nvd.nist.gov/vuln/detail/CVE-2023-1234",
|
URL: "https://nvd.nist.gov/vuln/detail/CVE-2023-1234",
|
||||||
|
|
@ -216,7 +216,7 @@ func TestExtractLinkFallback(t *testing.T) {
|
||||||
|
|
||||||
refs := []reference{
|
refs := []reference{
|
||||||
{Type: "PACKAGE", URL: "https://pypi.org/project/django"},
|
{Type: "PACKAGE", URL: "https://pypi.org/project/django"},
|
||||||
{Type: "WEB", URL: "https://example.com/info"},
|
{Type: refTypeWeb, URL: "https://example.com/info"},
|
||||||
}
|
}
|
||||||
|
|
||||||
got := extractLink(refs)
|
got := extractLink(refs)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue