-
-
Notifications
You must be signed in to change notification settings - Fork 240
fix: add room membership checks. Broadcast errors to room only #1391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jiexi
wants to merge
4
commits into
main
Choose a base branch
from
jl/WAPI-1549/add-room-membership-check-and-scope-channelId-error-broadcasts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import baseConfig from '../../jest.config.base'; | ||
|
|
||
| module.exports = { | ||
| ...baseConfig, | ||
| testMatch: [ | ||
| '**/__tests__/**/*.[jt]s?(x)', | ||
| '**/?(*.)+(spec|test).[tj]s?(x)', | ||
| ], | ||
| testPathIgnorePatterns: ['/node_modules/', '/dist/'], | ||
| setupFiles: ['<rootDir>/jest.setup.ts'], | ||
| moduleNameMapper: { | ||
| '^analytics-node$': '<rootDir>/e2e/analytics-node.ts', | ||
| }, | ||
| clearMocks: true, | ||
| resetMocks: false, | ||
| restoreMocks: false, | ||
| watchman: false, | ||
| }; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| /* eslint-disable node/no-process-env */ | ||
| // Jest setup: ensure required environment variables are present so that | ||
| // `analytics-api` does not call `process.exit(1)` when it loads. | ||
| process.env.REDIS_NODES = | ||
| process.env.REDIS_NODES ?? 'redis://localhost:6379'; | ||
| process.env.NODE_ENV = process.env.NODE_ENV ?? 'test'; | ||
|
|
||
| // Some tests mock `./analytics-api` so the side-effectful logger setup in | ||
| // `./config` never runs. Initialise the logger here so `getLogger()` returns | ||
| // a real winston logger from any module under test. | ||
| // eslint-disable-next-line import/no-unassigned-import | ||
| import './src/config'; |
191 changes: 191 additions & 0 deletions
191
packages/sdk-socket-server-next/src/protocol/handleChannelRejected.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| /* eslint-disable jsdoc/require-jsdoc */ | ||
| import { v4 as uuidv4 } from 'uuid'; | ||
|
|
||
| const mockPubClient = { | ||
| get: jest.fn(), | ||
| setex: jest.fn(), | ||
| }; | ||
|
|
||
| jest.mock('../analytics-api', () => ({ | ||
| pubClient: mockPubClient, | ||
| })); | ||
|
|
||
| jest.mock('@socket.io/redis-adapter', () => ({ | ||
| createAdapter: jest.fn(), | ||
| })); | ||
|
|
||
| import { | ||
| handleChannelRejected, | ||
| ChannelRejectedParams, | ||
| } from './handleChannelRejected'; | ||
| import { ChannelConfig } from './handleJoinChannel'; | ||
|
|
||
| function makeSocket({ | ||
| rooms, | ||
| socketId = 'socket-id-1', | ||
| }: { | ||
| rooms: string[]; | ||
| socketId?: string; | ||
| }) { | ||
| const broadcastToEmit = jest.fn(); | ||
| return { | ||
| id: socketId, | ||
| request: { socket: { remoteAddress: '127.0.0.1' } }, | ||
| rooms: new Set(rooms), | ||
| broadcast: { | ||
| to: jest.fn(() => ({ emit: broadcastToEmit })), | ||
| }, | ||
| } as any; | ||
| } | ||
|
|
||
| describe('handleChannelRejected participant check (HackerOne 3604630)', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('rejects requests from a non-participant socket on a fresh channel', async () => { | ||
| const channelId = uuidv4(); | ||
| const socket = makeSocket({ rooms: [] }); | ||
|
|
||
| // No existing channelConfig: this is a fresh channelId an attacker is | ||
| // poking at. | ||
| mockPubClient.get.mockResolvedValueOnce(null); | ||
|
|
||
| const callback = jest.fn(); | ||
| const params: ChannelRejectedParams = { | ||
| io: {} as any, | ||
| socket, | ||
| channelId, | ||
| }; | ||
|
|
||
| await handleChannelRejected(params, callback); | ||
|
|
||
| expect(callback).toHaveBeenCalledWith('not authorized', undefined); | ||
| // Must not write any rejected entry to redis on a non-participant request. | ||
| expect(mockPubClient.setex).not.toHaveBeenCalled(); | ||
| expect(socket.broadcast.to).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('rejects requests from a non-participant socket even when a channelConfig with no wallet exists', async () => { | ||
| const channelId = uuidv4(); | ||
| const socket = makeSocket({ rooms: [] }); | ||
|
|
||
| // Existing config but no wallet recorded yet (e.g. only the dapp has | ||
| // joined). The reconnect-and-reject flow only applies to wallets that | ||
| // had previously joined. | ||
| const existingConfig: ChannelConfig = { | ||
| clients: { dapp: 'dapp-socket-id', wallet: '' }, | ||
| createdAt: 1, | ||
| updatedAt: 1, | ||
| }; | ||
| mockPubClient.get.mockResolvedValueOnce(JSON.stringify(existingConfig)); | ||
|
|
||
| const callback = jest.fn(); | ||
| const params: ChannelRejectedParams = { | ||
| io: {} as any, | ||
| socket, | ||
| channelId, | ||
| }; | ||
|
|
||
| await handleChannelRejected(params, callback); | ||
|
|
||
| expect(callback).toHaveBeenCalledWith('not authorized', undefined); | ||
| expect(mockPubClient.setex).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('rejects requests with an invalid channelId', async () => { | ||
| const socket = makeSocket({ rooms: [] }); | ||
| const callback = jest.fn(); | ||
| const params: ChannelRejectedParams = { | ||
| io: {} as any, | ||
| socket, | ||
| channelId: 'not-a-uuid', | ||
| }; | ||
|
|
||
| await handleChannelRejected(params, callback); | ||
|
|
||
| expect(callback).toHaveBeenCalledWith('error_id', undefined); | ||
| expect(mockPubClient.get).not.toHaveBeenCalled(); | ||
| expect(mockPubClient.setex).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('allows the request when the socket is a live in-room participant', async () => { | ||
| const channelId = uuidv4(); | ||
| const socket = makeSocket({ rooms: [channelId] }); | ||
|
|
||
| mockPubClient.get.mockResolvedValueOnce(null); | ||
| mockPubClient.setex.mockResolvedValueOnce('OK'); | ||
|
|
||
| const callback = jest.fn(); | ||
| const params: ChannelRejectedParams = { | ||
| io: {} as any, | ||
| socket, | ||
| channelId, | ||
| }; | ||
|
|
||
| await handleChannelRejected(params, callback); | ||
|
|
||
| expect(mockPubClient.setex).toHaveBeenCalledTimes(1); | ||
| expect(callback).toHaveBeenCalledWith(null, { success: true }); | ||
| }); | ||
|
|
||
| it('allows the post-reconnect reject flow when channelConfig has a known wallet', async () => { | ||
| const channelId = uuidv4(); | ||
| // Wallet has reconnected, so its socket is not in the room. | ||
| const socket = makeSocket({ | ||
| rooms: [], | ||
| socketId: 'wallet-reconnected-socket-id', | ||
| }); | ||
|
|
||
| const existingConfig: ChannelConfig = { | ||
| clients: { | ||
| wallet: 'previous-wallet-socket-id', | ||
| dapp: 'dapp-socket-id', | ||
| }, | ||
| createdAt: 1, | ||
| updatedAt: 1, | ||
| }; | ||
| mockPubClient.get.mockResolvedValueOnce(JSON.stringify(existingConfig)); | ||
| mockPubClient.setex.mockResolvedValueOnce('OK'); | ||
|
|
||
| const callback = jest.fn(); | ||
| const params: ChannelRejectedParams = { | ||
| io: {} as any, | ||
| socket, | ||
| channelId, | ||
| }; | ||
|
|
||
| await handleChannelRejected(params, callback); | ||
|
|
||
| expect(mockPubClient.setex).toHaveBeenCalledTimes(1); | ||
| const payload = JSON.parse(mockPubClient.setex.mock.calls[0][2]); | ||
| expect(payload.rejected).toBe(true); | ||
|
|
||
| expect(socket.broadcast.to).toHaveBeenCalledWith(channelId); | ||
| expect(callback).toHaveBeenCalledWith(null, { success: true }); | ||
| }); | ||
|
|
||
| it('does not modify a channel that is already in the ready state', async () => { | ||
| const channelId = uuidv4(); | ||
| const socket = makeSocket({ rooms: [channelId] }); | ||
|
|
||
| const existingConfig: ChannelConfig = { | ||
| clients: { wallet: 'wallet-id', dapp: 'dapp-id' }, | ||
| ready: true, | ||
| createdAt: 1, | ||
| updatedAt: 1, | ||
| }; | ||
| mockPubClient.get.mockResolvedValueOnce(JSON.stringify(existingConfig)); | ||
|
|
||
| const callback = jest.fn(); | ||
| const params: ChannelRejectedParams = { | ||
| io: {} as any, | ||
| socket, | ||
| channelId, | ||
| }; | ||
|
|
||
| await handleChannelRejected(params, callback); | ||
|
|
||
| expect(mockPubClient.setex).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
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
95 changes: 95 additions & 0 deletions
95
packages/sdk-socket-server-next/src/protocol/handleMessage.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| /* eslint-disable jsdoc/require-jsdoc */ | ||
| import { v4 as uuidv4 } from 'uuid'; | ||
|
|
||
| const mockPubClient = { | ||
| get: jest.fn(), | ||
| set: jest.fn(), | ||
| rpush: jest.fn(), | ||
| expire: jest.fn(), | ||
| }; | ||
|
|
||
| jest.mock('../analytics-api', () => ({ | ||
| pubClient: mockPubClient, | ||
| })); | ||
|
|
||
| jest.mock('../rate-limiter', () => ({ | ||
| rateLimiterMessage: { consume: jest.fn().mockResolvedValue(undefined) }, | ||
| resetRateLimits: jest.fn(), | ||
| increaseRateLimits: jest.fn(), | ||
| setLastConnectionErrorTimestamp: jest.fn(), | ||
| })); | ||
|
|
||
| jest.mock('@socket.io/redis-adapter', () => ({ | ||
| createAdapter: jest.fn(), | ||
| })); | ||
|
|
||
| import { handleMessage, MessageParams } from './handleMessage'; | ||
|
|
||
| type Emit = jest.Mock; | ||
|
|
||
| function makeSocket(channelId: string) { | ||
| const broadcastToEmit: Emit = jest.fn(); | ||
| const broadcastEmit: Emit = jest.fn(); | ||
|
|
||
| const broadcast = { | ||
| to: jest.fn(() => ({ emit: broadcastToEmit })), | ||
| emit: broadcastEmit, | ||
| }; | ||
|
|
||
| return { | ||
| socket: { | ||
| id: 'socket-id-1', | ||
| handshake: { address: '127.0.0.1' }, | ||
| request: { socket: { remoteAddress: '127.0.0.1' } }, | ||
| rooms: new Set([channelId]), | ||
| broadcast, | ||
| emit: jest.fn(), | ||
| } as any, | ||
| broadcastToEmit, | ||
| broadcastEmit, | ||
| broadcastTo: broadcast.to, | ||
| }; | ||
| } | ||
|
|
||
| describe('handleMessage error path', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('scopes the error broadcast to the channel room (regression: HackerOne 3604630)', async () => { | ||
| const channelId = uuidv4(); | ||
| const { socket, broadcastTo, broadcastToEmit, broadcastEmit } = | ||
| makeSocket(channelId); | ||
|
|
||
| // Force handleMessage to throw inside its try-block by making | ||
| // pubClient.get reject. | ||
| mockPubClient.get.mockRejectedValueOnce(new Error('boom')); | ||
|
|
||
| const callback = jest.fn(); | ||
|
|
||
| const params: MessageParams = { | ||
| io: {} as any, | ||
| socket, | ||
| channelId, | ||
| clientType: 'dapp', | ||
| context: 'dapp', | ||
| message: 'encrypted-string', | ||
| hasRateLimit: false, | ||
| callback, | ||
| }; | ||
|
|
||
| await handleMessage(params); | ||
|
|
||
| expect(broadcastTo).toHaveBeenCalledWith(channelId); | ||
| expect(broadcastToEmit).toHaveBeenCalledWith( | ||
| `message-${channelId}`, | ||
| expect.objectContaining({ error: 'boom' }), | ||
| ); | ||
|
|
||
| // CRITICAL: error must NOT be broadcast to all sockets (which would | ||
| // leak the active channel ID). | ||
| expect(broadcastEmit).not.toHaveBeenCalled(); | ||
|
|
||
| expect(callback).toHaveBeenCalledWith('boom'); | ||
| }); | ||
| }); |
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reject auth too broad
Medium Severity
The new off-room guard allows any socket to complete
handleChannelRejectedwhenchannelConfig.clients.walletis set, without tying the request to the wallet. A non-member who knows the channel UUID can mark the channel rejected and broadcast to the room, not only a reconnecting wallet.Reviewed by Cursor Bugbot for commit 93319fb. Configure here.