Skip to content
Merged
8 changes: 8 additions & 0 deletions .changeset/atomic-model-operations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@rocket.chat/meteor': patch
'@rocket.chat/core-typings': patch
'@rocket.chat/model-typings': patch
'@rocket.chat/models': patch
---

Fixes race conditions in several check-then-write database flows by collapsing them into single atomic operations: CAS login tokens can no longer be consumed by two concurrent logins, revoking a room invite no longer emits duplicate removal notifications, and deleting an integration now enforces the creator-only permission scope in the delete itself
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Meteor } from 'meteor/meteor';
import { RocketChatFileCustomSoundsInstance } from '../startup/custom-sounds';

export const deleteCustomSound = async (_id: string): Promise<void> => {
const sound = await CustomSounds.findOneById(_id);
const sound = await CustomSounds.findOneAndDeleteById(_id);
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved

if (!sound) {
throw new Meteor.Error('Custom_Sound_Error_Invalid_Sound', 'Invalid sound', {
Expand All @@ -14,7 +14,6 @@ export const deleteCustomSound = async (_id: string): Promise<void> => {
}

await RocketChatFileCustomSoundsInstance.deleteFile(`${sound._id}.${sound.extension}`);
await CustomSounds.removeById(_id);

void api.broadcast('notify.deleteCustomSound', { soundData: sound });
};
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ export const deleteEmojiCustom = async (userId: string, emojiID: ICustomEmojiDes
throw new Meteor.Error('not_authorized');
}

const emoji = await EmojiCustom.findOneById(emojiID);
const emoji = await EmojiCustom.findOneAndDeleteById(emojiID);
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
if (emoji == null) {
throw new Meteor.Error('Custom_Emoji_Error_Invalid_Emoji', 'Invalid emoji', {
method: 'deleteEmojiCustom',
});
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
}

await RocketChatFileEmojiCustomInstance.deleteFile(encodeURIComponent(`${emoji.name}.${emoji.extension}`));
await EmojiCustom.removeById(emojiID);
void api.broadcast('emoji.deleteCustom', emoji);

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,27 @@ declare module '@rocket.chat/ddp-client' {
}

export const deleteIncomingIntegration = async (integrationId: string, userId: string): Promise<void> => {
let integration;
let canManageAllIntegrations = false;
Comment thread
KevLehman marked this conversation as resolved.
Resolved

if (userId && (await hasPermissionAsync(userId, 'manage-incoming-integrations'))) {
integration = Integrations.findOneById(integrationId);
} else if (userId && (await hasPermissionAsync(userId, 'manage-own-incoming-integrations'))) {
integration = Integrations.findOne({
'_id': integrationId,
'_createdBy._id': userId,
});
} else {
canManageAllIntegrations = true;
} else if (!userId || !(await hasPermissionAsync(userId, 'manage-own-incoming-integrations'))) {
throw new Meteor.Error('not_authorized', 'Unauthorized', {
method: 'deleteIncomingIntegration',
});
}

if (!(await integration)) {
const integration = await Integrations.removeByIdAndCreatedByIfExists({
_id: integrationId,
...(!canManageAllIntegrations && { createdBy: userId }),
});

if (!integration) {
throw new Meteor.Error('error-invalid-integration', 'Invalid integration', {
method: 'deleteIncomingIntegration',
});
}

await Integrations.removeById(integrationId);
void notifyOnIntegrationChangedById(integrationId, 'removed');
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,33 @@ declare module '@rocket.chat/ddp-client' {
}

export const deleteOutgoingIntegration = async (integrationId: string, userId: string): Promise<void> => {
let integration;

if (!userId) {
throw new Meteor.Error('not_authorized', 'Unauthorized', {
method: 'deleteOutgoingIntegration',
});
}

let canManageAllIntegrations = false;

if (await hasPermissionAsync(userId, 'manage-outgoing-integrations')) {
integration = Integrations.findOneById(integrationId);
} else if (await hasPermissionAsync(userId, 'manage-own-outgoing-integrations')) {
integration = Integrations.findOne({
'_id': integrationId,
'_createdBy._id': userId,
});
} else {
canManageAllIntegrations = true;
} else if (!(await hasPermissionAsync(userId, 'manage-own-outgoing-integrations'))) {
throw new Meteor.Error('not_authorized', 'Unauthorized', {
method: 'deleteOutgoingIntegration',
});
}

if (!(await integration)) {
const integration = await Integrations.removeByIdAndCreatedByIfExists({
_id: integrationId,
...(!canManageAllIntegrations && { createdBy: userId }),
});

if (!integration) {
throw new Meteor.Error('error-invalid-integration', 'Invalid integration', {
method: 'deleteOutgoingIntegration',
});
}

await Integrations.removeById(integrationId);
// Don't sending to IntegrationHistory listener since it don't waits for 'removed' events.
await IntegrationHistory.removeByIntegrationId(integrationId);
void notifyOnIntegrationChangedById(integrationId, 'removed');
Expand Down
7 changes: 2 additions & 5 deletions apps/meteor/app/invites/server/functions/removeInvite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@ export const removeInvite = async (userId: string, invite: Pick<IInvite, '_id'>)
});
}

// Before anything, let's check if there's an existing invite
const existing = await Invites.findOneById(invite._id);
const { deletedCount } = await Invites.removeById(invite._id);

if (!existing) {
if (!deletedCount) {
throw new Meteor.Error('invalid-invitation-id', 'Invalid Invitation _id', {
method: 'removeInvite',
});
}

await Invites.removeById(invite._id);

return true;
};
13 changes: 3 additions & 10 deletions apps/meteor/app/meteor-accounts-saml/server/lib/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,18 +150,11 @@ export const loadSamlServiceProviders = async function (): Promise<void> {
SAMLUtils.logger?.warn({ msg: 'SAML Provider not loaded due to invalid configuration', key });
}

const service = await LoginServiceConfiguration.findOneByService(serviceName, { projection: { _id: 1 } });
if (!service?._id) {
return false;
const service = await LoginServiceConfiguration.removeByService(serviceName);
if (service) {
void notifyOnLoginServiceConfigurationChanged({ _id: service._id }, 'removed');
}

const { deletedCount } = await LoginServiceConfiguration.removeService(service._id);
if (!deletedCount) {
return false;
}

void notifyOnLoginServiceConfigurationChanged({ _id: service._id }, 'removed');

return false;
Comment thread
KevLehman marked this conversation as resolved.
Resolved
}),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ export const deleteOAuthApp = async (userId: string, applicationId: IOAuthApps['
throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'deleteOAuthApp' });
}

const application = await OAuthApps.findOneById(applicationId);
const application = await OAuthApps.findOneAndDeleteById(applicationId);
if (!application) {
throw new Meteor.Error('error-application-not-found', 'Application not found', {
method: 'deleteOAuthApp',
});
}

await OAuthApps.deleteOne({ _id: applicationId });

await OAuthAccessTokens.deleteMany({ clientId: application.clientId });
await OAuthAuthCodes.deleteMany({ clientId: application.clientId });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,6 @@ export const updateOAuthApp = async (
});
}

