From 6af0ce48dcc7cfcb3034b8f56b4d55f67a4fc23a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 18 Aug 2022 13:00:54 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=9A=20Add=20wrapper=20around=20limit?= =?UTF-8?q?=20decorator=20to=20fix=20pytest=20discovery?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/octoprint/server/api/__init__.py | 3 ++- src/octoprint/server/util/flask.py | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/octoprint/server/api/__init__.py b/src/octoprint/server/api/__init__.py index 08d9ccdd0..f63096f34 100644 --- a/src/octoprint/server/api/__init__.py +++ b/src/octoprint/server/api/__init__.py @@ -35,6 +35,7 @@ from octoprint.server.util import ( from octoprint.server.util.flask import ( get_json_command_from_request, get_remote_address, + limit, no_firstrun_access, passive_login, ) @@ -281,7 +282,7 @@ def serverStatus(): @api.route("/login", methods=["POST"]) -@octoprint.server.limiter.limit( +@limit( "3/minute;5/10 minutes;10/hour", deduct_when=lambda response: response.status_code == 403, error_message="You have made too many failed login attempts. Please try again later.", diff --git a/src/octoprint/server/util/flask.py b/src/octoprint/server/util/flask.py index 8cc2c679f..52356336f 100644 --- a/src/octoprint/server/util/flask.py +++ b/src/octoprint/server/util/flask.py @@ -669,6 +669,24 @@ def passive_login(): return flask.jsonify(response) +# ~~ rate limiting helper + + +def limit(*args, **kwargs): + if octoprint.server.limiter: + return octoprint.server.limiter.limit(*args, **kwargs) + else: + + def decorator(f): + @functools.wraps(f) + def decorated_function(*args, **kwargs): + return f(*args, **kwargs) + + return decorated_function + + return decorator + + # ~~ cache decorator for cacheable views