From b03ad66a930ba2135e5114a6e611c63a7e0cd61f Mon Sep 17 00:00:00 2001 From: Keith Zantow Date: Tue, 15 Nov 2022 11:58:35 -0500 Subject: [PATCH] fix: unset metadata type when no metadata is present decoding an SBOM Signed-off-by: Keith Zantow --- syft/formats/common/cyclonedxhelpers/component.go | 5 +++++ .../formats/common/cyclonedxhelpers/component_test.go | 4 ++-- syft/formats/common/property_encoder.go | 2 +- syft/formats/syftjson/model/package.go | 11 +++++++---- syft/formats/syftjson/model/package_test.go | 4 ++-- syft/pkg/metadata.go | 2 +- 6 files changed, 18 insertions(+), 10 deletions(-) diff --git a/syft/formats/common/cyclonedxhelpers/component.go b/syft/formats/common/cyclonedxhelpers/component.go index 0f2c1fd89b33..fb178307abcd 100644 --- a/syft/formats/common/cyclonedxhelpers/component.go +++ b/syft/formats/common/cyclonedxhelpers/component.go @@ -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) } diff --git a/syft/formats/common/cyclonedxhelpers/component_test.go b/syft/formats/common/cyclonedxhelpers/component_test.go index d1f7835ae3c2..f25151077b71 100644 --- a/syft/formats/common/cyclonedxhelpers/component_test.go +++ b/syft/formats/common/cyclonedxhelpers/component_test.go @@ -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", diff --git a/syft/formats/common/property_encoder.go b/syft/formats/common/property_encoder.go index e22772db0eb5..9db409425cd4 100644 --- a/syft/formats/common/property_encoder.go +++ b/syft/formats/common/property_encoder.go @@ -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() { diff --git a/syft/formats/syftjson/model/package.go b/syft/formats/syftjson/model/package.go index 4567a139aeb5..0993cbba142d 100644 --- a/syft/formats/syftjson/model/package.go +++ b/syft/formats/syftjson/model/package.go @@ -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 diff --git a/syft/formats/syftjson/model/package_test.go b/syft/formats/syftjson/model/package_test.go index 34677b20bf24..b54ab6bb5fee 100644 --- a/syft/formats/syftjson/model/package_test.go +++ b/syft/formats/syftjson/model/package_test.go @@ -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", diff --git a/syft/pkg/metadata.go b/syft/pkg/metadata.go index c757c3f5ec66..e6d0f04c6b8d 100644 --- a/syft/pkg/metadata.go +++ b/syft/pkg/metadata.go @@ -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"