From 8a2821d542a13e5987eefb055d6f643dc58ea6c7 Mon Sep 17 00:00:00 2001 From: Neil Booth Date: Sun, 9 Apr 2017 14:15:10 +0900 Subject: [PATCH] Reject invalid hostnames in Env --- lib/util.py | 2 +- server/env.py | 7 ++++--- server/peers.py | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/util.py b/lib/util.py index e6fdd9a..78006b9 100644 --- a/lib/util.py +++ b/lib/util.py @@ -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(".")) diff --git a/server/env.py b/server/env.py index 600dc9d..c9f0b56 100644 --- a/server/env.py +++ b/server/env.py @@ -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))) diff --git a/server/peers.py b/server/peers.py index 9f5e651..51d6674 100644 --- a/server/peers.py +++ b/server/peers.py @@ -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):