Skip to content

Commit

Permalink
add url validation
Browse files Browse the repository at this point in the history
  • Loading branch information
roelarents committed Dec 10, 2024
1 parent c65d7c3 commit dfad0e5
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions config/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ package config

import (
"encoding/json"
"fmt"
"net/url"
"regexp"
"strings"

"gopkg.in/yaml.v3"
)

var (
validURLRegexp = regexp.MustCompile(`^(https?://.+|\$\{.+\}.*)$`) // https://regex101.com/r/IvhP6H/1
)

// URL Custom net.URL compatible with YAML and JSON (un)marshalling and kubebuilder.
// In addition, it also removes trailing slash if present, so we can easily
// append a longer path without having to worry about double slashes.
Expand Down Expand Up @@ -78,5 +84,8 @@ func (u *URL) DeepCopy() *URL {
}

func parseURL(s string) (*url.URL, error) {
if !validURLRegexp.MatchString(s) {
return nil, fmt.Errorf("invalid URL: %s", s)
}
return url.Parse(strings.TrimSuffix(s, "/"))
}

0 comments on commit dfad0e5

Please sign in to comment.