Repository navigation
Expand file tree
/
Copy pathosv.go
More file actions
137 lines (121 loc) · 4.13 KB
/
Copy pathosv.go
File metadata and controls
137 lines (121 loc) · 4.13 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
// Copyright 2021 OpenSSF Scorecard Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package clients
import (
"context"
"errors"
"fmt"
"log/slog"
"os"
"runtime/debug"
"github.com/google/osv-scanner/v2/pkg/osvscanner"
sce "github.com/ossf/scorecard/v5/errors"
)
var _ VulnerabilitiesClient = osvClient{}
type osvClient struct {
requestUserAgent string
local bool
}
type OSVConfig struct {
UserAgent string
ExperimentalLocal bool
}
func NewOSVClient(config *OSVConfig) VulnerabilitiesClient {
cfg := osvClient{}
if config != nil {
cfg.local = config.ExperimentalLocal
cfg.requestUserAgent = config.UserAgent
}
return cfg
}
// ListUnfixedVulnerabilities implements VulnerabilityClient.ListUnfixedVulnerabilities.
func (v osvClient) ListUnfixedVulnerabilities(
ctx context.Context,
commit,
localPath string,
) (_ VulnerabilitiesResponse, err error) {
osvscanner.SetLogger(slog.DiscardHandler)
defer func() {
if r := recover(); r != nil {
err = sce.CreateInternal(sce.ErrScorecardInternal, fmt.Sprintf("osv-scanner panic: %v", r))
fmt.Fprintf(os.Stderr, "osv-scanner panic: %v\n%s\n", r, string(debug.Stack()))
}
}()
directoryPaths := []string{}
if localPath != "" {
directoryPaths = append(directoryPaths, localPath)
}
gitCommits := []string{}
if commit != "" {
gitCommits = append(gitCommits, commit)
}
exp := osvscanner.ExperimentalScannerActions{
PluginsEnabled: []string{"python/requirements"},
PluginsDisabled: []string{"python/requirementsenhanceable"},
RequestUserAgent: v.requestUserAgent,
}
res, err := osvscanner.DoScan(osvscanner.ScannerActions{
DirectoryPaths: directoryPaths,
IncludeGitRoot: false,
Recursive: true,
GitCommits: gitCommits,
CompareOffline: v.local,
DownloadDatabases: v.local,
// swap out the transitive requirements scanning for offline extractor
ExperimentalScannerActions: exp,
}) // TODO: Do logging?
response := VulnerabilitiesResponse{}
// either no vulns found, or no packages detected by osvscanner, which likely means no vulns
// while there could still be vulns, not detecting any packages shouldn't be a runtime error.
if err == nil || errors.Is(err, osvscanner.ErrNoPackagesFound) {
return response, nil
}
// If vulnerabilities are found, err will be set to osvscanner.VulnerabilitiesFoundErr
if errors.Is(err, osvscanner.ErrVulnerabilitiesFound) {
vulns := res.Flatten()
for i := range vulns {
// ignore Go stdlib vulns. The go directive from the go.mod isn't a perfect metric
// of which version of Go will be used to build a project.
if vulns[i].Package.Ecosystem == "Go" && vulns[i].Package.Name == "stdlib" {
continue
}
response.Vulnerabilities = append(response.Vulnerabilities, Vulnerability{
ID: vulns[i].Vulnerability.GetId(),
Aliases: vulns[i].Vulnerability.GetAliases(),
})
// Remove duplicate vulnerability IDs for now as we don't report information
// on the source of each vulnerability yet, therefore having multiple identical
// vuln IDs might be confusing.
response.Vulnerabilities = removeDuplicate(
response.Vulnerabilities,
func(key Vulnerability) string { return key.ID },
)
}
return response, nil
}
return VulnerabilitiesResponse{}, fmt.Errorf("osvscanner.DoScan: %w", err)
}
// RemoveDuplicate removes duplicate entries from a slice.
func removeDuplicate[T any, K comparable](sliceList []T, keyExtract func(T) K) []T {
allKeys := make(map[K]bool)
list := []T{}
for _, item := range sliceList {
key := keyExtract(item)
if _, value := allKeys[key]; !value {
allKeys[key] = true
list = append(list, item)
}
}
return list
}