Detect connections to the UI from external ips

Show warning to protect the innocent who don't know better than to
port forward.

Warning is ignorable (per browser) to allow users who know what they are
doing to easily get rid of it. If you don't know what you are doing and
also actively ignore THIS warning I really can't help you anymore.
This commit is contained in:
Gina Häußge committed 2018-09-03 21:23:08 +02:00
1 parent 5dbc88b6f2
commit 286376321c
4 files changed
+95 -5

No files matched your search

+7 -2
View File
@@ -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:
+12 -3
View File
@@ -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))
@@ -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("<p>It seems that you are connecting to OctoPrint over the public internet.</p>" +
"<p>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.</p><p><strong>Please see " +
"<a href=\"%(url)s\" target=\"_blank\" rel=\"noreferrer noopener\">this blog post</a> for " +
"ways to safely access your OctoPrint instance from remote.</strong></p>" +
"<p><small>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.</small></p>");
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();
+29
View File
@@ -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