diff --git a/src/octoprint/server/api/__init__.py b/src/octoprint/server/api/__init__.py index 54742801b..863426d73 100644 --- a/src/octoprint/server/api/__init__.py +++ b/src/octoprint/server/api/__init__.py @@ -13,13 +13,14 @@ from flask_login import login_user, logout_user, current_user from flask_principal import Identity, identity_changed, AnonymousIdentity import octoprint.util as util +import octoprint.util.net as util_net import octoprint.users import octoprint.server import octoprint.plugin from octoprint.server import admin_permission, NO_CONTENT from octoprint.settings import settings as s, valid_boolean_trues from octoprint.server.util import noCachingExceptGetResponseHandler, enforceApiKeyRequestHandler, loginFromApiKeyRequestHandler, loginFromAuthorizationHeaderRequestHandler, corsRequestHandler, corsResponseHandler -from octoprint.server.util.flask import restricted_access, get_json_command_from_request, passive_login +from octoprint.server.util.flask import restricted_access, get_json_command_from_request, passive_login, get_remote_address #~~ init api blueprint, including sub modules @@ -213,7 +214,11 @@ def login(): g.user = user login_user(user, remember=remember) identity_changed.send(current_app._get_current_object(), identity=Identity(user.get_id())) - return jsonify(user.asDict()) + + response = user.asDict() + response["_is_external_client"] = not util_net.is_lan_address(get_remote_address(request)) + return jsonify(response) + return make_response(("User unknown or password incorrect", 401, [])) elif "passive" in data: diff --git a/src/octoprint/server/util/flask.py b/src/octoprint/server/util/flask.py index 2ab536e36..5aaa0fb7c 100644 --- a/src/octoprint/server/util/flask.py +++ b/src/octoprint/server/util/flask.py @@ -31,6 +31,7 @@ import octoprint.plugin from octoprint.util import DefaultOrderedDict from octoprint.util.json import JsonEncoding +from octoprint.util.net import is_lan_address from werkzeug.contrib.cache import BaseCache @@ -514,13 +515,19 @@ def passive_login(): else: user = flask_login.current_user + remoteAddr = get_remote_address(flask.request) + 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(), identity=flask_principal.Identity(user.get_id())) if hasattr(user, "session"): flask.session["usersession.id"] = user.session flask.g.user = user - return flask.jsonify(user.asDict()) + + response = user.asDict() + response["_is_external_client"] = not is_lan_address(remoteAddr) + return flask.jsonify(response) + elif settings().getBoolean(["accessControl", "autologinLocal"]) \ and settings().get(["accessControl", "autologinAs"]) is not None \ and settings().get(["accessControl", "localNetworks"]) is not None: @@ -531,7 +538,6 @@ def passive_login(): localNetworks.add(ip) try: - remoteAddr = get_remote_address(flask.request) if netaddr.IPAddress(remoteAddr) in localNetworks: user = octoprint.server.userManager.findUser(autologinAs) if user is not None and user.is_active(): @@ -541,7 +547,10 @@ def passive_login(): flask_login.login_user(user) flask_principal.identity_changed.send(flask.current_app._get_current_object(), identity=flask_principal.Identity(user.get_id())) - return flask.jsonify(user.asDict()) + + response = user.asDict() + response["_is_external_client"] = not is_lan_address(remoteAddr) + return flask.jsonify(response) except: logger = logging.getLogger(__name__) logger.exception("Could not autologin user %s for networks %r" % (autologinAs, localNetworks)) diff --git a/src/octoprint/static/js/app/viewmodels/loginstate.js b/src/octoprint/static/js/app/viewmodels/loginstate.js index 556bb8aa8..b021caaba 100644 --- a/src/octoprint/static/js/app/viewmodels/loginstate.js +++ b/src/octoprint/static/js/app/viewmodels/loginstate.js @@ -60,6 +60,53 @@ $(function() { callViewModels(self.allViewModels, "onUserLoggedIn", [response]); log.info("User " + response.name + " logged in") } + + // Show warning if connecting from what seems to be an external IP address, unless ignored + var ignorePublicAddressWarning = localStorage["loginState.ignorePublicAddressWarning"]; + if (ignorePublicAddressWarning === undefined) { + ignorePublicAddressWarning = false; + } else { + ignorePublicAddressWarning = JSON.parse(ignorePublicAddressWarning); + } + + if (response._is_external_client && !ignorePublicAddressWarning) { + var text = gettext("

It seems that you are connecting to OctoPrint over the public internet.

" + + "

This is strongly discouraged unless you have taken proper network security precautions. " + + "Your printer is an appliance you really should not be giving access to " + + "everyone with an internet connection.

Please see " + + "this blog post for " + + "ways to safely access your OctoPrint instance from remote.

" + + "

If you know what you are doing or you are sure this message is" + + "mistaken since you are in an isolated LAN, feel free to ignore it.

"); + text = _.sprintf(text, {url: "https://octoprint.org/blog/2018/09/03/safe-remote-access/"}); + new PNotify({ + title: gettext("Possible external access detected"), + text: text, + hide: false, + type: "error", + confirm: { + confirm: true, + buttons: [{ + text: gettext("Ignore"), + addClass: "btn btn-danger", + click: function(notice) { + notice.remove(); + localStorage["loginState.ignorePublicAddressWarning"] = JSON.stringify(true); + } + }, { + text: gettext("Later"), + addClass: "btn btn-primary", + click: function(notice) { + notice.remove(); + } + }] + }, + buttons: { + sticker: false + } + + }) + } } else { self.loggedIn(false); self.resetCurrentUserData(); diff --git a/src/octoprint/util/net.py b/src/octoprint/util/net.py index 50e536584..1c169a755 100644 --- a/src/octoprint/util/net.py +++ b/src/octoprint/util/net.py @@ -21,3 +21,32 @@ else: 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): + import netaddr + import netifaces + + ip = netaddr.IPAddress(address) + if ip.is_private(): + return True + + subnets = netaddr.IPSet() + for interface in netifaces.interfaces(): + addrs = netifaces.ifaddresses(interface) + for v4 in addrs.get(socket.AF_INET, ()): + try: + subnets.add(netaddr.IPNetwork(v4["addr"], v4["netmask"])) + except: + pass + + if HAS_V6: + for v6 in addrs.get(socket.AF_INET6, ()): + try: + subnets.add(netaddr.IPNetwork(v6["addr"], v6["addr"])) + except: + pass + + if ip in subnets: + return True + + return False