diff --git a/store/image.go b/store/image.go index ddf5a822a6915f60452ffc829cd93f176108a582..42f52895477368c05872fa49bcc77d3e61abddbc 100644 --- a/store/image.go +++ b/store/image.go @@ -285,7 +285,7 @@ func (s *Store) ImageAdd(data []byte, flake string) Image { // ImageUpdate updates image metadata. func (s *Store) ImageUpdate(hash, source, parent, commentary, commentaryTranslation string) { // Only accept URLs and below 1024 in length - if len(source) >= 1024 || !urlRegex.MatchString(source) { + if len(source) >= 1024 || !s.MatchURL(source) { return } diff --git a/store/misc.go b/store/misc.go index a1371988ef1a4b54051105c018de83eedc9c898b..b72fa66de57293797eeacc73b8fae7069d69144d 100644 --- a/store/misc.go +++ b/store/misc.go @@ -2,6 +2,7 @@ package store import ( "errors" + "net/url" "regexp" ) @@ -11,7 +12,6 @@ var ( nameRegex = regexp.MustCompile(`^[a-z0-9()_-]{3,}$`) sha256Regex = regexp.MustCompile(`\b[A-Fa-f0-9]{64}\b`) secretRegex = regexp.MustCompile(`\b[A-Za-z]{64}\b`) - urlRegex = regexp.MustCompile(`(\b(https?|ftp)://)?[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]`) ) var ( @@ -19,6 +19,15 @@ var ( AlreadyExists = errors.New("store path already exists") ) -func (s *Store) MatchName(subject string) bool { - return nameRegex.MatchString(subject) +// These two really shouldn't be methods... Maybe change that for v2. + +// MatchName determines if str is a valid name. +func (s *Store) MatchName(str string) bool { + return nameRegex.MatchString(str) +} + +// MatchURL determines if str is a valid URL. +func (s *Store) MatchURL(str string) bool { + u, err := url.Parse(str) + return err == nil && u.Scheme != "" && u.Host != "" }