mirror of
https://github.com/anchore/syft.git
synced 2026-11-05 17:06:32 +01:00
3122 valid license url characters (#3449)
* chore: strip unwanted characters from license URL --------- Signed-off-by: Christopher Phillips <32073428+spiffcs@users.noreply.github.com>
This commit is contained in:
+24
-2
@@ -2,7 +2,9 @@ package pkg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/scylladb/go-set/strset"
|
||||
|
||||
@@ -112,7 +114,12 @@ func NewLicenseFromURLs(value string, urls ...string) License {
|
||||
s := strset.New()
|
||||
for _, url := range urls {
|
||||
if url != "" {
|
||||
s.Add(url)
|
||||
sanitizedURL, err := stripUnwantedCharacters(url)
|
||||
if err != nil {
|
||||
log.Tracef("unable to sanitize url=%q: %s", url, err)
|
||||
continue
|
||||
}
|
||||
s.Add(sanitizedURL)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,13 +129,28 @@ func NewLicenseFromURLs(value string, urls ...string) License {
|
||||
return l
|
||||
}
|
||||
|
||||
func stripUnwantedCharacters(rawURL string) (string, error) {
|
||||
cleanedURL := strings.TrimSpace(rawURL)
|
||||
_, err := url.ParseRequestURI(cleanedURL)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("invalid URL: %w", err)
|
||||
}
|
||||
|
||||
return cleanedURL, nil
|
||||
}
|
||||
|
||||
func NewLicenseFromFields(value, url string, location *file.Location) License {
|
||||
l := NewLicense(value)
|
||||
if location != nil {
|
||||
l.Locations.Add(*location)
|
||||
}
|
||||
if url != "" {
|
||||
l.URLs = append(l.URLs, url)
|
||||
sanitizedURL, err := stripUnwantedCharacters(url)
|
||||
if err != nil {
|
||||
log.Tracef("unable to sanitize url=%q: %s", url, err)
|
||||
} else {
|
||||
l.URLs = append(l.URLs, sanitizedURL)
|
||||
}
|
||||
}
|
||||
|
||||
return l
|
||||
|
||||
Reference in New Issue
Block a user