Skip to content

Commit

Permalink
fix: unset metadata type when no metadata is present decoding an SBOM
Browse files Browse the repository at this point in the history
Signed-off-by: Keith Zantow <[email protected]>
  • Loading branch information
kzantow committed Nov 15, 2022
1 parent 1ec58ee commit b03ad66
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 10 deletions.
5 changes: 5 additions & 0 deletions syft/formats/common/cyclonedxhelpers/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ func decodeComponent(c *cyclonedx.Component) *pkg.Package {

p.Metadata = decodePackageMetadata(values, c, p.MetadataType)

// if there isn't a valid metadata struct, unset the type
if p.Metadata == nil {
p.MetadataType = pkg.NoMetadataType
}

if p.Type == "" {
p.Type = pkg.TypeFromPURL(p.PURL)
}
Expand Down
4 changes: 2 additions & 2 deletions syft/formats/common/cyclonedxhelpers/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ func Test_decodeComponent(t *testing.T) {
},
},
},
wantMetadataType: pkg.RpmMetadataType,
wantMetadata: pkg.RpmMetadata{},
wantMetadataType: pkg.NoMetadataType,
wantMetadata: nil,
},
{
name: "handle RpmdbMetadata type with properties",
Expand Down
2 changes: 1 addition & 1 deletion syft/formats/common/property_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ func decode(vals map[string]string, value reflect.Value, prefix string, fn Field

func PtrToStruct(ptr interface{}) interface{} {
v := reflect.ValueOf(ptr)
if v.IsZero() && v.Type().Kind() != reflect.Struct {
if v.IsZero() {
return nil
}
switch v.Type().Kind() {
Expand Down
11 changes: 7 additions & 4 deletions syft/formats/syftjson/model/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,13 @@ func unpackMetadata(p *Package, unpacker packageMetadataUnpacker) error {
typ, ok := pkg.MetadataTypeByName[p.MetadataType]
if ok {
val := reflect.New(typ).Interface()
if len(unpacker.Metadata) > 0 {
if err := json.Unmarshal(unpacker.Metadata, val); err != nil {
return err
}
if len(unpacker.Metadata) == 0 {
// if we don't have metadata, unset the metadata type
p.MetadataType = pkg.NoMetadataType
return nil
}
if err := json.Unmarshal(unpacker.Metadata, val); err != nil {
return err
}
p.Metadata = reflect.ValueOf(val).Elem().Interface()
return nil
Expand Down
4 changes: 2 additions & 2 deletions syft/formats/syftjson/model/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ func Test_unpackMetadata(t *testing.T) {
packageData: []byte(`{
"metadataType": "GolangBinMetadata"
}`),
metadataType: pkg.GolangBinMetadataType,
wantMetadata: pkg.GolangBinMetadata{},
metadataType: pkg.NoMetadataType,
wantMetadata: nil,
},
{
name: "can handle package with unknonwn metadata type and missing metadata",
Expand Down
2 changes: 1 addition & 1 deletion syft/pkg/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type MetadataType string

const (
// this is the full set of data shapes that can be represented within the pkg.Package.Metadata field

NoMetadataType MetadataType = ""
UnknownMetadataType MetadataType = "UnknownMetadata"
ApkMetadataType MetadataType = "ApkMetadata"
AlpmMetadataType MetadataType = "AlpmMetadata"
Expand Down

0 comments on commit b03ad66

Please sign in to comment.