create unknown userIdentity when processing /keys/query response

this can happen when the room isn't tracked yet, which is a use case
we add support for in the next commit to verify senders that we don't
know about yet (e.g. when the room isn't tracked).
This commit is contained in:
Bruno Windels 2022-11-10 17:28:18 +01:00
parent 31579b4945
commit c78bed846e

View File

@ -21,13 +21,17 @@ import {RoomMember} from "../room/members/RoomMember.js";
const TRACKING_STATUS_OUTDATED = 0; const TRACKING_STATUS_OUTDATED = 0;
const TRACKING_STATUS_UPTODATE = 1; const TRACKING_STATUS_UPTODATE = 1;
function addRoomToIdentity(identity, userId, roomId) { function createUserIdentity(userId, initialRoomId = undefined) {
if (!identity) { return {
identity = {
userId: userId, userId: userId,
roomIds: [roomId], roomIds: initialRoomId ? [initialRoomId] : [],
deviceTrackingStatus: TRACKING_STATUS_OUTDATED, deviceTrackingStatus: TRACKING_STATUS_OUTDATED,
}; };
}
function addRoomToIdentity(identity, userId, roomId) {
if (!identity) {
identity = createUserIdentity(userId, roomId);
return identity; return identity;
} else { } else {
if (!identity.roomIds.includes(roomId)) { if (!identity.roomIds.includes(roomId)) {
@ -272,7 +276,15 @@ export class DeviceTracker {
txn.deviceIdentities.set(deviceIdentity); txn.deviceIdentities.set(deviceIdentity);
} }
// mark user identities as up to date // mark user identities as up to date
const identity = await txn.userIdentities.get(userId); let identity = await txn.userIdentities.get(userId);
if (!identity) {
// create the identity if it doesn't exist, which can happen if
// we request devices before tracking the room.
// IMPORTANT here that the identity gets created without any roomId!
// if we claim that we share and e2ee room with the user without having
// checked, we could share keys with that user without them being in the room
identity = createUserIdentity(userId);
}
identity.deviceTrackingStatus = TRACKING_STATUS_UPTODATE; identity.deviceTrackingStatus = TRACKING_STATUS_UPTODATE;
txn.userIdentities.set(identity); txn.userIdentities.set(identity);