From 8f82e7a442a6a975f42951b623fd02a8b7b5451a Mon Sep 17 00:00:00 2001 From: Mikunj Date: Mon, 6 Jul 2020 11:29:22 +1000 Subject: [PATCH] Fix multi device pairing --- libloki/crypto.js | 4 ++-- package.json | 4 ++-- ts/receiver/contentMessage.ts | 8 +------- ts/receiver/multidevice.ts | 25 ++++++------------------- ts/receiver/sessionHandling.ts | 2 -- ts/session/snode_api/snodePool.ts | 12 +++++++----- yarn.lock | 27 +++++++++++---------------- 7 files changed, 29 insertions(+), 53 deletions(-) diff --git a/libloki/crypto.js b/libloki/crypto.js index e86b84528..cec6ae99a 100644 --- a/libloki/crypto.js +++ b/libloki/crypto.js @@ -223,7 +223,7 @@ requestSignature, grantSignature, } = authorisation; - const isGrant = !!grantSignature; + const isGrant = !!(grantSignature && grantSignature.length > 0); if (!primaryDevicePubKey || !secondaryDevicePubKey) { window.log.warn( 'Received a pairing request with missing pubkeys. Ignored.' @@ -277,7 +277,7 @@ } = authorisation; const alreadySecondaryDevice = !!window.storage.get('isSecondaryDevice'); const ourPubKey = textsecure.storage.user.getNumber(); - const isRequest = !grantSignature; + const isRequest = !(grantSignature && grantSignature.length > 0); if (isRequest && alreadySecondaryDevice) { window.log.warn( 'Received a pairing request while being a secondary device. Ignored.' diff --git a/package.json b/package.json index 69dcab6d3..91dedb27c 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "icon-gen": "electron-icon-maker --input=images/icon_1024.png --output=./build", "generate": "yarn icon-gen && yarn grunt", "build-release": "cross-env SIGNAL_ENV=production npm run build -- --config.directories.output=release", - "build-module-protobuf": "pbjs --target static-module --wrap commonjs --out ts/protobuf/compiled.js protos/*.proto && pbts --out ts/protobuf/compiled.d.ts ts/protobuf/compiled.js", + "build-module-protobuf": "pbjs --target static-module --wrap commonjs --out ts/protobuf/compiled.js protos/*.proto && pbts --out ts/protobuf/compiled.d.ts ts/protobuf/compiled.js --force-long", "clean-module-protobuf": "rm -f ts/protobuf/compiled.d.ts ts/protobuf/compiled.js", "build-protobuf": "yarn build-module-protobuf", "clean-protobuf": "yarn clean-module-protobuf", @@ -95,7 +95,7 @@ "os-locale": "2.1.0", "p-retry": "^4.2.0", "pify": "3.0.0", - "protobufjs": "6.8.6", + "protobufjs": "^6.9.0", "rc-slider": "^8.7.1", "react": "16.8.3", "react-contextmenu": "2.11.0", diff --git a/ts/receiver/contentMessage.ts b/ts/receiver/contentMessage.ts index 6444119c3..b6e73b9e4 100644 --- a/ts/receiver/contentMessage.ts +++ b/ts/receiver/contentMessage.ts @@ -356,16 +356,10 @@ export async function innerHandleContentMessage( } if (content.pairingAuthorisation) { - if (!content.dataMessage || !content.syncMessage) { - window.log.error('Missing fields in pairingAuthorisation'); - return; - } - await handlePairingAuthorisationMessage( envelope, content.pairingAuthorisation, - content.dataMessage, - content.syncMessage + content.dataMessage ); return; } diff --git a/ts/receiver/multidevice.ts b/ts/receiver/multidevice.ts index 881ff42bf..cd39dbab6 100644 --- a/ts/receiver/multidevice.ts +++ b/ts/receiver/multidevice.ts @@ -84,19 +84,18 @@ export async function handleUnpairRequest( export async function handlePairingAuthorisationMessage( envelope: EnvelopePlus, pairingAuthorisation: SignalService.IPairingAuthorisationMessage, - dataMessage: SignalService.IDataMessage, - syncMessage: SignalService.ISyncMessage + dataMessage: SignalService.IDataMessage | undefined | null ): Promise { const { secondaryDevicePubKey, grantSignature } = pairingAuthorisation; const isGrant = grantSignature && + grantSignature.length > 0 && secondaryDevicePubKey === window.textsecure.storage.user.getNumber(); if (isGrant) { await handleAuthorisationForSelf( envelope, pairingAuthorisation, - dataMessage, - syncMessage + dataMessage ); } else { await handlePairingRequest(envelope, pairingAuthorisation); @@ -134,8 +133,7 @@ async function handlePairingRequest( async function handleAuthorisationForSelf( envelope: EnvelopePlus, pairingAuthorisation: SignalService.IPairingAuthorisationMessage, - dataMessage: SignalService.IDataMessage, - syncMessage: SignalService.ISyncMessage + dataMessage: SignalService.IDataMessage | undefined | null ) { const { ConversationController, libloki, Whisper } = window; @@ -143,7 +141,6 @@ async function handleAuthorisationForSelf( pairingAuthorisation ); const alreadySecondaryDevice = !!window.storage.get('isSecondaryDevice'); - let removedFromCache = false; if (alreadySecondaryDevice) { window.log.warn( 'Received an unexpected pairing authorisation (device is already paired as secondary device). Ignoring.' @@ -154,7 +151,7 @@ async function handleAuthorisationForSelf( ); } else { const { primaryDevicePubKey, grantSignature } = pairingAuthorisation; - if (grantSignature) { + if (grantSignature && grantSignature.length > 0) { // Authorisation received to become a secondary device window.log.info( `Received pairing authorisation from ${primaryDevicePubKey}` @@ -188,21 +185,11 @@ async function handleAuthorisationForSelf( window.log.warn('profile or profileKey are missing in DataMessage'); } } - // Update contact list - if (syncMessage && syncMessage.contacts) { - // Note: we do not return here because we don't want to block the next message on - // this attachment download and a lot of processing of that attachment. - // This call already removes the envelope from the cache - void handleContacts(envelope, syncMessage.contacts); - removedFromCache = true; - } } else { window.log.warn('Unimplemented pairing authorisation message type'); } } - if (!removedFromCache) { - await removeFromCache(envelope); - } + await removeFromCache(envelope); } function parseContacts(arrbuf: ArrayBuffer): Array { diff --git a/ts/receiver/sessionHandling.ts b/ts/receiver/sessionHandling.ts index b543ebb82..6a8966149 100644 --- a/ts/receiver/sessionHandling.ts +++ b/ts/receiver/sessionHandling.ts @@ -114,8 +114,6 @@ export async function handleSessionRequestMessage( timestamp: Date.now(), }); await libsession.getMessageQueue().send(user, sessionEstablished); - - libloki.api.sendSessionEstablishedMessage(envelope.source); } catch (e) { log.warn('Failed to process session request', e); // TODO how to handle a failed session request? diff --git a/ts/session/snode_api/snodePool.ts b/ts/session/snode_api/snodePool.ts index d6672930d..a7c45a70d 100644 --- a/ts/session/snode_api/snodePool.ts +++ b/ts/session/snode_api/snodePool.ts @@ -321,10 +321,10 @@ async function refreshRandomPoolDetail(seedNodes: Array): Promise { } } -export async function refreshRandomPool(seedNodes: Array): Promise { +export async function refreshRandomPool(seedNodes?: Array): Promise { const { log } = window; - if (!seedNodes.length) { + if (!seedNodes || !seedNodes.length) { if (!window.seedNodeList || !window.seedNodeList.length) { log.error( 'LokiSnodeAPI:::refreshRandomPool - seedNodeList has not been loaded yet' @@ -335,9 +335,11 @@ export async function refreshRandomPool(seedNodes: Array): Promise { seedNodes = window.seedNodeList; } - return allowOnlyOneAtATime('refreshRandomPool', async () => - refreshRandomPoolDetail(seedNodes) - ); + return allowOnlyOneAtATime('refreshRandomPool', async () => { + if (seedNodes) { + await refreshRandomPoolDetail(seedNodes); + } + }); } export async function getSnodesFor(pubkey: string): Promise> { diff --git a/yarn.lock b/yarn.lock index fb7fd094f..c84970eed 100644 --- a/yarn.lock +++ b/yarn.lock @@ -322,16 +322,11 @@ resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.106.tgz#6093e9a02aa567ddecfe9afadca89e53e5dce4dd" integrity sha512-tOSvCVrvSqFZ4A/qrqqm6p37GZoawsZtoR0SJhlF7EonNZUgrn8FfT+RNQ11h+NUpMt6QVe36033f3qEKBwfWA== -"@types/long@*": +"@types/long@*", "@types/long@^4.0.1": version "4.0.1" resolved "https://registry.yarnpkg.com/@types/long/-/long-4.0.1.tgz#459c65fa1867dafe6a8f322c4c51695663cc55e9" integrity sha512-5tXH6Bx/kNGd3MgffdmP4dy2Z+G4eaXw0SE81Tq3BNadtnMR5/ySMzX4SLEzHJzSmPNn4HIdpQsBvXMUykr58w== -"@types/long@^3.0.32": - version "3.0.32" - resolved "https://registry.yarnpkg.com/@types/long/-/long-3.0.32.tgz#f4e5af31e9e9b196d8e5fca8a5e2e20aa3d60b69" - integrity sha512-ZXyOOm83p7X8p3s0IYM3VeueNmHpkk/yMlP8CLeOnEcu6hIwPH7YjZBvhQkR0ZFS2DqZAxKtJ/M5fcuv3OU5BA== - "@types/minimatch@*": version "3.0.3" resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d" @@ -367,10 +362,10 @@ resolved "https://registry.yarnpkg.com/@types/node/-/node-12.12.31.tgz#d6b4f9645fee17f11319b508fb1001797425da51" integrity sha512-T+wnJno8uh27G9c+1T+a1/WYCHzLeDqtsGJkoEdSp2X8RTh3oOCZQcUnjAx90CS8cmmADX51O0FI/tu9s0yssg== -"@types/node@^8.9.4": - version "8.10.59" - resolved "https://registry.yarnpkg.com/@types/node/-/node-8.10.59.tgz#9e34261f30183f9777017a13d185dfac6b899e04" - integrity sha512-8RkBivJrDCyPpBXhVZcjh7cQxVBSmRk9QM7hOketZzp6Tg79c0N8kkpAIito9bnJ3HCVCHVYz+KHTEbfQNfeVQ== +"@types/node@^13.7.0": + version "13.13.12" + resolved "https://registry.yarnpkg.com/@types/node/-/node-13.13.12.tgz#9c72e865380a7dc99999ea0ef20fc9635b503d20" + integrity sha512-zWz/8NEPxoXNT9YyF2osqyA9WjssZukYpgI4UYZpOjcyqwIUqWGkcCionaEb9Ki+FULyPyvNFpg/329Kd2/pbw== "@types/pify@3.0.2": version "3.0.2" @@ -7870,10 +7865,10 @@ proto-list@~1.2.1: resolved "https://registry.yarnpkg.com/proto-list/-/proto-list-1.2.4.tgz#212d5bfe1318306a420f6402b8e26ff39647a849" integrity sha1-IS1b/hMYMGpCD2QCuOJv85ZHqEk= -protobufjs@6.8.6: - version "6.8.6" - resolved "https://registry.yarnpkg.com/protobufjs/-/protobufjs-6.8.6.tgz#ce3cf4fff9625b62966c455fc4c15e4331a11ca2" - integrity sha512-eH2OTP9s55vojr3b7NBaF9i4WhWPkv/nq55nznWNp/FomKrLViprUcqnBjHph2tFQ+7KciGPTPsVWGz0SOhL0Q== +protobufjs@^6.9.0: + version "6.9.0" + resolved "https://registry.yarnpkg.com/protobufjs/-/protobufjs-6.9.0.tgz#c08b2bf636682598e6fabbf0edb0b1256ff090bd" + integrity sha512-LlGVfEWDXoI/STstRDdZZKb/qusoAWUnmLg9R8OLSO473mBLWHowx8clbX5/+mKDEI+v7GzjoK9tRPZMMcoTrg== dependencies: "@protobufjs/aspromise" "^1.1.2" "@protobufjs/base64" "^1.1.2" @@ -7885,8 +7880,8 @@ protobufjs@6.8.6: "@protobufjs/path" "^1.1.2" "@protobufjs/pool" "^1.1.0" "@protobufjs/utf8" "^1.1.0" - "@types/long" "^3.0.32" - "@types/node" "^8.9.4" + "@types/long" "^4.0.1" + "@types/node" "^13.7.0" long "^4.0.0" proxy-addr@~2.0.5: