From 28540ad50a8fd93db22aea585ca3441071e6c7f5 Mon Sep 17 00:00:00 2001 From: David Baker Date: Thu, 1 Nov 2018 12:59:38 +0000 Subject: [PATCH 1/9] Use the last olm session that got a message Implements https://github.com/matrix-org/matrix-doc/pull/1596 For https://github.com/vector-im/riot-web/issues/3822 Requires https://github.com/matrix-org/olm-backup/pull/77 (+release) --- src/crypto/OlmDevice.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/crypto/OlmDevice.js b/src/crypto/OlmDevice.js index 5ce7438a708..851cca18596 100644 --- a/src/crypto/OlmDevice.js +++ b/src/crypto/OlmDevice.js @@ -558,13 +558,20 @@ OlmDevice.prototype.getSessionIdsForDevice = async function(theirDeviceIdentityK * @return {Promise} session id, or null if no established session */ OlmDevice.prototype.getSessionIdForDevice = async function(theirDeviceIdentityKey) { - const sessionIds = await this.getSessionIdsForDevice(theirDeviceIdentityKey); - if (sessionIds.length === 0) { + const sessionInfos = await this.getSessionInfoForDevice(theirDeviceIdentityKey); + if (sessionInfos.length === 0) { return null; } - // Use the session with the lowest ID. - sessionIds.sort(); - return sessionIds[0]; + // Use the session that has most recently received a message + sessionInfos.sort((a, b) => { + if (a.lastReceivedMessageTs !== b.lastReceivedMessageTs) { + return a.lastReceivedMessageTs - b.lastReceivedMessageTs; + } else { + if (a.sessionId === b.sessionId) return 0; + return a.sessionId < b.sessionId ? -1 : 1; + } + }); + return sessionInfos[sessionInfos.length - 1].sessionId; }; /** @@ -589,6 +596,7 @@ OlmDevice.prototype.getSessionInfoForDevice = async function(deviceIdentityKey) for (const sessionId of sessionIds) { this._unpickleSession(sessions[sessionId], (session) => { info.push({ + lastReceivedMessageTs: session.last_received_message_ts(), hasReceivedMessage: session.has_received_message(), sessionId: sessionId, }); @@ -649,6 +657,7 @@ OlmDevice.prototype.decryptMessage = async function( (txn) => { this._getSession(theirDeviceIdentityKey, sessionId, txn, (session) => { payloadString = session.decrypt(messageType, ciphertext); + session.set_last_received_message_ts(Date.now()); this._saveSession(theirDeviceIdentityKey, session, txn); }); }, From 37f0a9ad7b2602918d8d914faadd6bdfcea1f164 Mon Sep 17 00:00:00 2001 From: David Baker Date: Thu, 1 Nov 2018 13:54:41 +0000 Subject: [PATCH 2/9] Try tests on node 10 --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 67382e7601e..823517fc5aa 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,5 @@ language: node_js node_js: - - node # Latest stable version of nodejs. + - "10.11.0" script: - ./travis.sh From 2a6a67c6cc849644a1c03e590ba5dec0151afca1 Mon Sep 17 00:00:00 2001 From: David Baker Date: Thu, 1 Nov 2018 17:08:43 +0000 Subject: [PATCH 3/9] Inbound session creation counts as a received message --- src/crypto/OlmDevice.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/crypto/OlmDevice.js b/src/crypto/OlmDevice.js index 851cca18596..46f879ec2c3 100644 --- a/src/crypto/OlmDevice.js +++ b/src/crypto/OlmDevice.js @@ -509,6 +509,8 @@ OlmDevice.prototype.createInboundSession = async function( this._storeAccount(txn, account); const payloadString = session.decrypt(messageType, ciphertext); + // this counts as an received message + session.set_last_received_message_ts(Date.now()); this._saveSession(theirDeviceIdentityKey, session, txn); From fcadf6ec4a052ba9f1a05fd26f23cadedb056b0c Mon Sep 17 00:00:00 2001 From: David Baker Date: Mon, 12 Nov 2018 18:10:11 +0000 Subject: [PATCH 4/9] Store last received message ts on olm session --- src/crypto/OlmDevice.js | 69 ++++++++++++------- .../store/indexeddb-crypto-store-backend.js | 19 +++-- src/crypto/store/indexeddb-crypto-store.js | 16 +++-- src/crypto/store/localStorage-crypto-store.js | 20 +++++- src/crypto/store/memory-crypto-store.js | 4 +- 5 files changed, 88 insertions(+), 40 deletions(-) diff --git a/src/crypto/OlmDevice.js b/src/crypto/OlmDevice.js index 46f879ec2c3..2c195fef3f0 100644 --- a/src/crypto/OlmDevice.js +++ b/src/crypto/OlmDevice.js @@ -295,8 +295,8 @@ OlmDevice.prototype._storeAccount = function(txn, account) { */ OlmDevice.prototype._getSession = function(deviceKey, sessionId, txn, func) { this._cryptoStore.getEndToEndSession( - deviceKey, sessionId, txn, (pickledSession) => { - this._unpickleSession(pickledSession, func); + deviceKey, sessionId, txn, (sessionInfo) => { + this._unpickleSession(sessionInfo, func); }, ); }; @@ -306,15 +306,17 @@ OlmDevice.prototype._getSession = function(deviceKey, sessionId, txn, func) { * function with it. The session object is destroyed once the function * returns. * - * @param {string} pickledSession + * @param {object} sessionInfo * @param {function} func * @private */ -OlmDevice.prototype._unpickleSession = function(pickledSession, func) { +OlmDevice.prototype._unpickleSession = function(sessionInfo, func) { const session = new global.Olm.Session(); try { - session.unpickle(this._pickleKey, pickledSession); - func(session); + session.unpickle(this._pickleKey, sessionInfo.session); + const unpickledSessInfo = Object.assign({}, sessionInfo, {session}); + + func(unpickledSessInfo); } finally { session.free(); } @@ -324,14 +326,17 @@ OlmDevice.prototype._unpickleSession = function(pickledSession, func) { * store our OlmSession in the session store * * @param {string} deviceKey - * @param {OlmSession} session + * @param {object} sessionInfo {session: OlmSession, lastReceivedMessageTs: int} * @param {*} txn Opaque transaction object from cryptoStore.doTxn() * @private */ -OlmDevice.prototype._saveSession = function(deviceKey, session, txn) { - const pickledSession = session.pickle(this._pickleKey); +OlmDevice.prototype._saveSession = function(deviceKey, sessionInfo, txn) { + const sessionId = sessionInfo.session.session_id(); + const pickledSessionInfo = Object.assign(sessionInfo, { + session: sessionInfo.session.pickle(this._pickleKey), + }); this._cryptoStore.storeEndToEndSession( - deviceKey, session.session_id(), pickledSession, txn, + deviceKey, sessionId, pickledSessionInfo, txn, ); }; @@ -461,7 +466,15 @@ OlmDevice.prototype.createOutboundSession = async function( session.create_outbound(account, theirIdentityKey, theirOneTimeKey); newSessionId = session.session_id(); this._storeAccount(txn, account); - this._saveSession(theirIdentityKey, session, txn); + const sessionInfo = { + session, + // Pretend we've received a message at this point, otherwise + // if we try to send a message to the device, it won't use + // this session (storing the creation time separately would + // make the pickle longer and would not be useful otherwise). + lastReceivedMessageTs: Date.now(), + }; + this._saveSession(theirIdentityKey, sessionInfo, txn); } finally { session.free(); } @@ -509,10 +522,14 @@ OlmDevice.prototype.createInboundSession = async function( this._storeAccount(txn, account); const payloadString = session.decrypt(messageType, ciphertext); - // this counts as an received message - session.set_last_received_message_ts(Date.now()); - this._saveSession(theirDeviceIdentityKey, session, txn); + const sessionInfo = { + session, + // this counts as an received message: set last received message time + // to now + lastReceivedMessageTs: Date.now(), + }; + this._saveSession(theirDeviceIdentityKey, sessionInfo, txn); result = { payload: payloadString, @@ -596,10 +613,10 @@ OlmDevice.prototype.getSessionInfoForDevice = async function(deviceIdentityKey) this._cryptoStore.getEndToEndSessions(deviceIdentityKey, txn, (sessions) => { const sessionIds = Object.keys(sessions).sort(); for (const sessionId of sessionIds) { - this._unpickleSession(sessions[sessionId], (session) => { + this._unpickleSession(sessions[sessionId], (sessInfo) => { info.push({ - lastReceivedMessageTs: session.last_received_message_ts(), - hasReceivedMessage: session.has_received_message(), + lastReceivedMessageTs: sessInfo.lastReceivedMessageTs, + hasReceivedMessage: sessInfo.session.has_received_message(), sessionId: sessionId, }); }); @@ -630,9 +647,9 @@ OlmDevice.prototype.encryptMessage = async function( await this._cryptoStore.doTxn( 'readwrite', [IndexedDBCryptoStore.STORE_SESSIONS], (txn) => { - this._getSession(theirDeviceIdentityKey, sessionId, txn, (session) => { - res = session.encrypt(payloadString); - this._saveSession(theirDeviceIdentityKey, session, txn); + this._getSession(theirDeviceIdentityKey, sessionId, txn, (sessionInfo) => { + res = sessionInfo.session.encrypt(payloadString); + this._saveSession(theirDeviceIdentityKey, sessionInfo, txn); }); }, ); @@ -657,10 +674,10 @@ OlmDevice.prototype.decryptMessage = async function( await this._cryptoStore.doTxn( 'readwrite', [IndexedDBCryptoStore.STORE_SESSIONS], (txn) => { - this._getSession(theirDeviceIdentityKey, sessionId, txn, (session) => { - payloadString = session.decrypt(messageType, ciphertext); - session.set_last_received_message_ts(Date.now()); - this._saveSession(theirDeviceIdentityKey, session, txn); + this._getSession(theirDeviceIdentityKey, sessionId, txn, (sessionInfo) => { + payloadString = sessionInfo.session.decrypt(messageType, ciphertext); + sessionInfo.lastReceivedMessageTs = Date.now(); + this._saveSession(theirDeviceIdentityKey, sessionInfo, txn); }); }, ); @@ -690,8 +707,8 @@ OlmDevice.prototype.matchesSession = async function( await this._cryptoStore.doTxn( 'readonly', [IndexedDBCryptoStore.STORE_SESSIONS], (txn) => { - this._getSession(theirDeviceIdentityKey, sessionId, txn, (session) => { - matches = session.matches_inbound(ciphertext); + this._getSession(theirDeviceIdentityKey, sessionId, txn, (sessionInfo) => { + matches = sessionInfo.session.matches_inbound(ciphertext); }); }, ); diff --git a/src/crypto/store/indexeddb-crypto-store-backend.js b/src/crypto/store/indexeddb-crypto-store-backend.js index 96bbec68ec8..ba15e6673f0 100644 --- a/src/crypto/store/indexeddb-crypto-store-backend.js +++ b/src/crypto/store/indexeddb-crypto-store-backend.js @@ -314,7 +314,10 @@ export class Backend { getReq.onsuccess = function() { const cursor = getReq.result; if (cursor) { - results[cursor.value.sessionId] = cursor.value.session; + results[cursor.value.sessionId] = { + session: cursor.value.session, + lastReceivedMessagets: cursor.value.lastReceivedMessageTs, + }; cursor.continue(); } else { try { @@ -332,7 +335,10 @@ export class Backend { getReq.onsuccess = function() { try { if (getReq.result) { - func(getReq.result.session); + func({ + session: getReq.result.session, + lastReceivedMessagets: getReq.result.lastReceivedMessageTs, + }); } else { func(null); } @@ -342,9 +348,14 @@ export class Backend { }; } - storeEndToEndSession(deviceKey, sessionId, session, txn) { + storeEndToEndSession(deviceKey, sessionId, sessionInfo, txn) { const objectStore = txn.objectStore("sessions"); - objectStore.put({deviceKey, sessionId, session}); + objectStore.put({ + deviceKey, + sessionId, + session: sessionInfo.session, + lastReceivedMessageTs: sessionInfo.lastReceivedMessageTs, + }); } // Inbound group sessions diff --git a/src/crypto/store/indexeddb-crypto-store.js b/src/crypto/store/indexeddb-crypto-store.js index c9210da2307..a4d7b48c028 100644 --- a/src/crypto/store/indexeddb-crypto-store.js +++ b/src/crypto/store/indexeddb-crypto-store.js @@ -284,7 +284,10 @@ export default class IndexedDBCryptoStore { * @param {string} sessionId The ID of the session to retrieve * @param {*} txn An active transaction. See doTxn(). * @param {function(object)} func Called with A map from sessionId - * to Base64 end-to-end session. + * to session information object with 'session' key being the + * Base64 end-to-end session and lastReceivedMessageTs being the + * timestamp in milliseconds at which the session last received + * a message. */ getEndToEndSession(deviceKey, sessionId, txn, func) { this._backendPromise.value().getEndToEndSession(deviceKey, sessionId, txn, func); @@ -296,7 +299,10 @@ export default class IndexedDBCryptoStore { * @param {string} deviceKey The public key of the other device. * @param {*} txn An active transaction. See doTxn(). * @param {function(object)} func Called with A map from sessionId - * to Base64 end-to-end session. + * to session information object with 'session' key being the + * Base64 end-to-end session and lastReceivedMessageTs being the + * timestamp in milliseconds at which the session last received + * a message. */ getEndToEndSessions(deviceKey, txn, func) { this._backendPromise.value().getEndToEndSessions(deviceKey, txn, func); @@ -306,12 +312,12 @@ export default class IndexedDBCryptoStore { * Store a session between the logged-in user and another device * @param {string} deviceKey The public key of the other device. * @param {string} sessionId The ID for this end-to-end session. - * @param {string} session Base64 encoded end-to-end session. + * @param {string} sessionInfo Session information object * @param {*} txn An active transaction. See doTxn(). */ - storeEndToEndSession(deviceKey, sessionId, session, txn) { + storeEndToEndSession(deviceKey, sessionId, sessionInfo, txn) { this._backendPromise.value().storeEndToEndSession( - deviceKey, sessionId, session, txn, + deviceKey, sessionId, sessionInfo, txn, ); } diff --git a/src/crypto/store/localStorage-crypto-store.js b/src/crypto/store/localStorage-crypto-store.js index ed0b7ede45e..a47861b53df 100644 --- a/src/crypto/store/localStorage-crypto-store.js +++ b/src/crypto/store/localStorage-crypto-store.js @@ -67,7 +67,21 @@ export default class LocalStorageCryptoStore extends MemoryCryptoStore { } _getEndToEndSessions(deviceKey, txn, func) { - return getJsonItem(this.store, keyEndToEndSessions(deviceKey)); + const sessions = getJsonItem(this.store, keyEndToEndSessions(deviceKey)); + const fixedSessions = {}; + + // fix up any old sessions to be objects rather than just the base64 pickle + for (const [sid, val] of Object.entries(sessions || {})) { + if (typeof val === 'string') { + fixedSessions[sid] = { + session: val, + }; + } else { + fixedSessions[sid] = val; + } + } + + return fixedSessions; } getEndToEndSession(deviceKey, sessionId, txn, func) { @@ -79,9 +93,9 @@ export default class LocalStorageCryptoStore extends MemoryCryptoStore { func(this._getEndToEndSessions(deviceKey) || {}); } - storeEndToEndSession(deviceKey, sessionId, session, txn) { + storeEndToEndSession(deviceKey, sessionId, sessionInfo, txn) { const sessions = this._getEndToEndSessions(deviceKey) || {}; - sessions[sessionId] = session; + sessions[sessionId] = sessionInfo; setJsonItem( this.store, keyEndToEndSessions(deviceKey), sessions, ); diff --git a/src/crypto/store/memory-crypto-store.js b/src/crypto/store/memory-crypto-store.js index 4c2baf9e1be..8c8543c18dd 100644 --- a/src/crypto/store/memory-crypto-store.js +++ b/src/crypto/store/memory-crypto-store.js @@ -234,13 +234,13 @@ export default class MemoryCryptoStore { func(this._sessions[deviceKey] || {}); } - storeEndToEndSession(deviceKey, sessionId, session, txn) { + storeEndToEndSession(deviceKey, sessionId, sessionInfo, txn) { let deviceSessions = this._sessions[deviceKey]; if (deviceSessions === undefined) { deviceSessions = {}; this._sessions[deviceKey] = deviceSessions; } - deviceSessions[sessionId] = session; + deviceSessions[sessionId] = sessionInfo; } // Inbound Group Sessions From e17a39d446e0acd389e49110d94044ced45da88d Mon Sep 17 00:00:00 2001 From: David Baker Date: Tue, 13 Nov 2018 12:10:26 +0000 Subject: [PATCH 5/9] PR feedback --- src/crypto/OlmDevice.js | 24 ++++++++++--------- .../store/indexeddb-crypto-store-backend.js | 4 ++-- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/crypto/OlmDevice.js b/src/crypto/OlmDevice.js index 2c195fef3f0..c87ef9525a6 100644 --- a/src/crypto/OlmDevice.js +++ b/src/crypto/OlmDevice.js @@ -470,8 +470,7 @@ OlmDevice.prototype.createOutboundSession = async function( session, // Pretend we've received a message at this point, otherwise // if we try to send a message to the device, it won't use - // this session (storing the creation time separately would - // make the pickle longer and would not be useful otherwise). + // this session lastReceivedMessageTs: Date.now(), }; this._saveSession(theirIdentityKey, sessionInfo, txn); @@ -525,7 +524,7 @@ OlmDevice.prototype.createInboundSession = async function( const sessionInfo = { session, - // this counts as an received message: set last received message time + // this counts as a received message: set last received message time // to now lastReceivedMessageTs: Date.now(), }; @@ -582,15 +581,18 @@ OlmDevice.prototype.getSessionIdForDevice = async function(theirDeviceIdentityKe return null; } // Use the session that has most recently received a message - sessionInfos.sort((a, b) => { - if (a.lastReceivedMessageTs !== b.lastReceivedMessageTs) { - return a.lastReceivedMessageTs - b.lastReceivedMessageTs; - } else { - if (a.sessionId === b.sessionId) return 0; - return a.sessionId < b.sessionId ? -1 : 1; + let idxOfMin = 0; + for (let i = 1; i < sessionInfos.length; i++) { + if ( + sessionInfos[i].lastReceivedMessageTs < sessionInfos[idxOfMin].lastReceiveMessageTs || ( + sessionInfos[i].lastReceivedMessageTs === sessionInfos[idxOfMin].lastReceiveMessageTs && + sessionInfos[i].sessionId < sessinInfos[idxOfMin].sessionId + ) + ) { + idxOfMin = i; } - }); - return sessionInfos[sessionInfos.length - 1].sessionId; + } + return sessionInfos[idxOfMin].sessionId; }; /** diff --git a/src/crypto/store/indexeddb-crypto-store-backend.js b/src/crypto/store/indexeddb-crypto-store-backend.js index ba15e6673f0..0f31584cdd9 100644 --- a/src/crypto/store/indexeddb-crypto-store-backend.js +++ b/src/crypto/store/indexeddb-crypto-store-backend.js @@ -316,7 +316,7 @@ export class Backend { if (cursor) { results[cursor.value.sessionId] = { session: cursor.value.session, - lastReceivedMessagets: cursor.value.lastReceivedMessageTs, + lastReceivedMessageTs: cursor.value.lastReceivedMessageTs, }; cursor.continue(); } else { @@ -337,7 +337,7 @@ export class Backend { if (getReq.result) { func({ session: getReq.result.session, - lastReceivedMessagets: getReq.result.lastReceivedMessageTs, + lastReceivedMessageTs: getReq.result.lastReceivedMessageTs, }); } else { func(null); From a30845f9ce55c4fac4fb46a6f6c4d2979d0dd824 Mon Sep 17 00:00:00 2001 From: David Baker Date: Wed, 14 Nov 2018 08:03:23 +0000 Subject: [PATCH 6/9] lint --- src/crypto/OlmDevice.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/crypto/OlmDevice.js b/src/crypto/OlmDevice.js index c87ef9525a6..0ac53b3edb1 100644 --- a/src/crypto/OlmDevice.js +++ b/src/crypto/OlmDevice.js @@ -583,10 +583,12 @@ OlmDevice.prototype.getSessionIdForDevice = async function(theirDeviceIdentityKe // Use the session that has most recently received a message let idxOfMin = 0; for (let i = 1; i < sessionInfos.length; i++) { + const thisSessInfo = sessionInfos[i]; + const minSessInfo = sessionInfos[idxOfMin]; if ( - sessionInfos[i].lastReceivedMessageTs < sessionInfos[idxOfMin].lastReceiveMessageTs || ( - sessionInfos[i].lastReceivedMessageTs === sessionInfos[idxOfMin].lastReceiveMessageTs && - sessionInfos[i].sessionId < sessinInfos[idxOfMin].sessionId + thisSessInfo.lastReceivedMessageTs < minSessInfo.lastReceiveMessageTs || ( + thisSessInfo.lastReceivedMessageTs === minSessInfo.lastReceiveMessageTs && + thisSessInfo.sessionId < minSessInfo.sessionId ) ) { idxOfMin = i; From 5bc68c0c6dad66d868b33f78643189f166d85db3 Mon Sep 17 00:00:00 2001 From: David Baker Date: Wed, 14 Nov 2018 14:29:03 +0000 Subject: [PATCH 7/9] Handle last received message ts being undefined --- src/crypto/OlmDevice.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/crypto/OlmDevice.js b/src/crypto/OlmDevice.js index 0ac53b3edb1..c63f0e3a576 100644 --- a/src/crypto/OlmDevice.js +++ b/src/crypto/OlmDevice.js @@ -584,10 +584,15 @@ OlmDevice.prototype.getSessionIdForDevice = async function(theirDeviceIdentityKe let idxOfMin = 0; for (let i = 1; i < sessionInfos.length; i++) { const thisSessInfo = sessionInfos[i]; + const thisLastReceived = thisSessInfo.lastReceivedMessageTs === undefined ? + 0 : thisSessInfo.lastReceivedMessageTs; + const minSessInfo = sessionInfos[idxOfMin]; + const minLastReceived = minSessInfo.lastReceivedMessageTs === undefined ? + 0 : thisSessInfo.lastReceivedMessageTs; if ( - thisSessInfo.lastReceivedMessageTs < minSessInfo.lastReceiveMessageTs || ( - thisSessInfo.lastReceivedMessageTs === minSessInfo.lastReceiveMessageTs && + thisLastReceived < minLastReceived || ( + thisLastReceived === minLastReceived && thisSessInfo.sessionId < minSessInfo.sessionId ) ) { From 408407b33d082cb5c600d71b8e33e3e94e3d7260 Mon Sep 17 00:00:00 2001 From: David Baker Date: Wed, 14 Nov 2018 14:34:36 +0000 Subject: [PATCH 8/9] Fix typo --- src/crypto/OlmDevice.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/crypto/OlmDevice.js b/src/crypto/OlmDevice.js index c63f0e3a576..61d61789d9a 100644 --- a/src/crypto/OlmDevice.js +++ b/src/crypto/OlmDevice.js @@ -589,7 +589,7 @@ OlmDevice.prototype.getSessionIdForDevice = async function(theirDeviceIdentityKe const minSessInfo = sessionInfos[idxOfMin]; const minLastReceived = minSessInfo.lastReceivedMessageTs === undefined ? - 0 : thisSessInfo.lastReceivedMessageTs; + 0 : minSessInfo.lastReceivedMessageTs; if ( thisLastReceived < minLastReceived || ( thisLastReceived === minLastReceived && From 3c85bd55d31384ce0173fe47f0feea32fe43e6f7 Mon Sep 17 00:00:00 2001 From: David Baker Date: Wed, 14 Nov 2018 14:57:48 +0000 Subject: [PATCH 9/9] Time goes forwards --- src/crypto/OlmDevice.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/crypto/OlmDevice.js b/src/crypto/OlmDevice.js index 61d61789d9a..e5b05fa4b16 100644 --- a/src/crypto/OlmDevice.js +++ b/src/crypto/OlmDevice.js @@ -581,25 +581,25 @@ OlmDevice.prototype.getSessionIdForDevice = async function(theirDeviceIdentityKe return null; } // Use the session that has most recently received a message - let idxOfMin = 0; + let idxOfBest = 0; for (let i = 1; i < sessionInfos.length; i++) { const thisSessInfo = sessionInfos[i]; const thisLastReceived = thisSessInfo.lastReceivedMessageTs === undefined ? 0 : thisSessInfo.lastReceivedMessageTs; - const minSessInfo = sessionInfos[idxOfMin]; - const minLastReceived = minSessInfo.lastReceivedMessageTs === undefined ? - 0 : minSessInfo.lastReceivedMessageTs; + const bestSessInfo = sessionInfos[idxOfBest]; + const bestLastReceived = bestSessInfo.lastReceivedMessageTs === undefined ? + 0 : bestSessInfo.lastReceivedMessageTs; if ( - thisLastReceived < minLastReceived || ( - thisLastReceived === minLastReceived && - thisSessInfo.sessionId < minSessInfo.sessionId + thisLastReceived > bestLastReceived || ( + thisLastReceived === bestLastReceived && + thisSessInfo.sessionId < bestSessInfo.sessionId ) ) { - idxOfMin = i; + idxOfBest = i; } } - return sessionInfos[idxOfMin].sessionId; + return sessionInfos[idxOfBest].sessionId; }; /**