The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: stamp authenticated legacy clients before shared session admission
Teknium4be08ae58a16
diff --git a/gateway/session_controls.py b/gateway/session_controls.pyindex 876b11e3af7..357a3183af4 100644--- a/gateway/session_controls.py+++ b/gateway/session_controls.py@@ -16,9 +16,10 @@ class AuthorityConnection:capabilities |= {'session:create'}if 'capabilities' in identity:capabilities = frozenset(identity['capabilities'])- if identity.get('instance_id', authority.instance_id) != authority.instance_id:+ if (not identity.get('user_id')+ or identity.get('instance_id', authority.instance_id) != authority.instance_id):capabilities = frozenset()- self.actor = Principal(str(identity.get('user_id') or 'authenticated-dashboard'),+ self.actor = Principal(str(identity.get('user_id') or 'unbound'),identity.get('profile_id', authority.profile_id),capabilities, uuid.uuid4().hex)self.subscriptions = {}diff --git a/hermes_cli/web_routers/chat_ws.py b/hermes_cli/web_routers/chat_ws.pyindex 6d4dc9f84e3..e6337a4b1f2 100644--- a/hermes_cli/web_routers/chat_ws.py+++ b/hermes_cli/web_routers/chat_ws.py@@ -547,9 +547,18 @@ async def gateway_ws(ws: WebSocket) -> None:# The authenticated identity (ticket / internal credential) stamped by# _ws_auth_reason becomes the identity authority for privileged RPCs# (browser.controller.register). None on the legacy token path.+ identity = getattr(ws, "_hermes_auth_identity", None)+ authority = getattr(ws.app.state, "session_authority", None)+ if authority is not None and identity is None:+ # The upgrade gate above has already verified the legacy token. Do not+ # infer owner permissions from an identityless connection downstream.+ identity = {'user_id': 'legacy-token-owner', 'provider': 'session-token',+ 'profile_id': authority.profile_id, 'instance_id': authority.instance_id,+ 'capabilities': ['session:create', 'session:read', 'session:submit',+ 'session:control', 'session:approve', 'session:respond']}await handle_ws(ws,- auth_identity=getattr(ws, "_hermes_auth_identity", None),+ auth_identity=identity,subprotocol=getattr(ws, "_hermes_ws_subprotocol", None),)diff --git a/tests/gateway/test_runtime_token_identity.py b/tests/gateway/test_runtime_token_identity.pynew file mode 100644index 00000000000..dfd53f9bb43--- /dev/null+++ b/tests/gateway/test_runtime_token_identity.py@@ -0,0 +1,47 @@+"""Legacy token authentication must become an explicit authority principal."""+import json++import pytest+from websockets.asyncio.client import connect+from websockets.exceptions import InvalidStatus+++async def test_authenticated_token_can_create_without_identityless_permissions(tmp_path):+ from gateway.config import GatewayConfig+ from gateway.run import GatewayRunner+ from gateway.run_runtime import initialize_gateway_runtime, start_gateway_runtime_api, publish_gateway_runtime_ready+ from gateway.runtime_ownership import process_ownership+ from gateway.session_controls import AuthorityConnection+ from hermes_constants import get_hermes_home+ from hermes_cli import web_server as web++ process_ownership.reserve([get_hermes_home()])+ runner = GatewayRunner(GatewayConfig())+ try:+ await initialize_gateway_runtime(runner)+ await start_gateway_runtime_api(runner)+ assert await runner.start()+ publish_gateway_runtime_ready(runner)+ url = runner.session_api.api_origin.replace('http:', 'ws:') + '/api/ws'+ with pytest.raises(InvalidStatus):+ async with connect(url + '?token=wrong'):+ pass+ async with connect(url + '?token=' + web._SESSION_TOKEN) as ws:+ await ws.send(json.dumps({'jsonrpc': '2.0', 'id': 1, 'method': 'session.create',+ 'params': {'request_id': 'token-create', 'source': 'cli'}}))+ import asyncio+ async with asyncio.timeout(10):+ while True:+ reply = json.loads(await ws.recv())+ if reply.get('id') == 1:+ break+ assert 'result' in reply, reply+ unbound = AuthorityConnection(runner.session_authority, object(), {})+ try:+ assert not unbound.actor.capabilities+ finally:+ await unbound.close()+ finally:+ await runner.stop()+ process_ownership.close()