Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
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: 16 additions & 8 deletions syft/cataloger/cpe.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cataloger

import (
"fmt"
"sort"
"strings"

"github.com/anchore/syft/internal"
Expand All @@ -12,6 +13,17 @@ import (
// this is functionally equivalent to "*" and consistent with no input given (thus easier to test)
const any = ""

func newCPE(product, vendor, version, targetSW string) wfn.Attributes {
cpe := *(wfn.NewAttributesWithAny())
cpe.Part = "a"
cpe.Product = product
cpe.Vendor = vendor
cpe.Version = version
cpe.TargetSW = targetSW

return cpe
}

// generatePackageCPEs Create a list of CPEs, trying to guess the vendor, product tuple and setting TargetSoftware if possible
func generatePackageCPEs(p pkg.Package) []pkg.CPE {
targetSws := candidateTargetSoftwareAttrs(p)
Expand All @@ -31,18 +43,14 @@ func generatePackageCPEs(p pkg.Package) []pkg.CPE {
keys.Add(key)

// add a new entry...
candidateCpe := wfn.NewAttributesWithAny()
candidateCpe.Part = "a"
candidateCpe.Product = product
candidateCpe.Vendor = vendor
candidateCpe.Version = p.Version
candidateCpe.TargetSW = targetSw

cpes = append(cpes, *candidateCpe)
c := newCPE(product, vendor, p.Version, targetSw)
cpes = append(cpes, c)
}
}
}

sort.Sort(ByCPESpecificity(cpes))

return cpes
}

Expand Down
31 changes: 31 additions & 0 deletions syft/cataloger/cpe_specificity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cataloger

import "github.com/facebookincubator/nvdtools/wfn"

type ByCPESpecificity []wfn.Attributes

// Implementing sort.Interface
func (c ByCPESpecificity) Len() int { return len(c) }
func (c ByCPESpecificity) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
func (c ByCPESpecificity) Less(i, j int) bool {
return countSpecifiedFields(c[i]) > countSpecifiedFields(c[j])
}

func countSpecifiedFields(cpe wfn.Attributes) int {
checksForSpecifiedField := []func(cpe wfn.Attributes) bool{
func(cpe wfn.Attributes) bool { return cpe.Part != "" },
func(cpe wfn.Attributes) bool { return cpe.Product != "" },
func(cpe wfn.Attributes) bool { return cpe.Vendor != "" },
func(cpe wfn.Attributes) bool { return cpe.Version != "" },
func(cpe wfn.Attributes) bool { return cpe.TargetSW != "" },
}

count := 0
for _, fieldIsSpecified := range checksForSpecifiedField {
if fieldIsSpecified(cpe) {
count++
}
}

return count
}