Return 403 instead of 401 on missing/insufficient credentials
Closes #2297
This commit is contained in:
1 parent
b5aae0b8fa
commit
b1f9e070a4
4 files changed
+25
-16
No files matched your search
@@ -148,6 +148,11 @@ def load_user(id):
|
||||
return None
|
||||
|
||||
|
||||
def unauthorized_user():
|
||||
from flask import abort
|
||||
abort(403)
|
||||
|
||||
|
||||
#~~ startup code
|
||||
|
||||
|
||||
@@ -440,6 +445,7 @@ class Server(object):
|
||||
loginManager = LoginManager()
|
||||
loginManager.session_protection = "strong"
|
||||
loginManager.user_callback = load_user
|
||||
loginManager.unauthorized_callback = unauthorized_user
|
||||
if not userManager.enabled:
|
||||
loginManager.anonymous_user = users.DummyUser
|
||||
principals.identity_loaders.appendleft(users.dummy_identity_loader)
|
||||
|
||||
@@ -240,6 +240,9 @@ def _logout(user):
|
||||
octoprint.server.userManager.logout_user(user)
|
||||
|
||||
|
||||
#~~ Test utils
|
||||
|
||||
|
||||
@api.route("/util/test", methods=["POST"])
|
||||
@restricted_access
|
||||
@admin_permission.require(403)
|
||||
@@ -395,25 +398,25 @@ def utilTestPath():
|
||||
port = int(data["port"])
|
||||
except:
|
||||
return make_response("{!r} is not a valid value for port (must be int)".format(data["port"]), 400)
|
||||
|
||||
|
||||
timeout = 3.05
|
||||
if "timeout" in data:
|
||||
try:
|
||||
timeout = float(data["timeout"])
|
||||
except:
|
||||
return make_response("{!r} is not a valid value for timeout (must be int or float)".format(data["timeout"]), 400)
|
||||
|
||||
|
||||
protocol = data.get("protocol", "tcp")
|
||||
if protocol not in ("tcp", "udp"):
|
||||
return make_response("{!r} is not a valid value for protocol, must be tcp or udp".format(protocol), 400)
|
||||
|
||||
|
||||
from octoprint.util import server_reachable
|
||||
reachable = server_reachable(host, port, timeout=timeout, proto=protocol)
|
||||
|
||||
|
||||
result = dict(host=host,
|
||||
port=port,
|
||||
protocol=protocol,
|
||||
result=reachable)
|
||||
|
||||
|
||||
return jsonify(**result)
|
||||
|
||||
@@ -40,11 +40,11 @@ def enforceApiKeyRequestHandler():
|
||||
apikey = get_api_key(_flask.request)
|
||||
|
||||
if apikey is None:
|
||||
return _flask.make_response("No API key provided", 401)
|
||||
return _flask.make_response("No API key provided", 403)
|
||||
|
||||
if apikey != octoprint.server.UI_API_KEY and not settings().getBoolean(["api", "enabled"]):
|
||||
# api disabled => 401
|
||||
return _flask.make_response("API disabled", 401)
|
||||
# api disabled => 403
|
||||
return _flask.make_response("API disabled", 403)
|
||||
|
||||
apiKeyRequestHandler = deprecated("apiKeyRequestHandler has been renamed to enforceApiKeyRequestHandler")(enforceApiKeyRequestHandler)
|
||||
|
||||
@@ -57,22 +57,22 @@ def loginFromApiKeyRequestHandler():
|
||||
"""
|
||||
|
||||
apikey = get_api_key(_flask.request)
|
||||
|
||||
|
||||
if not apikey:
|
||||
return
|
||||
|
||||
|
||||
if apikey == octoprint.server.UI_API_KEY:
|
||||
return
|
||||
|
||||
|
||||
if octoprint.server.appSessionManager.validate(apikey):
|
||||
return
|
||||
|
||||
|
||||
user = get_user_for_apikey(apikey)
|
||||
if user is not None and _flask.ext.login.login_user(user, remember=False):
|
||||
_flask.ext.principal.identity_changed.send(_flask.current_app._get_current_object(),
|
||||
identity=_flask.ext.principal.Identity(user.get_id()))
|
||||
else:
|
||||
return _flask.make_response("Invalid API key", 401)
|
||||
return _flask.make_response("Invalid API key", 403)
|
||||
|
||||
|
||||
def corsRequestHandler():
|
||||
@@ -153,13 +153,13 @@ def get_user_for_apikey(apikey):
|
||||
if apikey == settings().get(["api", "key"]) or octoprint.server.appSessionManager.validate(apikey):
|
||||
# master key or an app session key was used
|
||||
return ApiUser()
|
||||
|
||||
|
||||
if octoprint.server.userManager.enabled:
|
||||
user = octoprint.server.userManager.findUser(apikey=apikey)
|
||||
if user is not None:
|
||||
# user key was used
|
||||
return user
|
||||
|
||||
|
||||
apikey_hooks = plugin_manager().get_hooks("octoprint.accesscontrol.keyvalidator")
|
||||
for name, hook in apikey_hooks.items():
|
||||
try:
|
||||
|
||||
@@ -2216,7 +2216,7 @@ class MachineCom(object):
|
||||
# Make sure we are only handling one sending job at a time
|
||||
with self._sendingLock:
|
||||
if again:
|
||||
# If we are about to last line from the active resend request
|
||||
# If we are about to send the last line from the active resend request
|
||||
# again, we first need to increment resend delta. It might already
|
||||
# be set to None if the last resend line was already sent, so
|
||||
# if that's the case we set it to 0. It will then be incremented,
|
||||
|
||||
Reference in new issue
Block a user