fix: WebSocket 403 — per-route require_active_plugin instead of router-level

Router-level dependencies=[Depends(require_active_plugin)] was applied
to ALL routes including WebSocket. Now adding the dependency per-HTTP-route
only, WebSocket routes are skipped entirely.
This commit is contained in:
Agent Zero
2026-07-27 01:48:18 +02:00
parent b281c541b2
commit d0ae93a422
+10 -9
View File
@@ -436,20 +436,21 @@ def create_app() -> FastAPI:
router_module = importlib.import_module(route_def.module) router_module = importlib.import_module(route_def.module)
router = getattr(router_module, route_def.router_attr) router = getattr(router_module, route_def.router_attr)
# Wrap each HTTP route handler with plugin error isolation # Wrap each HTTP route handler with plugin error isolation
# Skip WebSocket routes — the wrapper breaks WS parameter # and add active-plugin check per-route (not router-level)
# resolution and returns JSONResponse instead of WS close. # so WebSocket routes are NOT affected.
from starlette.routing import WebSocketRoute from starlette.routing import WebSocketRoute
from fastapi import APIRouter as _AR
plugin_dep = Depends(require_active_plugin(plugin_name))
for route in router.routes: for route in router.routes:
if isinstance(route, WebSocketRoute): if isinstance(route, WebSocketRoute):
continue continue # WebSocket: no wrap, no plugin check
if hasattr(route, 'endpoint'): if hasattr(route, 'endpoint'):
route.endpoint = wrap_plugin_route(route.endpoint) route.endpoint = wrap_plugin_route(route.endpoint)
# Add active-plugin check as a router-level dependency # Add require_active_plugin to each HTTP route's dependencies
# (require_active_plugin skips WebSocket requests internally) if not hasattr(route, 'dependencies'):
app.include_router( route.dependencies = []
router, route.dependencies.append(plugin_dep)
dependencies=[Depends(require_active_plugin(plugin_name))], app.include_router(router)
)
except Exception as exc: except Exception as exc:
logger.error(f"Failed to register route {route_def.module}.{route_def.router_attr}: {exc}") logger.error(f"Failed to register route {route_def.module}.{route_def.router_attr}: {exc}")
break break