Skip to content

Commit

Permalink
Merge pull request #275 from vector-im/bwindels/retryeventidsforkeyba…
Browse files Browse the repository at this point in the history
…ckup

Extract retry event ids for key before overwriting key on key backup
  • Loading branch information
bwindels authored Mar 15, 2021
2 parents ebca393 + 4b62e0a commit a9027e1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
12 changes: 7 additions & 5 deletions src/matrix/e2ee/RoomEncryption.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,14 @@ export class RoomEncryption {
}
let roomKey = this._megolmDecryption.roomKeyFromBackup(this._room.id, sessionId, session);
if (roomKey) {
let keyIsBestOne = false;
let retryEventIds;
try {
const txn = await this._storage.readWriteTxn([this._storage.storeNames.inboundGroupSessions]);
try {
keyIsBestOne = await this._megolmDecryption.writeRoomKey(roomKey, txn);
const keyIsBestOne = await this._megolmDecryption.writeRoomKey(roomKey, txn);
if (keyIsBestOne) {
retryEventIds = roomKey.eventIds;
}
} catch (err) {
txn.abort();
throw err;
Expand All @@ -225,9 +228,8 @@ export class RoomEncryption {
// this is just clearing the internal sessionInfo
roomKey.dispose();
}
if (keyIsBestOne) {
// wrote the key, meaning we didn't have a better one, go ahead and reattempt decryption
await this._room.notifyRoomKey(roomKey);
if (retryEventIds?.length) {
await this._room.retryDecryption(retryEventIds);
}
}
} else if (session?.algorithm) {
Expand Down
10 changes: 10 additions & 0 deletions src/matrix/e2ee/megolm/decryption/RoomKey.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export class BaseRoomKey {
constructor() {
this._sessionInfo = null;
this._isBetter = null;
this._eventIds = null;
}

async createSessionInfo(olm, pickleKey, txn) {
Expand Down Expand Up @@ -44,6 +45,11 @@ export class BaseRoomKey {
existingSession.free();
}
}
// store the event ids that can be decrypted with this key
// before we overwrite them if called from `write`.
if (existingSessionEntry?.eventIds) {
this._eventIds = existingSessionEntry.eventIds;
}
return isBetter;
}

Expand Down Expand Up @@ -71,6 +77,10 @@ export class BaseRoomKey {
return false;
}

get eventIds() {
return this._eventIds;
}

dispose() {
if (this._sessionInfo) {
this._sessionInfo.release();
Expand Down
32 changes: 17 additions & 15 deletions src/matrix/room/Room.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,35 +55,32 @@ export class Room extends EventEmitter {
this._observedEvents = null;
}

async _getRetryDecryptEntriesForKey(roomKey, roomEncryption, txn) {
const retryEventIds = await roomEncryption.getEventIdsForMissingKey(roomKey, txn);
async _eventIdsToEntries(eventIds, txn) {
const retryEntries = [];
if (retryEventIds) {
for (const eventId of retryEventIds) {
const storageEntry = await txn.timelineEvents.getByEventId(this._roomId, eventId);
if (storageEntry) {
retryEntries.push(new EventEntry(storageEntry, this._fragmentIdComparer));
}
await Promise.all(eventIds.map(async eventId => {
const storageEntry = await txn.timelineEvents.getByEventId(this._roomId, eventId);
if (storageEntry) {
retryEntries.push(new EventEntry(storageEntry, this._fragmentIdComparer));
}
}
}));
return retryEntries;
}

/**
* Used for keys received from other sources than sync, like key backup.
* Used for retrying decryption from other sources than sync, like key backup.
* @internal
* @param {RoomKey} roomKey
* @param {Array<string>} eventIds
* @return {Promise}
*/
async notifyRoomKey(roomKey) {
async retryDecryption(eventIds) {
if (!this._roomEncryption) {
return;
}
const txn = await this._storage.readTxn([
this._storage.storeNames.timelineEvents,
this._storage.storeNames.inboundGroupSessions,
]);
const retryEntries = await this._getRetryDecryptEntriesForKey(roomKey, this._roomEncryption, txn);
const retryEntries = await this._eventIdsToEntries(eventIds, txn);
if (retryEntries.length) {
const decryptRequest = this._decryptEntries(DecryptionSource.Retry, retryEntries, txn);
// this will close txn while awaiting decryption
Expand Down Expand Up @@ -157,8 +154,13 @@ export class Room extends EventEmitter {
}

async _getSyncRetryDecryptEntries(newKeys, roomEncryption, txn) {
const entriesPerKey = await Promise.all(newKeys.map(key => this._getRetryDecryptEntriesForKey(key, roomEncryption, txn)));
let retryEntries = entriesPerKey.reduce((allEntries, entries) => allEntries.concat(entries), []);
const entriesPerKey = await Promise.all(newKeys.map(async key => {
const retryEventIds = await roomEncryption.getEventIdsForMissingKey(key, txn);
if (retryEventIds) {
return this._eventIdsToEntries(retryEventIds, txn);
}
}));
let retryEntries = entriesPerKey.reduce((allEntries, entries) => entries ? allEntries.concat(entries) : allEntries, []);
// If we have the timeline open, see if there are more entries for the new keys
// as we only store missing session information for synced events, not backfilled.
// We want to decrypt all events we can though if the user is looking
Expand Down

0 comments on commit a9027e1

Please sign in to comment.