move some validation of into session backup

This commit is contained in:
Bruno Windels 2022-01-25 18:48:03 +01:00
parent 8f4e3c62ce
commit ffece4f357

View file

@ -182,35 +182,30 @@ export class RoomEncryption {
log.set("id", sessionId); log.set("id", sessionId);
log.set("senderKey", senderKey); log.set("senderKey", senderKey);
try { try {
const session = await this._sessionBackup.getSession(this._room.id, sessionId, log); const roomKey = await this._sessionBackup.getRoomKey(this._room.id, sessionId, log);
if (session?.algorithm === MEGOLM_ALGORITHM) { if (roomKey) {
let roomKey = this._megolmDecryption.roomKeyFromBackup(this._room.id, sessionId, session); if (roomKey.senderKey !== senderKey) {
if (roomKey) { log.set("wrong_sender_key", roomKey.senderKey);
if (roomKey.senderKey !== senderKey) { log.logLevel = log.level.Warn;
log.set("wrong_sender_key", roomKey.senderKey); return;
log.logLevel = log.level.Warn; }
return; let keyIsBestOne = false;
} let retryEventIds;
let keyIsBestOne = false; const txn = await this._storage.readWriteTxn([this._storage.storeNames.inboundGroupSessions]);
let retryEventIds; try {
const txn = await this._storage.readWriteTxn([this._storage.storeNames.inboundGroupSessions]); keyIsBestOne = await this._megolmDecryption.writeRoomKey(roomKey, txn);
try { log.set("isBetter", keyIsBestOne);
keyIsBestOne = await this._megolmDecryption.writeRoomKey(roomKey, txn); if (keyIsBestOne) {
log.set("isBetter", keyIsBestOne); retryEventIds = roomKey.eventIds;
if (keyIsBestOne) { }
retryEventIds = roomKey.eventIds; } catch (err) {
} txn.abort();
} catch (err) { throw err;
txn.abort(); }
throw err; await txn.complete();
} if (keyIsBestOne) {
await txn.complete(); await log.wrap("retryDecryption", log => this._room.notifyRoomKey(roomKey, retryEventIds || [], log));
if (keyIsBestOne) {
await log.wrap("retryDecryption", log => this._room.notifyRoomKey(roomKey, retryEventIds || [], log));
}
} }
} else if (session?.algorithm) {
log.set("unknown algorithm", session.algorithm);
} }
} catch (err) { } catch (err) {
if (!(err.name === "HomeServerError" && err.errcode === "M_NOT_FOUND")) { if (!(err.name === "HomeServerError" && err.errcode === "M_NOT_FOUND")) {