Skip to content

Commit c6b8c6f

Browse files
committed
avoid to join non-private or encrypted rooms based on settings
1 parent 4711b48 commit c6b8c6f

File tree

4 files changed

+72
-28
lines changed

4 files changed

+72
-28
lines changed

apps/meteor/server/settings/federation-service.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,21 @@ export const createFederationServiceSettings = async (): Promise<void> => {
8484
invalidValue: false,
8585
alert: 'Federation_Service_EDU_Process_Presence_Alert',
8686
});
87+
88+
await this.add('Federation_Service_Join_Encrypted_Rooms', false, {
89+
type: 'boolean',
90+
public: false,
91+
enterprise: true,
92+
modules: ['federation'],
93+
invalidValue: false,
94+
});
95+
96+
await this.add('Federation_Service_Join_Non_Private_Rooms', false, {
97+
type: 'boolean',
98+
public: false,
99+
enterprise: true,
100+
modules: ['federation'],
101+
invalidValue: false,
102+
});
87103
});
88104
};

ee/packages/federation-matrix/src/api/_matrix/invite.ts

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { FederationMatrix, Room } from '@rocket.chat/core-services';
22
import { isUserNativeFederated, type IUser } from '@rocket.chat/core-typings';
3-
import { eventIdSchema, roomIdSchema } from '@rocket.chat/federation-sdk';
43
import type {
54
HomeserverServices,
65
RoomService,
@@ -9,6 +8,7 @@ import type {
98
PersistentEventBase,
109
RoomVersion,
1110
} from '@rocket.chat/federation-sdk';
11+
import { eventIdSchema, roomIdSchema, NotAllowedError } from '@rocket.chat/federation-sdk';
1212
import { Router } from '@rocket.chat/http-router';
1313
import { Rooms, Users } from '@rocket.chat/models';
1414
import { ajv } from '@rocket.chat/rest-typings/dist/v1/Ajv';
@@ -355,32 +355,52 @@ export const getMatrixInviteRoutes = (services: HomeserverServices) => {
355355
throw new Error('user not found not processing invite');
356356
}
357357

358-
const inviteEvent = await invite.processInvite(
359-
event,
360-
roomIdSchema.parse(roomId),
361-
eventIdSchema.parse(eventId),
362-
roomVersion,
363-
c.get('authenticatedServer'),
364-
);
365-
366-
setTimeout(
367-
() => {
368-
void startJoiningRoom({
369-
inviteEvent,
370-
user: ourUser,
371-
room,
372-
state,
373-
});
374-
},
375-
inviteEvent.event.content.is_direct ? 2000 : 0,
376-
);
377-
378-
return {
379-
body: {
380-
event: inviteEvent.event,
381-
},
382-
statusCode: 200,
383-
};
358+
try {
359+
const inviteEvent = await invite.processInvite(
360+
event,
361+
roomIdSchema.parse(roomId),
362+
eventIdSchema.parse(eventId),
363+
roomVersion,
364+
c.get('authenticatedServer'),
365+
);
366+
367+
setTimeout(
368+
() => {
369+
void startJoiningRoom({
370+
inviteEvent,
371+
user: ourUser,
372+
room,
373+
state,
374+
});
375+
},
376+
inviteEvent.event.content.is_direct ? 2000 : 0,
377+
);
378+
379+
return {
380+
body: {
381+
event: inviteEvent.event,
382+
},
383+
statusCode: 200,
384+
};
385+
} catch (error) {
386+
if (error instanceof NotAllowedError) {
387+
return {
388+
body: {
389+
errcode: 'M_FORBIDDEN',
390+
error: 'This server does not allow joining this type of room based on federation settings.',
391+
},
392+
statusCode: 403,
393+
};
394+
}
395+
396+
return {
397+
body: {
398+
errcode: 'M_UNKNOWN',
399+
error: error instanceof Error ? error.message : 'Internal server error while processing request',
400+
},
401+
statusCode: 500,
402+
};
403+
}
384404
},
385405
);
386406
};

