-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
decode.go
407 lines (357 loc) · 11 KB
/
decode.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package io
import (
"context"
"errors"
"slices"
"sort"
"strconv"
"sync"
debver "github.com/knqyf263/go-deb-version"
rpmver "github.com/knqyf263/go-rpm-version"
"github.com/package-url/packageurl-go"
"github.com/samber/lo"
"golang.org/x/xerrors"
"github.com/aquasecurity/trivy/pkg/dependency"
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
"github.com/aquasecurity/trivy/pkg/log"
"github.com/aquasecurity/trivy/pkg/purl"
"github.com/aquasecurity/trivy/pkg/sbom/core"
"github.com/aquasecurity/trivy/pkg/types"
"github.com/aquasecurity/trivy/pkg/uuid"
)
var (
ErrPURLEmpty = errors.New("purl empty error")
ErrUnsupportedType = errors.New("unsupported type")
)
type Decoder struct {
bom *core.BOM
osID uuid.UUID
pkgs map[uuid.UUID]*ftypes.Package
apps map[uuid.UUID]*ftypes.Application
logger *log.Logger
}
func NewDecoder(bom *core.BOM) *Decoder {
return &Decoder{
bom: bom,
pkgs: make(map[uuid.UUID]*ftypes.Package),
apps: make(map[uuid.UUID]*ftypes.Application),
logger: log.WithPrefix("sbom"),
}
}
func (m *Decoder) Decode(ctx context.Context, sbom *types.SBOM) error {
// Parse the root component
if err := m.decodeRoot(sbom); err != nil {
return xerrors.Errorf("failed to decode root component: %w", err)
}
// Parse all components
if err := m.decodeComponents(ctx, sbom); err != nil {
return xerrors.Errorf("failed to decode components: %w", err)
}
// Build dependency graph between packages
m.buildDependencyGraph()
// Add OS packages
m.addOSPkgs(sbom)
// Add language-specific packages
m.addLangPkgs(sbom)
// Add remaining packages
if err := m.addOrphanPkgs(ctx, sbom); err != nil {
return xerrors.Errorf("failed to aggregate packages: %w", err)
}
sort.Slice(sbom.Applications, func(i, j int) bool {
if sbom.Applications[i].Type != sbom.Applications[j].Type {
return sbom.Applications[i].Type < sbom.Applications[j].Type
}
return sbom.Applications[i].FilePath < sbom.Applications[j].FilePath
})
sbom.BOM = m.bom
return nil
}
func (m *Decoder) decodeRoot(s *types.SBOM) error {
root := m.bom.Root()
if root == nil {
return nil // No root found
}
var err error
for _, prop := range root.Properties {
switch prop.Name {
case core.PropertyImageID:
s.Metadata.ImageID = prop.Value
case core.PropertySize:
if s.Metadata.Size, err = strconv.ParseInt(prop.Value, 10, 64); err != nil {
return xerrors.Errorf("failed to convert size: %w", err)
}
case core.PropertyRepoDigest:
s.Metadata.RepoDigests = append(s.Metadata.RepoDigests, prop.Value)
case core.PropertyDiffID:
s.Metadata.DiffIDs = append(s.Metadata.DiffIDs, prop.Value)
case core.PropertyRepoTag:
s.Metadata.RepoTags = append(s.Metadata.RepoTags, prop.Value)
}
}
return nil
}
func (m *Decoder) decodeComponents(ctx context.Context, sbom *types.SBOM) error {
onceMultiOSWarn := sync.OnceFunc(func() {
m.logger.WarnContext(ctx, "Multiple OS components are not supported, taking the first one and ignoring the rest")
})
for id, c := range m.bom.Components() {
switch c.Type {
case core.TypeOS:
if m.osID != uuid.Nil {
onceMultiOSWarn()
continue
}
m.osID = id
sbom.Metadata.OS = &ftypes.OS{
Family: ftypes.OSType(c.Name),
Name: c.Version,
}
continue
case core.TypeApplication:
if app := m.decodeApplication(c); app.Type != "" {
m.apps[id] = app
continue
}
}
// Third-party SBOMs may contain packages in types other than "Library"
if c.Type == core.TypeLibrary || c.PkgIdentifier.PURL != nil {
pkg, err := m.decodePackage(ctx, c)
if errors.Is(err, ErrUnsupportedType) || errors.Is(err, ErrPURLEmpty) {
continue
} else if err != nil {
return xerrors.Errorf("failed to decode package: %w", err)
}
m.pkgs[id] = pkg
}
}
return nil
}
// buildDependencyGraph builds a dependency graph between packages
func (m *Decoder) buildDependencyGraph() {
for id, rels := range m.bom.Relationships() {
pkg, ok := m.pkgs[id]
if !ok {
continue
}
for _, rel := range rels {
dep, ok := m.pkgs[rel.Dependency]
if !ok {
continue
}
pkg.DependsOn = append(pkg.DependsOn, dep.ID)
}
continue
}
}
func (m *Decoder) decodeApplication(c *core.Component) *ftypes.Application {
var app ftypes.Application
for _, prop := range c.Properties {
if prop.Name == core.PropertyType {
app.Type = ftypes.LangType(prop.Value)
}
}
// Aggregation Types use the name of the language (e.g. `Java`, `Python`, etc.) as the component name.
// Other language files use the file path as their name.
if !slices.Contains(ftypes.AggregatingTypes, app.Type) {
app.FilePath = c.Name
}
return &app
}
func (m *Decoder) decodePackage(ctx context.Context, c *core.Component) (*ftypes.Package, error) {
p := (*purl.PackageURL)(c.PkgIdentifier.PURL)
if p == nil {
m.logger.DebugContext(ctx, "Skipping a component without PURL",
log.String("name", c.Name), log.String("version", c.Version))
return nil, ErrPURLEmpty
}
pkg := p.Package()
if p.Class() == types.ClassUnknown {
m.logger.DebugContext(ctx, "Skipping a component with an unsupported type",
log.String("name", c.Name), log.String("version", c.Version), log.String("type", p.Type))
return nil, ErrUnsupportedType
}
pkg.Name = m.pkgName(pkg, c)
pkg.ID = dependency.ID(p.LangType(), pkg.Name, p.Version) // Re-generate ID with the updated name
var err error
for _, prop := range c.Properties {
switch prop.Name {
case core.PropertyPkgID:
pkg.ID = prop.Value
case core.PropertyFilePath:
pkg.FilePath = prop.Value
case core.PropertySrcName:
pkg.SrcName = prop.Value
case core.PropertySrcVersion:
pkg.SrcVersion = prop.Value
case core.PropertySrcRelease:
pkg.SrcRelease = prop.Value
case core.PropertySrcEpoch:
if pkg.SrcEpoch, err = strconv.Atoi(prop.Value); err != nil {
return nil, xerrors.Errorf("invalid src epoch: %w", err)
}
case core.PropertyModularitylabel:
pkg.Modularitylabel = prop.Value
case core.PropertyLayerDigest:
pkg.Layer.Digest = prop.Value
case core.PropertyLayerDiffID:
pkg.Layer.DiffID = prop.Value
}
}
pkg.Identifier.BOMRef = c.PkgIdentifier.BOMRef
pkg.Licenses = c.Licenses
for _, f := range c.Files {
if f.Path != "" && pkg.FilePath == "" {
pkg.FilePath = f.Path
}
// An empty path represents a package digest
if f.Path == "" && len(f.Digests) > 0 {
pkg.Digest = f.Digests[0]
}
}
if p.Class() == types.ClassOSPkg {
m.fillSrcPkg(ctx, c, pkg)
}
return pkg, nil
}
// pkgName returns the package name.
// PURL loses case-sensitivity (e.g. Go, Npm, PyPI), so we have to use an original package name.
func (m *Decoder) pkgName(pkg *ftypes.Package, c *core.Component) string {
p := c.PkgIdentifier.PURL
// A name from PURL takes precedence for CocoaPods since it has subpath.
if c.PkgIdentifier.PURL.Type == packageurl.TypeCocoapods {
return pkg.Name
}
// `maven purl type` has no restrictions on using lowercase letters.
// Also, `spdx-maven-plugin` uses `name` instead of `artifactId` for the `package name` field.
// So we need to use `purl` for maven/gradle packages
// See https://github.com/aquasecurity/trivy/issues/7007 for more information.
if p.Type == packageurl.TypeMaven || p.Type == packageurl.TypeGradle {
return pkg.Name
}
// TODO(backward compatibility): Remove after 03/2025
// Bitnami used different pkg.Name and the name from PURL.
// For backwards compatibility - we need to use PURL.
// cf. https://github.com/aquasecurity/trivy/issues/6981
if c.PkgIdentifier.PURL.Type == packageurl.TypeBitnami {
return pkg.Name
}
if c.Group != "" {
return c.Group + "/" + c.Name
}
return c.Name
}
func (m *Decoder) fillSrcPkg(ctx context.Context, c *core.Component, pkg *ftypes.Package) {
if c.SrcName != "" && pkg.SrcName == "" {
pkg.SrcName = c.SrcName
}
m.parseSrcVersion(ctx, pkg, c.SrcVersion)
// Source info was added from component or properties
if pkg.SrcName != "" && pkg.SrcVersion != "" {
return
}
// Fill source package information for components in third-party SBOMs .
if pkg.SrcName == "" {
pkg.SrcName = pkg.Name
}
if pkg.SrcVersion == "" {
pkg.SrcVersion = pkg.Version
}
if pkg.SrcRelease == "" {
pkg.SrcRelease = pkg.Release
}
if pkg.SrcEpoch == 0 {
pkg.SrcEpoch = pkg.Epoch
}
}
// parseSrcVersion parses the version of the source package.
func (m *Decoder) parseSrcVersion(ctx context.Context, pkg *ftypes.Package, ver string) {
if ver == "" {
return
}
switch pkg.Identifier.PURL.Type {
case packageurl.TypeRPM:
v := rpmver.NewVersion(ver)
pkg.SrcEpoch = v.Epoch()
pkg.SrcVersion = v.Version()
pkg.SrcRelease = v.Release()
case packageurl.TypeDebian:
v, err := debver.NewVersion(ver)
if err != nil {
m.logger.DebugContext(ctx, "Failed to parse Debian version", log.Err(err))
return
}
pkg.SrcEpoch = v.Epoch()
pkg.SrcVersion = v.Version()
pkg.SrcRelease = v.Revision()
}
}
// addOSPkgs traverses relationships and adds OS packages
func (m *Decoder) addOSPkgs(sbom *types.SBOM) {
var pkgs []ftypes.Package
for _, rel := range m.bom.Relationships()[m.osID] {
pkg, ok := m.pkgs[rel.Dependency]
if !ok {
continue
}
pkgs = append(pkgs, *pkg)
delete(m.pkgs, rel.Dependency) // Delete the added package
}
if len(pkgs) == 0 {
return
}
sbom.Packages = []ftypes.PackageInfo{{Packages: pkgs}}
}
// addLangPkgs traverses relationships and adds language-specific packages
func (m *Decoder) addLangPkgs(sbom *types.SBOM) {
for id, app := range m.apps {
for _, rel := range m.bom.Relationships()[id] {
pkg, ok := m.pkgs[rel.Dependency]
if !ok {
continue
}
app.Packages = append(app.Packages, *pkg)
delete(m.pkgs, rel.Dependency) // Delete the added package
}
sbom.Applications = append(sbom.Applications, *app)
}
}
// addOrphanPkgs adds orphan packages.
// Orphan packages are packages that are not related to any components.
func (m *Decoder) addOrphanPkgs(ctx context.Context, sbom *types.SBOM) error {
osPkgMap := make(map[string]ftypes.Packages)
langPkgMap := make(map[ftypes.LangType]ftypes.Packages)
for _, pkg := range m.pkgs {
p := (*purl.PackageURL)(pkg.Identifier.PURL)
switch p.Class() {
case types.ClassOSPkg:
osPkgMap[p.Type] = append(osPkgMap[p.Type], *pkg)
case types.ClassLangPkg:
langType := p.LangType()
langPkgMap[langType] = append(langPkgMap[langType], *pkg)
}
}
if len(osPkgMap) > 1 {
return xerrors.Errorf("multiple types of OS packages in SBOM are not supported (%q)", lo.Keys(osPkgMap))
}
// Add OS packages only when OS is detected.
for _, pkgs := range osPkgMap {
if sbom.Metadata.OS == nil || !sbom.Metadata.OS.Detected() {
m.logger.WarnContext(ctx, "Ignore the OS package as no OS is detected.")
break
}
// TODO: mismatch between the OS and the packages should be rejected.
// e.g. OS: debian, Packages: rpm
sort.Sort(pkgs)
sbom.Packages = append(sbom.Packages, ftypes.PackageInfo{Packages: pkgs})
break // Just take the first element
}
// Add language-specific packages
for pkgType, pkgs := range langPkgMap {
sort.Sort(pkgs)
sbom.Applications = append(sbom.Applications, ftypes.Application{
Type: pkgType,
Packages: pkgs,
})
}
return nil
}