feat(server): token-based WebSocket authentication#5531
Conversation
✅ Deploy Preview for rsbuild ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
There was a problem hiding this comment.
Pull Request Overview
This PR replaces the previous compilationId mechanism with secure WebSocket tokens for development, updates the server and client to use token-based authentication, and removes obsolete compilation ID utilities.
- Adds
webSocketTokento the environment context and generates it in dev mode. - Refactors the socket server and middleware to authenticate and route messages by token.
- Updates client HMR code to append and respect the new token parameter.
Reviewed Changes
Copilot reviewed 7 out of 8 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/core/src/types/hooks.ts | Add webSocketToken to EnvironmentContext |
| packages/core/src/server/socketServer.ts | Refactor upgrade, connection tracking, and messaging to use tokens |
| packages/core/src/server/helper.ts | Remove unused getCompilationId helper |
| packages/core/src/server/compilationMiddleware.ts | Update server hooks to use token instead of compilationId |
| packages/core/src/server/compilationManager.ts | Pass environments and tokens into middleware and socket server |
| packages/core/src/createContext.ts | Generate secure WebSocket tokens in dev mode |
| packages/core/src/client/hmr.ts | Append token to HMR URL and remove old compilationId logic |
Comments suppressed due to low confidence (2)
packages/core/src/server/socketServer.ts:61
- Add unit or integration tests for the WebSocket
upgradeflow, covering valid and invalid token scenarios to ensure authentication logic behaves as expected.
public upgrade = (
packages/core/src/types/hooks.ts:225
- Public API docs (README or type reference) should be updated to reflect the new
webSocketTokenfield and explain its usage in the environment API.
webSocketToken: string;
There was a problem hiding this comment.
Bug: Compiler Index Mismatch Disables HMR
The setupCompiler function's signature changed to expect an index parameter. However, applyToCompiler may not pass this index, or the passed index (representing the compiler's position) may not consistently match the env.index used for webSocketToken lookup in options.environments. This mismatch or absence of index causes the token lookup to fail, leading setupCompiler to return early and silently disable HMR functionality for the affected compiler(s).
packages/core/src/server/compilationMiddleware.ts#L147-L174
rsbuild/packages/core/src/server/compilationMiddleware.ts
Lines 147 to 174 in d2a2194
Bug: WebSocket Token Overwrite Causes Untracked Connections
The SocketServer's sockets Map incorrectly handles multiple WebSocket connections sharing the same webSocketToken. When a new connection with an existing token is established, it overwrites the previous connection in the Map. However, the close handler of the old connection remains active and, upon its closure, removes the new, active connection from the Map. This causes active connections to become untracked, preventing them from receiving HMR/live-reload updates and breaking multi-tab development scenarios.
packages/core/src/server/socketServer.ts#L222-L227
rsbuild/packages/core/src/server/socketServer.ts
Lines 222 to 227 in d2a2194
packages/core/src/server/socketServer.ts#L37-L38
rsbuild/packages/core/src/server/socketServer.ts
Lines 37 to 38 in d2a2194
Was this report helpful? Give feedback by reacting with 👍 or 👎

Summary
Enhance the security and functionality of the WebSocket across the codebase:
compilationIdmechanism with secure WebSocket tokens.environmentContext.webSocketToken:Related Links
Checklist