/
osvscanner.go
351 lines (306 loc) · 10.1 KB
/
osvscanner.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
package osvscanner
import (
"bufio"
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/google/osv-scanner/internal/osv"
"github.com/google/osv-scanner/internal/output"
"github.com/google/osv-scanner/internal/sbom"
"github.com/google/osv-scanner/pkg/config"
"github.com/google/osv-scanner/pkg/lockfile"
"github.com/google/osv-scanner/pkg/models"
)
type ScannerActions struct {
LockfilePaths []string
SBOMPaths []string
DirectoryPaths []string
GitCommits []string
Recursive bool
SkipGit bool
DockerContainerNames []string
ConfigOverridePath string
}
// Error for when no packages is found during a scan.
var NoPackagesFoundErr = errors.New("no packages found in scan")
var VulnerabilitiesFoundErr = errors.New("vulnerabilities found")
// scanDir walks through the given directory to try to find any relevant files
// These include:
// - Any lockfiles with scanLockfile
// - Any SBOM files with scanSBOMFile
// - Any git repositories with scanGit
func scanDir(r *output.Reporter, query *osv.BatchedQuery, dir string, skipGit bool, recursive bool) error {
root := true
return filepath.WalkDir(dir, func(path string, info os.DirEntry, err error) error {
if err != nil {
r.PrintText(fmt.Sprintf("Failed to walk %s: %v\n", path, err))
return err
}
path, err = filepath.Abs(path)
if err != nil {
r.PrintError(fmt.Sprintf("Failed to walk path %s\n", err))
return err
}
if !skipGit && info.IsDir() && info.Name() == ".git" {
err := scanGit(r, query, filepath.Dir(path)+"/")
if err != nil {
r.PrintText(fmt.Sprintf("scan failed for git repository, %s: %v\n", path, err))
// Not fatal, so don't return and continue scanning other files
}
return filepath.SkipDir
}
if !info.IsDir() {
if parser, _ := lockfile.FindParser(path, ""); parser != nil {
err := scanLockfile(r, query, path)
if err != nil {
r.PrintError(fmt.Sprintf("Attempted to scan lockfile but failed: %s\n", path))
}
}
// No need to check for error
// If scan fails, it means it isn't a valid SBOM file,
// so just move onto the next file
_ = scanSBOMFile(r, query, path)
}
if !root && !recursive && info.IsDir() {
return filepath.SkipDir
}
root = false
return nil
})
}
// scanLockfile will load, identify, and parse the lockfile path passed in, and add the dependencies specified
// within to `query`
func scanLockfile(r *output.Reporter, query *osv.BatchedQuery, path string) error {
parsedLockfile, err := lockfile.Parse(path, "")
if err != nil {
return err
}
r.PrintText(fmt.Sprintf("Scanned %s file and found %d packages\n", path, len(parsedLockfile.Packages)))
for _, pkgDetail := range parsedLockfile.Packages {
pkgDetailQuery := osv.MakePkgRequest(pkgDetail)
pkgDetailQuery.Source = models.SourceInfo{
Path: path,
Type: "lockfile",
}
query.Queries = append(query.Queries, pkgDetailQuery)
}
return nil
}
// scanSBOMFile will load, identify, and parse the SBOM path passed in, and add the dependencies specified
// within to `query`
func scanSBOMFile(r *output.Reporter, query *osv.BatchedQuery, path string) error {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
for _, provider := range sbom.Providers {
if provider.Name() == "SPDX" &&
!strings.Contains(strings.ToLower(filepath.Base(path)), ".spdx") {
// All spdx files should have the .spdx in the filename, even if
// it's not the extension: https://spdx.github.io/spdx-spec/v2.3/conformance/
// Skip if this isn't the case to avoid panics
continue
}
err := provider.GetPackages(file, func(id sbom.Identifier) error {
purlQuery := osv.MakePURLRequest(id.PURL)
purlQuery.Source = models.SourceInfo{
Path: path,
Type: "sbom",
}
query.Queries = append(query.Queries, purlQuery)
return nil
})
if err == nil {
// Found the right format.
r.PrintText(fmt.Sprintf("Scanned %s SBOM\n", provider.Name()))
return nil
}
if errors.Is(err, sbom.InvalidFormat) {
continue
}
return err
}
return nil
}
func getCommitSHA(repoDir string) (string, error) {
cmd := exec.Command("git", "-C", repoDir, "rev-parse", "HEAD")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
if exiterr, ok := err.(*exec.ExitError); ok && exiterr.ExitCode() == 128 {
return "", fmt.Errorf("Failed to get commit hash, no commits exist? %w", exiterr)
} else {
return "", fmt.Errorf("failed to get commit hash: %w", err)
}
}
return strings.TrimSpace(out.String()), nil
}
// Scan git repository. Expects repoDir to end with /
func scanGit(r *output.Reporter, query *osv.BatchedQuery, repoDir string) error {
commit, err := getCommitSHA(repoDir)
if err != nil {
return err
}
r.PrintText(fmt.Sprintf("Scanning %s at commit %s\n", repoDir, commit))
return scanGitCommit(r, query, commit, repoDir)
}
func scanGitCommit(r *output.Reporter, query *osv.BatchedQuery, commit string, source string) error {
gitQuery := osv.MakeCommitRequest(commit)
gitQuery.Source = models.SourceInfo{
Path: source,
Type: "git",
}
query.Queries = append(query.Queries, gitQuery)
return nil
}
func scanDebianDocker(r *output.Reporter, query *osv.BatchedQuery, dockerImageName string) error {
cmd := exec.Command("docker", "run", "--rm", "--entrypoint", "/usr/bin/dpkg-query", dockerImageName, "-f", "${Package}###${Version}\\n", "-W")
stdout, err := cmd.StdoutPipe()
if err != nil {
r.PrintError(fmt.Sprintf("Failed to get stdout: %s\n", err))
return err
}
err = cmd.Start()
if err != nil {
r.PrintError(fmt.Sprintf("Failed to start docker image: %s\n", err))
return err
}
defer cmd.Wait()
if err != nil {
r.PrintError(fmt.Sprintf("Failed to run docker: %s\n", err))
return err
}
scanner := bufio.NewScanner(stdout)
packages := 0
for scanner.Scan() {
text := scanner.Text()
text = strings.TrimSpace(text)
if len(text) == 0 {
continue
}
splitText := strings.Split(text, "###")
if len(splitText) != 2 {
r.PrintError(fmt.Sprintf("Unexpected output from Debian container: \n\n%s\n", text))
return fmt.Errorf("unexpected output from Debian container: \n\n%s", text)
}
pkgDetailsQuery := osv.MakePkgRequest(lockfile.PackageDetails{
Name: splitText[0],
Version: splitText[1],
// TODO(rexpan): Get and specify exact debian release version
Ecosystem: "Debian",
})
pkgDetailsQuery.Source = models.SourceInfo{
Path: dockerImageName,
Type: "docker",
}
query.Queries = append(query.Queries, pkgDetailsQuery)
packages += 1
}
r.PrintText(fmt.Sprintf("Scanned docker image with %d packages\n", packages))
return nil
}
// Filters response according to config, returns number of responses removed
func filterResponse(r *output.Reporter, query osv.BatchedQuery, resp *osv.BatchedResponse, configManager *config.ConfigManager) int {
hiddenVulns := map[string]config.IgnoreEntry{}
for i, result := range resp.Results {
var filteredVulns []osv.MinimalVulnerability
configToUse := configManager.Get(r, query.Queries[i].Source.Path)
for _, vuln := range result.Vulns {
ignore, ignoreLine := configToUse.ShouldIgnore(vuln.ID)
if ignore {
hiddenVulns[vuln.ID] = ignoreLine
} else {
filteredVulns = append(filteredVulns, vuln)
}
}
resp.Results[i].Vulns = filteredVulns
}
for id, ignoreLine := range hiddenVulns {
r.PrintText(fmt.Sprintf("%s has been filtered out because: %s\n", id, ignoreLine.Reason))
}
return len(hiddenVulns)
}
// Perform osv scanner action, with optional reporter to output information
func DoScan(actions ScannerActions, r *output.Reporter) (models.VulnerabilityResults, error) {
if r == nil {
r = output.NewVoidReporter()
}
configManager := config.ConfigManager{
DefaultConfig: config.Config{},
ConfigMap: make(map[string]config.Config),
}
var query osv.BatchedQuery
if actions.ConfigOverridePath != "" {
err := configManager.UseOverride(actions.ConfigOverridePath)
if err != nil {
r.PrintError(fmt.Sprintf("Failed to read config file: %s\n", err))
return models.VulnerabilityResults{}, err
}
}
for _, container := range actions.DockerContainerNames {
// TODO: Automatically figure out what docker base image
// and scan appropriately.
scanDebianDocker(r, &query, container)
}
for _, lockfileElem := range actions.LockfilePaths {
lockfileElem, err := filepath.Abs(lockfileElem)
if err != nil {
r.PrintError(fmt.Sprintf("Failed to resolved path with error %s\n", err))
return models.VulnerabilityResults{}, err
}
err = scanLockfile(r, &query, lockfileElem)
if err != nil {
return models.VulnerabilityResults{}, err
}
}
for _, sbomElem := range actions.SBOMPaths {
sbomElem, err := filepath.Abs(sbomElem)
if err != nil {
return models.VulnerabilityResults{}, fmt.Errorf("failed to resolved path with error %s\n", err)
}
err = scanSBOMFile(r, &query, sbomElem)
if err != nil {
return models.VulnerabilityResults{}, err
}
}
for _, commit := range actions.GitCommits {
err := scanGitCommit(r, &query, commit, "HASH")
if err != nil {
return models.VulnerabilityResults{}, err
}
}
for _, dir := range actions.DirectoryPaths {
r.PrintText(fmt.Sprintf("Scanning dir %s\n", dir))
err := scanDir(r, &query, dir, actions.SkipGit, actions.Recursive)
if err != nil {
return models.VulnerabilityResults{}, err
}
}
if len(query.Queries) == 0 {
return models.VulnerabilityResults{}, NoPackagesFoundErr
}
resp, err := osv.MakeRequest(query)
if err != nil {
return models.VulnerabilityResults{}, fmt.Errorf("scan failed %v", err)
}
filtered := filterResponse(r, query, resp, &configManager)
if filtered > 0 {
r.PrintText(fmt.Sprintf("Filtered %d vulnerabilities from output\n", filtered))
}
hydratedResp, err := osv.Hydrate(resp)
if err != nil {
return models.VulnerabilityResults{}, fmt.Errorf("failed to hydrate OSV response: %v", err)
}
vulnerabilityResults := groupResponseBySource(r, query, hydratedResp)
// if vulnerability exists it should return error
if len(vulnerabilityResults.Results) > 0 {
return vulnerabilityResults, VulnerabilitiesFoundErr
}
return vulnerabilityResults, nil
}