The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(web): clamp dashboard pagination and analytics-days params (#39200 + #74778 salvage)
aydnOktay105aba6705c1
diff --git a/hermes_cli/web_routers/profiles.py b/hermes_cli/web_routers/profiles.pyindex 17bd37c1412..13aea9fbcf0 100644--- a/hermes_cli/web_routers/profiles.py+++ b/hermes_cli/web_routers/profiles.py@@ -56,7 +56,10 @@ _write_profile_model = late("_write_profile_model")@sessions_router.get("/api/profiles/sessions")def get_profiles_sessions(- limit: int = Query(20, ge=0),+ # ``le=100`` caps the per-request page size (idea from #39200) — this+ # endpoint fans the query out across EVERY profile's state.db, so an+ # unbounded limit multiplies the damage.+ limit: int = Query(20, ge=0, le=100),offset: int = Query(0, ge=0),min_messages: int = 0,archived: str = "exclude",diff --git a/hermes_cli/web_routers/sessions.py b/hermes_cli/web_routers/sessions.pyindex 86af9fc075f..c692d7b21e1 100644--- a/hermes_cli/web_routers/sessions.py+++ b/hermes_cli/web_routers/sessions.py@@ -49,7 +49,10 @@ _strip_session_list_rows = late("_strip_session_list_rows")@list_router.get("/api/sessions")def get_sessions(- limit: int = Query(20, ge=0),+ # ``le=100`` caps the page size (idea from #39200): an unbounded limit+ # lets one request drag every session row (plus correlated-subquery+ # preview work) out of SQLite in a single hit.+ limit: int = Query(20, ge=0, le=100),offset: int = Query(0, ge=0),min_messages: int = 0,archived: str = "exclude",diff --git a/hermes_cli/web_server.py b/hermes_cli/web_server.pyindex d9296415d5a..953aa0ee703 100644--- a/hermes_cli/web_server.py+++ b/hermes_cli/web_server.py@@ -102,7 +102,7 @@ from utils import env_var_enabledtry:from fastapi import (- FastAPI, File, Form, HTTPException, Request, UploadFile,+ FastAPI, File, Form, HTTPException, Query, Request, UploadFile,WebSocket, WebSocketDisconnect,)from fastapi.middleware.cors import CORSMiddleware@@ -118,7 +118,7 @@ except ImportError:from tools.lazy_deps import ensure as _lazy_ensure_lazy_ensure("tool.dashboard", prompt=False)from fastapi import (- FastAPI, File, Form, HTTPException, Request, UploadFile,+ FastAPI, File, Form, HTTPException, Query, Request, UploadFile,WebSocket, WebSocketDisconnect,)from fastapi.middleware.cors import CORSMiddleware@@ -14093,7 +14093,14 @@ def _get_usage_analytics(days: int = 30, profile: Optional[str] = None):@app.get("/api/analytics/usage")-async def get_usage_analytics(days: int = 30, profile: Optional[str] = None):+async def get_usage_analytics(+ days: int = Query(30, ge=1, le=365),+ profile: Optional[str] = None,+):+ """``days`` is clamped to 1-365 (idea from #74778): huge or non-positive+ values would force expensive full-history SQL and InsightsEngine work, or+ produce empty/inverted time windows. The UI only offers 7/30/90-day+ presets."""return await asyncio.to_thread(_get_usage_analytics, days, profile)@@ -14274,7 +14281,11 @@ def _get_models_analytics(days: int = 30, profile: Optional[str] = None):@app.get("/api/analytics/models")-async def get_models_analytics(days: int = 30, profile: Optional[str] = None):+async def get_models_analytics(+ days: int = Query(30, ge=1, le=365),+ profile: Optional[str] = None,+):+ # ``days`` clamped to 1-365 (idea from #74778) — see get_usage_analytics."""Return model analytics without blocking the serving event loop."""return await asyncio.to_thread(_get_models_analytics, days, profile)diff --git a/tests/hermes_cli/test_dashboard_param_clamps.py b/tests/hermes_cli/test_dashboard_param_clamps.pynew file mode 100644index 00000000000..74c27f6ad43--- /dev/null+++ b/tests/hermes_cli/test_dashboard_param_clamps.py@@ -0,0 +1,55 @@+"""Dashboard query-param clamps (#39200 + #74778 salvage).++FastAPI Query bounds reject out-of-range values at the validation layer+(422) instead of letting them reach SQL/insights code: an unbounded+``limit`` drags every session row out of SQLite in one hit (multiplied+across every profile's state.db on the fan-out endpoint), and an+unbounded/inverted ``days`` forces full-history InsightsEngine work.+"""++from __future__ import annotations++import pytest++fastapi = pytest.importorskip("fastapi")+from fastapi.testclient import TestClient # noqa: E402+++def client(tmp_path, monkeypatch):+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))+ monkeypatch.setenv("HERMES_DASHBOARD_SESSION_TOKEN", "clamp-test-token")+ from hermes_cli import web_server++ with TestClient(web_server.app, raise_server_exceptions=False) as c:+ c.headers["Authorization"] = "Bearer clamp-test-token"+ yield c+++class TestSessionPaginationClamps:+ def test_oversized_limit_rejected(self, client):+ r = client.get("/api/sessions", params={"limit": 10_000})+ assert r.status_code == 422++ def test_negative_limit_rejected(self, client):+ r = client.get("/api/sessions", params={"limit": -1})+ assert r.status_code == 422++ def test_profile_fanout_limit_clamped(self, client):+ r = client.get("/api/profiles/sessions", params={"limit": 10_000})+ assert r.status_code == 422++ def test_in_range_limit_accepted(self, client):+ r = client.get("/api/sessions", params={"limit": 50})+ assert r.status_code == 200+++class TestAnalyticsDaysClamps:+ @pytest.mark.parametrize("days", [0, -5, 100_000])+ def test_out_of_range_days_rejected(self, client, days):+ r = client.get("/api/analytics/usage", params={"days": days})+ assert r.status_code == 422++ def test_in_range_days_accepted(self, client):+ r = client.get("/api/analytics/usage", params={"days": 30})+ assert r.status_code == 200