-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvulns.go
More file actions
222 lines (197 loc) · 6.32 KB
/
Copy pathvulns.go
File metadata and controls
222 lines (197 loc) · 6.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Package vulns provides a unified interface for querying vulnerability data
// from various sources. It uses PURL (Package URL) as the primary identifier
// and OSV (Open Source Vulnerabilities) format as the canonical data model.
package vulns
import (
"context"
"time"
"github.com/git-pkgs/purl"
)
// Source represents a vulnerability data source.
type Source interface {
// Name returns the name of this source (e.g., "osv", "nvd").
Name() string
// Query returns vulnerabilities affecting the package identified by the PURL.
// If the PURL includes a version, only vulnerabilities affecting that version
// are returned. If no version is specified, all known vulnerabilities for the
// package are returned.
Query(ctx context.Context, p *purl.PURL) ([]Vulnerability, error)
// QueryBatch queries multiple packages at once. Returns a slice of results
// in the same order as the input PURLs.
QueryBatch(ctx context.Context, purls []*purl.PURL) ([][]Vulnerability, error)
// Get fetches a specific vulnerability by its ID.
Get(ctx context.Context, id string) (*Vulnerability, error)
}
// Vulnerability represents a security vulnerability in OSV format.
// This is the canonical format used across all sources.
type Vulnerability struct {
SchemaVersion string `json:"schema_version,omitempty"`
ID string `json:"id"`
Summary string `json:"summary,omitempty"`
Details string `json:"details,omitempty"`
Aliases []string `json:"aliases,omitempty"`
Related []string `json:"related,omitempty"`
Modified time.Time `json:"modified,omitzero"`
Published time.Time `json:"published,omitzero"`
Withdrawn *time.Time `json:"withdrawn,omitempty"`
References []Reference `json:"references,omitempty"`
Affected []Affected `json:"affected,omitempty"`
Severity []Severity `json:"severity,omitempty"`
Credits []Credit `json:"credits,omitempty"`
DatabaseSpecific map[string]any `json:"database_specific,omitempty"`
}
// Reference is a link to more information about a vulnerability.
type Reference struct {
Type string `json:"type"`
URL string `json:"url"`
}
// Affected describes which package versions are affected.
type Affected struct {
Package Package `json:"package,omitzero"`
Ranges []Range `json:"ranges,omitempty"`
Versions []string `json:"versions,omitempty"`
EcosystemSpecific map[string]any `json:"ecosystem_specific,omitempty"`
DatabaseSpecific map[string]any `json:"database_specific,omitempty"`
}
// Package identifies a package.
type Package struct {
Ecosystem string `json:"ecosystem"`
Name string `json:"name"`
PURL string `json:"purl,omitempty"`
}
// Range describes a version range.
type Range struct {
Type string `json:"type"`
Repo string `json:"repo,omitempty"`
Events []Event `json:"events,omitempty"`
}
// Event is a version event (introduced, fixed, etc).
type Event struct {
Introduced string `json:"introduced,omitempty"`
Fixed string `json:"fixed,omitempty"`
LastAffected string `json:"last_affected,omitempty"`
Limit string `json:"limit,omitempty"`
}
// Severity describes the severity of a vulnerability.
type Severity struct {
Type string `json:"type"`
Score string `json:"score"`
}
// Credit gives credit to vulnerability reporters/fixers.
type Credit struct {
Name string `json:"name"`
Contact []string `json:"contact,omitempty"`
Type string `json:"type,omitempty"`
}
// SeverityLevel returns a normalized severity level (critical, high, medium, low, unknown).
func (v *Vulnerability) SeverityLevel() string {
for _, sev := range v.Severity {
if cvss, err := CVSSFromSeverity(sev); err == nil {
return cvss.Level
}
}
if v.DatabaseSpecific != nil {
if severity, ok := v.DatabaseSpecific["severity"].(string); ok {
switch severity {
case "CRITICAL", "critical":
return LevelCritical
case "HIGH", "high":
return LevelHigh
case "MODERATE", "MEDIUM", "moderate", "medium":
return LevelMedium
case "LOW", "low":
return LevelLow
}
}
}
return "unknown"
}
// CVSSScore returns the highest CVSS score if available, or -1 if not.
func (v *Vulnerability) CVSSScore() float64 {
var highest float64 = -1
for _, sev := range v.Severity {
if cvss, err := CVSSFromSeverity(sev); err == nil && cvss.Score > highest {
highest = cvss.Score
}
}
return highest
}
// CVSS returns parsed CVSS information from the vulnerability's severity data.
// Returns nil if no CVSS information is available.
func (v *Vulnerability) CVSS() *CVSS {
for _, sev := range v.Severity {
if cvss, err := CVSSFromSeverity(sev); err == nil {
return cvss
}
}
return nil
}
// FixedVersion returns the first fixed version for the given package, if available.
func (v *Vulnerability) FixedVersion(ecosystem, name string) string {
for _, a := range v.Affected {
if !matchesPackage(a.Package, ecosystem, name) {
continue
}
for _, r := range a.Ranges {
for _, e := range r.Events {
if e.Fixed != "" {
return e.Fixed
}
}
}
}
return ""
}
// IsVersionAffected checks if a specific version of a package is affected.
func (v *Vulnerability) IsVersionAffected(ecosystem, name, version string) bool {
for _, a := range v.Affected {
if !matchesPackage(a.Package, ecosystem, name) {
continue
}
if isAffectedVersion(a, version) {
return true
}
}
return false
}
func matchesPackage(pkg Package, ecosystem, name string) bool {
pkgEco := purl.NormalizeEcosystem(pkg.Ecosystem)
checkEco := purl.NormalizeEcosystem(ecosystem)
return pkgEco == checkEco && pkg.Name == name
}
// scanFloat parses a float from the beginning of a string.
// Used internally for parsing bare CVSS scores.
func scanFloat(s string, v *float64) (int, error) {
const base = 10
var f float64
n := 0
i := 0
for ; i < len(s); i++ {
c := s[i]
if c < '0' || c > '9' {
break
}
f = f*base + float64(c-'0')
n++
}
if i < len(s) && s[i] == '.' {
n++
frac := 0.1
for i++; i < len(s); i++ {
c := s[i]
if c < '0' || c > '9' {
break
}
f += float64(c-'0') * frac
frac /= base
n++
}
}
if n == 0 {
return 0, &parseError{}
}
*v = f
return n, nil
}
type parseError struct{}
func (e *parseError) Error() string { return "parse error" }