Reject invalid hostnames in Env

This commit is contained in:
Neil Booth 2017-04-09 14:15:10 +09:00
parent b6d8b86dd6
commit 8a2821d542
3 changed files with 6 additions and 5 deletions

View File

@ -249,6 +249,6 @@ def is_valid_hostname(hostname):
if len(hostname) > 255:
return False
# strip exactly one dot from the right, if present
if hostname[-1] == ".":
if hostname and hostname[-1] == ".":
hostname = hostname[:-1]
return all(SEGMENT_REGEX.match(x) for x in hostname.split("."))

View File

@ -14,13 +14,13 @@ from ipaddress import ip_address
from os import environ
from lib.coins import Coin
from lib.util import LoggedClass
import lib.util as lib_util
NetIdentity = namedtuple('NetIdentity', 'host tcp_port ssl_port nick_suffix')
class Env(LoggedClass):
class Env(lib_util.LoggedClass):
'''Wraps environment configuration.'''
class Error(Exception):
@ -126,7 +126,8 @@ class Env(LoggedClass):
try:
ip = ip_address(host)
except ValueError:
bad = host.lower().strip() in ('', 'localhost')
bad = (not lib_util.is_valid_hostname(host)
or hostname.lower() == 'localhost')
else:
bad = (ip.is_multicast or ip.is_unspecified
or (ip.is_private and (self.irc or self.peer_announce)))

View File

@ -118,7 +118,7 @@ class PeerSession(JSONSession):
self.peer.update_features(features)
else:
self.bad = True
self.log_warning('marking DNS alias bad')
self.log_warning('ignoring - not listed in features')
self.close_if_done()
def on_headers(self, result, error):