Skip to content

Commit 3c9f75e

Browse files
committed
check for federated users being added on non-federated rooms
1 parent 3a85856 commit 3c9f75e

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

apps/meteor/app/lib/server/methods/addUsersToRoom.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { api } from '@rocket.chat/core-services';
22
import type { IUser } from '@rocket.chat/core-typings';
3+
import { isRoomNativeFederated, isUserNativeFederated } from '@rocket.chat/core-typings';
34
import type { ServerMethods } from '@rocket.chat/ddp-client';
45
import { Subscriptions, Users, Rooms } from '@rocket.chat/models';
56
import { Match } from 'meteor/check';
@@ -17,10 +18,6 @@ declare module '@rocket.chat/ddp-client' {
1718
}
1819
}
1920

20-
const isAFederatedUsername = (username: string) => {
21-
return username.includes('@') && username.includes(':');
22-
};
23-
2421
export const addUsersToRoomMethod = async (userId: string, data: { rid: string; users: string[] }, user?: IUser): Promise<boolean> => {
2522
if (!userId) {
2623
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
@@ -83,7 +80,20 @@ export const addUsersToRoomMethod = async (userId: string, data: { rid: string;
8380
await Promise.all(
8481
data.users.map(async (username) => {
8582
const newUser = await Users.findOneByUsernameIgnoringCase(username);
86-
if (!newUser && !isAFederatedUsername(username)) {
83+
84+
if (!newUser) {
85+
throw new Meteor.Error('error-user-not-found', 'User not found', {
86+
method: 'addUsersToRoom',
87+
});
88+
}
89+
90+
if (isUserNativeFederated(newUser) && !isRoomNativeFederated(room)) {
91+
throw new Meteor.Error('error-federated-users-in-non-federated-rooms', 'Cannot add federated users to non-federated rooms', {
92+
method: 'addUsersToRoom',
93+
});
94+
}
95+
96+
if (!newUser && !isUserNativeFederated(newUser)) {
8797
throw new Meteor.Error('error-invalid-username', 'Invalid username', {
8898
method: 'addUsersToRoom',
8999
});

apps/meteor/app/slashcommands-invite/server/server.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import { addUsersToRoomMethod } from '../../lib/server/methods/addUsersToRoom';
88
import { settings } from '../../settings/server';
99
import { slashCommands } from '../../utils/server/slashCommand';
1010

11+
const isFederatedUsername = (username: string) => {
12+
return username.includes('@') && username.includes(':');
13+
};
14+
1115
/*
1216
* Invite is a named function that will replace /invite commands
1317
* @param {Object} message - The message object
@@ -17,16 +21,12 @@ slashCommands.add({
1721
callback: async ({ params, message, userId }: SlashCommandCallbackParams<'invite'>): Promise<void> => {
1822
const usernames = params
1923
.split(/[\s,]/)
20-
.map((username) => username.replace(/(^@)|( @)/, ''))
24+
.map((username) => (isFederatedUsername(username) ? username : username.replace(/(^@)|( @)/, '')))
2125
.filter((a) => a !== '');
2226
if (usernames.length === 0) {
2327
return;
2428
}
25-
const users = await Users.find({
26-
username: {
27-
$in: usernames,
28-
},
29-
}).toArray();
29+
const users = await Users.findByUsernames(usernames).toArray();
3030
if (users.length === 0) {
3131
void api.broadcast('notify.ephemeralMessage', userId, message.rid, {
3232
msg: i18n.t('User_doesnt_exist', {
@@ -81,7 +81,12 @@ slashCommands.add({
8181
if (typeof error !== 'string') {
8282
return;
8383
}
84-
if (error === 'cant-invite-for-direct-room') {
84+
85+
if (error === 'error-federated-users-in-non-federated-rooms') {
86+
void api.broadcast('notify.ephemeralMessage', userId, message.rid, {
87+
msg: i18n.t('You_cannot_add_external_users_to_non_federated_room', { lng: settings.get('Language') || 'en' }),
88+
});
89+
} else if (error === 'cant-invite-for-direct-room') {
8590
void api.broadcast('notify.ephemeralMessage', userId, message.rid, {
8691
msg: i18n.t('Cannot_invite_users_to_direct_rooms', { lng: settings.get('Language') || 'en' }),
8792
});

0 commit comments

Comments
 (0)