summaryrefslogtreecommitdiffstats
path: root/model/utils.go
diff options
context:
space:
mode:
authorMartin Kraft <mkraft@users.noreply.github.com>2018-07-30 14:59:08 -0400
committerGitHub <noreply@github.com>2018-07-30 14:59:08 -0400
commit65cd447a61efa852da2c0e7db25f385c2436e236 (patch)
tree15954e1f0a1405cb916e19462174c7186684e7b2 /model/utils.go
parentd23ca07133e9bc5eed14d87af563471b4ef963cd (diff)
downloadchat-65cd447a61efa852da2c0e7db25f385c2436e236.tar.gz
chat-65cd447a61efa852da2c0e7db25f385c2436e236.tar.bz2
chat-65cd447a61efa852da2c0e7db25f385c2436e236.zip
MM-11301: Validates listen address config value. (#9138)
* MM-11301: Validates listen address config value. * MM-11301: Adds some invalid port test cases. * MM-11301: Accept domain names. * MM-11301: Fix for max port.
Diffstat (limited to 'model/utils.go')
-rw-r--r--model/utils.go54
1 files changed, 54 insertions, 0 deletions
diff --git a/model/utils.go b/model/utils.go
index 9f8ef90e1..892f8c278 100644
--- a/model/utils.go
+++ b/model/utils.go
@@ -536,3 +536,57 @@ func checkNowhereNil(t *testing.T, name string, value interface{}) bool {
return true
}
}
+
+// Copied from https://golang.org/src/net/dnsclient.go#L119
+func IsDomainName(s string) bool {
+ // See RFC 1035, RFC 3696.
+ // Presentation format has dots before every label except the first, and the
+ // terminal empty label is optional here because we assume fully-qualified
+ // (absolute) input. We must therefore reserve space for the first and last
+ // labels' length octets in wire format, where they are necessary and the
+ // maximum total length is 255.
+ // So our _effective_ maximum is 253, but 254 is not rejected if the last
+ // character is a dot.
+ l := len(s)
+ if l == 0 || l > 254 || l == 254 && s[l-1] != '.' {
+ return false
+ }
+
+ last := byte('.')
+ ok := false // Ok once we've seen a letter.
+ partlen := 0
+ for i := 0; i < len(s); i++ {
+ c := s[i]
+ switch {
+ default:
+ return false
+ case 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || c == '_':
+ ok = true
+ partlen++
+ case '0' <= c && c <= '9':
+ // fine
+ partlen++
+ case c == '-':
+ // Byte before dash cannot be dot.
+ if last == '.' {
+ return false
+ }
+ partlen++
+ case c == '.':
+ // Byte before dot cannot be dot, dash.
+ if last == '.' || last == '-' {
+ return false
+ }
+ if partlen > 63 || partlen == 0 {
+ return false
+ }
+ partlen = 0
+ }
+ last = c
+ }
+ if last == '-' || partlen > 63 {
+ return false
+ }
+
+ return ok
+}