-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathosv.go
221 lines (189 loc) · 5.47 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
package osv
import (
"encoding/json"
"fmt"
"io"
"log"
"path/filepath"
"strings"
"time"
bolt "go.etcd.io/bbolt"
"golang.org/x/xerrors"
"github.com/aquasecurity/trivy-db/pkg/db"
"github.com/aquasecurity/trivy-db/pkg/types"
"github.com/aquasecurity/trivy-db/pkg/utils"
"github.com/aquasecurity/trivy-db/pkg/vulnsrc/bucket"
"github.com/aquasecurity/trivy-db/pkg/vulnsrc/vulnerability"
)
const (
osvDir = "osv"
dataSource = "Open Source Vulnerability"
sourceID = vulnerability.OSV
)
var ecosystems = []ecosystem{
{
dir: "python",
name: vulnerability.Pip,
dataSource: types.DataSource{
ID: sourceID,
Name: "Python Packaging Advisory Database",
URL: "https://github.com/pypa/advisory-db",
},
},
// Cargo ecosystem advisories in OSV were disabled,
// because GitHub Advisory Database contains almost all information.
/*
{
dir: "rust",
name: vulnerability.Cargo,
dataSource: types.DataSource{
ID: sourceID,
Name: "RustSec Advisory Database",
URL: "https://github.com/RustSec/advisory-db",
},
},
*/
// Go ecosystem advisories in OSV were disabled,
// because GitHub Advisory Database contains almost all information.
//{dir: "go", pkgType: vulnerability.Go, sourceID: vulnerability.OSVGo},
}
type ecosystem struct {
dir string
name types.Ecosystem
dataSource types.DataSource
}
type VulnSrc struct {
dbc db.Operation
}
func NewVulnSrc() VulnSrc {
return VulnSrc{
dbc: db.Config{},
}
}
func (vs VulnSrc) Name() types.SourceID {
return sourceID
}
func (vs VulnSrc) Update(dir string) error {
for _, eco := range ecosystems {
log.Printf(" Updating Open Source Vulnerability %s", eco.name)
rootDir := filepath.Join(dir, "vuln-list", osvDir, eco.dir)
var entries []Entry
err := utils.FileWalk(rootDir, func(r io.Reader, path string) error {
var entry Entry
if err := json.NewDecoder(r).Decode(&entry); err != nil {
return xerrors.Errorf("JSON decode error (%s): %w", path, err)
}
// GHSA-IDs are already stored via ghsa package.
// Skip them to avoid duplication.
if strings.HasPrefix(entry.ID, "GHSA") {
return nil
}
entries = append(entries, entry)
return nil
})
if err != nil {
return xerrors.Errorf("walk error: %w", err)
}
if err = vs.save(eco, entries); err != nil {
return xerrors.Errorf("save error: %w", err)
}
}
return nil
}
func (vs VulnSrc) save(eco ecosystem, entries []Entry) error {
err := vs.dbc.BatchUpdate(func(tx *bolt.Tx) error {
for _, entry := range entries {
if err := vs.commit(tx, eco, entry); err != nil {
return err
}
}
return nil
})
if err != nil {
return xerrors.Errorf("batch update error: %w", err)
}
return nil
}
func (vs VulnSrc) commit(tx *bolt.Tx, eco ecosystem, entry Entry) error {
if entry.Withdrawn != nil && entry.Withdrawn.Before(time.Now()) {
return nil
}
bktName := bucket.Name(string(eco.name), dataSource)
if err := vs.dbc.PutDataSource(tx, bktName, eco.dataSource); err != nil {
return xerrors.Errorf("failed to put data source: %w", err)
}
// Aliases contain CVE-IDs
vulnIDs := filterCveIDs(entry.Aliases)
if len(vulnIDs) == 0 {
// e.g. PYSEC-2021-335
vulnIDs = []string{entry.ID}
}
var references []string
for _, ref := range entry.References {
references = append(references, ref.URL)
}
for _, affected := range entry.Affected {
pkgName := vulnerability.NormalizePkgName(eco.name, affected.Package.Name)
var patchedVersions, vulnerableVersions []string
for _, affects := range affected.Ranges {
if affects.Type == RangeTypeGit {
continue
}
var vulnerable string
for _, event := range affects.Events {
switch {
case event.Introduced != "":
// e.g. {"introduced": "1.2.0}, {"introduced": "2.2.0}
if vulnerable != "" {
vulnerableVersions = append(vulnerableVersions, vulnerable)
}
vulnerable = fmt.Sprintf(">=%s", event.Introduced)
// Entries in the events array can contain either last_affected or fixed events, but not both
// cf. https://ossf.github.io/osv-schema/#requirements
case event.Fixed != "":
// patched versions
patchedVersions = append(patchedVersions, event.Fixed)
// e.g. {"introduced": "1.2.0}, {"fixed": "1.2.5}
vulnerable = fmt.Sprintf("%s, <%s", vulnerable, event.Fixed)
case event.LastAffected != "":
vulnerable = fmt.Sprintf("%s, <=%s", vulnerable, event.LastAffected)
}
}
if vulnerable != "" {
vulnerableVersions = append(vulnerableVersions, vulnerable)
}
}
advisory := types.Advisory{
VulnerableVersions: vulnerableVersions,
PatchedVersions: patchedVersions,
}
for _, vulnID := range vulnIDs {
if err := vs.dbc.PutAdvisoryDetail(tx, vulnID, pkgName, []string{bktName}, advisory); err != nil {
return xerrors.Errorf("failed to save OSV advisory: %w", err)
}
}
}
for _, vulnID := range vulnIDs {
vuln := types.VulnerabilityDetail{
Title: entry.Summary,
Description: entry.Details,
References: references,
}
if err := vs.dbc.PutVulnerabilityDetail(tx, vulnID, sourceID, vuln); err != nil {
return xerrors.Errorf("failed to put vulnerability detail (%s): %w", vulnID, err)
}
if err := vs.dbc.PutVulnerabilityID(tx, vulnID); err != nil {
return xerrors.Errorf("failed to put vulnerability id (%s): %w", vulnID, err)
}
}
return nil
}
func filterCveIDs(aliases []string) []string {
var cveIDs []string
for _, a := range aliases {
if strings.HasPrefix(a, "CVE-") {
cveIDs = append(cveIDs, a)
}
}
return cveIDs
}