Repository navigation
feat: enhance-realtime-authorization - #620
Conversation
- Removed the session management logic that persisted session data in Valkey. - Updated the socket connection logic to handle token expiry in memory. - Introduced ExpiryWatcher to manage token expiration and reauthentication. - Enhanced event routing to utilize authorization descriptors for event delivery. - Added tests for the new token expiry handling and event routing logic.
There was a problem hiding this comment.
ℹ️ No critical issues — one minor suggestion inline.
Reviewed changes
- Event envelopes now carry IAM metadata.
services/api/internal/events/authz.goaddsAuthz,AuthzForandPublishRealtime;Fanoutand the direct view/environment/automation publishers emitauthzviarealtimeMessage, andTestEveryTopicIsClassifiedforces a rule (or an explicitnotRealtimeTopicsentry) for every topic. - Batched access probe. New
POST /projects/{id}/members/me/permissions/check(CheckMyAccess+middleware.ProbeAccessoveriam.Authorizer.AuthorizeMany) evaluates{action, resource}pairs as the caller, resources constrained to the URL project, gating nothing. - Realtime consumes
authz. Newaccess.ts(cached, batched, fail-closed decider; per-resourceKeyedQueue; 401 disconnect),subscriber.tsrouting on the descriptor with an explicit prefix fallback,rooms.ts,resync.ts(oniam.changed/60s),token-expiry.ts;conversation-access.tsand the Valkeysession.tsare deleted. iam.changedwiring.events.NotifyIAMChangeswraps the invalidator and is threaded throughbootstrap/app.goto role, project, agent, service-account and user services.- Web client. Join-ack recording + retry,
permissions:changedhandling,reauthvia a sharedrefreshSession, and reconnect refetch inuse-project-realtime.ts. - Behavior changes:
view.*nowviews:read;ProjectWideActions/?scope=projectremoved; rooms join onEffectiveActions(child-only grants included) with per-event enforcement.
I read the full diff, traced the security-critical paths (the per-event check uses each socket's own account/token, the resource comes from authz not the payload, unknown actions drop, API failure fails closed, and task activities set TaskID so a task-scoped Deny applies), and confirmed go build ./... and the changed Go tests pass locally. The realtime TS tests were not re-run here (dependencies not installed); note the author did not run the apps/e2e Playwright specs, so the browser-side reauth/reconnect handling rests on unit tests.
deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏
- services/api: put WithAuthorizer's doc comment back on WithAuthorizer and give WithGrantsNotifier its own; the misplaced comment failed revive in CI. - services/realtime: a 401 for one socket token no longer disconnects every socket of the account. Try each distinct token, end only the sockets whose own token is rejected, and apply the accepted answer to the rest. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
Reviewed the delta since the prior pullfrog review at c61c81e: a per-token fix to the realtime resync path plus a Go doc-comment relocation.
resyncGroupre-syncs per token. A 401 for one socket's token no longer disconnects every socket of the account; the resync now tries each distinct token, ends only the sockets whose own token the API rejected, and applies the accepted answer to the rest.- New tests cover the split. A stale token beside a fresh one (only the stale socket ends, the healthy one is re-synced) and all tokens rejected (every socket ends).
- Go doc comments relocated.
WithAuthorizer's comment is back onWithAuthorizerandWithGrantsNotifiergets its own — the misplaced comment failedrevivein CI.
The prior inline thread on resyncGroup is resolved by this change.
deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

Summary
Redesigns how
services/realtimeauthorizes live events so it follows the API's IAM authorization instead of approximating it.Before: a socket joined project-wide rooms from the account's project-wide actions, and everything in a room was delivered to everyone in it. Only conversations were checked per entity. So a
Denyon one task, or a role limited to one sprint's tasks, was not honoured by live events (a leak in one case, no updates at all in the other). Permissions were a snapshot taken atjoin, a role edit or removed member had no effect on an open socket, a socket outlived its access token, and the event-to-permission mapping lived separately in TypeScript.Now: the API stays the only decision-maker and realtime only asks it and caches the answers.
authz: {action, resource}(for exampletasks:readonproject/<p>/task/<id>), derived inevents.AuthzFor.TestEveryTopicIsClassifiedfails when a topic has neither a rule nor an explicit "not realtime" entry, so a new topic cannot skip IAM.POST /projects/{id}/members/me/permissions/check, as that socket's own account, using the same IAM engine as the REST gates. Questions from one event-loop turn share one request, answers are cached 30s, failures deliver nothing, and events of one resource stay in order.iam.changedwhen a role, an attachment, a membership or an account changes. Realtime drops its cached answers, re-syncs room membership and emitspermissions:changed(a 60s resync is the backstop for a missed message).reauthis sent about a minute before expiry and the socket is disconnected at expiry, from one shared scan rather than a timer per socket.What changed
services/apievents/authz.go:Authz,AuthzFor,PublishRealtime.Fanoutand the direct publishers (view, environment, automation) use the new envelope.POST .../members/me/permissions/check:ProbeAccessmiddleware evaluates through the newAuthorizer.AuthorizeMany(grants read once per batch). It reports and gates nothing, like the other "my permissions" routes, and the handler only writes the answers.events/iam.go:NotifyIAMChangeswraps the authorizer's invalidator, wired for role edits and attachment changes, and for removing a member or agent and deleting a user, agent, service account or project.ProjectWideActionsand?scope=project.services/agent-runner:PublishRealtimeattachesconversations:readon the conversation for project-shared conversations. The API derives the same foragent.session.started.services/realtime:access.ts(decider, batching, per-resource queue),subscriber.ts(routes onauthz),rooms.ts,resync.ts,token-expiry.ts;joinis acked with the granted namespaces; newviewsroom (views:read). Replacesconversation-access.ts; deletessession.ts.apps/web: answersreauth(refresh then reconnect, with one shared refresh request so the rotating refresh token is used once), records the join ack and retries an unacked join, refetches permissions onpermissions:changed(and the whole project when a namespace was lost), and refetches the project after a reconnect once the rejoin is acked.Docs:
docs/architecture/authorization.md("Realtime events"),docs/architecture/service-boundaries.md, the two API docs, and the realtime README.Behaviour changes to be aware of
view.*events now requireviews:readon the view, matching the REST routes. Realtime previously usedsprints:read.fetchProjectPermissionspreviously threw a plainError, so the "401 on join, then disconnect" path never fired. It now throwsApiRequestError.authzdescriptor (a publisher that predates it) fall back to the old prefix routing and are logged once per type.agent.*conversation events without one are checked asconversations:readon the conversation.Testing
go vetandgo test ./...pass; the Go e2e suite (PACA_E2E=1) passes.tsc, biome, 39 tests) and web (tsc -b, biome, vitest) pass.Denyon one task stops that task's events for the account while it still gets the rest; role edits take effect on an open socket without reconnecting; anAllowlimited to one task still places the account in the room; a short-lived token getsreauthand is disconnected at expiry; removing a member and deleting a user both take the account out of every room.iam.changed; fixed, with tests for every removal and deletion path.apps/e2e, so the web client's reauth and reconnect handling is covered by unit tests and the server-side run, not by a real browser.Type of Change
Checklist
🤖 Generated with Claude Code