Skip to content

Commit

Permalink
Merge pull request #1326 from matrix-org/dbkr/serialise_prekey_decryp…
Browse files Browse the repository at this point in the history
…tions

Serialise Olm prekey decryptions
  • Loading branch information
dbkr authored Apr 17, 2020
2 parents 5723341 + 83b33d9 commit 1dcb163
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/crypto/OlmDevice.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ export function OlmDevice(cryptoStore) {
// Keep track of sessions that we're starting, so that we don't start
// multiple sessions for the same device at the same time.
this._sessionsInProgress = {};

// Used by olm to serialise prekey message decryptions
this._olmPrekeyPromise = Promise.resolve();
}

/**
Expand Down
19 changes: 19 additions & 0 deletions src/crypto/algorithms/olm.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,25 @@ OlmDecryption.prototype.decryptEvent = async function(event) {
*/
OlmDecryption.prototype._decryptMessage = async function(
theirDeviceIdentityKey, message,
) {
// This is a wrapper that serialises decryptions of prekey messages, because
// otherwise we race between deciding we have no active sessions for the message
// and creating a new one, which we can only do once because it removes the OTK.
if (message.type !== 0) {
// not a prekey message: we can safely just try & decrypt it
return this._reallyDecryptMessage(theirDeviceIdentityKey, message);
} else {
const myPromise = this._olmDevice._olmPrekeyPromise.then(() => {
return this._reallyDecryptMessage(theirDeviceIdentityKey, message);
});
// we want the error, but don't propagate it to the next decryption
this._olmDevice._olmPrekeyPromise = myPromise.catch(() => {});
return await myPromise;
}
};

OlmDecryption.prototype._reallyDecryptMessage = async function(
theirDeviceIdentityKey, message,
) {
const sessionIds = await this._olmDevice.getSessionIdsForDevice(
theirDeviceIdentityKey,
Expand Down

0 comments on commit 1dcb163

Please sign in to comment.