From e408acfa7df50f49ec050a52ee48ea3c4736dce8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Diot?= Date: Wed, 31 Jan 2024 14:07:53 +0100 Subject: [PATCH] Refactor plugin handling in main.py --- src/ui/main.py | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/src/ui/main.py b/src/ui/main.py index c52d5ec9b..4aef20f70 100755 --- a/src/ui/main.py +++ b/src/ui/main.py @@ -1187,18 +1187,6 @@ def plugins(): return redirect(url_for("loading", next=url_for("plugins"), message="Reloading plugins")) - if request.args.get("plugin_id", False): - plugin_id = request.args.get("plugin_id") - page = db.get_plugin_template(plugin_id) - - if page: - return render_template( - Environment(loader=FileSystemLoader(join(sep, "usr", "share", "bunkerweb", "ui", "templates") + "/")).from_string(page.decode("utf-8")), - dark_mode=app.config["DARK_MODE"], - username=current_user.get_id(), - **app.jinja_env.globals, - ) - plugins = app.config["CONFIG"].get_plugins() plugins_internal = 0 plugins_external = 0 @@ -1270,12 +1258,33 @@ def upload_plugin(): return {"status": "ok"}, 201 -@app.route("/plugins/", methods=["POST"]) +@app.route("/plugins/", methods=["GET", "POST"]) @login_required def custom_plugin(plugin): + message = "" if not plugin_id_rx.match(plugin): + message = f'Invalid plugin id, "{plugin}" (must be between 1 and 64 characters, only letters, numbers, underscores and hyphens)' + app.logger.error(message) + if request.method == "GET": + return message, 400 return {"message": f'Invalid plugin id, "{plugin}" (must be between 1 and 64 characters, only letters, numbers, underscores and hyphens)'}, 400 + if request.method == "GET": + plugin_id = request.args.get("plugin_id") + page = db.get_plugin_template(plugin_id) + + if page: + return render_template( + Environment(loader=FileSystemLoader(join(sep, "usr", "share", "bunkerweb", "ui", "templates") + "/")).from_string(page.decode("utf-8")), + dark_mode=app.config["DARK_MODE"], + username=current_user.get_id(), + **app.jinja_env.globals, + ) + + message = f'The plugin "{plugin}" does not have a template' + app.logger.error(message) + return message, 404 + module = db.get_plugin_actions(plugin) if module is None: @@ -1294,7 +1303,6 @@ def custom_plugin(plugin): error = None res = None - message = "" try: # Try to get the custom plugin custom function and call it