github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/syft/pkg/cataloger/common/cpe/vendors_from_url.go (about)

     1  package cpe
     2  
     3  import (
     4  	"regexp"
     5  	"strings"
     6  
     7  	"github.com/anchore/syft/internal"
     8  )
     9  
    10  var (
    11  	urlPrefixToVendors = map[string][]string{
    12  		"https://www.gnu.org/":         {"gnu"},
    13  		"https://developer.gnome.org/": {"gnome"},
    14  		"https://www.ruby-lang.org/":   {"ruby-lang"},
    15  		"https://llvm.org/":            {"llvm"},
    16  		"https://www.isc.org/":         {"isc"},
    17  		"https://musl.libc.org/":       {"musl-libc"},
    18  		"https://www.mozilla.org/":     {"mozilla"},
    19  		"https://www.x.org/":           {"x.org"},
    20  		"https://w1.fi/":               {"w1.fi"},
    21  	}
    22  
    23  	vendorExtractionPatterns = []*regexp.Regexp{
    24  		regexp.MustCompile(`^(?:https|http|git)://(?:github|gitlab)\.com/(?P<vendor>[\w\-]*?)/.*$`),
    25  	}
    26  )
    27  
    28  func candidateVendorsFromURL(url string) fieldCandidateSet {
    29  	vendors := newFieldCandidateSet()
    30  
    31  	for urlPrefix, additionalVendors := range urlPrefixToVendors {
    32  		if strings.HasPrefix(url, urlPrefix) {
    33  			for _, v := range additionalVendors {
    34  				vendors.add(fieldCandidate{
    35  					value:                       v,
    36  					disallowSubSelections:       true,
    37  					disallowDelimiterVariations: true,
    38  				})
    39  
    40  				return vendors
    41  			}
    42  		}
    43  	}
    44  
    45  	for _, p := range vendorExtractionPatterns {
    46  		groups := internal.MatchNamedCaptureGroups(p, url)
    47  
    48  		if v, ok := groups["vendor"]; ok {
    49  			vendors.add(fieldCandidate{
    50  				value:                       v,
    51  				disallowSubSelections:       true,
    52  				disallowDelimiterVariations: true,
    53  			})
    54  
    55  			return vendors
    56  		}
    57  	}
    58  
    59  	return vendors
    60  }