const currentApplication = await OAuthApps.findOneById(applicationId);
if (currentApplication == null) {
throw new Meteor.Error('error-application-not-found', 'Application not found', {
method: 'updateOAuthApp',
});
}

const redirectUri = parseUriList(application.redirectUri);

if (redirectUri.length === 0) {
Expand All @@ -57,23 +50,24 @@ export const updateOAuthApp = async (
});
}

await OAuthApps.updateOne(
{ _id: applicationId },
{
$set: {
name: application.name,
active: application.active,
redirectUri,
_updatedAt: new Date(),
_updatedBy: await Users.findOneById(userId, {
projection: {
username: 1,
},
}),
const updatedApplication = await OAuthApps.updateById(applicationId, {
name: application.name,
active: application.active,
redirectUri,
_updatedBy: await Users.findOneById(userId, {
projection: {
username: 1,
},
},
);
return OAuthApps.findOneById(applicationId);
}),
});

if (updatedApplication == null) {
throw new Meteor.Error('error-application-not-found', 'Application not found', {
method: 'updateOAuthApp',
});
}

return updatedApplication;
};

Meteor.methods<ServerMethods>({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ export const deleteCustomUserStatus = async (userId: string, userStatusID: strin
throw new Meteor.Error('not_authorized');
}

const userStatus = await CustomUserStatus.findOneById(userStatusID);
const userStatus = await CustomUserStatus.findOneAndDeleteById(userStatusID);
if (userStatus == null) {
throw new Meteor.Error('Custom_User_Status_Error_Invalid_User_Status', 'Invalid user status', { method: 'deleteCustomUserStatus' });
}

await CustomUserStatus.removeById(userStatusID);
void api.broadcast('user.deleteCustomStatus', userStatus);

return true;
Expand Down
10 changes: 2 additions & 8 deletions apps/meteor/server/lib/cas/findExistingCASUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ export const findExistingCASUser = async (username: string): Promise<IUser | und
// If that user was not found, check if there's any Rocket.Chat user with that username
// With this, CAS login will continue to work if the user is renamed on both sides and also if the user is renamed only on Rocket.Chat.
// It'll also allow non-CAS users to switch to CAS based login
// #TODO: Remove regex based search
const regex = new RegExp(`^${username}$`, 'i');
const user = await Users.findOne({ username: regex });
if (user) {
// Update the user's external_id to reflect this new username.
await Users.updateOne({ _id: user._id }, { $set: { 'services.cas.external_id': username } });
return user;
}
// Update the user's external_id to reflect this new username.
return (await Users.setCasExternalIdByUsername(username)) ?? undefined;
};
5 changes: 1 addition & 4 deletions apps/meteor/server/lib/cas/loginHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@ export const loginHandlerCAS = async (options: any): Promise<undefined | Account
return undefined;
}

