]> git.ipfire.org Git - thirdparty/wireguard-apple.git/commitdiff
Model: Endpoint host should not have invalid characters
authorRoopesh Chander <roop@roopc.net>
Tue, 23 Oct 2018 10:44:10 +0000 (16:14 +0530)
committerRoopesh Chander <roop@roopc.net>
Sat, 27 Oct 2018 09:43:01 +0000 (15:13 +0530)
Signed-off-by: Roopesh Chander <roop@roopc.net>
WireGuard/WireGuard/Model/Endpoint.swift

index 0b1619b566b23f280b98f4734ec508aeeef7fc5c..c8ade2f85b91eab7d66c5d04b1b697ef1f594dc0 100644 (file)
@@ -23,26 +23,29 @@ extension Endpoint {
         // Separation of host and port is based on 'parse_endpoint' function in
         // https://git.zx2c4.com/WireGuard/tree/src/tools/config.c
         guard (!string.isEmpty) else { return nil }
+        let startOfPort: String.Index
+        let hostString: String
         if (string.first! == "[") {
             // Look for IPv6-style endpoint, like [::1]:80
             let startOfHost = string.index(after: string.startIndex)
             guard let endOfHost = string.dropFirst().firstIndex(of: "]") else { return nil }
             let afterEndOfHost = string.index(after: endOfHost)
             guard (string[afterEndOfHost] == ":") else { return nil }
-            let startOfPort = string.index(after: afterEndOfHost)
-            let hostString = String(string[startOfHost ..< endOfHost])
-            guard let endpointPort = NWEndpoint.Port(String(string[startOfPort ..< string.endIndex])) else { return nil }
-            host = NWEndpoint.Host(hostString)
-            port = endpointPort
+            startOfPort = string.index(after: afterEndOfHost)
+            hostString = String(string[startOfHost ..< endOfHost])
         } else {
             // Look for an IPv4-style endpoint, like 127.0.0.1:80
             guard let endOfHost = string.firstIndex(of: ":") else { return nil }
-            let startOfPort = string.index(after: endOfHost)
-            let hostString = String(string[string.startIndex ..< endOfHost])
-            guard let endpointPort = NWEndpoint.Port(String(string[startOfPort ..< string.endIndex])) else { return nil }
-            host = NWEndpoint.Host(hostString)
-            port = endpointPort
+            startOfPort = string.index(after: endOfHost)
+            hostString = String(string[string.startIndex ..< endOfHost])
         }
+        guard let endpointPort = NWEndpoint.Port(String(string[startOfPort ..< string.endIndex])) else { return nil }
+        let invalidCharacterIndex = hostString.unicodeScalars.firstIndex { (c) -> Bool in
+            return !CharacterSet.urlHostAllowed.contains(c)
+        }
+        guard (invalidCharacterIndex == nil) else { return nil }
+        host = NWEndpoint.Host(hostString)
+        port = endpointPort
     }
     func stringRepresentation() -> String {
         switch (host) {