Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.
Python agent-contextagent-memoryagent-pluginsagentic-ragcontext-databasedsh-pluginknowledge-basememorymemory-managementself-evolvingvector-database
fix(server): reject invalid credentials on health check (#5471)
fukalous168037c8d99f
diff --git a/openviking/server/routers/system.py b/openviking/server/routers/system.pyindex f21539152..93ad7ecca 100644--- a/openviking/server/routers/system.py+++ b/openviking/server/routers/system.py@@ -16,6 +16,7 @@ from openviking.server.dependencies import get_servicefrom openviking.server.identity import AuthMode, RequestContext, Rolefrom openviking.server.models import Responsefrom openviking.storage.viking_fs import get_viking_fs+from openviking_cli.exceptions import UnauthenticatedErrorfrom openviking_cli.utils import get_loggerlogger = get_logger(__name__)@@ -89,8 +90,12 @@ async def health_check(request: Request):result["account_id"] = str(identity.account_id)result["user_id"] = str(identity.user_id)result["role"] = str(identity.role)+ except UnauthenticatedError:+ raiseexcept Exception as e:logger.warning(f"Failed to resolve identity: {e}")+ except UnauthenticatedError:+ raiseexcept Exception as e:logger.error(f"Failed to get health check: {e}")diff --git a/tests/server/test_server_health.py b/tests/server/test_server_health.pyindex f8c6cb846..cf6c724c8 100644--- a/tests/server/test_server_health.py+++ b/tests/server/test_server_health.py@@ -6,12 +6,14 @@import asyncioimport timefrom types import SimpleNamespace+from unittest.mock import AsyncMockimport httpximport pytestfrom openviking.server.app import _initialize_auth_plugin, _initialize_runtime_state, create_appfrom openviking.server.config import ServerConfig+from openviking_cli.exceptions import UnauthenticatedErrorasync def test_health_endpoint(client: httpx.AsyncClient):@@ -49,6 +51,35 @@ async def test_health_endpoint_resolves_identity_with_api_key(caplog, service):assert "Failed to resolve identity" not in caplog.text+async def test_health_endpoint_rejects_invalid_api_key():+ """An explicitly supplied invalid credential should not look healthy."""+ app = create_app(+ config=ServerConfig(+ auth_mode="api_key",+ host="127.0.0.1",+ root_api_key="test-root-key",+ ),+ service=SimpleNamespace(),+ )+ app.state.auth_plugin = SimpleNamespace(+ resolve_identity=AsyncMock(+ side_effect=UnauthenticatedError("Invalid API Key"),+ ),+ )++ async with httpx.AsyncClient(+ transport=httpx.ASGITransport(app=app), base_url="http://test"+ ) as client:+ response = await client.get("/health", headers={"X-API-Key": "stale-key"})++ assert response.status_code == 401+ assert response.json()["error"] == {+ "code": "UNAUTHENTICATED",+ "message": "Invalid API Key",+ "details": {},+ }++async def test_health_endpoint_without_api_key():"""Without an API key, /health should not return identity information."""app = create_app(@@ -98,7 +129,11 @@ async def test_health_trusted_identity_contract(root_key, credentials, caplog, s) as client:response = await client.get("/health", headers=headers)body = response.json()- assert response.status_code == 200+ expected_status = 401 if root_key and credentials in ("trusted", "malformed") else 200+ assert response.status_code == expected_status+ if expected_status == 401:+ assert body["error"]["code"] == "UNAUTHENTICATED"+ returnif credentials == "keyed" or (credentials == "trusted" and not root_key):assert (body["account_id"], body["user_id"], body["role"]) == ("account-a", "alice", "user")else: