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

improve SBOM validation #272

Closed
wants to merge 8 commits into from
Closed
Changes from all commits
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
24 changes: 22 additions & 2 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package libcnb
import (
"errors"
"fmt"
"mime"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -390,10 +391,29 @@ func validateSBOMFormats(layersPath string, acceptedSBOMFormats []string) error
return fmt.Errorf("unable to parse SBOM %s\n%w", sbomFormat, err)
}

if !contains(acceptedSBOMFormats, sbomFormat.MediaType()) {
return fmt.Errorf("unable to find actual SBOM Type %s in list of supported SBOM types %s", sbomFormat.MediaType(), acceptedSBOMFormats)
mimeType := sbomFormat.MediaType()

if !(contains(acceptedSBOMFormats, mimeType)) {
return fmt.Errorf("unable to find actual SBOM Type %s in list of supported SBOM types %v", mimeType, acceptedSBOMFormats)
}

if err := ensureDeclared(acceptedSBOMFormats, mimeType); err != nil {
return fmt.Errorf("error validating SBOM Type %s: %w", mimeType, err)
}
}

return nil
}

func ensureDeclared(declaredTypes []string, foundType string) error {
for _, declaredType := range declaredTypes {
dType, _, err := mime.ParseMediaType(declaredType)
if err != nil {
return fmt.Errorf("parsing declared media type: %w", err)
}
if foundType == dType {
return nil
}
}
return fmt.Errorf("undeclared SBOM media type: '%s'", foundType)
}