fix(collab): authorize the user for the flow on the Yjs websocket - #95
Merged
Conversation
The /yjs/:flowId endpoint authenticated the caller but never authorized them for that flowId: getOrCreateRoom loaded the document by id alone and persistRoom wrote it back by id alone, so any authenticated user could both read and write any flow's live document, bypassing the owner and collaborator model the tRPC procedures enforce via requireFlowAccess. Authorize at the websocket boundary, before the room is created, reusing requireFlowAccess so there stays one source of truth for who counts as what. The resolved role also decides write access: viewers were only held read-only on the client, and Yjs is bidirectional, so an invited viewer could still write through the socket. handleConnection now takes canWrite as a required argument (no default, so no caller grants write by omission), and read-only connections may only send sync step 1 -- step 2 and update both write into the doc. Viewers still read: the client sends its own step 1 on connect and the server answers with step 2. Reported by Eesh Saxena (github.com/eeshsaxena) through SECURITY.md. Co-authored-by: eeshsaxena <139802361+eeshsaxena@users.noreply.github.com> Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.
Fixes a broken-access-control issue in the realtime collaboration server, reported privately through
SECURITY.mdby Eesh Saxena, who is credited as co-author.The problem
GET /yjs/:flowIdauthenticated the caller but never authorized them for that specificflowId.getOrCreateRoomloaded the document by id alone andpersistRoomwrote it back by id alone, withuserIdused only for awareness and logging. The ownership check the tRPC procedures perform throughrequireFlowAccesswas never on this path, so any authenticated user could read and write any flow's live document.Related, and fixed here too: viewers were only held read-only on the client (
readOnlyinuse-node-controls,board-target-picker, …). Yjs is bidirectional, so an invited viewer could still write through the socket.The fix
apps/server/src/index.ts, aftergetSessionand before the room is ever created, by reusingrequireFlowAccess— one source of truth for who counts as what. Unauthorized callers get1008 Forbidden.handleConnectiontakescanWriteas a required argument, so no caller can grant write access by omission, andhandler.onOpenfails closed if the access decision is missing.ConnectionInfois treated as read-only.Viewers still read normally: the client sends its own step 1 on connect (
sync-provider.ts) and the server answers with step 2.Verification
packages/collab/src/__tests__/yjs-server-access.test.ts— 3 tests covering editor writes landing, viewer writes dropped while reads still work, and unknown connections treated read-only. The db is stubbed withmock.moduleso noDATABASE_URLis needed. With the guard neutered, 2 of the 3 fail, so they are not vacuous.bun test packages apps/web/src→ 277 pass, 0 failbun run check-types→ cleanNote
The fix only reaches users once the server redeploys.
🤖 Generated with Claude Code