Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix(bot): reject untrusted identities in rootless trusted mode (#5929)

zhanggaoyuan5b695058ddab
diff --git a/bot/tests/test_openapi_auth.py b/bot/tests/test_openapi_auth.py
index 849e44e0b..52aff0743 100644
--- a/bot/tests/test_openapi_auth.py
+++ b/bot/tests/test_openapi_auth.py
@@ -379,6 +379,7 @@ class TestOpenAPIAuth:
return {
"status": "ok",
"auth_mode": "trusted",
+ "root_api_key_required": True,
"role": "user",
"account_id": request.headers["X-OpenViking-Account"],
"user_id": request.headers["X-OpenViking-User"],
@@ -1265,7 +1266,7 @@ class TestOpenAPIAuth:
assert response.status_code == 401
assert response.json()["detail"] == "OpenViking API key header required"
- def test_compile_status_accepts_trusted_identity_when_no_root_key_is_configured(
+ def test_compile_status_rejects_untrusted_identity_when_no_root_key_is_configured(
self, message_bus, temp_workspace, monkeypatch
):
config = SimpleNamespace(
@@ -1299,7 +1300,74 @@ class TestOpenAPIAuth:
return None
async def get(self, url, headers=None):
- assert "X-API-Key" not in headers
+ raise AssertionError("untrusted keyless identity must not hit upstream health")
+
+ async def fake_get_task(task_id, principal_scope):
+ raise AssertionError("compile task must not be read for untrusted keyless identity")
+
+ compile_service.get_task = fake_get_task
+ monkeypatch.setattr("vikingbot.channels.openapi.httpx.AsyncClient", FakeAsyncClient)
+ client = _make_client(channel)
+
+ response = client.get(
+ "/bot/v1/compile/cmp_1",
+ headers={
+ "X-OpenViking-Account": "acct",
+ "X-OpenViking-User": "alice",
+ },
+ )
+
+ assert response.status_code == 401
+ assert (
+ response.json()["detail"]
+ == "Trusted OpenViking identity requires a trusted local proxy or configured root API key"
+ )
+
+ @pytest.mark.parametrize(
+ ("root_api_key_required", "expected_status"),
+ [(False, 401), (True, 200)],
+ ids=["rootless-rejects-request-key", "root-key-validates-request-key"],
+ )
+ def test_compile_status_validates_request_key_when_not_statically_configured(
+ self,
+ message_bus,
+ temp_workspace,
+ monkeypatch,
+ root_api_key_required,
+ expected_status,
+ ):
+ config = SimpleNamespace(
+ gateway=SimpleNamespace(host="127.0.0.1", token=""),
+ ov_server=SimpleNamespace(
+ server_url="http://ov.local",
+ effective_auth_mode="trusted",
+ api_key_type="root",
+ api_key="",
+ account_id="",
+ admin_user_id="",
+ ),
+ )
+ compile_service = SimpleNamespace()
+ channel = OpenAPIChannel(
+ OpenAPIChannelConfig(),
+ message_bus,
+ workspace_path=temp_workspace,
+ global_config=config,
+ compile_service=compile_service,
+ )
+
+ class FakeAsyncClient:
+ def __init__(self, **kwargs):
+ self.kwargs = kwargs
+
+ async def __aenter__(self):
+ return self
+
+ async def __aexit__(self, exc_type, exc, tb):
+ return None
+
+ async def get(self, url, headers=None):
+ assert headers["X-API-Key"] == "request-root-key"
assert headers["X-OpenViking-Account"] == "acct"
assert headers["X-OpenViking-User"] == "alice"
return httpx.Response(
@@ -1307,6 +1375,7 @@ class TestOpenAPIAuth:
json={
"status": "ok",
"auth_mode": "trusted",
+ "root_api_key_required": root_api_key_required,
"role": "user",
"account_id": "acct",
"user_id": "alice",
@@ -1315,6 +1384,8 @@ class TestOpenAPIAuth:
)
async def fake_get_task(task_id, principal_scope):
+ if not root_api_key_required:
+ raise AssertionError("rootless request key must not authorize compile access")
assert task_id == "cmp_1"
assert principal_scope == channel._principal_scope("openviking:acct:alice")
return {
@@ -1329,6 +1400,89 @@ class TestOpenAPIAuth:
monkeypatch.setattr("vikingbot.channels.openapi.httpx.AsyncClient", FakeAsyncClient)
client = _make_client(channel)
+ response = client.get(
+ "/bot/v1/compile/cmp_1",
+ headers={
+ "X-API-Key": "request-root-key",
+ "X-OpenViking-Account": "acct",
+ "X-OpenViking-User": "alice",
+ },
+ )
+
+ assert response.status_code == expected_status
+ if not root_api_key_required:
+ assert (
+ response.json()["detail"]
+ == "Trusted OpenViking identity requires a trusted local proxy or upstream-validated root API key"
+ )
+
+ def test_compile_status_accepts_local_trusted_identity_when_no_root_key_is_configured(
+ self, message_bus, temp_workspace, monkeypatch
+ ):
+ config = SimpleNamespace(
+ gateway=SimpleNamespace(host="127.0.0.1", token=""),
+ ov_server=SimpleNamespace(
+ server_url="http://ov.local",
+ effective_auth_mode="trusted",
+ api_key_type="root",
+ api_key="",
+ account_id="",
+ admin_user_id="",
+ ),
+ )
+ compile_service = SimpleNamespace()
+ channel = OpenAPIChannel(
+ OpenAPIChannelConfig(),
+ message_bus,
+ workspace_path=temp_workspace,
+ global_config=config,
+ compile_service=compile_service,
+ )
+
+ class FakeAsyncClient:
+ def __init__(self, **kwargs):
+ self.kwargs = kwargs
+
+ async def __aenter__(self):
+ return self
+
+ async def __aexit__(self, exc_type, exc, tb):
+ return None
+
+ async def get(self, url, headers=None):
+ assert "X-API-Key" not in headers
+ assert headers["X-OpenViking-Account"] == "acct"
+ assert headers["X-OpenViking-User"] == "alice"
+ return httpx.Response(
+ 200,
+ json={
+ "status": "ok",
+ "auth_mode": "trusted",
+ "role": "user",
+ "account_id": "acct",
+ "user_id": "alice",
+ },
+ headers={"content-type": "application/json"},
+ )
+
+ async def fake_get_task(task_id, principal_scope):
+ assert task_id == "cmp_1"
+ assert principal_scope == channel._principal_scope("openviking:acct:alice")
+ return {
+ "task_id": task_id,
+ "status": "running",
+ "stage": "agent",
+ "created_at": "2026-07-20T00:00:00Z",
+ "updated_at": "2026-07-20T00:00:01Z",
+ }
+
+ compile_service.get_task = fake_get_task
+ monkeypatch.setattr("vikingbot.channels.openapi.httpx.AsyncClient", FakeAsyncClient)
+ app = FastAPI()
+ app.include_router(channel.get_router(), prefix="/bot/v1")
+ app.include_router(channel.get_gateway_router())
+ client = TestClient(app, client=("127.0.0.1", 50000))
+
response = client.get(
"/bot/v1/compile/cmp_1",
headers={
diff --git a/bot/vikingbot/channels/openapi.py b/bot/vikingbot/channels/openapi.py
index 71adffe28..01e0e8dfd 100644
--- a/bot/vikingbot/channels/openapi.py
+++ b/bot/vikingbot/channels/openapi.py
@@ -983,7 +983,11 @@ class OpenAPIChannel(BaseChannel):
actor_peer_id=request.headers.get("X-OpenViking-Actor-Peer", ""),
)
- async def _resolve_trusted_connection(self, request: Request) -> dict[str, Any] | None:
+ async def _resolve_trusted_connection(
+ self,
+ request: Request,
+ auth: GatewayRequestAuth,
+ ) -> dict[str, Any] | None:
if not self._ov_server_url():
return None
api_key = self._extract_api_key(request)
@@ -992,6 +996,14 @@ class OpenAPIChannel(BaseChannel):
).strip()
if not api_key and configured_api_key:
raise HTTPException(status_code=401, detail="OpenViking API key header required")
+ if not configured_api_key and not api_key and not auth.can_trust_forwarded_connection:
+ raise HTTPException(
+ status_code=401,
+ detail=(
+ "Trusted OpenViking identity requires a trusted local proxy "
+ "or configured root API key"
+ ),
+ )
account_id = str(request.headers.get("X-OpenViking-Account") or "").strip()
user_id = str(request.headers.get("X-OpenViking-User") or "").strip()
if not account_id or not user_id:
@@ -1010,6 +1022,18 @@ class OpenAPIChannel(BaseChannel):
)
health = await self._request_upstream_health(request)
self._assert_runtime_health_mode(health)
+ if (
+ not configured_api_key
+ and not auth.can_trust_forwarded_connection
+ and health.get("root_api_key_required") is not True
+ ):
+ raise HTTPException(
+ status_code=401,
+ detail=(
+ "Trusted OpenViking identity requires a trusted local proxy "
+ "or upstream-validated root API key"
+ ),
+ )
resolved_account_id = str(health.get("account_id") or "").strip()
resolved_user_id = str(health.get("user_id") or "").strip()
role = str(health.get("role") or "").strip().lower()
@@ -1181,7 +1205,7 @@ class OpenAPIChannel(BaseChannel):
self._assert_runtime_health_mode(health)
connection = self._resolve_api_key_connection(http_request, health)
elif auth_mode == "trusted":
- connection = await self._resolve_trusted_connection(http_request)
+ connection = await self._resolve_trusted_connection(http_request, auth)
else:
raise HTTPException(
status_code=503,