/
osv.go
382 lines (340 loc) · 11.2 KB
/
osv.go
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package osv
import (
"encoding/json"
"fmt"
"io"
"path/filepath"
"strings"
"time"
"github.com/goark/go-cvss/v3/metric"
"github.com/samber/lo"
bolt "go.etcd.io/bbolt"
"go.uber.org/zap"
"golang.org/x/exp/maps"
"golang.org/x/xerrors"
"github.com/khulnasoft-lab/vul-db/pkg/db"
"github.com/khulnasoft-lab/vul-db/pkg/log"
"github.com/khulnasoft-lab/vul-db/pkg/types"
"github.com/khulnasoft-lab/vul-db/pkg/utils"
"github.com/khulnasoft-lab/vul-db/pkg/vulnsrc/bucket"
"github.com/khulnasoft-lab/vul-db/pkg/vulnsrc/vulnerability"
)
type Advisory struct {
Ecosystem types.Ecosystem
PkgName string
VulnerabilityID string
Aliases []string
// Advisory detail
VulnerableVersions []string
PatchedVersions []string
// Vulnerability detail
Severity types.Severity
Title string
Description string
References []string
CVSSScoreV3 float64
CVSSVectorV3 string
}
type OSV struct {
dir string
dbc db.Operation
sourceID types.SourceID
dataSources map[types.Ecosystem]types.DataSource
transformer Transformer
}
type Transformer interface {
TransformAdvisories([]Advisory, Entry) ([]Advisory, error)
}
type defaultTransformer struct{}
func (t *defaultTransformer) TransformAdvisories(advs []Advisory, _ Entry) ([]Advisory, error) {
return advs, nil
}
func New(dir string, sourceID types.SourceID, dataSources map[types.Ecosystem]types.DataSource, transformer Transformer) OSV {
if transformer == nil {
transformer = &defaultTransformer{}
}
return OSV{
dir: dir,
dbc: db.Config{},
sourceID: sourceID,
dataSources: dataSources,
transformer: transformer,
}
}
func (o OSV) Name() types.SourceID {
return o.sourceID
}
func (o OSV) Update(root string) error {
rootDir := filepath.Join(root, o.dir)
var entries []Entry
err := utils.FileWalk(rootDir, func(r io.Reader, path string) error {
if filepath.Ext(path) != ".json" {
return nil
}
var entry Entry
if err := json.NewDecoder(r).Decode(&entry); err != nil {
return xerrors.Errorf("JSON decode error (%s): %w", path, err)
}
entries = append(entries, entry)
return nil
})
if err != nil {
return xerrors.Errorf("walk error: %w", err)
}
if err = o.save(entries); err != nil {
return xerrors.Errorf("save error: %w", err)
}
return nil
}
func (o OSV) save(entries []Entry) error {
err := o.dbc.BatchUpdate(func(tx *bolt.Tx) error {
for _, entry := range entries {
if err := o.commit(tx, entry); err != nil {
return err
}
}
return nil
})
if err != nil {
return xerrors.Errorf("batch update error: %w", err)
}
return nil
}
func (o OSV) commit(tx *bolt.Tx, entry Entry) error {
if entry.Withdrawn != nil && entry.Withdrawn.Before(time.Now()) {
return nil
}
// Group IDs into primary vulnerability IDs and aliases.
vulnIDs, aliases := groupVulnIDs(entry.ID, entry.Aliases)
references := lo.Map(entry.References, func(ref Reference, _ int) string {
return ref.URL
})
// Parse []affected
advisories, err := parseAffected(entry, vulnIDs, aliases, references)
if err != nil {
return xerrors.Errorf("failed to parse affected: %w", err)
}
// Transform advisories
advisories, err = o.transformer.TransformAdvisories(advisories, entry)
if err != nil {
return xerrors.Errorf("failed to transform advisories: %w", err)
}
for _, adv := range advisories {
dataSource, ok := o.dataSources[adv.Ecosystem]
if !ok {
continue
}
bktName := bucket.Name(adv.Ecosystem, dataSource.Name)
if err = o.dbc.PutDataSource(tx, bktName, dataSource); err != nil {
return xerrors.Errorf("failed to put data source: %w", err)
}
// Store advisories
advisory := types.Advisory{
VendorIDs: adv.Aliases,
VulnerableVersions: adv.VulnerableVersions,
PatchedVersions: adv.PatchedVersions,
}
if err = o.dbc.PutAdvisoryDetail(tx, adv.VulnerabilityID, adv.PkgName, []string{bktName}, advisory); err != nil {
return xerrors.Errorf("failed to save OSV advisory: %w", err)
}
// Store vulnerability details
vuln := types.VulnerabilityDetail{
Severity: adv.Severity,
References: adv.References,
Title: adv.Title,
Description: adv.Description,
CvssScoreV3: adv.CVSSScoreV3,
CvssVectorV3: adv.CVSSVectorV3,
}
if err = o.dbc.PutVulnerabilityDetail(tx, adv.VulnerabilityID, o.sourceID, vuln); err != nil {
return xerrors.Errorf("failed to put vulnerability detail (%s): %w", adv.VulnerabilityID, err)
}
if err = o.dbc.PutVulnerabilityID(tx, adv.VulnerabilityID); err != nil {
return xerrors.Errorf("failed to put vulnerability id (%s): %w", adv.VulnerabilityID, err)
}
}
return nil
}
func groupVulnIDs(id string, aliases []string) ([]string, []string) {
var cveIDs, nonCVEIDs []string
for _, a := range append(aliases, id) {
if strings.HasPrefix(a, "CVE-") {
cveIDs = append(cveIDs, a)
} else {
nonCVEIDs = append(nonCVEIDs, a)
}
}
if len(cveIDs) == 0 {
// Use the original vulnerability ID
// e.g. PYSEC-2021-335 and GHSA-wjx8-cgrm-hh8p
return []string{id}, aliases
}
return cveIDs, nonCVEIDs
}
// parseAffected parses the affected fields
// cf. https://ossf.github.io/osv-schema/#affected-fields
func parseAffected(entry Entry, vulnIDs, aliases, references []string) ([]Advisory, error) {
// Severities can be found both in severity and affected[].severity fields.
cvssVectorV3, cvssScoreV3, err := parseSeverity(entry.Severities)
if err != nil {
return nil, xerrors.Errorf("failed to decode CVSS vector (%s): %w", entry.ID, err)
}
uniqAdvisories := map[string]Advisory{}
for _, affected := range entry.Affected {
ecosystem := convertEcosystem(affected.Package.Ecosystem)
if ecosystem == vulnerability.Unknown {
continue
}
pkgName := vulnerability.NormalizePkgName(ecosystem, affected.Package.Name)
vulnerableVersions, patchedVersions, err := parseAffectedVersions(affected)
if err != nil {
return nil, xerrors.Errorf("failed to parse affected: %w", err)
}
// Parse affected[].severity
if vecV3, scoreV3, err := parseSeverity(affected.Severities); err != nil {
return nil, xerrors.Errorf("failed to decode CVSS vector (%s): %w", entry.ID, err)
} else if vecV3 != "" {
// Overwrite the CVSS vector and score if affected[].severity is set
cvssVectorV3, cvssScoreV3 = vecV3, scoreV3
}
key := fmt.Sprintf("%s/%s", ecosystem, pkgName)
for _, vulnID := range vulnIDs {
if adv, ok := uniqAdvisories[key]; ok {
// The same package could be repeated with different version ranges.
// cf. https://github.com/github/advisory-database/blob/0996f81ca6f1b65ba25f8e71fba263cb1e54ced5/advisories/github-reviewed/2019/12/GHSA-wjx8-cgrm-hh8p/GHSA-wjx8-cgrm-hh8p.json
adv.VulnerableVersions = append(adv.VulnerableVersions, vulnerableVersions...)
adv.PatchedVersions = append(adv.PatchedVersions, patchedVersions...)
uniqAdvisories[key] = adv
} else {
uniqAdvisories[key] = Advisory{
Ecosystem: ecosystem,
PkgName: pkgName,
VulnerabilityID: vulnID,
Aliases: aliases,
VulnerableVersions: vulnerableVersions,
PatchedVersions: patchedVersions,
Title: entry.Summary,
Description: entry.Details,
References: references,
CVSSVectorV3: cvssVectorV3,
CVSSScoreV3: cvssScoreV3,
}
}
}
}
return maps.Values(uniqAdvisories), nil
}
// parseAffectedVersions parses the affected.versions and affected.ranges fields
// cf.
// - https://ossf.github.io/osv-schema/#affectedversions-field
// - https://ossf.github.io/osv-schema/#affectedranges-field
func parseAffectedVersions(affected Affected) ([]string, []string, error) {
var patchedVersions, vulnerableVersions []string
var affectedRanges []VersionRange
for _, affects := range affected.Ranges {
if affects.Type == RangeTypeGit {
continue
}
var index int
for _, event := range affects.Events {
switch {
// Each "introduced" event implies a new version range
// e.g. {"introduced": "1.2.0"}, {"introduced": "2.2.0"}
case event.Introduced != "":
affectedRanges = append(affectedRanges, NewVersionRange(affected.Package.Ecosystem, event.Introduced))
index = len(affectedRanges) - 1
// e.g. {"introduced": "1.2.0"}, {"fixed": "1.2.5"}
case event.Fixed != "":
affectedRanges[index].SetFixed(event.Fixed)
patchedVersions = append(patchedVersions, event.Fixed)
// e.g. {"introduced": "1.2.0"}, {"last_affected": "1.2.5"}
case event.LastAffected != "":
affectedRanges[index].SetLastAffected(event.LastAffected)
}
}
}
for _, r := range affectedRanges {
vulnerableVersions = append(vulnerableVersions, r.String())
}
for _, v := range affected.Versions {
// We don't need to add the versions that are already included in the ranges
ok, err := versionContains(affectedRanges, v)
if err != nil {
log.Logger.Errorw("Version comparison error",
zap.String("ecosystem", string(affected.Package.Ecosystem)),
zap.String("package", affected.Package.Name),
zap.Error(err),
)
}
if !ok {
vulnerableVersions = append(vulnerableVersions, fmt.Sprintf("=%s", v))
}
}
return vulnerableVersions, patchedVersions, nil
}
// parseSeverity parses the severity field and returns CVSSv3 vector and score
// cf.
// - https://ossf.github.io/osv-schema/#severity-field
// - https://ossf.github.io/osv-schema/#affectedseverity-field
func parseSeverity(severities []Severity) (string, float64, error) {
for _, s := range severities {
if s.Type == "CVSS_V3" && s.Score != "" {
// CVSS vectors possibly have `/` suffix
// e.g. https://github.com/github/advisory-database/blob/2d3bc73d2117893b217233aeb95b9236c7b93761/advisories/github-reviewed/2019/05/GHSA-j59f-6m4q-62h6/GHSA-j59f-6m4q-62h6.json#L14
// Trim the suffix to avoid errors
cvssVectorV3 := strings.TrimSuffix(s.Score, "/")
metrics, err := metric.NewTemporal().Decode(cvssVectorV3)
if err != nil {
return "", 0, xerrors.Errorf("failed to decode CVSSv3 vector: %w", err)
}
cvssScoreV3 := metrics.Score()
return cvssVectorV3, cvssScoreV3, nil
}
}
return "", 0, nil
}
func convertEcosystem(eco Ecosystem) types.Ecosystem {
// cf. https://ossf.github.io/osv-schema/#affectedpackage-field
switch strings.ToLower(string(eco)) {
case "go":
return vulnerability.Go
case "npm":
return vulnerability.Npm
case "pypi":
return vulnerability.Pip
case "rubygems":
return vulnerability.RubyGems
case "crates.io":
return vulnerability.Cargo
case "packagist":
return vulnerability.Composer
case "maven":
return vulnerability.Maven
case "nuget":
return vulnerability.NuGet
case "hex":
return vulnerability.Erlang
case "pub":
return vulnerability.Pub
case "swifturl", "purl-type:swift":
// GHSA still uses "purl-type:swift" for Swift advisories.
// cf. https://github.com/github/advisory-database/blob/db1cdfb553e48f18aa27d7e929d200563451391a/advisories/github-reviewed/2023/07/GHSA-jq43-q8mx-r7mq/GHSA-jq43-q8mx-r7mq.json#L20
return vulnerability.Swift
case "bitnami":
return vulnerability.Bitnami
case "kubernetes":
return vulnerability.Kubernetes
default:
return vulnerability.Unknown
}
}
func versionContains(ranges []VersionRange, version string) (bool, error) {
for _, r := range ranges {
if ok, err := r.Contains(version); err != nil {
return false, err
} else if ok {
return true, nil
}
}
return false, nil
}