diff --git a/lib/handlers/peer-to-peer-message-handler.js b/lib/handlers/peer-to-peer-message-handler.js --- a/lib/handlers/peer-to-peer-message-handler.js +++ b/lib/handlers/peer-to-peer-message-handler.js @@ -9,6 +9,7 @@ type PeerToPeerMessage, } from '../types/tunnelbroker/peer-to-peer-message-types.js'; import { getConfig } from '../utils/config.js'; +import { olmSessionErrors } from '../utils/olm-utils.js'; async function peerToPeerMessageHandler( message: PeerToPeerMessage, @@ -16,17 +17,16 @@ ): Promise { const { olmAPI } = getConfig(); if (message.type === peerToPeerMessageTypes.OUTBOUND_SESSION_CREATION) { + const { senderInfo, encryptedData, sessionVersion } = message; + const { userID: senderUserID, deviceID: senderDeviceID } = senderInfo; try { - const { senderInfo, encryptedData, sessionVersion } = message; - const { keys } = await identityClient.getInboundKeysForUser( - senderInfo.userID, - ); + const { keys } = await identityClient.getInboundKeysForUser(senderUserID); - const deviceKeys: ?DeviceOlmInboundKeys = keys[senderInfo.deviceID]; + const deviceKeys: ?DeviceOlmInboundKeys = keys[senderDeviceID]; if (!deviceKeys) { throw new Error( 'No keys for the device that requested creating a session, ' + - `deviceID: ${senderInfo.deviceID}`, + `deviceID: ${senderDeviceID}`, ); } @@ -38,14 +38,27 @@ ); console.log( 'Created inbound session with device ' + - `${message.senderInfo.deviceID}: ${result}, ` + + `${senderDeviceID}: ${result}, ` + `session version: ${sessionVersion}`, ); } catch (e) { - console.log( - 'Error creating inbound session with device ' + - `${message.senderInfo.deviceID}: ${e.message}`, - ); + if (e.message?.includes(olmSessionErrors.alreadyCreated)) { + console.log( + 'Received session request with lower session version from ' + + `${senderDeviceID}, session version: ${sessionVersion}`, + ); + } else if (e.message?.includes(olmSessionErrors.raceCondition)) { + console.log( + 'Race condition while creating session with ' + + `${senderDeviceID}, session version: ${sessionVersion}`, + ); + } else { + console.log( + 'Error creating inbound session with device ' + + `${senderDeviceID}: ${e.message}, ` + + `session version: ${sessionVersion}`, + ); + } } } else if (message.type === peerToPeerMessageTypes.ENCRYPTED_MESSAGE) { try { diff --git a/lib/utils/olm-utils.js b/lib/utils/olm-utils.js --- a/lib/utils/olm-utils.js +++ b/lib/utils/olm-utils.js @@ -116,6 +116,15 @@ return { identityKeys, prekey, prekeySignature }; } +const olmSessionErrors = Object.freeze({ + // Two clients send the session request to each other at the same time, + // we choose which session to keep based on `deviceID`. + raceCondition: 'OLM_SESSION_CREATION_RACE_CONDITION', + // The client received a session request with a lower session version, + // this request can be ignored. + alreadyCreated: 'OLM_SESSION_ALREADY_CREATED', +}); + export { retrieveAccountKeysSet, getAccountPrekeysSet, @@ -123,4 +132,5 @@ shouldRotatePrekey, getAccountOneTimeKeys, retrieveIdentityKeysAndPrekeys, + olmSessionErrors, };