Skip to content

Commit

Permalink
fix: allow valid cyclonedx input with no components (#1873)
Browse files Browse the repository at this point in the history
fix: allow valid cyclonedx input with no components
---------

Signed-off-by: James Neate <[email protected]>
Signed-off-by: Christopher Phillips <[email protected]>
Co-authored-by: Christopher Phillips <[email protected]>
  • Loading branch information
jneate and spiffcs authored Jul 11, 2023
1 parent 72616db commit 5a7c200
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions syft/formats/common/cyclonedxhelpers/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cyclonedxhelpers
import (
"fmt"
"io"
"strings"

"github.com/CycloneDX/cyclonedx-go"

Expand All @@ -15,15 +16,18 @@ import (
"github.com/anchore/syft/syft/source"
)

const cycloneDXXmlSchema = "http://cyclonedx.org/schema/bom"

func GetValidator(format cyclonedx.BOMFileFormat) sbom.Validator {
return func(reader io.Reader) error {
bom := &cyclonedx.BOM{}
err := cyclonedx.NewBOMDecoder(reader, format).Decode(bom)
if err != nil {
return err
}
// random JSON does not necessarily cause an error (e.g. SPDX)
if (cyclonedx.BOM{} == *bom || bom.Components == nil) {

xmlWithoutNS := format == cyclonedx.BOMFileFormatXML && !strings.Contains(bom.XMLNS, cycloneDXXmlSchema)
if (cyclonedx.BOM{} == *bom || bom.Components == nil || xmlWithoutNS) {
return fmt.Errorf("not a valid CycloneDX document")
}
return nil
Expand Down

0 comments on commit 5a7c200

Please sign in to comment.