Skip to content

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

fix(bot): re-validate forwarded openviking_connection identity (#4866)

LIb8bed5a1ad3a
diff --git a/bot/tests/test_openapi_auth.py b/bot/tests/test_openapi_auth.py
index 15fc50c48..849e44e0b 100644
--- a/bot/tests/test_openapi_auth.py
+++ b/bot/tests/test_openapi_auth.py
@@ -602,6 +602,313 @@ class TestOpenAPIAuth:
assert response.status_code == 403
assert "openviking_connection is only accepted" in response.json()["detail"]
+ def test_chat_rebinds_forged_tenant_on_forwarded_connection_with_api_key(
+ self, message_bus, temp_workspace, monkeypatch
+ ):
+ """Loopback trust is not enough: API-key connections must match /health."""
+ captured = {}
+ config = SimpleNamespace(
+ gateway=SimpleNamespace(host="127.0.0.1", token=""),
+ ov_server=SimpleNamespace(
+ server_url="http://ov.local",
+ effective_auth_mode="api_key",
+ api_key_type="user",
+ ),
+ )
+ channel = OpenAPIChannel(
+ OpenAPIChannelConfig(),
+ message_bus,
+ workspace_path=temp_workspace,
+ global_config=config,
+ )
+
+ 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):
+ captured["health_headers"] = headers
+ return httpx.Response(
+ 200,
+ json={
+ "status": "ok",
+ "auth_mode": "api_key",
+ "role": "user",
+ "account_id": "real-acct",
+ "user_id": "alice",
+ },
+ )
+
+ async def fake_handle_chat(request):
+ captured["connection"] = request.openviking_connection.model_dump(exclude_none=True)
+ return ChatResponse(
+ session_id=request.session_id or "default", message="ok", events=None
+ )
+
+ monkeypatch.setattr("vikingbot.channels.openapi.httpx.AsyncClient", FakeAsyncClient)
+ monkeypatch.setattr(channel, "_handle_chat", fake_handle_chat)
+ app = FastAPI()
+ app.include_router(channel.get_router(), prefix="/bot/v1")
+ client = TestClient(app, client=("127.0.0.1", 50000))
+
+ response = client.post(
+ "/bot/v1/chat",
+ json={
+ "message": "hello",
+ "openviking_connection": {
+ "api_key": "user-key",
+ "account_id": "forged-acct",
+ "user_id": "forged-user",
+ "role": "root",
+ "api_key_type": "root",
+ "actor_peer_id": "peer-a",
+ "agent_id": "web-playground",
+ "server_url": "http://evil.example",
+ },
+ },
+ )
+
+ assert response.status_code == 200
+ assert captured["health_headers"]["X-API-Key"] == "user-key"
+ assert captured["connection"] == {
+ "api_key": "user-key",
+ "account_id": "real-acct",
+ "user_id": "alice",
+ "agent_id": "web-playground",
+ "actor_peer_id": "peer-a",
+ "role": "user",
+ "api_key_type": "user",
+ "server_url": "http://ov.local",
+ }
+
+ def test_chat_rejects_keyless_forwarded_connection_in_api_key_mode(
+ self, message_bus, temp_workspace, monkeypatch
+ ):
+ """api_key upstream must not accept forged identity without an API key."""
+ config = SimpleNamespace(
+ gateway=SimpleNamespace(host="127.0.0.1", token=""),
+ ov_server=SimpleNamespace(
+ server_url="http://ov.local",
+ effective_auth_mode="api_key",
+ api_key_type="user",
+ ),
+ )
+ channel = OpenAPIChannel(
+ OpenAPIChannelConfig(),
+ message_bus,
+ workspace_path=temp_workspace,
+ global_config=config,
+ )
+
+ 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):
+ # Unauthenticated /health in api_key mode: mode only, no identity.
+ return httpx.Response(200, json={"status": "ok", "auth_mode": "api_key"})
+
+ async def fake_handle_chat(_request):
+ raise AssertionError("chat handler must not run for keyless api_key forwards")
+
+ monkeypatch.setattr("vikingbot.channels.openapi.httpx.AsyncClient", FakeAsyncClient)
+ monkeypatch.setattr(channel, "_handle_chat", fake_handle_chat)
+ app = FastAPI()
+ app.include_router(channel.get_router(), prefix="/bot/v1")
+ client = TestClient(app, client=("127.0.0.1", 50000))
+
+ response = client.post(
+ "/bot/v1/chat",
+ json={
+ "message": "hello",
+ "openviking_connection": {
+ "account_id": "forged-acct",
+ "user_id": "forged-user",
+ "role": "root",
+ "api_key_type": "root",
+ "server_url": "http://evil.example",
+ },
+ },
+ )
+
+ assert response.status_code == 401
+ assert "API key required on forwarded connection" in response.json()["detail"]
+
+ def test_compile_rejects_keyless_forwarded_connection_in_api_key_mode(
+ self, message_bus, temp_workspace, monkeypatch
+ ):
+ class FakeCompileService:
+ async def create_task(self, request, *, principal_scope, task_id=None):
+ raise AssertionError("compile task must not start for keyless api_key forwards")
+
+ config = SimpleNamespace(
+ gateway=SimpleNamespace(host="127.0.0.1", token=""),
+ ov_server=SimpleNamespace(
+ server_url="http://ov.local",
+ effective_auth_mode="api_key",
+ api_key_type="user",
+ ),
+ )
+ channel = OpenAPIChannel(
+ OpenAPIChannelConfig(),
+ message_bus,
+ workspace_path=temp_workspace,
+ global_config=config,
+ compile_service=FakeCompileService(),
+ )
+
+ 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):
+ return httpx.Response(200, json={"status": "ok", "auth_mode": "api_key"})
+
+ 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.post(
+ "/runtime/v1/tasks",
+ json={
+ "task_type": "compile",
+ "payload": {
+ "from": ["viking://resources/source"],
+ "to": "viking://resources/wiki",
+ "skill": "viking://agent/skills/wiki",
+ "instruction": "build something",
+ "openviking_connection": {
+ "account_id": "forged-acct",
+ "user_id": "forged-user",
+ "role": "root",
+ },
+ },
+ },
+ )
+
+ assert response.status_code == 401
+ assert "API key required on forwarded connection" in response.json()["detail"]
+
+ def test_feedback_rejects_keyless_forwarded_connection_in_api_key_mode(
+ 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="api_key",
+ api_key_type="user",
+ ),
+ )
+ channel = OpenAPIChannel(
+ OpenAPIChannelConfig(),
+ message_bus,
+ workspace_path=temp_workspace,
+ global_config=config,
+ )
+
+ 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):
+ return httpx.Response(200, json={"status": "ok", "auth_mode": "api_key"})
+
+ async def fake_handle_feedback(_request):
+ raise AssertionError("feedback handler must not run for keyless api_key forwards")
+
+ monkeypatch.setattr("vikingbot.channels.openapi.httpx.AsyncClient", FakeAsyncClient)
+ monkeypatch.setattr(channel, "_handle_feedback", fake_handle_feedback)
+ app = FastAPI()
+ app.include_router(channel.get_router(), prefix="/bot/v1")
+ client = TestClient(app, client=("127.0.0.1", 50000))
+
+ response = client.post(
+ "/bot/v1/feedback",
+ json={
+ "session_id": "session-1",
+ "response_id": "resp-1",
+ "feedback_type": "thumb_up",
+ "openviking_connection": {
+ "account_id": "forged-acct",
+ "user_id": "forged-user",
+ "role": "root",
+ },
+ },
+ )
+
+ assert response.status_code == 401
+ assert "API key required on forwarded connection" in response.json()["detail"]
+
+ def test_chat_rejects_forwarded_connection_without_gateway_token_when_configured(
+ self, message_bus, temp_workspace, monkeypatch
+ ):
+ """Configured gateway token must authenticate openviking_connection forwards."""
+ config = SimpleNamespace(
+ gateway=SimpleNamespace(host="127.0.0.1", token="gateway-secret"),
+ ov_server=SimpleNamespace(
+ server_url="http://ov.local",
+ effective_auth_mode="api_key",
+ api_key_type="user",
+ ),
+ )
+ channel = OpenAPIChannel(
+ OpenAPIChannelConfig(),
+ message_bus,
+ workspace_path=temp_workspace,
+ global_config=config,
+ )
+
+ async def fake_handle_chat(_request):
+ raise AssertionError("chat handler should not run for unauthenticated connection")
+
+ monkeypatch.setattr(channel, "_handle_chat", fake_handle_chat)
+ app = FastAPI()
+ app.include_router(channel.get_router(), prefix="/bot/v1")
+ client = TestClient(app, client=("127.0.0.1", 50000))
+
+ response = client.post(
+ "/bot/v1/chat",
+ json={
+ "message": "hello",
+ "openviking_connection": {
+ "api_key": "user-key",
+ "account_id": "acct",
+ "user_id": "alice",
+ "server_url": "http://ov.local",
+ },
+ },
+ )
+
+ assert response.status_code == 403
+ assert "openviking_connection is only accepted" in response.json()["detail"]
+
def test_chat_resolves_openviking_api_key_identity(
self, message_bus, temp_workspace, monkeypatch
):
diff --git a/bot/vikingbot/channels/openapi.py b/bot/vikingbot/channels/openapi.py
index c0dd73cab..71adffe28 100644
--- a/bot/vikingbot/channels/openapi.py
+++ b/bot/vikingbot/channels/openapi.py
@@ -426,9 +426,13 @@ class OpenAPIChannel(BaseChannel):
status_code=403,
detail=("openviking_connection is only accepted from trusted server proxy"),
)
- await channel._assert_runtime_upstream_auth_mode(
+ health = await channel._assert_runtime_upstream_auth_mode(
channel._identity_headers_from_connection(request.openviking_connection)
)
+ request.openviking_connection = channel._reconcile_forwarded_connection(
+ request.openviking_connection,
+ health,
+ )
request._principal_scope = channel._connection_principal_scope(
request.openviking_connection
)
@@ -853,6 +857,86 @@ class OpenAPIChannel(BaseChannel):
self._assert_runtime_health_mode(health)
return health
+ def _reconcile_forwarded_connection(
+ self,
+ connection: Any,
+ health: dict[str, Any],
+ ) -> OpenVikingConnection:
+ """Re-bind a trusted forwarded connection to upstream-verified identity.
+
+ Loopback / gateway-token trust only proves the carrier is the Server
+ proxy path (#4848). Claimed ``account_id`` / ``user_id`` / ``role`` on a
+ connection that carries an API key must still match what ``/health``
+ resolves for that key.
+ """
+ if isinstance(connection, OpenVikingConnection):
+ values = connection.model_dump(exclude_none=True)
+ elif isinstance(connection, dict):
+ values = dict(connection)
+ else:
+ values = {}
+
+ api_key = str(values.get("api_key") or "").strip()
+ actor_peer_id = str(values.get("actor_peer_id") or "").strip()
+ agent_id = str(values.get("agent_id") or "").strip() or DEFAULT_OPENVIKING_AGENT_ID
+ auth_mode = self._ov_server_auth_mode()
+
+ if api_key:
+ role = str(health.get("role") or "").strip().lower()
+ account_id = str(health.get("account_id") or "").strip()
+ user_id = str(health.get("user_id") or "").strip()
+ if auth_mode == "api_key":
+ if role not in {"user", "admin"} or not account_id or not user_id:
+ raise HTTPException(
+ status_code=401,
+ detail=(
+ "OpenViking credentials did not resolve to a usable "
+ "User/Admin identity"
+ ),
+ )
+ api_key_type = "user"
+ else:
+ if not account_id or not user_id:
+ raise HTTPException(
+ status_code=401,
+ detail="Invalid OpenViking credentials",
+ )
+ api_key_type = "root" if auth_mode == "trusted" else "user"
+ role = role or str(values.get("role") or "user").strip().lower() or "user"
+ rebuilt = self._build_openviking_connection(
+ api_key=api_key,
+ account_id=account_id,
+ user_id=user_id,
+ role=role,
+ api_key_type=api_key_type,
+ actor_peer_id=actor_peer_id,
+ )
+ rebuilt["agent_id"] = agent_id
+ return OpenVikingConnection(**rebuilt)
+
+ # Keyless forwards are only valid for trusted/dev upstreams. In api_key
+ # mode, unauthenticated /health still returns 200 with auth_mode and no
+ # resolved identity, so claimed account/user/role must not be accepted
+ # without a real API key (Web Studio review on #4866).
+ if auth_mode not in {"trusted", "dev"}:
+ raise HTTPException(
+ status_code=401,
+ detail=(
+ "OpenViking API key required on forwarded connection "
+ f"when upstream auth_mode is {auth_mode or 'unknown'}"
+ ),
+ )
+
+ # Trusted / DEV forwards may omit api_key; keep proxy-asserted identity
+ # but never honor a client-chosen upstream URL.
+ values["server_url"] = self._ov_server_url()
+ values["agent_id"] = agent_id
+ if actor_peer_id:
+ values["actor_peer_id"] = actor_peer_id
+ return OpenVikingConnection(
+ **{key: value for key, value in values.items() if value not in ("", None)}
+ )
+
def _build_openviking_connection(
self,
*,
@@ -956,6 +1040,10 @@ class OpenAPIChannel(BaseChannel):
gateway_is_localhost = self._is_gateway_localhost()
gateway_challenge_headers = {"X-VikingBot-Gateway": "true"}
+ if token_configured and x_gateway_token:
+ # Validate on localhost too: connection trust needs token_valid even
+ # when the gateway itself still allows unauthenticated loopback calls.
+ token_valid = secrets.compare_digest(x_gateway_token, gateway_token)
if not gateway_is_localhost:
if not token_configured:
raise HTTPException(
@@ -963,14 +1051,12 @@ class OpenAPIChannel(BaseChannel):
detail="OpenAPI gateway token is required when host is non-localhost",
headers=gateway_challenge_headers,
)
- if x_gateway_token:
- token_valid = secrets.compare_digest(x_gateway_token, gateway_token)
- if not token_valid:
- raise HTTPException(
- status_code=403,
- detail="Invalid X-Gateway-Token",
- headers=gateway_challenge_headers,
- )
+ if x_gateway_token and not token_valid:
+ raise HTTPException(
+ status_code=403,
+ detail="Invalid X-Gateway-Token",
+ headers=gateway_challenge_headers,
+ )
if not token_valid:
raise HTTPException(
status_code=401,
@@ -980,7 +1066,13 @@ class OpenAPIChannel(BaseChannel):
loopback_request = self._is_loopback_request(request)
local_forward_trust = loopback_request and gateway_is_localhost
- forwarded_trusted = loopback_request and (token_valid or local_forward_trust)
+ # Loopback alone is only enough when no gateway token is configured
+ # (legacy --with-bot). Once a token exists, only authenticated proxy
+ # forwards may supply openviking_connection (#4848 follow-up).
+ if token_configured:
+ forwarded_trusted = loopback_request and token_valid
+ else:
+ forwarded_trusted = local_forward_trust
return GatewayRequestAuth(
gateway_token_configured=token_configured,
@@ -1006,9 +1098,13 @@ class OpenAPIChannel(BaseChannel):
status_code=503,
detail=OPENVIKING_UPSTREAM_NOT_CONFIGURED_DETAIL,
)
- await self._assert_runtime_upstream_auth_mode(
+ health = await self._assert_runtime_upstream_auth_mode(
self._identity_headers_from_connection(chat_request.openviking_connection)
)
+ chat_request.openviking_connection = self._reconcile_forwarded_connection(
+ chat_request.openviking_connection,
+ health,
+ )
chat_request._principal_scope = self._connection_principal_scope(
chat_request.openviking_connection
)
@@ -1045,9 +1141,13 @@ class OpenAPIChannel(BaseChannel):
status_code=503,
detail=OPENVIKING_UPSTREAM_NOT_CONFIGURED_DETAIL,
)
- await self._assert_runtime_upstream_auth_mode(
+ health = await self._assert_runtime_upstream_auth_mode(
self._identity_headers_from_connection(compile_request.openviking_connection)
)
+ compile_request.openviking_connection = self._reconcile_forwarded_connection(
+ compile_request.openviking_connection,
+ health,
+ )
compile_request._principal_scope = self._connection_principal_scope(
compile_request.openviking_connection
)