-
Notifications
You must be signed in to change notification settings - Fork 13.9k
fix: federation valid request being reject due to wrong schemas #41785
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
sampaiodiego
wants to merge
25
commits into
develop
Choose a base branch
from
fix-federation-endpoint-schemas-audit
base: develop
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
25 commits
Select commit
Hold shift + click to select a range
9f37f5e
fix(federation): publicRooms endpoints requiring optional params
sampaiodiego ba10d47
fix(federation): query/profile rejecting spec-valid fields
sampaiodiego 87ccdd9
fix(federation): get_missing_events requiring optional limit
sampaiodiego 4745b95
fix(federation): make_join 500 on unsupported room versions
sampaiodiego ba99512
fix(federation): backfill rejecting spec-valid limit values
sampaiodiego efa89fc
fix(federation): send transaction rejected on single malformed PDU
sampaiodiego f60daa2
fix(federation): state/state_ids declared response shapes
sampaiodiego d1ea287
fix(federation): invite errcode for invite_room_state validation
sampaiodiego c756ed1
docs(federation): link each endpoint to its Matrix spec definition
sampaiodiego 2317484
fix: handle limits <= 0
sampaiodiego 0b014f8
fix: add limit validation to backfill as well
sampaiodiego 65e8586
fix(federation): return a matrix error for non-invite events
sampaiodiego 233fe05
fix min_depth
sampaiodiego e25b65a
enhance public_rooms schema
sampaiodiego ed599fd
make transactions endpoint more compliant
sampaiodiego ee4fd21
fix backfill schema
sampaiodiego ac0803b
validate invites for local users only
sampaiodiego c19059c
check state_key type
sampaiodiego b118d5e
reject negative limit on POST publicRooms
sampaiodiego cb298e6
return 500 when a transaction fails to be processed
sampaiodiego 76bdfca
return 403 instead of throwing on invite for unknown local user
sampaiodiego 35441ba
reject invite state_key with an empty localpart
sampaiodiego 589502f
test invite state_key validation and unknown local user
sampaiodiego f7cd3c8
add full path comments for searchability
sampaiodiego f6fb274
fix import order
sampaiodiego 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| --- | ||
| '@rocket.chat/federation-matrix': patch | ||
| '@rocket.chat/meteor': patch | ||
| --- | ||
|
|
||
| Fixes federation endpoints rejecting requests that are valid per the Matrix specification: | ||
|
|
||
| - `publicRooms` (GET and POST) required params/fields the spec marks optional | ||
| - `query/profile` rejected spec-valid profile fields such as `m.tz` | ||
| - `get_missing_events` required the optional `limit` field and bounded it | ||
| - `make_join` returned 500 instead of 400 `M_INCOMPATIBLE_ROOM_VERSION` for unsupported room versions | ||
| - `backfill` rejected spec-valid `limit` values | ||
| - `send` rejected an entire transaction when a single PDU didn't match a fixed event shape, instead of reporting failures per PDU | ||
|
|
||
| Also links every federation endpoint to its definition in the Matrix specification. |
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
144 changes: 144 additions & 0 deletions
144
ee/packages/federation-matrix/src/api/_matrix/invite.spec.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,144 @@ | ||
| import { FederationMatrix } from '@rocket.chat/core-services'; | ||
| import { federationSDK } from '@rocket.chat/federation-sdk'; | ||
| import { Users } from '@rocket.chat/models'; | ||
|
|
||
| import { getMatrixInviteRoutes } from './invite'; | ||
|
|
||
| jest.mock('@rocket.chat/core-services', () => ({ | ||
| FederationMatrix: { | ||
| canUserAccessFederation: jest.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| jest.mock('@rocket.chat/models', () => ({ | ||
| Users: { | ||
| findOneByUsername: jest.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| jest.mock('@rocket.chat/federation-sdk', () => ({ | ||
| federationSDK: { | ||
| verifyRequestSignature: jest.fn(), | ||
| getConfig: jest.fn(), | ||
| processInvite: jest.fn(), | ||
| }, | ||
| NotAllowedError: class NotAllowedError extends Error {}, | ||
| errCodes: { | ||
| M_UNAUTHORIZED: { errcode: 'M_UNAUTHORIZED', error: 'Unauthorized', status: 401 }, | ||
| M_UNKNOWN: { errcode: 'M_UNKNOWN', error: 'Unknown error' }, | ||
| }, | ||
| })); | ||
|
|
||
| const mockVerifyRequestSignature = federationSDK.verifyRequestSignature as jest.MockedFunction<typeof federationSDK.verifyRequestSignature>; | ||
| const mockGetConfig = federationSDK.getConfig as jest.MockedFunction<typeof federationSDK.getConfig>; | ||
| const mockProcessInvite = federationSDK.processInvite as jest.MockedFunction<typeof federationSDK.processInvite>; | ||
| const mockFindOneByUsername = Users.findOneByUsername as jest.MockedFunction<typeof Users.findOneByUsername>; | ||
| const mockCanUserAccessFederation = FederationMatrix.canUserAccessFederation as jest.MockedFunction< | ||
| typeof FederationMatrix.canUserAccessFederation | ||
| >; | ||
|
|
||
| const OUR_SERVER_NAME = 'rocketchat.local'; | ||
|
|
||
| const buildInviteEvent = (stateKey: string) => ({ | ||
| type: 'm.room.member', | ||
| state_key: stateKey, | ||
| sender: '@attacker:attacker.com', | ||
| room_id: '!room:attacker.com', | ||
| origin_server_ts: 1600000000000, | ||
| depth: 1, | ||
| prev_events: [], | ||
| auth_events: [], | ||
| content: { membership: 'invite' }, | ||
| }); | ||
|
|
||
| const sendInvite = async (event: unknown) => | ||
| getMatrixInviteRoutes() | ||
| .getHonoRouter() | ||
| .request('/v2/invite/!room:attacker.com/$event', { | ||
| method: 'PUT', | ||
| headers: { | ||
| 'Authorization': 'X-Matrix origin="attacker.com"', | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| body: JSON.stringify({ | ||
| room_version: '10', | ||
| event, | ||
| invite_room_state: [{ type: 'm.room.create', state_key: '', content: { creator: '@attacker:attacker.com' } }], | ||
| }), | ||
| }); | ||
|
|
||
| describe('PUT /_matrix/federation/v2/invite/:roomId/:eventId', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
|
|
||
| mockVerifyRequestSignature.mockResolvedValue({ origin: 'attacker.com' } as any); | ||
| mockGetConfig.mockImplementation((key) => (key === 'serverName' ? OUR_SERVER_NAME : undefined) as any); | ||
| mockFindOneByUsername.mockResolvedValue({ _id: 'victimId', username: 'victim' } as any); | ||
| mockCanUserAccessFederation.mockResolvedValue(true); | ||
| mockProcessInvite.mockImplementation(async (event: any) => ({ event }) as any); | ||
| }); | ||
|
|
||
| it('should reject an invite whose state_key belongs to another homeserver', async () => { | ||
| const response = await sendInvite(buildInviteEvent(`@victim:attacker.com`)); | ||
|
|
||
| expect(response.status).toBe(400); | ||
| expect(await response.json()).toEqual({ | ||
| errcode: 'M_UNKNOWN', | ||
| error: 'The invite event must be for a user of this server', | ||
| }); | ||
| expect(mockFindOneByUsername).not.toHaveBeenCalled(); | ||
| expect(mockProcessInvite).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should reject an invite whose state_key is not a valid user ID', async () => { | ||
| const response = await sendInvite(buildInviteEvent('victim')); | ||
|
|
||
| expect(response.status).toBe(400); | ||
| expect(await response.json()).toEqual({ | ||
| errcode: 'M_UNKNOWN', | ||
| error: 'The invite event state_key is not a valid user ID', | ||
| }); | ||
| expect(mockFindOneByUsername).not.toHaveBeenCalled(); | ||
| expect(mockProcessInvite).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should reject an invite whose state_key has an empty localpart', async () => { | ||
| const response = await sendInvite(buildInviteEvent(`@:${OUR_SERVER_NAME}`)); | ||
|
|
||
| expect(response.status).toBe(400); | ||
| expect(await response.json()).toEqual({ | ||
| errcode: 'M_UNKNOWN', | ||
| error: 'The invite event state_key is not a valid user ID', | ||
| }); | ||
| expect(mockFindOneByUsername).not.toHaveBeenCalled(); | ||
| expect(mockProcessInvite).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should reject an invite addressed to a user that does not exist on this server', async () => { | ||
| mockFindOneByUsername.mockResolvedValue(null); | ||
|
|
||
| const response = await sendInvite(buildInviteEvent(`@ghost:${OUR_SERVER_NAME}`)); | ||
|
|
||
| expect(response.status).toBe(403); | ||
| expect(await response.json()).toEqual({ | ||
| errcode: 'M_FORBIDDEN', | ||
| error: 'User does not have permission to access federation', | ||
| }); | ||
| expect(mockProcessInvite).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should accept a state_key whose localpart contains characters the spec allows', async () => { | ||
| const response = await sendInvite(buildInviteEvent(`@victim+1/2:${OUR_SERVER_NAME}`)); | ||
|
|
||
| expect(response.status).toBe(200); | ||
| expect(mockFindOneByUsername).toHaveBeenCalledWith('victim+1/2'); | ||
| }); | ||
|
|
||
| it('should process an invite addressed to a user of this server', async () => { | ||
| const response = await sendInvite(buildInviteEvent(`@victim:${OUR_SERVER_NAME}`)); | ||
|
|
||
| expect(response.status).toBe(200); | ||
| expect(mockFindOneByUsername).toHaveBeenCalledWith('victim'); | ||
| expect(mockProcessInvite).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); |
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
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.
Uh oh!
There was an error while loading. Please reload this page.