-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
cargo.go
252 lines (211 loc) · 7 KB
/
cargo.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
package cargo
import (
"context"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"sort"
"github.com/BurntSushi/toml"
"github.com/samber/lo"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"golang.org/x/xerrors"
"github.com/aquasecurity/go-dep-parser/pkg/rust/cargo"
godeptypes "github.com/aquasecurity/go-dep-parser/pkg/types"
"github.com/aquasecurity/go-version/pkg/semver"
goversion "github.com/aquasecurity/go-version/pkg/version"
"github.com/aquasecurity/trivy/pkg/detector/library/compare"
"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
"github.com/aquasecurity/trivy/pkg/fanal/analyzer/language"
"github.com/aquasecurity/trivy/pkg/fanal/types"
"github.com/aquasecurity/trivy/pkg/log"
"github.com/aquasecurity/trivy/pkg/utils/fsutils"
)
func init() {
analyzer.RegisterPostAnalyzer(analyzer.TypeCargo, newCargoAnalyzer)
}
const version = 1
var requiredFiles = []string{
types.CargoLock,
types.CargoToml,
}
type cargoAnalyzer struct {
lockParser godeptypes.Parser
comparer compare.GenericComparer
}
func newCargoAnalyzer(_ analyzer.AnalyzerOptions) (analyzer.PostAnalyzer, error) {
return &cargoAnalyzer{
lockParser: cargo.NewParser(),
comparer: compare.GenericComparer{},
}, nil
}
func (a cargoAnalyzer) PostAnalyze(_ context.Context, input analyzer.PostAnalysisInput) (*analyzer.AnalysisResult, error) {
var apps []types.Application
required := func(path string, d fs.DirEntry) bool {
return filepath.Base(path) == types.CargoLock
}
err := fsutils.WalkDir(input.FS, ".", required, func(path string, d fs.DirEntry, r io.Reader) error {
// Parse Cargo.lock
app, err := a.parseCargoLock(path, r)
if err != nil {
return xerrors.Errorf("parse error: %w", err)
} else if app == nil {
return nil
}
// Parse Cargo.toml alongside Cargo.lock to identify the direct dependencies
if err = a.removeDevDependencies(input.FS, filepath.Dir(path), app); err != nil {
log.Logger.Warnf("Unable to parse %q to identify direct dependencies: %s", filepath.Join(filepath.Dir(path), types.CargoToml), err)
}
sort.Sort(app.Libraries)
apps = append(apps, *app)
return nil
})
if err != nil {
return nil, xerrors.Errorf("cargo walk error: %w", err)
}
return &analyzer.AnalysisResult{
Applications: apps,
}, nil
}
func (a cargoAnalyzer) Required(filePath string, _ os.FileInfo) bool {
fileName := filepath.Base(filePath)
return slices.Contains(requiredFiles, fileName)
}
func (a cargoAnalyzer) Type() analyzer.Type {
return analyzer.TypeCargo
}
func (a cargoAnalyzer) Version() int {
return version
}
func (a cargoAnalyzer) parseCargoLock(path string, r io.Reader) (*types.Application, error) {
return language.Parse(types.Cargo, path, r, a.lockParser)
}
func (a cargoAnalyzer) removeDevDependencies(fsys fs.FS, dir string, app *types.Application) error {
cargoTOMLPath := filepath.Join(dir, types.CargoToml)
directDeps, err := a.parseCargoTOML(fsys, cargoTOMLPath)
if errors.Is(err, fs.ErrNotExist) {
log.Logger.Debugf("Cargo: %s not found", cargoTOMLPath)
return nil
} else if err != nil {
return xerrors.Errorf("unable to parse %s: %w", cargoTOMLPath, err)
}
// Cargo.toml file can contain same libraries with different versions.
// Save versions separately for version comparison by comparator
pkgIDs := lo.SliceToMap(app.Libraries, func(pkg types.Package) (string, types.Package) {
return pkg.ID, pkg
})
// Identify direct dependencies
pkgs := make(map[string]types.Package)
for name, constraint := range directDeps {
for _, pkg := range app.Libraries {
if pkg.Name != name {
continue
}
if match, err := a.matchVersion(pkg.Version, constraint); err != nil {
log.Logger.Warnf("Unable to match Cargo version: package: %s, error: %s", pkg.ID, err)
continue
} else if match {
// Mark as a direct dependency
pkg.Indirect = false
pkgs[pkg.ID] = pkg
break
}
}
}
// Walk indirect dependencies
// Since it starts from direct dependencies, devDependencies will not appear in this walk.
for _, pkg := range pkgs {
a.walkIndirectDependencies(pkg, pkgIDs, pkgs)
}
pkgSlice := maps.Values(pkgs)
sort.Sort(types.Packages(pkgSlice))
// Save only prod libraries
app.Libraries = pkgSlice
return nil
}
type cargoToml struct {
Dependencies Dependencies `toml:"dependencies"`
Target map[string]map[string]Dependencies `toml:"target"`
Workspace map[string]Dependencies `toml:"workspace"`
}
type Dependencies map[string]interface{}
func (a cargoAnalyzer) parseCargoTOML(fsys fs.FS, path string) (map[string]string, error) {
// Parse Cargo.json
f, err := fsys.Open(path)
if err != nil {
return nil, xerrors.Errorf("file open error: %w", err)
}
defer func() { _ = f.Close() }()
tomlFile := cargoToml{}
deps := make(map[string]string)
_, err = toml.NewDecoder(f).Decode(&tomlFile)
if err != nil {
return nil, xerrors.Errorf("toml decode error: %w", err)
}
// There are cases when toml file doesn't include `Dependencies` field (then map will be nil).
// e.g. when only `workspace.Dependencies` are used
// declare `dependencies` to avoid panic
dependencies := Dependencies{}
maps.Copy(dependencies, tomlFile.Dependencies)
// https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#platform-specific-dependencies
for _, target := range tomlFile.Target {
maps.Copy(dependencies, target["dependencies"])
}
// https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#inheriting-a-dependency-from-a-workspace
maps.Copy(dependencies, tomlFile.Workspace["dependencies"])
for name, value := range dependencies {
switch ver := value.(type) {
case string:
// e.g. regex = "1.5"
deps[name] = ver
case map[string]interface{}:
// e.g. serde = { version = "1.0", features = ["derive"] }
for k, v := range ver {
if k == "version" {
if vv, ok := v.(string); ok {
deps[name] = vv
}
break
}
}
}
}
return deps, nil
}
func (a cargoAnalyzer) walkIndirectDependencies(pkg types.Package, pkgIDs, deps map[string]types.Package) {
for _, pkgID := range pkg.DependsOn {
if _, ok := deps[pkgID]; ok {
continue
}
dep, ok := pkgIDs[pkgID]
if !ok {
continue
}
dep.Indirect = true
deps[dep.ID] = dep
a.walkIndirectDependencies(dep, pkgIDs, deps)
}
}
// cf. https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html
func (a cargoAnalyzer) matchVersion(currentVersion, constraint string) (bool, error) {
// `` == `^` - https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#caret-requirements
// Add `^` for correct version comparison
// - 1.2.3 -> ^1.2.3
// - 1.2.* -> 1.2.*
// - ^1.2 -> ^1.2
if _, err := goversion.Parse(constraint); err == nil {
constraint = fmt.Sprintf("^%s", constraint)
}
ver, err := semver.Parse(currentVersion)
if err != nil {
return false, xerrors.Errorf("version error (%s): %s", currentVersion, err)
}
c, err := semver.NewConstraints(constraint)
if err != nil {
return false, xerrors.Errorf("constraint error (%s): %s", currentVersion, err)
}
return c.Check(ver), nil
}