ee/packages/federation-matrix/src/setup.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ export async function setupFederationMatrix(instanceId: string): Promise<boolean
4545
const signingKey = (await Settings.getValueById<string>('Federation_Service_Matrix_Signing_Key')) || '';
4646
const signingAlg = (await Settings.getValueById<string>('Federation_Service_Matrix_Signing_Algorithm')) || '';
4747
const signingVersion = (await Settings.getValueById<string>('Federation_Service_Matrix_Signing_Version')) || '';
48+
const allowedEncryptedRooms = (await Settings.getValueById<boolean>('Federation_Service_Join_Encrypted_Rooms')) || false;
49+
const allowedNonPrivateRooms = (await Settings.getValueById<boolean>('Federation_Service_Join_Non_Private_Rooms')) || false;
4850

4951
// TODO are these required?
5052
const mongoUri = process.env.MONGO_URL || 'mongodb://localhost:3001/meteor';
@@ -83,6 +85,10 @@ export async function setupFederationMatrix(instanceId: string): Promise<boolean
8385
downloadPerMinute: Number.parseInt(process.env.MEDIA_DOWNLOAD_RATE_LIMIT || '60', 10),
8486
},
8587
},
88+
invite: {
89+
allowedEncryptedRooms,
90+
allowedNonPrivateRooms,
91+
},
8692
});
8793

8894
const eventHandler = new Emitter<HomeserverEventSignatures>();

packages/i18n/src/locales/en.i18n.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2161,7 +2161,7 @@
21612161
"Federation_Service_EDU_Process_Presence": "Process Presence events",
21622162
"Federation_Service_EDU_Process_Presence_Description": "Send and receive events of user presence (online, offline, etc.) between federated servers.",
21632163
"Federation_Service_EDU_Process_Presence_Alert": "Enabling presence events may increase the load on your server and network traffic considerably, especially if you have many users. Only enable this option if you understand the implications and have the necessary resources to handle the additional load.",
2164-
"Federation_Service_Alert": "<strong>This is an alfa feature not intended for production usage!</strong><br/>It may not be stable and/or performatic. Please be aware that it may change, break, or even be removed in the future without any notice.",
2164+
"Federation_Service_Alert": "<strong>This is an Alpha feature not intended for production usage!</strong><br/>It may not be stable and/or performatic. Please be aware that it may change, break, or even be removed in the future without any notice.",
21652165
"Federation_Service_Domain": "Federated Domain",
21662166
"Federation_Service_Domain_Description": "The domain that this server should respond to, for example: `acme.com`. This will be used as the suffix for user IDs (e.g., `@user:acme.com`).<br/>If your chat server is accessible from a different domain than the one you want to use for federation, you should follow our documentation to configure the `.well-known` file on your web server.",
21672167
"Federation_Service_Domain_Alert": "Inform only the domain, do not include http(s)://, slashes or any path after it.<br/>Use something like `acme.com` and not `https://acme.com/chat`.",
@@ -2172,6 +2172,8 @@
21722172
"Federation_Service_max_allowed_size_of_public_rooms_to_join": "Maximum number of members when joining a public room in a remote server",
21732173
"Federation_Service_max_allowed_size_of_public_rooms_to_join_Alert": "Keep in mind, that the bigger the room you allow for users to join, the more time it will take to join that room, besides the amount of resource it will use. <a target=\"_blank\" href=\"https://matrix.org/blog/2022/10/18/testing-faster-remote-room-joins\">Read more</a>",
21742174
"Federation_Service_max_allowed_size_of_public_rooms_to_join_Description": "The user limit from a public room in a remote server that can still be joined. Rooms that exceed this setting will still be listed, but users won't be able to join them",
2175+
"Federation_Service_Join_Encrypted_Rooms": "Allow joining encrypted federated rooms",
2176+
"Federation_Service_Join_Non_Private_Rooms": "Allow joining non-private rooms",
21752177
"Federation_Service_Allow_List": "Domain Allow List",
21762178
"Federation_Service_Allow_List_Description": "Restrict federation to the given allow list of domains.",
21772179
"Field": "Field",

0 commit comments

Comments
 (0)