Skip to content

fix(ws): scope /ws to the client's accessible agents (trinity-enterprise#467) - #2483

Merged
vybe merged 2 commits into
devfrom
fix/ent467-ws-agent-scope
Sep 2, 2026
Merged

vybe merged 2 commits into
devfrom
fix/ent467-ws-agent-scope

Conversation

@dolho

@dolho dolho commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

What

/ws was SCOPE_ALL and unfiltered. Every agent_activity, agent_created, operator_queue_*, agent_report and agent_shared event reached every authenticated client — fleet-wide agent names, execution ids and activity types, plus another user's email address on the two sharing events. POST /api/ws/ticket is plain get_current_user, so any role=user with a single shared agent could mint a ticket and read the whole instance.

/ws/events has scoped by accessible_agents since #306. /ws never did — which also left every execution-id-keyed write route one forgotten check away from a cross-agent action (this is the enabling disclosure the trinity#2433 /cso --diff verifier found, deferred at the time because that branch closed the write half with an entry gate).

A /ws client now sees a scope=all event only when every agent the payload names is one it may access.

How

  • Identity at connect — services/ws_identity_service.resolve_ws_identity turns the ticket subject into {email, is_admin, accessible_agents}. Fails closed: unknown subject, suspended account (Enterprise: User & Organization Management (Org/Team + advanced RBAC) on the #847 seam #995), or a raising lookup closes the socket with 4001. A non-admin row with no email is a resolved identity with an empty roster, not a refusal — ownership joins users.email and agent_sharing is keyed on it, so the empty set is the exact answer, and the frontend never retries a 4001 (refusing would leave a legacy row permanently dark rather than merely agent-less).
  • Admins are never filtered and deliberately resolve no roster. Theirs would be a connect-time snapshot, so filtering on it would blind the operator to every agent created after the page loaded — the person most likely to be watching one being created.
  • Identity is derived from the payload, once per event, by agent_names_in_payload() over one named key vocabulary. The 36 live manager.broadcast sites do not agree on where the agent name lives: top-level agent_name, data.name, data.agent_name, two keys at once for agent_collaboration, old+new for agent_renamed. details is read narrowly (the two collaboration keys only), because activity details is free-form and a details["name"] holding a tool name would match no accessible agent and hide the event from the agent's own owner — over-filtering is how this change breaks a working UI.
  • Both delivery paths filter. Live fan-out is the obvious one; the last-event-id replay re-reads history straight out of Redis, so a filter wired only into _fanout would hand the entire unfiltered backlog to any client that reconnects. Pinned structurally, not by example.
  • A live roster is refreshed by the stream itself. The dispatcher watches for agent_created/agent_deleted/agent_shared/agent_unshared/agent_renamed and re-resolves each non-admin slot off the event loop through an injected resolver — deduped per email, not per socket, since several tabs are several slots with one roster. Every worker reads the same Redis stream, so one publish invalidates rosters fleet-wide with no call-site edits and no cross-worker plumbing. A missed invalidation degrades to the /ws/events contract (stale until reconnect), never to a leak, because the stale roster is the smaller one.
  • SCOPE_SCOPED (/ws/events) is untouched — a security fix to one surface must not quietly move what the other delivers.
  • No Redis envelope change, so entries written by a pre-ent#467 worker during a rolling deploy still filter correctly. No API change, no frontend change, no schema change, no migration.

The guard is the safety net, not the extractor

An event naming no agent stays fleet-visible — fail-open by design, because notifications_cleared for a fleet-wide clear and resync_required name nobody. That is exactly why tests/unit/test_ent467_ws_agent_scope.py AST-discovers every /ws broadcast payload in the OSS tree (36 sites, found by callee name and resolved through json.dumps and local _broadcast({...}) wrappers, with per-function scoping so one function's event local cannot resolve another's) and fails unless each is agent-keyed or listed in FLEET_LEVEL_ALLOWLIST with its reason. A new event shape that stores its agent under a novel key reds a test instead of leaking silently.

The residual is stated in the source: a future payload whose top-level name means something else would be read as an agent and over-filter. That direction is left uncovered deliberately — an event that stops arriving gets noticed, an event that reaches a stranger does not.

Verification

  • 37 tests in the new file; test_event_bus.py's 23 unchanged and green.
  • Six mutations confirmed red then reverted: drop the derived set in _fanout · make SCOPE_ALL unconditionally visible · fail open on an unknown ticket subject · move a broadcast's agent under a novel key · break the _broadcast({...}) wrapper resolution · double-count the AST passes.
  • Full unit suite: 1755 passed (head, -x) + 1472 passed / 15 skipped (the tail that run never reached). The only failures are 8 pre-existing ::ffff: IPv4-mapped parametrizations in test_mcp_validator.py::TestCgnatSsrfGuard and test_ent14_registry_url_ssrf.py, which reproduce identically on clean origin/dev — the local venv is Python 3.12.3 and the repo targets 3.13.

Residual, filed

debt:2026-09-02-room-events-not-membership-scoped (trinity-dev main). The ent#170 room triggers are allowlisted: room_participant_state.identity is an agent name, but the column is polymorphic (agent name / user id / client email), so keying on it would drop the event for the room's own human participants. Room-membership scoping is a different scope dimension the dispatcher has no notion of.

Fixes abilityai/trinity-enterprise#467

🤖 Generated with Claude Code

https://claude.ai/code/session_01NLfHNPtB5UCMk4LonZiJux

dolho and others added 2 commits September 2, 2026 11:33
`/ws` was `SCOPE_ALL` and unfiltered, so every `agent_activity`,
`agent_created`, `operator_queue_*`, `agent_report` and `agent_shared`
event reached every authenticated client — fleet-wide agent names,
execution ids and activity types, plus another user's email address on
the two sharing events. `POST /api/ws/ticket` is plain `get_current_user`,
so any `role=user` with a single shared agent could mint a ticket and read
the whole instance. `/ws/events` has scoped by `accessible_agents` since
#306; `/ws` never did, which also left every execution-id-keyed write
route one forgotten check away from a cross-agent action (the trinity#2433
finding's enabling disclosure).

A `/ws` client now sees a `scope=all` event only when every agent the
payload names is one it may access.

- Identity resolves at connect (`services/ws_identity_service.py`) and
  fails CLOSED: unknown subject, suspended account (#995) or a raising
  lookup closes the socket with 4001. A non-admin row with no email is a
  resolved identity with an EMPTY roster rather than a refusal — ownership
  joins `users.email` and sharing is keyed on it, so the empty set is the
  exact answer, and the frontend never retries a 4001.
- Admins are never filtered and deliberately resolve no roster: a
  connect-time snapshot would blind the operator to every agent created
  after the page loaded.
- Agent identity is derived from the payload once per event by
  `agent_names_in_payload()` over one named key vocabulary, because the 36
  live `manager.broadcast` sites disagree about where the name lives.
  `details` is read narrowly (the two collaboration keys) since a
  free-form `details["name"]` holding a tool name would hide the event
  from the agent's own owner.
- Both delivery paths filter. The `last-event-id` replay re-reads history
  straight from Redis, so a filter wired only into `_fanout` would hand
  the entire unfiltered backlog to any client that reconnects.
- A live roster is refreshed by the stream itself: the dispatcher watches
  for `agent_created`/`agent_deleted`/`agent_shared`/`agent_unshared`/
  `agent_renamed` and re-resolves each non-admin slot off the event loop
  through an injected resolver (deduped per email, not per socket). Every
  worker reads the same stream, so one publish invalidates rosters
  fleet-wide with no call-site edits; a missed invalidation degrades to
  the `/ws/events` contract (stale until reconnect), never to a leak.
- No Redis envelope change, so entries written by a pre-ent#467 worker
  during a rolling deploy still filter correctly. No API change, no
  frontend change, no schema change.

An event naming no agent stays fleet-visible — fail-open by design — so
`tests/unit/test_ent467_ws_agent_scope.py` (37 tests) AST-discovers every
`/ws` broadcast payload in the OSS tree and fails unless each is
agent-keyed or listed in `FLEET_LEVEL_ALLOWLIST` with its reason. The
guard, not the extractor, is the safety net. Four mutations confirmed red
then reverted (drop the derived set in `_fanout` · make `SCOPE_ALL`
unconditionally visible · fail open on an unknown ticket subject · move a
broadcast's agent under a novel key).

Residual, filed to the debt inbox: the ent#170 room triggers are
allowlisted because `room_participant_state.identity` is polymorphic
(agent name / user id / client email), so keying on it would drop the
event for the room's own human participants. Room-membership scoping is
the follow-up.

Related to Abilityai/trinity-enterprise#467

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01NLfHNPtB5UCMk4LonZiJux
…conflicts in learnings.md + tests/registry.json with #2476)

@vybe vybe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Validated via /validate-pr: fail-closed identity at connect (unknown/suspended/db-error close before accept), both delivery paths filter (live fan-out + last-event-id replay, AST-pinned), admins short-circuit, /ws/events untouched, 37 new tests (+ test_event_bus: 60 locally) and a 36-site broadcast-payload discovery guard. Body patched to 'Fixes abilityai/trinity-enterprise#467'; branch merged with dev to resolve the both-append conflicts in learnings.md + tests/registry.json against #2476. 22/22 checks green on 05d2474 incl. six-seed pytest + regression diff. Approving; merging. ent#467 needs a manual status-in-dev bump (cross-tracker).

@vybe
vybe merged commit dd91056 into dev Sep 2, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants