diff --git a/grype/pkg/syft_sbom_provider.go b/grype/pkg/syft_sbom_provider.go index 108fcd13..e22cf500 100644 --- a/grype/pkg/syft_sbom_provider.go +++ b/grype/pkg/syft_sbom_provider.go @@ -14,6 +14,14 @@ import ( "github.com/anchore/syft/syft" ) +type errEmptySBOM struct { + sbomFilepath string +} + +func (e errEmptySBOM) Error() string { + return fmt.Sprintf("SBOM file is empty: %s", e.sbomFilepath) +} + func syftSBOMProvider(userInput string, config ProviderConfig) ([]Package, Context, error) { reader, err := getSBOMReader(userInput) if err != nil { @@ -49,6 +57,10 @@ func getSBOMReader(userInput string) (io.Reader, error) { return nil, fmt.Errorf("unable to use specified SBOM: %w", err) } + if !sbomHasContent(sbom) { + return nil, errEmptySBOM{filepath} + } + return sbom, nil } @@ -64,6 +76,25 @@ func getSBOMReader(userInput string) (io.Reader, error) { return nil, errDoesNotProvide } +// sbomHasContent returns a bool indicating whether the given SBOM file has data that could possibly be utilized in +// downstream processing. +func sbomHasContent(sbom *os.File) bool { + if sbom == nil { + return false + } + + info, err := sbom.Stat() + if err != nil { + return false + } + + if size := info.Size(); size > 0 { + return true + } + + return false +} + func stdinReader() io.Reader { isPipedInput, err := internal.IsPipedInput() if err != nil { diff --git a/grype/pkg/syft_sbom_provider_test.go b/grype/pkg/syft_sbom_provider_test.go index cbf9e934..74d9a2ca 100644 --- a/grype/pkg/syft_sbom_provider_test.go +++ b/grype/pkg/syft_sbom_provider_test.go @@ -1,11 +1,13 @@ package pkg import ( + "os" "strings" "testing" "github.com/go-test/deep" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "github.com/anchore/syft/syft/linux" "github.com/anchore/syft/syft/pkg" @@ -124,8 +126,6 @@ func TestParseSyftJSON(t *testing.T) { Tags: []string{ "alpine:fake", }, - //RawManifest: []byte("eyJzY2hlbWFWZXJzaW9uIjoyLCJtZWRpYVR5cGUiOiJhcHBsaWNhdGlvbi92bmQuZG9ja2VyLmRpc3RyaWJ1dGlvbi5tYW5pZmVz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L3YzLjkvbWFpbi94ODZfNjQvbGlidm5jc2VydmVyLTAuOS4xMS1yMy5hcGsifSx7ImNyZWF0ZWQiOiIyMDIwLTA5LTI0VDIyOjI2OjQ1LjY3MDg1MzhaIiwiY3JlYXRlZF9ieSI6Ii9iaW4vc2ggLWMgYXBrIGFkZCAgbGlidm5jc2VydmVyLTAuOS4xMS1yMy5hcGsifSx7ImNyZWF0ZWQiOiIyMDIwLTA5LTI0VDIyOjI2OjQ2LjE2NzYxOTRaIiwiY3JlYXRlZF9ieSI6Ii9iaW4vc2ggLWMgc2VkIC1pICdzL1Y6MC45LjExLXIzL1Y6MC45LjktcjAvJyAvbGliL2Fway9kYi9pbnN0YWxsZWQifV0sIm9zIjoibGludXgiLCJyb290ZnMiOnsidHlwZSI6ImxheWVycyIsImRpZmZfaWRzIjpbInNoYTI1Njo1MDY0NGMyOWVmNWEyN2M5YTQwYzM5M2E3M2VjZTI0NzlkZTc4MzI1Y2FlN2Q3NjJlZjNjZGMxOWJmNDJkZDBhIiwic2hhMjU2OmNjMGZmMWRkYWQ2ZmU0OTc4ZDgzMjYzMGE5MzAzODgzYWRjNTZlZGZjNzdjYWEzNjkyMjM5YzJkODFjZjVkMDAiLCJzaGEyNTY6M2RkMmRiNDgzYmM5ZDZiNTYxY2U1Y2MxMTA1ZTBiNmQxOTYxY2EyNDlhNzM2YmJhODM3MWFiMjhlYTMwNGY4NCIsInNoYTI1Njo5M2NmNGNmYjY3M2M3ZTE2YTllNzRmNzMxZDY3NjdiNzBiOTJhMGI3YzlmNTlkMDZlZmQ3MmZiZmY1MzUzNzFjIl19fQ=="), }, Path: "", }, @@ -253,3 +253,15 @@ var springImageTestCase = struct { }, }, } + +func TestGetSBOMReader_EmptySBOM(t *testing.T) { + sbomFile, err := os.CreateTemp("", "empty.sbom") + require.NoError(t, err) + defer sbomFile.Close() + + filepath := sbomFile.Name() + userInput := "sbom:" + filepath + + _, err = getSBOMReader(userInput) + assert.ErrorAs(t, err, &errEmptySBOM{}) +} diff --git a/internal/file/getter_test.go b/internal/file/getter_test.go index 222b02df..f0bdaf90 100644 --- a/internal/file/getter_test.go +++ b/internal/file/getter_test.go @@ -158,7 +158,7 @@ func assertErrNonArchiveSource(t assert.TestingT, err error, _ ...interface{}) b } func removeTrustedCAs(client *http.Client) { - client.Transport.(*http.Transport).TLSClientConfig.RootCAs = nil + client.Transport.(*http.Transport).TLSClientConfig.RootCAs = x509.NewCertPool() } // createTarball makes a single-file tarball and returns it as a byte slice.