1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-12-01 04:43:29 +03:00

Serialise Olm prekey decryptions

If they overlap, they can both try to create new sessions, but this
removes the OTK so it will only work once.

Fixes https://github.com/vector-im/riot-web/issues/13229
This commit is contained in:
David Baker
2020-04-17 17:47:46 +01:00
parent 57233416d9
commit 83879fa945
2 changed files with 20 additions and 0 deletions

View File

@@ -105,6 +105,9 @@ export function OlmDevice(cryptoStore) {
// Keep track of sessions that we're starting, so that we don't start // Keep track of sessions that we're starting, so that we don't start
// multiple sessions for the same device at the same time. // multiple sessions for the same device at the same time.
this._sessionsInProgress = {}; this._sessionsInProgress = {};
// Used by olm to serialise prekey message decryptions
this._olmPrekeyPromise = Promise.resolve();
} }
/** /**

View File

@@ -264,6 +264,23 @@ OlmDecryption.prototype.decryptEvent = async function(event) {
*/ */
OlmDecryption.prototype._decryptMessage = async function( OlmDecryption.prototype._decryptMessage = async function(
theirDeviceIdentityKey, message, 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 {
this._olmDevice._olmPrekeyPromise = this._olmDevice._olmPrekeyPromise.then(() => {
return this._reallyDecryptMessage(theirDeviceIdentityKey, message);
});
return await this._olmDevice._olmPrekeyPromise;
}
};
OlmDecryption.prototype._reallyDecryptMessage = async function(
theirDeviceIdentityKey, message,
) { ) {
const sessionIds = await this._olmDevice.getSessionIdsForDevice( const sessionIds = await this._olmDevice.getSessionIdsForDevice(
theirDeviceIdentityKey, theirDeviceIdentityKey,