Merge branch 'staging/bugfix' into maintenance
This commit is contained in:
commit
f654060d8f
6 files changed
+26
-23
No files matched your search
+3
-3
@@ -24,10 +24,10 @@ maintenance 1.9.0 fa2c4680cca0d1d6b46e6c1ed68ceea4d9b79afb pep440-dev
|
||||
fix/.* 1.9.0 fa2c4680cca0d1d6b46e6c1ed68ceea4d9b79afb pep440-dev
|
||||
improve/.* 1.9.0 fa2c4680cca0d1d6b46e6c1ed68ceea4d9b79afb pep440-dev
|
||||
|
||||
# staging/bugfix is the branch for preparation of the 1.8.3 bugfix release
|
||||
# staging/bugfix is the branch for preparation of the 1.8.4 bugfix release
|
||||
# so are any bug/... branches
|
||||
staging/bugfix 1.8.3 87f4a4e8c967dab147beb6213cbf61cca32bec62 pep440-dev
|
||||
bug/.* 1.8.3 87f4a4e8c967dab147beb6213cbf61cca32bec62 pep440-dev
|
||||
staging/bugfix 1.8.4 d0d226f3e0ad0a6436e4f32519bf38a82aa576ca pep440-dev
|
||||
bug/.* 1.8.4 d0d226f3e0ad0a6436e4f32519bf38a82aa576ca pep440-dev
|
||||
|
||||
# staging/maintenance is currently the branch for preparation of 1.8.0rc6
|
||||
# so is regressionfix/...
|
||||
|
||||
@@ -177,8 +177,10 @@ class UserManager(GroupChangeListener):
|
||||
def cleanup_legacy_hashes(self):
|
||||
pass
|
||||
|
||||
def signature_key_for_user(self, username, salt=None):
|
||||
return self.create_password_hash(username, salt=salt)
|
||||
def signature_key_for_user(self, username, secret):
|
||||
return hashlib.sha512(
|
||||
to_bytes(username, encoding="utf-8", errors="replace") + to_bytes(secret)
|
||||
).hexdigest()
|
||||
|
||||
def add_user(self, username, password, active, permissions, groups, overwrite=False):
|
||||
pass
|
||||
@@ -809,14 +811,16 @@ class FilebasedUserManager(UserManager):
|
||||
self._settings.remove(["accessControl", "salt"])
|
||||
self._settings.save()
|
||||
|
||||
def signature_key_for_user(self, username, salt=None):
|
||||
def signature_key_for_user(self, username, secret):
|
||||
if username == "_api":
|
||||
return super().signature_key_for_user(username, secret)
|
||||
if username not in self._users:
|
||||
raise UnknownUser(username)
|
||||
user = self._users[username]
|
||||
|
||||
return hashlib.sha512(
|
||||
to_bytes(username + user._passwordHash, encoding="utf-8", errors="replace")
|
||||
+ to_bytes(salt)
|
||||
+ to_bytes(secret)
|
||||
).hexdigest()
|
||||
|
||||
def change_user_setting(self, username, key, value):
|
||||
|
||||
@@ -43,7 +43,7 @@ def loginFromApiKeyRequestHandler():
|
||||
if loginUserFromApiKey():
|
||||
_flask.g.login_via_apikey = True
|
||||
except InvalidApiKeyException:
|
||||
_flask.abort(403)
|
||||
_flask.abort(403, "Invalid API key")
|
||||
|
||||
|
||||
def loginFromAuthorizationHeaderRequestHandler():
|
||||
@@ -54,7 +54,7 @@ def loginFromAuthorizationHeaderRequestHandler():
|
||||
if loginUserFromAuthorizationHeader():
|
||||
_flask.g.login_via_header = True
|
||||
except InvalidApiKeyException:
|
||||
_flask.abort(403)
|
||||
_flask.abort(403, "Invalid credentials in Basic Authorization header")
|
||||
|
||||
|
||||
class InvalidApiKeyException(Exception):
|
||||
@@ -69,7 +69,7 @@ def loginUserFromApiKey():
|
||||
user = get_user_for_apikey(apikey)
|
||||
if user is None:
|
||||
# invalid API key = no API key
|
||||
return False
|
||||
raise InvalidApiKeyException("Invalid API key")
|
||||
|
||||
return loginUser(user, login_mechanism="apikey")
|
||||
|
||||
|
||||
@@ -65,7 +65,7 @@ def validate_csrf_tokens(cookie, header):
|
||||
|
||||
def add_csrf_cookie(response):
|
||||
if not isinstance(response, OctoPrintFlaskResponse):
|
||||
return
|
||||
response = flask.make_response(response)
|
||||
|
||||
token = generate_csrf_token()
|
||||
response.set_cookie("csrf_token", token, httponly=False)
|
||||
@@ -77,8 +77,9 @@ def validate_csrf_request(request):
|
||||
# Irrelevant method for CSRF, bypass
|
||||
return
|
||||
|
||||
if getattr(flask.g, "login_via_apikey", False):
|
||||
# API key authorization, bypass
|
||||
session = getattr(flask, "session", {})
|
||||
if len(session) == 0 or session.get("login_mechanism") == "apikey":
|
||||
# empty session, not a browser context
|
||||
return
|
||||
|
||||
if is_exempt(request.endpoint):
|
||||
|
||||
@@ -468,7 +468,7 @@ def encode_remember_me_cookie(value):
|
||||
name = value.split("|")[0]
|
||||
try:
|
||||
remember_key = userManager.signature_key_for_user(
|
||||
name, salt=current_app.config["SECRET_KEY"]
|
||||
name, current_app.config["SECRET_KEY"]
|
||||
)
|
||||
timestamp = datetime.utcnow().timestamp()
|
||||
return encode_cookie(f"{name}|{timestamp}", key=remember_key)
|
||||
@@ -488,7 +488,7 @@ def decode_remember_me_cookie(value):
|
||||
try:
|
||||
# valid signature?
|
||||
signature_key = userManager.signature_key_for_user(
|
||||
name, salt=current_app.config["SECRET_KEY"]
|
||||
name, current_app.config["SECRET_KEY"]
|
||||
)
|
||||
cookie = decode_cookie(value, key=signature_key)
|
||||
if cookie:
|
||||
@@ -1944,11 +1944,12 @@ class OctoPrintJsonProvider(flask.json.provider.DefaultJSONProvider):
|
||||
def session_signature(user, session):
|
||||
from octoprint.server import userManager
|
||||
|
||||
key = userManager.signature_key_for_user(user, salt=current_app.config["SECRET_KEY"])
|
||||
key = userManager.signature_key_for_user(user, current_app.config["SECRET_KEY"])
|
||||
return hmac.new(
|
||||
key.encode("utf-8"), session.encode("utf-8"), hashlib.sha512
|
||||
).hexdigest()
|
||||
|
||||
|
||||
def validate_session_signature(sig, user, session):
|
||||
return hmac.compare_digest(sig, session_signature(user, session))
|
||||
user_sig = session_signature(user, session)
|
||||
return len(user_sig) == len(sig) and hmac.compare_digest(sig, user_sig)
|
||||
@@ -225,8 +225,7 @@ def login():
|
||||
_logger.exception("Error processing theming CSS, ignoring")
|
||||
|
||||
resp = make_response(render_template("login.jinja2", **render_kwargs))
|
||||
add_csrf_cookie(resp)
|
||||
return resp
|
||||
return add_csrf_cookie(resp)
|
||||
|
||||
|
||||
@app.route("/recovery")
|
||||
@@ -258,8 +257,7 @@ def recovery():
|
||||
_logger.exception("Error adding backup upload size info, ignoring")
|
||||
|
||||
resp = make_response(render_template("recovery.jinja2", **render_kwargs))
|
||||
add_csrf_cookie(resp)
|
||||
return resp
|
||||
return add_csrf_cookie(resp)
|
||||
|
||||
|
||||
@app.route("/cached.gif")
|
||||
@@ -700,8 +698,7 @@ def index():
|
||||
if response is None:
|
||||
return abort(404)
|
||||
|
||||
add_csrf_cookie(response)
|
||||
return response
|
||||
return add_csrf_cookie(response)
|
||||
|
||||
|
||||
def _get_render_kwargs(templates, plugin_names, plugin_vars, now):
|
||||
|
||||
Reference in new issue
Block a user