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: allow valid cyclonedx input with no components #1873

Merged
merged 3 commits into from
Jul 11, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 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,6 +16,8 @@ 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{}
Expand All @@ -23,8 +26,14 @@ func GetValidator(format cyclonedx.BOMFileFormat) sbom.Validator {
return err
}
// random JSON does not necessarily cause an error (e.g. SPDX)
if (cyclonedx.BOM{} == *bom || ((format == cyclonedx.BOMFileFormatXML && bom.XMLNS == "") || bom.Components == nil)) {
return fmt.Errorf("not a valid CycloneDX document")
if format == cyclonedx.BOMFileFormatXML {
if (!strings.Contains(bom.XMLNS, cycloneDXXmlSchema) || cyclonedx.BOM{} == *bom) {
return fmt.Errorf("not a valid CycloneDX document")
}
} else {
if (bom.Components == nil || cyclonedx.BOM{} == *bom) {
return fmt.Errorf("not a valid CycloneDX document")
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having nested if statements like this duplicates some of the logic and makes things a little harder to reason about. What about a change like this?

Suggested change
if format == cyclonedx.BOMFileFormatXML {
if (!strings.Contains(bom.XMLNS, cycloneDXXmlSchema) || cyclonedx.BOM{} == *bom) {
return fmt.Errorf("not a valid CycloneDX document")
}
} else {
if (bom.Components == nil || cyclonedx.BOM{} == *bom) {
return fmt.Errorf("not a valid CycloneDX document")
}
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