Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix decoding package groups from cyclonedx sboms #1345

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 23 additions & 3 deletions syft/formats/common/cyclonedxhelpers/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cyclonedxhelpers

import (
"reflect"
"strings"

"github.com/CycloneDX/cyclonedx-go"

Expand All @@ -27,10 +28,15 @@ func encodeComponent(p pkg.Package) cyclonedx.Component {
properties = &props
}

name, group := encodeName(p.Name)
if group == "" {
group = encodeGroup(p)
}

return cyclonedx.Component{
Type: cyclonedx.ComponentTypeLibrary,
Name: p.Name,
Group: encodeGroup(p),
Name: name,
Group: group,
Version: p.Version,
PackageURL: p.PURL,
Licenses: encodeLicenses(p),
Expand All @@ -44,6 +50,13 @@ func encodeComponent(p pkg.Package) cyclonedx.Component {
}
}

func encodeName(name string) (string, string) {
if strings.Contains(name, "/") {
parts := strings.Split(name, "/")
return parts[0], parts[1]
}
}

func deriveBomRef(p pkg.Package) string {
// try and parse the PURL if possible and append syft id to it, to make
// the purl unique in the BOM.
Expand All @@ -70,7 +83,7 @@ func decodeComponent(c *cyclonedx.Component) *pkg.Package {
}

p := &pkg.Package{
Name: c.Name,
Name: decodeName(c.Group, c.Name),
Version: c.Version,
Locations: decodeLocations(values),
Licenses: decodeLicenses(c),
Expand All @@ -95,6 +108,13 @@ func decodeComponent(c *cyclonedx.Component) *pkg.Package {
return p
}

func decodeName(group string, name string) string {
if group != "" {
return group + "/" + name
}
return name
}

func decodeLocations(vals map[string]string) source.LocationSet {
v := common.Decode(reflect.TypeOf([]source.Location{}), vals, "syft:location", CycloneDXFields)
out, ok := v.([]source.Location)
Expand Down