Repository navigation
fix(ws): scope /ws to the client's accessible agents (trinity-enterprise#467) - #2483
Merged
Merged
Conversation
`/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
approved these changes
Sep 2, 2026
vybe
left a comment
Contributor
There was a problem hiding this comment.
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
/wswasSCOPE_ALLand unfiltered. Everyagent_activity,agent_created,operator_queue_*,agent_reportandagent_sharedevent 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/ticketis plainget_current_user, so anyrole=userwith a single shared agent could mint a ticket and read the whole instance./ws/eventshas scoped byaccessible_agentssince #306./wsnever 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 --diffverifier found, deferred at the time because that branch closed the write half with an entry gate).A
/wsclient now sees ascope=allevent only when every agent the payload names is one it may access.How
services/ws_identity_service.resolve_ws_identityturns 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 joinsusers.emailandagent_sharingis 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).agent_names_in_payload()over one named key vocabulary. The 36 livemanager.broadcastsites do not agree on where the agent name lives: top-levelagent_name,data.name,data.agent_name, two keys at once foragent_collaboration, old+new foragent_renamed.detailsis read narrowly (the two collaboration keys only), because activitydetailsis free-form and adetails["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.last-event-idreplay re-reads history straight out of Redis, so a filter wired only into_fanoutwould hand the entire unfiltered backlog to any client that reconnects. Pinned structurally, not by example.agent_created/agent_deleted/agent_shared/agent_unshared/agent_renamedand 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/eventscontract (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.The guard is the safety net, not the extractor
An event naming no agent stays fleet-visible — fail-open by design, because
notifications_clearedfor a fleet-wide clear andresync_requiredname nobody. That is exactly whytests/unit/test_ent467_ws_agent_scope.pyAST-discovers every/wsbroadcast payload in the OSS tree (36 sites, found by callee name and resolved throughjson.dumpsand local_broadcast({...})wrappers, with per-function scoping so one function'seventlocal cannot resolve another's) and fails unless each is agent-keyed or listed inFLEET_LEVEL_ALLOWLISTwith 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
namemeans 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
test_event_bus.py's 23 unchanged and green._fanout· makeSCOPE_ALLunconditionally 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.-x) + 1472 passed / 15 skipped (the tail that run never reached). The only failures are 8 pre-existing::ffff:IPv4-mapped parametrizations intest_mcp_validator.py::TestCgnatSsrfGuardandtest_ent14_registry_url_ssrf.py, which reproduce identically on cleanorigin/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-devmain). The ent#170 room triggers are allowlisted:room_participant_state.identityis 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