andrexibiza W1 (P0): a multiplexed gateway serving alpha + beta let a ticket minted for alpha read beta's live sessions. `AuthorityConnection._sibling_for` copied the connection identity, rewrote profile_id/instance_id to the sibling named by a `profile` param and carried the operator capability over, so `session.list` / `session.resume` (and the submit/mutate/control verbs on the same dispatch map) with `profile: 'beta'` answered from beta's authority. No beta grant was ever presented. Reproduced on r6-base against a real `hermes gateway run` multiplexer: alpha-bound and root-bound tickets both list and resume beta's session. Root cause: the grant had no notion of profile reach; every ticket was implicitly host-wide. Fix: the reach is now part of the grant. `TicketStore.mint(scope=...)` stamps `scope` = 'profile' (default) or 'host' (interactive purpose only). The control socket's closed `session-ticket` params accept an optional `scope`, and run_api carries it into the connection identity as `profile_scope`. `_sibling_for` routes to a sibling only under a host-scoped grant; a profile-bound ticket naming any other profile gets `profile_mismatch`, the same answer an unserved profile gets. No profile_id is rewritten for a profile-bound grant. Legitimate cross-profile flows are kept, and each one asks for host scope explicitly: - Desktop shared-primary socket (main routes every local profile onto the one host socket with a `profile` param, #118246/#120005): `mintLocalGatewayTicket` and the Python ticket bridge request `scope: 'host'` for the interactive purpose. native-http stays profile-bound; its `?profile=` reads already mint for the routed home. - The dashboard `/api/ws` sidecar (already behind the dashboard gate, operator=True, REST `?profile=` host-wide) is stamped host scope. - The CLI, Ink TUI, ACP, cron, Kanban, bot relay, A2A and worker store only ever select their own profile, so the default profile-bound ticket covers them unchanged (own-profile selectors still succeed). Red/green: tests/gateway/test_ticket_profile_scope.py fails on r6-base (alpha ticket's session.list profile=beta returns beta-private) and passes after. It covers list/resume/submit/interrupt/mutate/create for alpha- and root-bound tickets, own-profile success, unserved profile_mismatch, the host-scoped sibling route, and refusal of host scope for non-interactive purposes. The Desktop vitest pins the `scope: 'host'` wire field on both mint paths. Review: https://github.com/NousResearch/hermes-agent/pull/106742#pullrequestreview-5460639040
73 lines
3.4 KiB
Python
73 lines
3.4 KiB
Python
"""Private local bootstrap grants; never put these credentials in discovery."""
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import secrets
|
|
import threading
|
|
import time
|
|
|
|
_PURPOSE_CAPABILITIES = {
|
|
'interactive': frozenset({'session:create', 'session:read', 'session:submit',
|
|
'session:control', 'session:approve', 'session:respond'}),
|
|
'native-http': frozenset({'http:owner'}),
|
|
'exposure': frozenset({'transport:delegate'}),
|
|
'worker-adoption': frozenset({'worker:adopt'}),
|
|
}
|
|
# A grant's profile reach: its own home, or (interactive only) every served sibling.
|
|
_SCOPES = frozenset({'profile', 'host'})
|
|
|
|
|
|
class TicketStore:
|
|
TTL_SECONDS = 30
|
|
MAX_ENTRIES = 1024
|
|
|
|
def __init__(self, instance_id: str, profile_ids: frozenset[str]):
|
|
if not instance_id or not profile_ids:
|
|
raise ValueError('bootstrap requires instance and served profiles')
|
|
self.instance_id = instance_id
|
|
self.profile_ids = frozenset(profile_ids)
|
|
self._entries: dict[str, tuple[float, dict]] = {}
|
|
self._lock = threading.Lock()
|
|
|
|
def mint(self, *, profile_id, subject, purpose, scope='profile') -> str:
|
|
"""``scope='host'`` lets an interactive connection select every profile this process
|
|
serves (the Desktop's one shared socket); the default binds exactly ``profile_id``."""
|
|
if (profile_id not in self.profile_ids or purpose not in _PURPOSE_CAPABILITIES or not subject
|
|
or scope not in _SCOPES or (scope == 'host' and purpose != 'interactive')):
|
|
raise PermissionError('bootstrap binding rejected')
|
|
with self._lock:
|
|
now = time.monotonic()
|
|
self._entries = {k: v for k, v in self._entries.items() if v[0] > now}
|
|
if len(self._entries) >= self.MAX_ENTRIES:
|
|
raise PermissionError('bootstrap capacity exhausted')
|
|
ticket = secrets.token_urlsafe(32)
|
|
self._entries[hashlib.sha256(ticket.encode()).hexdigest()] = (
|
|
now + self.TTL_SECONDS,
|
|
{'instance_id': self.instance_id, 'profile_id': profile_id, 'subject': subject,
|
|
'purpose': purpose, 'capabilities': _PURPOSE_CAPABILITIES[purpose], 'scope': scope})
|
|
return ticket
|
|
|
|
def redeem(self, ticket, *, profile_id, purpose) -> dict:
|
|
"""``profile_id=None`` accepts a ticket minted for ANY served profile; the returned grant's
|
|
``profile_id`` then selects the authority. A named profile must match exactly."""
|
|
if not isinstance(ticket, str) or len(ticket) > 256:
|
|
raise PermissionError('invalid bootstrap ticket')
|
|
key = hashlib.sha256(ticket.encode()).hexdigest()
|
|
with self._lock:
|
|
entry = self._entries.get(key)
|
|
if entry is None:
|
|
raise PermissionError('invalid bootstrap ticket')
|
|
expires, grant = entry
|
|
if expires <= time.monotonic():
|
|
del self._entries[key]
|
|
raise PermissionError('expired bootstrap ticket')
|
|
served = grant['profile_id'] in self.profile_ids if profile_id is None else grant['profile_id'] == profile_id
|
|
if not served or grant['purpose'] != purpose:
|
|
raise PermissionError('bootstrap binding rejected')
|
|
del self._entries[key]
|
|
return dict(grant)
|
|
|
|
def revoke(self) -> None:
|
|
with self._lock:
|
|
self._entries.clear()
|