Agent Zero AI framework
Refactor API routing, add cache and security
frdel4e243a996cb2
diff --git a/python/helpers/api.py b/python/helpers/api.pyindex 9593d98c9..046c97ef6 100644--- a/python/helpers/api.py+++ b/python/helpers/api.py@@ -1,17 +1,24 @@from abc import abstractmethodimport json+import socket+import structimport threading+from functools import wraps+from pathlib import Pathfrom typing import Union, TypedDict, Dict, Any-from attr import dataclass-from flask import Request, Response, jsonify, Flask, session, request, send_file+from flask import Request, Response, jsonify, Flask, session, request, send_file, redirect, url_for+from werkzeug.wrappers.response import Response as BaseResponsefrom agent import AgentContextfrom initialize import initialize_agentfrom python.helpers.print_style import PrintStylefrom python.helpers.errors import format_error-from werkzeug.serving import make_server+from python.helpers import files, cacheThreadLockType = Union[threading.Lock, threading.RLock]+CACHE_AREA = "api_handlers"++Input = dictOutput = Union[Dict[str, Any], Response, TypedDict] # type: ignore@@ -100,3 +107,165 @@ class ApiHandler:else:raise Exception(f"Context {ctxid} not found")++++def is_loopback_address(address: str) -> bool:+ loopback_checker = {+ socket.AF_INET: lambda x: (+ struct.unpack("!I", socket.inet_aton(x))[0] >> (32 - 8)+ ) == 127,+ socket.AF_INET6: lambda x: x == "::1",+ }+ address_type = "hostname"+ try:+ socket.inet_pton(socket.AF_INET6, address)+ address_type = "ipv6"+ except socket.error:+ try:+ socket.inet_pton(socket.AF_INET, address)+ address_type = "ipv4"+ except socket.error:+ address_type = "hostname"++ if address_type == "ipv4":+ return loopback_checker[socket.AF_INET](address)+ elif address_type == "ipv6":+ return loopback_checker[socket.AF_INET6](address)+ else:+ for family in (socket.AF_INET, socket.AF_INET6):+ try:+ r = socket.getaddrinfo(address, None, family, socket.SOCK_STREAM)+ except socket.gaierror:+ return False+ for family, _, _, _, sockaddr in r:+ if not loopback_checker[family](sockaddr[0]):+ return False+ return True+++def requires_api_key(f):+ @wraps(f)+ async def decorated(*args, **kwargs):+ from python.helpers.settings import get_settings+ valid_api_key = get_settings()["mcp_server_token"]++ if api_key := request.headers.get("X-API-KEY"):+ if api_key != valid_api_key:+ return Response("Invalid API key", 401)+ elif request.json and request.json.get("api_key"):+ api_key = request.json.get("api_key")+ if api_key != valid_api_key:+ return Response("Invalid API key", 401)+ else:+ return Response("API key required", 401)+ return await f(*args, **kwargs)++ return decorated+++def requires_loopback(f):+ @wraps(f)+ async def decorated(*args, **kwargs):+ if not is_loopback_address(request.remote_addr):+ return Response("Access denied.", 403, {})+ return await f(*args, **kwargs)++ return decorated+++def requires_auth(f):+ @wraps(f)+ async def decorated(*args, **kwargs):+ from python.helpers import login+ user_pass_hash = login.get_credentials_hash()+ if not user_pass_hash:+ return await f(*args, **kwargs)+ if session.get("authentication") != user_pass_hash:+ return redirect(url_for("login_handler"))+ return await f(*args, **kwargs)++ return decorated+++def csrf_protect(f):+ @wraps(f)+ async def decorated(*args, **kwargs):+ from python.helpers import runtime+ token = session.get("csrf_token")+ header = request.headers.get("X-CSRF-Token")+ cookie = request.cookies.get("csrf_token_" + runtime.get_runtime_id())+ sent = header or cookie+ if not token or not sent or token != sent:+ return Response("CSRF token missing or invalid", 403)+ return await f(*args, **kwargs)++ return decorated+++def register_api_route(app: Flask, lock: ThreadLockType) -> None:+ from python.helpers.extract_tools import load_classes_from_file+ from python.helpers import plugins++ async def _dispatch(path: str) -> BaseResponse:+ # Return cached wrapped handler if available+ cached = cache.get(CACHE_AREA, path)+ if cached is not None:+ return await cached()++ # Resolve file path for the handler+ # Try built-in api folder first, then plugin api folders+ handler_cls: type[ApiHandler] | None = None++ # Check built-in python/api/<path>.py+ builtin_file = files.get_abs_path(f"python/api/{path}.py")+ if files.is_in_dir(builtin_file, files.get_abs_path("python/api")) and files.exists(builtin_file):+ classes = load_classes_from_file(builtin_file, ApiHandler)+ if classes:+ handler_cls = classes[0]++ # Check plugin api folders: path format plugins/<plugin_name>/<handler>+ if handler_cls is None and path.startswith("plugins/"):+ parts = path.split("/", 2)+ if len(parts) == 3:+ _, plugin_name, handler_name = parts+ plugin_dir = plugins.find_plugin_dir(plugin_name)+ if plugin_dir:+ plugin_file = Path(plugin_dir) / "api" / f"{handler_name}.py"+ if plugin_file.is_file():+ classes = load_classes_from_file(str(plugin_file), ApiHandler)+ if classes:+ handler_cls = classes[0]++ if handler_cls is None:+ return Response(f"API endpoint not found: {path}", 404)++ # Check method is allowed+ if request.method not in handler_cls.get_methods():+ return Response(f"Method {request.method} not allowed for: {path}", 405)++ # Build handler call, wrapping with security decorators as required+ async def call_handler() -> BaseResponse:+ instance = handler_cls(app, lock)+ return await instance.handle_request(request=request)++ handler_fn = call_handler+ if handler_cls.requires_csrf():+ handler_fn = csrf_protect(handler_fn)+ if handler_cls.requires_api_key():+ handler_fn = requires_api_key(handler_fn)+ if handler_cls.requires_auth():+ handler_fn = requires_auth(handler_fn)+ if handler_cls.requires_loopback():+ handler_fn = requires_loopback(handler_fn)++ cache.add(CACHE_AREA, path, handler_fn)+ return await handler_fn()++ app.add_url_rule(+ "/api/<path:path>",+ "api_dispatch",+ _dispatch,+ methods=["GET", "POST", "PUT", "PATCH", "DELETE"],+ )+diff --git a/python/helpers/cache.py b/python/helpers/cache.pynew file mode 100644index 000000000..3a9c598a8--- /dev/null+++ b/python/helpers/cache.py@@ -0,0 +1,33 @@+import threading+from typing import Any++_lock = threading.RLock()+_cache: dict[str, dict[str, Any]] = {}+++def add(area: str, key: str, data: Any) -> None:+ with _lock:+ if area not in _cache:+ _cache[area] = {}+ _cache[area][key] = data+++def get(area: str, key: str, default: Any = None) -> Any:+ with _lock:+ return _cache.get(area, {}).get(key, default)+++def remove(area: str, key: str) -> None:+ with _lock:+ if area in _cache:+ _cache[area].pop(key, None)+++def clear(area: str) -> None:+ with _lock:+ _cache.pop(area, None)+++def clear_all() -> None:+ with _lock:+ _cache.clear()diff --git a/python/helpers/runtime.py b/python/helpers/runtime.pyindex 2120b36cf..dc2de961e 100644--- a/python/helpers/runtime.py+++ b/python/helpers/runtime.py@@ -138,7 +138,7 @@ def _get_rfc_url() -> str:if url.endswith("/"):url = url[:-1]url = url + ":" + str(set["rfc_port_http"])- url += "/rfc"+ url += "/api/rfc"return urldiff --git a/run_ui.py b/run_ui.pyindex c4f01020a..ee8aebbf5 100644--- a/run_ui.py+++ b/run_ui.py@@ -2,19 +2,12 @@ from datetime import timedeltaimport osimport secretsimport time-import socket-import struct-from functools import wrapsimport threadingimport asyncio-from pathlib import Pathimport urllib.request-import urllib.error-from regex.regex import Fimport uvicornfrom flask import Flask, request, Response, session, redirect, url_for, render_template_string-from werkzeug.wrappers.response import Response as BaseResponsefrom werkzeug.wrappers.request import Request as WerkzeugRequestimport initialize@@ -22,8 +15,7 @@ from python.helpers import files, git, mcp_server, fasta2a_server, settings as sfrom python.helpers.files import get_abs_pathfrom python.helpers import runtime, dotenv, processfrom python.helpers.websocket import WebSocketHandler, validate_ws_origin-from python.helpers.extract_tools import load_classes_from_folder-from python.helpers.api import ApiHandler+from python.helpers.api import register_api_route, requires_authfrom python.helpers.print_style import PrintStylefrom python.helpers import loginimport socketio # type: ignore[import-untyped]@@ -90,107 +82,6 @@ websocket_manager.set_server_restart_broadcast(# basic_auth = BasicAuth(webapp)-def is_loopback_address(address):- loopback_checker = {- socket.AF_INET: lambda x: (- struct.unpack("!I", socket.inet_aton(x))[0] >> (32 - 8)- ) == 127,- socket.AF_INET6: lambda x: x == "::1",- }- address_type = "hostname"- try:- socket.inet_pton(socket.AF_INET6, address)- address_type = "ipv6"- except socket.error:- try:- socket.inet_pton(socket.AF_INET, address)- address_type = "ipv4"- except socket.error:- address_type = "hostname"-- if address_type == "ipv4":- return loopback_checker[socket.AF_INET](address)- elif address_type == "ipv6":- return loopback_checker[socket.AF_INET6](address)- else:- for family in (socket.AF_INET, socket.AF_INET6):- try:- r = socket.getaddrinfo(address, None, family, socket.SOCK_STREAM)- except socket.gaierror:- return False- for family, _, _, _, sockaddr in r:- if not loopback_checker[family](sockaddr[0]):- return False- return True---def requires_api_key(f):- @wraps(f)- async def decorated(*args, **kwargs):- # Use the auth token from settings (same as MCP server)- from python.helpers.settings import get_settings- valid_api_key = get_settings()["mcp_server_token"]-- if api_key := request.headers.get("X-API-KEY"):- if api_key != valid_api_key:- return Response("Invalid API key", 401)- elif request.json and request.json.get("api_key"):- api_key = request.json.get("api_key")- if api_key != valid_api_key:- return Response("Invalid API key", 401)- else:- return Response("API key required", 401)- return await f(*args, **kwargs)-- return decorated---# allow only loopback addresses-def requires_loopback(f):- @wraps(f)- async def decorated(*args, **kwargs):- if not is_loopback_address(request.remote_addr):- return Response(- "Access denied.",- 403,- {},- )- return await f(*args, **kwargs)-- return decorated---# require authentication for handlers-def requires_auth(f):- @wraps(f)- async def decorated(*args, **kwargs):- user_pass_hash = login.get_credentials_hash()- # If no auth is configured, just proceed- if not user_pass_hash:- return await f(*args, **kwargs)-- if session.get('authentication') != user_pass_hash:- return redirect(url_for('login_handler'))-- return await f(*args, **kwargs)-- return decorated---def csrf_protect(f):- @wraps(f)- async def decorated(*args, **kwargs):- token = session.get("csrf_token")- header = request.headers.get("X-CSRF-Token")- cookie = request.cookies.get("csrf_token_" + runtime.get_runtime_id())- sent = header or cookie- if not token or not sent or token != sent:- return Response("CSRF token missing or invalid", 403)- return await f(*args, **kwargs)-- return decorated--@webapp.route("/login", methods=["GET", "POST"])async def login_handler():error = None@@ -480,46 +371,7 @@ def run():runtime.get_arg("host") or dotenv.get_dotenv_value("WEB_UI_HOST") or "localhost")- def register_api_handler(app, handler: type[ApiHandler], url_prefix: str = ""):- name = handler.__module__.split(".")[-1]- instance = handler(app, lock)-- async def handler_wrap() -> BaseResponse:- return await instance.handle_request(request=request)-- if handler.requires_loopback():- handler_wrap = requires_loopback(handler_wrap)- if handler.requires_auth():- handler_wrap = requires_auth(handler_wrap)- if handler.requires_api_key():- handler_wrap = requires_api_key(handler_wrap)- if handler.requires_csrf():- handler_wrap = csrf_protect(handler_wrap)-- route = f"{url_prefix}/{name}"- app.add_url_rule(- route,- route,- handler_wrap,- methods=handler.get_methods(),- )-- handlers = load_classes_from_folder("python/api", "*.py", ApiHandler)- for handler in handlers:- register_api_handler(webapp, handler, url_prefix="/api")-- # Load API handlers from plugins (prefixed with /plugins/{plugin_id}/)- from python.helpers import plugins-- for plugin in plugins.get_enhanced_plugins_list():- api_path = Path(plugin.path) / "api"- if not api_path.exists() or not api_path.is_dir():- continue-- plugin_handlers = load_classes_from_folder(str(api_path), "*.py", ApiHandler)- for handler in plugin_handlers:- # prefixed route for explicit namespacing- register_api_handler(webapp, handler, url_prefix=f"/api/plugins/{plugin.name}")+ register_api_route(webapp, lock)handlers_by_namespace = _build_websocket_handlers_by_namespace(socketio_server, lock)configure_websocket_namespaces(