-
Notifications
You must be signed in to change notification settings - Fork 19
feat: add improved errors messaging #305
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7f407d6
feat: add improved errors messaging
ricardogarim 5cf78a4
chore: remove httpCode from FederationValidationError
ricardogarim 7e52690
chore: remove try catch block from checkRoomAcl
ricardogarim 0c34985
disable validation during tests
sampaiodiego 326cb5e
simplify config
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
Some comments aren't visible on the classic Files Changed page.
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
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
116 changes: 116 additions & 0 deletions
116
packages/federation-sdk/src/services/federation-validation.service.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,116 @@ | ||
| import type { RoomID, UserID } from '@rocket.chat/federation-room'; | ||
| import { extractDomainFromId } from '@rocket.chat/federation-room'; | ||
| import { singleton } from 'tsyringe'; | ||
| import { FederationEndpoints } from '../specs/federation-api'; | ||
| import { ConfigService } from './config.service'; | ||
| import { EventAuthorizationService } from './event-authorization.service'; | ||
| import { FederationRequestService } from './federation-request.service'; | ||
| import { StateService } from './state.service'; | ||
|
|
||
| export class FederationValidationError extends Error { | ||
| public error: string; | ||
|
|
||
| constructor( | ||
| public code: 'POLICY_DENIED' | 'CONNECTION_FAILED' | 'USER_NOT_FOUND', | ||
| public userMessage: string, | ||
| ) { | ||
| super(userMessage); | ||
| this.name = 'FederationValidationError'; | ||
| this.error = `federation-${code.toLowerCase().replace(/_/g, '-')}`; | ||
| } | ||
| } | ||
|
|
||
| @singleton() | ||
| export class FederationValidationService { | ||
| constructor( | ||
| private readonly configService: ConfigService, | ||
| private readonly federationRequestService: FederationRequestService, | ||
| private readonly stateService: StateService, | ||
| private readonly eventAuthorizationService: EventAuthorizationService, | ||
| ) {} | ||
|
|
||
| async validateOutboundUser(userId: UserID): Promise<void> { | ||
| const domain = extractDomainFromId(userId); | ||
| await this.checkDomainReachable(domain); | ||
| await this.checkUserExists(userId, domain); | ||
| } | ||
|
|
||
| async validateOutboundInvite(userId: UserID, roomId: RoomID): Promise<void> { | ||
| const domain = extractDomainFromId(userId); | ||
| await this.checkRoomAcl(roomId, domain); | ||
| await this.checkDomainReachable(domain); | ||
| await this.checkUserExists(userId, domain); | ||
| } | ||
|
|
||
| private async checkRoomAcl(roomId: RoomID, domain: string): Promise<void> { | ||
| const state = await this.stateService.getLatestRoomState(roomId); | ||
| const aclEvent = state.get('m.room.server_acl:'); | ||
| if (!aclEvent || !aclEvent.isServerAclEvent()) { | ||
| return; | ||
| } | ||
|
|
||
| const isAllowed = await this.eventAuthorizationService.checkServerAcl( | ||
| aclEvent, | ||
| domain, | ||
| ); | ||
| if (!isAllowed) { | ||
| throw new FederationValidationError( | ||
| 'POLICY_DENIED', | ||
| "Action Blocked. The room's access control policy blocks communication with this domain.", | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| private async checkDomainReachable(domain: string): Promise<void> { | ||
| const timeoutMs = | ||
| this.configService.getConfig('networkCheckTimeoutMs') || 5000; | ||
|
|
||
| try { | ||
| const versionPromise = this.federationRequestService.get<{ | ||
| server: { name?: string; version?: string }; | ||
| }>(domain, FederationEndpoints.version); | ||
|
|
||
| await this.withTimeout(versionPromise, timeoutMs); | ||
| } catch (_error) { | ||
| throw new FederationValidationError( | ||
| 'CONNECTION_FAILED', | ||
| 'Connection Failed. The server domain could not be reached or does not support federation.', | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| private async checkUserExists(userId: UserID, domain: string): Promise<void> { | ||
| const timeoutMs = | ||
| this.configService.getConfig('userCheckTimeoutMs') || 10000; | ||
|
|
||
| try { | ||
| const uri = FederationEndpoints.queryProfile(userId); | ||
| const queryParams = { user_id: userId }; | ||
|
|
||
| const profilePromise = this.federationRequestService.get<{ | ||
| displayname?: string; | ||
| avatar_url?: string; | ||
| }>(domain, uri, queryParams); | ||
|
|
||
| await this.withTimeout(profilePromise, timeoutMs); | ||
| } catch (_error) { | ||
| throw new FederationValidationError( | ||
| 'USER_NOT_FOUND', | ||
| "Invitation blocked. The specified user couldn't be found on their homeserver.", | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| private async withTimeout<T>( | ||
| promise: Promise<T>, | ||
| timeoutMs: number, | ||
| ): Promise<T> { | ||
| const timeoutPromise = new Promise<never>((_, reject) => { | ||
| setTimeout(() => { | ||
| reject(new Error(`Operation timed out after ${timeoutMs}ms`)); | ||
| }, timeoutMs); | ||
| }); | ||
|
|
||
| return Promise.race([promise, timeoutPromise]); | ||
| } | ||
| } |
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
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.
🧩 Analysis chain
Consider the room cleanup implications.
The validation occurs after the DM room has been fully created and persisted (lines 1406-1522). If
validateOutboundInvitefails, the room remains in the database with only the creator as a member.This design trade-off exists because the ACL check requires room state. However, it could lead to orphaned single-member rooms accumulating over time. Consider:
🏁 Script executed:
Length of output: 0
🏁 Script executed:
Length of output: 0
🏁 Script executed:
Length of output: 3203
🏁 Script executed:
Length of output: 0
🏁 Script executed:
Length of output: 2735
🏁 Script executed:
Length of output: 1304
🏁 Script executed:
Length of output: 290
🏁 Script executed:
Length of output: 721
🏁 Script executed:
Length of output: 701
Add cleanup logic or validate external user before room creation.
The concern is confirmed: if
validateOutboundInvitefails (e.g., domain unreachable, user not found), the DM room persists in the database with only the creator as a member. The controller's error handler (direct-message.controller.ts:38-44) returns an error response but does not remove the orphaned room.While the validation must occur after room creation (because the ACL check requires room state), consider: