Repository navigation
Expand file tree
/
Copy pathconfig.go
More file actions
168 lines (142 loc) · 5.24 KB
/
Copy pathconfig.go
File metadata and controls
168 lines (142 loc) · 5.24 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
// Package config manages the configuration for osv-scanner.
package config
import (
"slices"
"time"
"github.com/google/osv-scalibr/extractor"
"github.com/google/osv-scanner/v2/internal/cachedregexp"
"github.com/google/osv-scanner/v2/internal/cmdlogger"
"github.com/google/osv-scanner/v2/internal/imodels"
)
var OSVScannerConfigName = "osv-scanner.toml"
type Config struct {
IgnoredVulns []*IgnoreEntry `toml:"IgnoredVulns"`
PackageOverrides []PackageOverrideEntry `toml:"PackageOverrides"`
GoVersionOverride string `toml:"GoVersionOverride"`
ScanGoModVersion bool `toml:"ScanGoModVersion"`
// The path to config file that this config was loaded from,
// set by the scanner after having successfully parsed the file
LoadPath string `toml:"-"`
}
type IgnoreEntry struct {
ID string `toml:"id"`
IgnoreUntil time.Time `toml:"ignoreUntil"`
Reason string `toml:"reason"`
Used bool `toml:"-"`
}
func (ie *IgnoreEntry) MarkAsUsed() {
ie.Used = true
}
type PackageOverrideEntry struct {
Name string `toml:"name"`
// If the version is empty, the entry applies to all versions.
Version string `toml:"version"`
Ecosystem string `toml:"ecosystem"`
Group string `toml:"group"`
NameIsRegex bool `toml:"nameIsRegex"`
Ignore bool `toml:"ignore"`
Vulnerability Vulnerability `toml:"vulnerability"`
License License `toml:"license"`
EffectiveUntil time.Time `toml:"effectiveUntil"`
Reason string `toml:"reason"`
}
func (e PackageOverrideEntry) matches(pkg *extractor.Package) bool {
if e.Name != "" {
if e.NameIsRegex {
re, err := cachedregexp.Compile("^" + e.Name + "$")
if err != nil {
// This should not happen as regex is validated at config load time
return false
}
if !re.MatchString(imodels.Name(pkg)) {
return false
}
} else if e.Name != imodels.Name(pkg) {
return false
}
}
if e.Version != "" && e.Version != imodels.Version(pkg) {
return false
}
// If there is an ecosystem filter, the filter must not match both the:
// - Full ecosystem + suffix
// - The base ecosystem
if e.Ecosystem != "" && (e.Ecosystem != imodels.Ecosystem(pkg).String() && e.Ecosystem != string(imodels.Ecosystem(pkg).Ecosystem)) {
return false
}
if e.Group != "" && !slices.Contains(imodels.DepGroups(pkg), e.Group) {
return false
}
return true
}
type Vulnerability struct {
Ignore bool `toml:"ignore"`
}
type License struct {
Override []string `toml:"override"`
Ignore bool `toml:"ignore"`
}
func (c *Config) UnusedIgnoredVulns() []*IgnoreEntry {
unused := make([]*IgnoreEntry, 0, len(c.IgnoredVulns))
for _, entry := range c.IgnoredVulns {
if !entry.Used {
unused = append(unused, entry)
}
}
return unused
}
func (c *Config) ShouldIgnore(vulnID string) (bool, *IgnoreEntry) {
index := slices.IndexFunc(c.IgnoredVulns, func(e *IgnoreEntry) bool { return e.ID == vulnID })
if index == -1 {
return false, &IgnoreEntry{}
}
ignoredLine := c.IgnoredVulns[index]
return shouldIgnoreTimestamp(ignoredLine.IgnoreUntil), ignoredLine
}
func (c *Config) filterPackageVersionEntries(pkg *extractor.Package, condition func(PackageOverrideEntry) bool) (bool, PackageOverrideEntry) {
index := slices.IndexFunc(c.PackageOverrides, func(e PackageOverrideEntry) bool {
return e.matches(pkg) && condition(e)
})
if index == -1 {
return false, PackageOverrideEntry{}
}
ignoredLine := c.PackageOverrides[index]
return shouldIgnoreTimestamp(ignoredLine.EffectiveUntil), ignoredLine
}
// ShouldIgnorePackage determines if the given package should be ignored based on override entries in the config
func (c *Config) ShouldIgnorePackage(pkg *extractor.Package) (bool, PackageOverrideEntry) {
return c.filterPackageVersionEntries(pkg, func(e PackageOverrideEntry) bool {
return e.Ignore
})
}
// ShouldIgnorePackageVulnerabilities determines if the given package should have its vulnerabilities ignored based on override entries in the config
func (c *Config) ShouldIgnorePackageVulnerabilities(pkg *extractor.Package) bool {
overrides, _ := c.filterPackageVersionEntries(pkg, func(e PackageOverrideEntry) bool {
return e.Vulnerability.Ignore
})
return overrides
}
// ShouldOverridePackageLicense determines if the given package should have its license ignored or changed based on override entries in the config
func (c *Config) ShouldOverridePackageLicense(pkg *extractor.Package) (bool, PackageOverrideEntry) {
return c.filterPackageVersionEntries(pkg, func(e PackageOverrideEntry) bool {
return e.License.Ignore || len(e.License.Override) > 0
})
}
func shouldIgnoreTimestamp(ignoreUntil time.Time) bool {
if ignoreUntil.IsZero() {
// If IgnoreUntil is not set, should ignore.
return true
}
// Should ignore if IgnoreUntil is still after current time
// Takes timezone offsets into account if it is specified. otherwise it's using local time
return ignoreUntil.After(time.Now())
}
func (c *Config) warnAboutDuplicates() {
seen := make(map[string]struct{})
for _, vuln := range c.IgnoredVulns {
if _, ok := seen[vuln.ID]; ok {
cmdlogger.Warnf("warning: %s has multiple ignores for %s - only the first will be used!", c.LoadPath, vuln.ID)
}
seen[vuln.ID] = struct{}{}
}
}