validate TLD

This commit is contained in:
Timon Ringwald 2022-05-08 18:38:42 +02:00
parent 95bb6041a3
commit fd01dbc34e
2 changed files with 1112 additions and 5 deletions

19
main.go
View File

@ -107,13 +107,22 @@ func search(w http.ResponseWriter, r *http.Request) {
}
func ParseURI(uri string) (*url.URL, error) {
if !strings.ContainsRune(uri, '.') {
return nil, errors.New("no top level domain")
}
if !strings.HasPrefix(uri, "http://") && !strings.HasPrefix(uri, "https://") {
uri = "https://" + uri
}
ret, err := url.ParseRequestURI(uri)
return ret, err
if err != nil {
return nil, err
}
splits := strings.Split(ret.Hostname(), ".")
if len(splits) <= 1 {
return nil, errors.New("hostname doesn't have a TLD")
}
if _, ok := TLDs[splits[len(splits)-1]]; !ok {
return nil, errors.New("invalid top level domain")
}
return ret, nil
}

1098
top_level_domains.go Normal file

File diff suppressed because it is too large Load Diff