From a92910088925098b84c857e911d7af71bed26104 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Tue, 4 Sep 2018 09:26:29 +0200 Subject: [PATCH] Allow to disable ip check & define trusted subnets Will need manually editing config.yaml for the time being. Someone who is not able to edit a yaml file should not be able to disable the warning. --- src/octoprint/server/api/__init__.py | 4 +++- src/octoprint/server/util/flask.py | 8 ++++++-- src/octoprint/settings.py | 4 ++++ src/octoprint/util/net.py | 15 ++++++++++++--- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/octoprint/server/api/__init__.py b/src/octoprint/server/api/__init__.py index 863426d73..bf9b7efbe 100644 --- a/src/octoprint/server/api/__init__.py +++ b/src/octoprint/server/api/__init__.py @@ -216,7 +216,9 @@ def login(): identity_changed.send(current_app._get_current_object(), identity=Identity(user.get_id())) response = user.asDict() - response["_is_external_client"] = not util_net.is_lan_address(get_remote_address(request)) + response["_is_external_client"] = s().getBoolean(["server", "ipCheck", "enabled"]) \ + and not util_net.is_lan_address(get_remote_address(request), + additional_private=s().get(["server", "ipCheck", "trustedSubnets"])) return jsonify(response) return make_response(("User unknown or password incorrect", 401, [])) diff --git a/src/octoprint/server/util/flask.py b/src/octoprint/server/util/flask.py index 5aaa0fb7c..822e20c0b 100644 --- a/src/octoprint/server/util/flask.py +++ b/src/octoprint/server/util/flask.py @@ -516,6 +516,8 @@ def passive_login(): user = flask_login.current_user remoteAddr = get_remote_address(flask.request) + ipCheckEnabled = settings().getBoolean(["server", "ipCheck", "enabled"]) + ipCheckTrusted = settings().get(["server", "ipCheck", "trustedSubnets"]) if user is not None and not user.is_anonymous() and user.is_active(): flask_principal.identity_changed.send(flask.current_app._get_current_object(), @@ -525,7 +527,8 @@ def passive_login(): flask.g.user = user response = user.asDict() - response["_is_external_client"] = not is_lan_address(remoteAddr) + response["_is_external_client"] = ipCheckEnabled and not is_lan_address(remoteAddr, + additional_private=ipCheckTrusted) return flask.jsonify(response) elif settings().getBoolean(["accessControl", "autologinLocal"]) \ @@ -549,7 +552,8 @@ def passive_login(): identity=flask_principal.Identity(user.get_id())) response = user.asDict() - response["_is_external_client"] = not is_lan_address(remoteAddr) + response["_is_external_client"] = ipCheckEnabled and not is_lan_address(remoteAddr, + additional_private=ipCheckTrusted) return flask.jsonify(response) except: logger = logging.getLogger(__name__) diff --git a/src/octoprint/settings.py b/src/octoprint/settings.py index 72b85a696..4358d16bf 100644 --- a/src/octoprint/settings.py +++ b/src/octoprint/settings.py @@ -198,6 +198,10 @@ default_settings = { "preemptiveCache": { "exceptions": [], "until": 7 + }, + "ipCheck": { + "enabled": True, + "trustedSubnets": [] } }, "webcam": { diff --git a/src/octoprint/util/net.py b/src/octoprint/util/net.py index 1c169a755..030f8e3b3 100644 --- a/src/octoprint/util/net.py +++ b/src/octoprint/util/net.py @@ -22,15 +22,24 @@ else: # Whatever we are running on here, it we don't want to use V6 on here due to lack of dual stack support HAS_V6 = False -def is_lan_address(address): +def is_lan_address(address, additional_private=None): import netaddr import netifaces ip = netaddr.IPAddress(address) - if ip.is_private(): + if ip.is_private() or ip.is_loopback(): return True + if additional_private is None or not isinstance(additional_private, (list, tuple)): + additional_private = [] + subnets = netaddr.IPSet() + for additional in additional_private: + try: + subnets.add(netaddr.IPNetwork(additional)) + except: + pass + for interface in netifaces.interfaces(): addrs = netifaces.ifaddresses(interface) for v4 in addrs.get(socket.AF_INET, ()): @@ -42,7 +51,7 @@ def is_lan_address(address): if HAS_V6: for v6 in addrs.get(socket.AF_INET6, ()): try: - subnets.add(netaddr.IPNetwork(v6["addr"], v6["addr"])) + subnets.add(netaddr.IPNetwork(v6["addr"], v6["netmask"])) except: pass