Przeglądaj źródła

more permissive hostname validation

In particular, allow hostnames without periods (like on a LAN).
This shouldn't be a client compability concern since we allow
vhosts without periods.
tags/v2.0.0-rc1
Shivaram Lingamneni 4 lat temu
rodzic
commit
f1e2bbc0e4
3 zmienionych plików z 28 dodań i 12 usunięć
  1. 1
    1
      irc/config.go
  2. 12
    9
      irc/utils/net.go
  3. 15
    2
      irc/utils/net_test.go

+ 1
- 1
irc/config.go Wyświetl plik

593
 	if config.Server.Name == "" {
593
 	if config.Server.Name == "" {
594
 		return nil, ErrServerNameMissing
594
 		return nil, ErrServerNameMissing
595
 	}
595
 	}
596
-	if !utils.IsHostname(config.Server.Name) {
596
+	if !utils.IsServerName(config.Server.Name) {
597
 		return nil, ErrServerNameNotHostname
597
 		return nil, ErrServerNameNotHostname
598
 	}
598
 	}
599
 	if config.Datastore.Path == "" {
599
 	if config.Datastore.Path == "" {

+ 12
- 9
irc/utils/net.go Wyświetl plik

6
 
6
 
7
 import (
7
 import (
8
 	"net"
8
 	"net"
9
+	"regexp"
9
 	"strings"
10
 	"strings"
10
 )
11
 )
11
 
12
 
13
 	// subnet mask for an ipv6 /128:
14
 	// subnet mask for an ipv6 /128:
14
 	mask128             = net.CIDRMask(128, 128)
15
 	mask128             = net.CIDRMask(128, 128)
15
 	IPv4LoopbackAddress = net.ParseIP("127.0.0.1").To16()
16
 	IPv4LoopbackAddress = net.ParseIP("127.0.0.1").To16()
17
+
18
+	validHostnameLabelRegexp = regexp.MustCompile(`^[0-9A-Za-z.\-]+$`)
16
 )
19
 )
17
 
20
 
18
 // AddrIsLocal returns whether the address is from a trusted local connection (loopback or unix).
21
 // AddrIsLocal returns whether the address is from a trusted local connection (loopback or unix).
49
 	return ipStr
52
 	return ipStr
50
 }
53
 }
51
 
54
 
52
-var allowedHostnameChars = "abcdefghijklmnopqrstuvwxyz1234567890-."
53
-
54
 // IsHostname returns whether we consider `name` a valid hostname.
55
 // IsHostname returns whether we consider `name` a valid hostname.
55
 func IsHostname(name string) bool {
56
 func IsHostname(name string) bool {
56
-	// IRC hostnames specifically require a period
57
-	if !strings.Contains(name, ".") || len(name) < 1 || len(name) > 253 {
57
+	name = strings.TrimSuffix(name, ".")
58
+	if len(name) < 1 || len(name) > 253 {
58
 		return false
59
 		return false
59
 	}
60
 	}
60
 
61
 
63
 		if len(part) < 1 || len(part) > 63 || strings.HasPrefix(part, "-") || strings.HasSuffix(part, "-") {
64
 		if len(part) < 1 || len(part) > 63 || strings.HasPrefix(part, "-") || strings.HasSuffix(part, "-") {
64
 			return false
65
 			return false
65
 		}
66
 		}
66
-	}
67
-
68
-	// ensure all chars of hostname are valid
69
-	for _, char := range strings.Split(strings.ToLower(name), "") {
70
-		if !strings.Contains(allowedHostnameChars, char) {
67
+		if !validHostnameLabelRegexp.MatchString(part) {
71
 			return false
68
 			return false
72
 		}
69
 		}
73
 	}
70
 	}
75
 	return true
72
 	return true
76
 }
73
 }
77
 
74
 
75
+// IsServerName returns whether we consider `name` a valid IRC server name.
76
+func IsServerName(name string) bool {
77
+	// IRC server names specifically require a period
78
+	return IsHostname(name) && strings.IndexByte(name, '.') != -1
79
+}
80
+
78
 // Convenience to test whether `ip` is contained in any of `nets`.
81
 // Convenience to test whether `ip` is contained in any of `nets`.
79
 func IPInNets(ip net.IP, nets []net.IPNet) bool {
82
 func IPInNets(ip net.IP, nets []net.IPNet) bool {
80
 	for _, network := range nets {
83
 	for _, network := range nets {

+ 15
- 2
irc/utils/net_test.go Wyświetl plik

24
 		"gsf.ds342.co.uk",
24
 		"gsf.ds342.co.uk",
25
 		"324.net.uk",
25
 		"324.net.uk",
26
 		"xn--bcher-kva.ch",
26
 		"xn--bcher-kva.ch",
27
+		"pentos",
28
+		"pentos.",
29
+		"www.google.com.",
27
 	}
30
 	}
28
 
31
 
29
 	badHostnames = []string{
32
 	badHostnames = []string{
30
 		"-lol-.net.uk",
33
 		"-lol-.net.uk",
31
 		"-lol.net.uk",
34
 		"-lol.net.uk",
32
 		"_irc._sctp.lol.net.uk",
35
 		"_irc._sctp.lol.net.uk",
33
-		"irc",
34
-		"com",
36
+		"irc.l%l.net.uk",
37
+		"irc..net.uk",
38
+		".",
35
 		"",
39
 		"",
36
 	}
40
 	}
37
 )
41
 )
56
 	}
60
 	}
57
 }
61
 }
58
 
62
 
63
+func TestIsServerName(t *testing.T) {
64
+	if IsServerName("pentos") {
65
+		t.Error("irc server names must contain a period")
66
+	}
67
+	if !IsServerName("darwin.network") {
68
+		t.Error("failed to validate a perfectly good server name")
69
+	}
70
+}
71
+
59
 func TestNormalizeToNet(t *testing.T) {
72
 func TestNormalizeToNet(t *testing.T) {
60
 	a := net.ParseIP("8.8.8.8")
73
 	a := net.ParseIP("8.8.8.8")
61
 	b := net.ParseIP("8.8.4.4")
74
 	b := net.ParseIP("8.8.4.4")

Ładowanie…
Anuluj
Zapisz