// TODO: Sync wrapper due to the chain conversion to async models
const credentials = await CredentialTokens.findOneNotExpiredById(options.cas.credentialToken);
const credentials = await CredentialTokens.removeNotExpiredById(options.cas.credentialToken);
if (credentials === undefined || credentials === null) {
throw new Meteor.Error(Accounts.LoginCancelledError.numericError, 'no matching login attempt found');
}

await CredentialTokens.removeById(credentials._id);

const result = credentials.userInfo;
const syncUserDataFieldMap = settings.get<string>('CAS_Sync_User_Data_FieldMap').trim();
const casVersion = parseFloat(settings.get('CAS_version') ?? '1.0');
Expand Down
9 changes: 3 additions & 6 deletions apps/meteor/server/lib/oauth/updateOAuthServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,9 @@ export async function updateOAuthServices(): Promise<void> {
await LoginServiceConfiguration.createOrUpdateService(serviceKey, data);
void notifyOnLoginServiceConfigurationChangedByService(serviceKey);
} else {
const service = await LoginServiceConfiguration.findOneByService(serviceName, { projection: { _id: 1 } });
if (service?._id) {
const { deletedCount } = await LoginServiceConfiguration.removeService(service._id);
if (deletedCount > 0) {
void notifyOnLoginServiceConfigurationChanged({ _id: service._id }, 'removed');
}
const service = await LoginServiceConfiguration.removeByService(serviceName);
if (service) {
void notifyOnLoginServiceConfigurationChanged({ _id: service._id }, 'removed');
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions apps/meteor/server/services/room/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,11 @@ export class RoomService extends ServiceClassInternal implements IRoomService {
}

async revokeInvite(room: IRoom, user: IUser): Promise<void> {
const subscription = await Subscriptions.findOneByRoomIdAndUserId(room._id, user._id);
if (subscription?.status !== 'INVITED') {
const subscription = await Subscriptions.removeInvitedByRoomIdAndUserId(room._id, user._id);
if (!subscription) {
return;
}

await Subscriptions.removeById(subscription._id);
void notifyOnSubscriptionChanged(subscription, 'removed');
}

Expand Down
31 changes: 6 additions & 25 deletions apps/meteor/server/services/team/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -936,40 +936,21 @@ export class TeamService extends ServiceClassInternal implements ITeamService {
}

async addRolesToMember(teamId: string, userId: string, roles: Array<string>): Promise<boolean> {
const isMember = await TeamMember.findOneByUserIdAndTeamId(userId, teamId, {
projection: { _id: 1 },
});

if (!isMember) {
// TODO should this throw an error instead?
return false;
}
const { matchedCount } = await TeamMember.updateRolesByTeamIdAndUserId(teamId, userId, roles);

return !!(await TeamMember.updateRolesByTeamIdAndUserId(teamId, userId, roles));
return matchedCount > 0;
}

async addRolesToSubscription(roomId: string, userId: string, roles: Array<string>): Promise<boolean> {
const subscription = await Subscriptions.findOneByRoomIdAndUserId(roomId, userId);
const { matchedCount } = await Subscriptions.addRolesByUserId(userId, roles, roomId);

if (!subscription) {
// TODO should this throw an error instead?
return false;
}

return !!(await Subscriptions.addRolesByUserId(userId, roles, roomId));
return matchedCount > 0;
}

async removeRolesFromMember(teamId: string, userId: string, roles: Array<string>): Promise<boolean> {
const isMember = await TeamMember.findOneByUserIdAndTeamId(userId, teamId, {
projection: { _id: 1 },
});

if (!isMember) {
// TODO should this throw an error instead?
return false;
}
const { matchedCount } = await TeamMember.removeRolesByTeamIdAndUserId(teamId, userId, roles);

return !!(await TeamMember.removeRolesByTeamIdAndUserId(teamId, userId, roles));
return matchedCount > 0;
}

async getInfoByName(teamName: string): Promise<Omit<ITeam, 'usernames'> | null> {
Expand Down
4 changes: 4 additions & 0 deletions packages/core-typings/src/IOAuthApps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@ export interface IOAuthApps extends IRocketChatRecord {
_id: string;
username: string;
};
_updatedBy?: {
_id: string;
username?: string;
} | null;
appId?: string;
}
1 change: 1 addition & 0 deletions packages/model-typings/src/models/IBaseModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export interface IBaseModel<
updateFromUpdater(query: Filter<T>, updater: Updater<T>, options?: UpdateOptions): Promise<UpdateResult>;

findOneAndDelete(filter: Filter<T>, options?: FindOneAndDeleteOptions): Promise<null | WithId<T>>;
findOneAndDeleteById(_id: T['_id'], options?: FindOneAndDeleteOptions): Promise<null | WithId<T>>;
findOneAndUpdate(query: Filter<T>, update: UpdateFilter<T> | T, options?: FindOneAndUpdateOptions): Promise<null | WithId<T>>;

findOneById(_id: T['_id'], options?: FindOptions<T> | undefined): Promise<T | null>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ import type { IBaseModel } from './IBaseModel';
export interface ICredentialTokensModel extends IBaseModel<ICredentialToken> {
create(_id: string, userInfo: ICredentialToken['userInfo']): Promise<void>;
findOneNotExpiredById(_id: string): Promise<ICredentialToken | null>;
removeNotExpiredById(_id: string): Promise<ICredentialToken | null>;
}
1 change: 1 addition & 0 deletions packages/model-typings/src/models/IIntegrationsModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface IIntegrationsModel extends IBaseModel<IIntegration> {
findByChannels(channels: IIntegration['channel']): FindCursor<IIntegration>;
findByUserId(userId: IIntegration['userId']): FindCursor<Pick<IIntegration, '_id'>>;
findOneByIdAndCreatedByIfExists(params: { _id: IIntegration['_id']; createdBy?: IUser['_id'] }): Promise<IIntegration | null>;
removeByIdAndCreatedByIfExists(params: { _id: IIntegration['_id']; createdBy?: IUser['_id'] }): Promise<IIntegration | null>;
findOneByUrl(url: string): Promise<IIntegration | null>;
updateRoomName(oldRoomName: string, newRoomName: string): ReturnType<IBaseModel<IIntegration>['updateMany']>;
findOneByIdAndToken<P extends IIntegration = IIntegration>(
Expand Down
Loading
Loading