Skip to content
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

Regression: Do not allow create federated DMs from the UI on CE #26279

Merged
merged 2 commits into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,19 @@ export class FederationRoomServiceSender {
throw new Error('error-this-is-an-ee-feature');
}
}

public async beforeCreateDirectMessageFromUI(internalUsers: (IUser | string)[]): Promise<void> {
const usernames = internalUsers.map((user) => {
if (typeof user === 'string') {
return user;
}
return user.username;
});
const isThereAnyFederatedUser =
usernames.some((username) => username?.includes(':')) ||
internalUsers.filter((user) => typeof user !== 'string').some((user) => (user as IUser).federated);
if (isThereAnyFederatedUser) {
throw new Error('error-this-is-an-ee-feature');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export class FederationFactory {
);
FederationHooks.canAddTheUserToTheRoom((user: IUser | string, room: IRoom) => roomServiceSender.canAddThisUserToTheRoom(user, room));
FederationHooks.canAddUsersToTheRoom((user: IUser | string, room: IRoom) => roomServiceSender.canAddUsersToTheRoom(user, room));
FederationHooks.beforeCreateDirectMessage((members: (IUser | string)[]) => roomServiceSender.beforeCreateDirectMessageFromUI(members));
}

public static removeListeners(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export class RocketChatUserAdapter {
public async createFederatedUser(federatedUser: FederatedUser): Promise<void> {
const existingLocalUser = await Users.findOneByUsername(federatedUser.internalReference.username || '');
if (existingLocalUser) {
await Users.setAsFederated(existingLocalUser._id);
return MatrixBridgedUser.upsert(
{ uid: existingLocalUser._id },
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,19 @@ export class FederationHooks {
);
}

public static beforeCreateDirectMessage(callback: Function): void {
callbacks.add(
'federation.beforeCreateDirectMessage',
(members: IUser[]): void => {
Promise.await(callback(members));
},
callbacks.priority.HIGH,
'federation-v2-before-create-direct-message-ce',
);
}

public static removeCEValidation(): void {
callbacks.remove('federation.beforeAddUserAToRoom', 'federation-v2-can-add-users-to-the-room');
callbacks.remove('federation.beforeCreateDirectMessage', 'federation-v2-before-create-direct-message-ce');
}
}
1 change: 1 addition & 0 deletions apps/meteor/lib/callbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type EventLikeCallbackSignatures = {
'onValidateLogin': (login: ILoginAttempt) => void;
'federation.afterCreateFederatedRoom': (room: IRoom, second: { owner: IUser; originalMemberList: string[] }) => void;
'beforeCreateDirectRoom': (members: IUser[]) => void;
'federation.beforeCreateDirectMessage': (members: IUser[]) => void;
'federation.beforeAddUserAToRoom': (params: { user: IUser | string; inviter: IUser }, room: IRoom) => void;
'onJoinVideoConference': (callId: VideoConference['_id'], userId?: IUser['_id']) => Promise<void>;
};
Expand Down
6 changes: 6 additions & 0 deletions apps/meteor/server/methods/createDirectMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Users, Rooms } from '../../app/models/server';
import { RateLimiterClass as RateLimiter } from '../../app/lib/server/lib/RateLimiter';
import { createRoom } from '../../app/lib/server/functions/createRoom';
import { addUser } from '../../app/federation/server/functions/addUser';
import { callbacks } from '../../lib/callbacks';

export function createDirectMessage(usernames, userId, excludeSelf = false) {
check(usernames, [String]);
Expand Down Expand Up @@ -89,6 +90,11 @@ export function createDirectMessage(usernames, userId, excludeSelf = false) {
if (excludeSelf && hasPermission(this.userId, 'view-room-administration')) {
options.subscriptionExtra = { open: true };
}
try {
callbacks.run('federation.beforeCreateDirectMessage', roomUsers);
} catch (error) {
throw new Meteor.Error(error?.message);
}
const { _id: rid, inserted, ...room } = createRoom('d', null, null, roomUsers, null, {}, options);

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,4 +327,22 @@ describe('Federation - Application - FederationRoomServiceSender', () => {
expect(bridge.leaveRoom.calledWith('externalId', 'externalId')).to.be.true;
});
});

describe('#beforeCreateDirectMessageFromUI()', () => {
it('should throw an error if there at least one user with federated: true', async () => {
try {
await service.beforeCreateDirectMessageFromUI([{ federated: true } as any, {} as any]);
} catch (e: any) {
expect(e.message).to.be.equal('error-this-is-an-ee-feature');
}
});

it('should throw an error if there at least one new external user', async () => {
try {
await service.beforeCreateDirectMessageFromUI(['@myexternal:external.com', {} as any]);
} catch (e: any) {
expect(e.message).to.be.equal('error-this-is-an-ee-feature');
}
});
});
});