Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 2s
124 lines
3.8 KiB
TypeScript
124 lines
3.8 KiB
TypeScript
import type { ISubscription, IUser, IRoom } from '@rocket.chat/core-typings';
|
|
import { Rooms, Subscriptions, Users } from '@rocket.chat/models';
|
|
import type { AnyBulkWriteOperation } from 'mongodb';
|
|
|
|
import { notifyOnRoomChanged, notifyOnSubscriptionChanged } from '../../notifyListener';
|
|
|
|
export async function resetRoomKey(roomId: string, userId: string, newRoomKey: string, newRoomKeyId: string) {
|
|
const user = await Users.findOneById<Pick<IUser, 'e2e'>>(userId, { projection: { e2e: 1 } });
|
|
if (!user) {
|
|
throw new Error('error-user-not-found');
|
|
}
|
|
|
|
if (!user.e2e?.private_key || !user.e2e?.public_key) {
|
|
throw new Error('error-user-has-no-keys');
|
|
}
|
|
|
|
const room = await Rooms.findOneById<Pick<IRoom, 'e2eKeyId'>>(roomId, { projection: { e2eKeyId: 1 } });
|
|
if (!room) {
|
|
throw new Error('error-room-not-found');
|
|
}
|
|
|
|
if (!room.e2eKeyId) {
|
|
throw new Error('error-room-not-encrypted');
|
|
}
|
|
|
|
// We will update the subs of everyone who has a key for the room. For the ones that don't have, we will do nothing
|
|
const notifySubs = [];
|
|
const updateOps: AnyBulkWriteOperation<ISubscription>[] = [];
|
|
const e2eQueue: IRoom['usersWaitingForE2EKeys'] = [];
|
|
|
|
for await (const sub of Subscriptions.find({
|
|
rid: roomId,
|
|
$or: [{ E2EKey: { $exists: true } }, { E2ESuggestedKey: { $exists: true } }],
|
|
})) {
|
|
// This replicates the oldRoomKeys array modifications allowing us to have the changes locally without finding them again
|
|
// which allows for quicker notifying
|
|
const keys = replicateMongoSlice(room.e2eKeyId, sub);
|
|
delete sub.E2ESuggestedKey;
|
|
delete sub.E2EKey;
|
|
delete sub.suggestedOldRoomKeys;
|
|
|
|
const updateSet = {
|
|
$set: {
|
|
...(keys && { oldRoomKeys: keys }),
|
|
},
|
|
};
|
|
updateOps.push({
|
|
updateOne: {
|
|
filter: { _id: sub._id },
|
|
update: {
|
|
$unset: { E2EKey: 1, E2ESuggestedKey: 1, suggestedOldRoomKeys: 1 },
|
|
...(Object.keys(updateSet.$set).length && updateSet),
|
|
},
|
|
},
|
|
});
|
|
|
|
if (userId !== sub.u._id) {
|
|
// Avoid notifying requesting user as notify will happen at the end
|
|
notifySubs.push({
|
|
...sub,
|
|
...(keys && { oldRoomKeys: keys }),
|
|
});
|
|
|
|
// This is for allowing the key distribution process to start inmediately
|
|
pushToLimit(e2eQueue, { userId: sub.u._id, ts: new Date() });
|
|
}
|
|
|
|
if (updateOps.length >= 100) {
|
|
await writeAndNotify(updateOps, notifySubs);
|
|
}
|
|
}
|
|
|
|
if (updateOps.length > 0) {
|
|
await writeAndNotify(updateOps, notifySubs);
|
|
}
|
|
|
|
// after the old keys have been moved to the new prop, store room key on room + the e2e queue so key can be exchanged
|
|
// todo move to model method
|
|
const roomResult = await Rooms.resetRoomKeyAndSetE2EEQueueByRoomId(roomId, newRoomKeyId, e2eQueue);
|
|
// And set the new key to the user that called the func
|
|
const result = await Subscriptions.setE2EKeyByUserIdAndRoomId(userId, roomId, newRoomKey);
|
|
|
|
if (result) {
|
|
void notifyOnSubscriptionChanged(result);
|
|
}
|
|
if (roomResult) {
|
|
void notifyOnRoomChanged(roomResult);
|
|
}
|
|
}
|
|
|
|
export function pushToLimit(
|
|
arr: NonNullable<IRoom['usersWaitingForE2EKeys']>,
|
|
item: NonNullable<IRoom['usersWaitingForE2EKeys']>[number],
|
|
limit = 50,
|
|
) {
|
|
if (arr.length < limit) {
|
|
arr.push(item);
|
|
}
|
|
}
|
|
|
|
async function writeAndNotify(updateOps: AnyBulkWriteOperation<ISubscription>[], notifySubs: ISubscription[]) {
|
|
await Subscriptions.col.bulkWrite([...updateOps]);
|
|
notifySubs.forEach((sub) => {
|
|
void notifyOnSubscriptionChanged(sub);
|
|
});
|
|
notifySubs.length = 0;
|
|
updateOps.length = 0;
|
|
}
|
|
|
|
export function replicateMongoSlice(keyId: string, sub: ISubscription) {
|
|
if (!sub.E2EKey) {
|
|
return;
|
|
}
|
|
|
|
if (!sub.oldRoomKeys) {
|
|
return [{ e2eKeyId: keyId, ts: new Date(), E2EKey: sub.E2EKey }];
|
|
}
|
|
|
|
const sortedKeys = sub.oldRoomKeys.toSorted((a, b) => b.ts.getTime() - a.ts.getTime());
|
|
sortedKeys.unshift({ e2eKeyId: keyId, ts: new Date(), E2EKey: sub.E2EKey });
|
|
|
|
return sortedKeys.slice(0, 10);
|
|
}
|