remove room from all user identities when leaving

and delete identity as well as all device identities if no rooms left
This commit is contained in:
Bruno Windels 2021-05-06 15:23:58 +02:00
parent a12f10dc3c
commit f16c08f13e
5 changed files with 56 additions and 13 deletions

View File

@ -121,26 +121,40 @@ export class DeviceTracker {
} }
} }
async _applyMemberChange(memberChange, txn) { async _removeRoomFromUserIdentity(roomId, userId, txn) {
// TODO: depends whether we encrypt for invited users?? const {userIdentities, deviceIdentities} = txn;
// add room const identity = await userIdentities.get(userId);
if (memberChange.previousMembership !== "join" && memberChange.membership === "join") {
await this._writeMember(memberChange.member, txn);
}
// remove room
else if (memberChange.previousMembership === "join" && memberChange.membership !== "join") {
const {userIdentities} = txn;
const identity = await userIdentities.get(memberChange.userId);
if (identity) { if (identity) {
identity.roomIds = identity.roomIds.filter(roomId => roomId !== memberChange.roomId); identity.roomIds = identity.roomIds.filter(id => id !== roomId);
// no more encrypted rooms with this user, remove // no more encrypted rooms with this user, remove
if (identity.roomIds.length === 0) { if (identity.roomIds.length === 0) {
userIdentities.remove(identity.userId); userIdentities.remove(userId);
deviceIdentities.removeAllForUser(userId);
} else { } else {
userIdentities.set(identity); userIdentities.set(identity);
} }
} }
} }
async _applyMemberChange(memberChange, txn) {
// TODO: depends whether we encrypt for invited users??
// add room
if (memberChange.hasJoined) {
await this._writeMember(memberChange.member, txn);
}
// remove room
else if (memberChange.hasLeft) {
const {roomId} = memberChange;
// if we left the room, remove room from all user identities in the room
if (memberChange.userId === this._ownUserId) {
const userIds = await txn.roomMembers.getAllUserIds(roomId);
await Promise.all(userIds.map(userId => {
return this._removeRoomFromUserIdentity(roomId, userId, txn);
}));
} else {
await this._removeRoomFromUserIdentity(roomId, memberChange.userId, txn);
}
}
} }
async _queryKeys(userIds, hsApi, log) { async _queryKeys(userIds, hsApi, log) {

View File

@ -85,6 +85,7 @@ export class RoomEncryption {
async writeMemberChanges(memberChanges, txn, log) { async writeMemberChanges(memberChanges, txn, log) {
let shouldFlush = false; let shouldFlush = false;
const memberChangesArray = Array.from(memberChanges.values()); const memberChangesArray = Array.from(memberChanges.values());
// this also clears our session if we leave the room ourselves
if (memberChangesArray.some(m => m.hasLeft)) { if (memberChangesArray.some(m => m.hasLeft)) {
log.log({ log.log({
l: "discardOutboundSession", l: "discardOutboundSession",

View File

@ -14,6 +14,8 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import {MAX_UNICODE, MIN_UNICODE} from "./common.js";
function encodeKey(userId, deviceId) { function encodeKey(userId, deviceId) {
return `${userId}|${deviceId}`; return `${userId}|${deviceId}`;
} }
@ -66,4 +68,11 @@ export class DeviceIdentityStore {
remove(userId, deviceId) { remove(userId, deviceId) {
this._store.delete(encodeKey(userId, deviceId)); this._store.delete(encodeKey(userId, deviceId));
} }
removeAllForUser(userId) {
// exclude both keys as they are theoretical min and max,
// but we should't have a match for just the room id, or room id with max
const range = IDBKeyRange.bound(encodeKey(userId, MIN_UNICODE), encodeKey(userId, MAX_UNICODE), true, true);
this._store.delete(range);
}
} }

View File

@ -1,5 +1,6 @@
/* /*
Copyright 2020 Bruno Windels <bruno@windels.cloud> Copyright 2020 Bruno Windels <bruno@windels.cloud>
Copyright 2021 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -14,7 +15,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
const MAX_UNICODE = "\u{10FFFF}"; import {MAX_UNICODE} from "./common.js";
export class RoomStateStore { export class RoomStateStore {
constructor(idbStore) { constructor(idbStore) {

View File

@ -0,0 +1,18 @@
/*
Copyright 2021 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
export const MIN_UNICODE = "\u{0}";
export const MAX_UNICODE = "\u{10FFFF}";