Skip to content

Commit

Permalink
improve SBOM validation
Browse files Browse the repository at this point in the history
  • Loading branch information
sagnik3788 committed Jan 18, 2024
1 parent 1d2cd33 commit 6c1a4cb
Showing 1 changed file with 22 additions and 2 deletions.
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 @@ -371,10 +372,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)
}

0 comments on commit 6c1a4cb

Please sign in to comment.