Skip to content

Commit

Permalink
feat: extract username as vendor candidate from github/gitlab
Browse files Browse the repository at this point in the history
Signed-off-by: Weston Steimel <weston.steimel@anchore.com>
  • Loading branch information
westonsteimel committed Feb 25, 2023
1 parent 187c745 commit ac012d9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
27 changes: 25 additions & 2 deletions syft/pkg/cataloger/common/cpe/vendors_from_url.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,56 @@
package cpe

import (
"regexp"
"strings"

"github.com/anchore/syft/internal"
)

var (
urlPrefixVendors = map[string][]string{
urlPrefixToVendors = map[string][]string{
"https://www.gnu.org/": {"gnu"},
"https://developer.gnome.org/": {"gnome"},
"https://www.ruby-lang.org/": {"ruby-lang"},
"https://llvm.org/": {"llvm"},
"https://www.isc.org/": {"isc"},
}

vendorExtractionPatterns = []*regexp.Regexp{
regexp.MustCompile(`^https://(?:github|gitlab)\.com/(?P<vendor>[\w\-]*?)/.*$`),
}
)

func candidateVendorsFromURL(url string) fieldCandidateSet {
vendors := newFieldCandidateSet()

for urlPrefix, additionalVendors := range urlPrefixVendors {
for urlPrefix, additionalVendors := range urlPrefixToVendors {
if strings.HasPrefix(url, urlPrefix) {
for _, v := range additionalVendors {
vendors.add(fieldCandidate{
value: v,
disallowSubSelections: true,
disallowDelimiterVariations: true,
})

return vendors
}
}
}

for _, p := range vendorExtractionPatterns {
groups := internal.MatchNamedCaptureGroups(p, url)

if v, ok := groups["vendor"]; ok {
vendors.add(fieldCandidate{
value: v,
disallowSubSelections: true,
disallowDelimiterVariations: true,
})

return vendors
}
}

return vendors
}
25 changes: 25 additions & 0 deletions syft/pkg/cataloger/common/cpe/vendors_from_url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,31 @@ func Test_candidateVendorsFromURL(t *testing.T) {
url: "https://www.gnu.org/software/make",
expected: []string{"gnu"},
},
{
name: "github username as vendor",
url: "https://github.com/armadillo/abcxyz-12345",
expected: []string{"armadillo"},
},
{
name: "github username with - as vendor",
url: "https://github.com/1234-abc-xyz/hello",
expected: []string{"1234-abc-xyz"},
},
{
name: "gitlab username as vendor",
url: "https://gitlab.com/armadillo/abcxyz-12345",
expected: []string{"armadillo"},
},
{
name: "gitlab username with - as vendor",
url: "https://gitlab.com/1234-abc-xyz/hello",
expected: []string{"1234-abc-xyz"},
},
{
name: "github username as vendor from longer url",
url: "https://github.com/armadillo/abcxyz-12345/a/b/c/d/e/f/g",
expected: []string{"armadillo"},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Expand Down

0 comments on commit ac012d9

Please sign in to comment.