diff --git a/src/Config.py b/src/Config.py index 7f0a35ae..f884511f 100644 --- a/src/Config.py +++ b/src/Config.py @@ -13,7 +13,7 @@ class Config(object): def __init__(self, argv): self.version = "0.6.4" - self.rev = 3741 + self.rev = 3742 self.argv = argv self.action = None self.pending_changes = {} diff --git a/src/Ui/UiRequest.py b/src/Ui/UiRequest.py index 42ca72b8..894f449e 100644 --- a/src/Ui/UiRequest.py +++ b/src/Ui/UiRequest.py @@ -4,7 +4,6 @@ import os import mimetypes import json import cgi -import socket import gevent @@ -49,31 +48,6 @@ class UiRequest(object): self.user = None self.script_nonce = None # Nonce for script tags in wrapper html - # Test if a string is a valid IP address - def isIp(self, host, strip_port=False): - if strip_port: - # Remove the port from the IP address - host = ":".join(host.split(":")[:-1]) - - try: - # This function will return an exception on a non-valid IP - # address - socket.inet_aton(host) - return True - - except socket.error: - # Try for a IPv6 address - try: - socket.inet_pton(socket.AF_INET6, host) - return True - - except socket.error: - if not strip_port: - # Try stripping the port and re-checking - return self.isIp(host, strip_port=True) - - return False - def learnHost(self, host): self.server.allowed_hosts.add(host) self.server.log.info("Added %s as allowed host" % host) @@ -84,7 +58,11 @@ class UiRequest(object): # Allow any IP address as they are not affected by DNS rebinding # attacks - if self.isIp(host): + if helper.isIp(host): + self.learnHost(host) + return True + + if ":" in host and helper.isIp(host.rsplit(":", 1)[0]): # Test without port self.learnHost(host) return True diff --git a/src/util/helper.py b/src/util/helper.py index 298ebced..440400cb 100644 --- a/src/util/helper.py +++ b/src/util/helper.py @@ -219,6 +219,21 @@ def avg(items): else: return 0 +def isIp(ip): + if ":" in ip: # IPv6 + try: + socket.inet_pton(socket.AF_INET6, ip) + return True + except: + return False + + else: # IPv4 + try: + socket.inet_aton(ip) + return True + except: + return False + local_ip_pattern = re.compile(r"^(127\.)|(192\.168\.)|(10\.)|(172\.1[6-9]\.)|(172\.2[0-9]\.)|(172\.3[0-1]\.)|(::1$)|([fF][cCdD])") def isPrivateIp(ip): return local_ip_pattern.match(ip)