3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-25 21:39:25 +01:00

Require that server names must be hostnames, and nicks cannot be hostnames

This commit is contained in:
Daniel Oaks 2016-04-21 10:48:15 +10:00
parent 9ac09a7027
commit 77bf7173ff
3 changed files with 32 additions and 0 deletions

View File

@ -110,6 +110,9 @@ func LoadConfig(filename string) (config *Config, err error) {
if config.Server.Name == "" {
return nil, errors.New("Server name missing")
}
if !IsHostname(config.Server.Name) {
return nil, errors.New("Server name must match the format of a hostname")
}
if config.Server.Database == "" {
return nil, errors.New("Server database missing")
}

View File

@ -27,3 +27,28 @@ func LookupHostname(addr Name) Name {
hostname := strings.TrimSuffix(names[0], ".")
return Name(hostname)
}
var allowedHostnameChars = "abcdefghijklmnopqrstuvwxyz1234567890-."
func IsHostname(name string) bool {
// IRC hostnames specifically require a period
if !strings.Contains(name, ".") || len(name) < 1 || len(name) > 253 {
return false
}
// ensure each part of hostname is valid
for _, part := range strings.Split(name, ".") {
if len(part) < 1 || len(part) > 63 || strings.HasPrefix(part, "-") || strings.HasSuffix(part, "-") {
return false
}
}
// ensure all chars of hostname are valid
for _, char := range strings.Split(strings.ToLower(name), "") {
if !strings.Contains(allowedHostnameChars, char) {
return false
}
}
return true
}

View File

@ -47,6 +47,10 @@ func (name Name) IsNickname() bool {
strings.Contains(namestr, "!") || strings.Contains(namestr, "@") {
return false
}
// names that look like hostnames are restricted to servers, as with other ircds
if IsHostname(namestr) {
return false
}
return NicknameExpr.MatchString(namestr)
}