From 6de6b762e68a6ceadead4252d46d8d981ae2e561 Mon Sep 17 00:00:00 2001 From: sachaaaaa Date: Tue, 10 Sep 2019 11:21:27 +1000 Subject: [PATCH 1/3] Ensure the latest contact signed prekey is used --- app/sql.js | 2 +- js/background.js | 6 ++++++ js/views/app_view.js | 6 ++++++ js/views/device_pairing_dialog_view.js | 5 +++++ js/views/standalone_registration_view.js | 2 -- libtextsecure/message_receiver.js | 12 ++++++++++-- 6 files changed, 28 insertions(+), 5 deletions(-) diff --git a/app/sql.js b/app/sql.js index 227f59729..dc93630da 100644 --- a/app/sql.js +++ b/app/sql.js @@ -1169,7 +1169,7 @@ async function getContactSignedPreKeyById(id) { } async function getContactSignedPreKeyByIdentityKey(key) { const row = await db.get( - `SELECT * FROM ${CONTACT_SIGNED_PRE_KEYS_TABLE} WHERE identityKeyString = $identityKeyString;`, + `SELECT * FROM ${CONTACT_SIGNED_PRE_KEYS_TABLE} WHERE identityKeyString = $identityKeyString ORDER BY keyId DESC;`, { $identityKeyString: key, } diff --git a/js/background.js b/js/background.js index 37c0cf493..87cf5905f 100644 --- a/js/background.js +++ b/js/background.js @@ -171,6 +171,8 @@ return -1; }; Whisper.events = _.clone(Backbone.Events); + Whisper.events.isListenedTo = eventName => + Whisper.events._events ? !!Whisper.events._events[eventName] : false; let accountManager; window.getAccountManager = () => { if (!accountManager) { @@ -757,6 +759,10 @@ cb(e); } }); + + Whisper.events.on('devicePairingRequestRejected', async pubKey => { + await window.libloki.storage.removeContactPreKeyBundle(pubKey); + }); } window.getSyncRequest = () => diff --git a/js/views/app_view.js b/js/views/app_view.js index b23ce8223..e913cadd0 100644 --- a/js/views/app_view.js +++ b/js/views/app_view.js @@ -207,6 +207,12 @@ dialog.once('devicePairingRequestAccepted', (pubKey, cb) => Whisper.events.trigger('devicePairingRequestAccepted', pubKey, cb) ); + dialog.on('devicePairingRequestRejected', (pubKey, cb) => + Whisper.events.trigger('devicePairingRequestRejected', pubKey) + ); + dialog.once('close', () => { + Whisper.events.off('devicePairingRequestReceived'); + }); this.el.append(dialog.el); }, }); diff --git a/js/views/device_pairing_dialog_view.js b/js/views/device_pairing_dialog_view.js index b8235826d..f2fb0087a 100644 --- a/js/views/device_pairing_dialog_view.js +++ b/js/views/device_pairing_dialog_view.js @@ -58,6 +58,7 @@ this.$('.requestAcceptedView .ok').show(); }, skipDevice() { + this.trigger('devicePairingRequestRejected', this.pubKey); this.nextPubKey(); this.showView(); }, @@ -91,6 +92,10 @@ }, close() { this.remove(); + if (this.pubKey && !this.accepted) { + this.trigger('devicePairingRequestRejected', this.pubKey); + } + this.trigger('close'); }, }); })(); diff --git a/js/views/standalone_registration_view.js b/js/views/standalone_registration_view.js index 3d0843cfc..6cf43b501 100644 --- a/js/views/standalone_registration_view.js +++ b/js/views/standalone_registration_view.js @@ -145,8 +145,6 @@ }, async resetRegistration() { await window.Signal.Data.removeAllIdentityKeys(); - await window.Signal.Data.removeAllPreKeys(); - await window.Signal.Data.removeAllSignedPreKeys(); await window.Signal.Data.removeAllConversations(); Whisper.Registration.remove(); // Do not remove all items since they are only set diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index ed7f6d191..ba88a42e3 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -1095,12 +1095,20 @@ MessageReceiver.prototype.extend({ }, async handlePairingRequest(envelope, pairingRequest) { const valid = await this.validateAuthorisation(pairingRequest); - if (valid) { - await window.libloki.storage.savePairingAuthorisation(pairingRequest); + if (!valid) { + return this.removeFromCache(envelope); + } + await window.libloki.storage.savePairingAuthorisation(pairingRequest); + if (Whisper.events.isListenedTo('devicePairingRequestReceived')) { Whisper.events.trigger( 'devicePairingRequestReceived', pairingRequest.secondaryDevicePubKey ); + } else { + Whisper.events.trigger( + 'devicePairingRequestRejected', + pairingRequest.secondaryDevicePubKey + ); } return this.removeFromCache(envelope); }, From bd16bc5b9b1ecc815ca8a3e62f93fb49e604c0e5 Mon Sep 17 00:00:00 2001 From: sachaaaaa Date: Tue, 10 Sep 2019 15:38:06 +1000 Subject: [PATCH 2/3] lint --- js/views/app_view.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/views/app_view.js b/js/views/app_view.js index e913cadd0..c39c83ab6 100644 --- a/js/views/app_view.js +++ b/js/views/app_view.js @@ -207,7 +207,7 @@ dialog.once('devicePairingRequestAccepted', (pubKey, cb) => Whisper.events.trigger('devicePairingRequestAccepted', pubKey, cb) ); - dialog.on('devicePairingRequestRejected', (pubKey, cb) => + dialog.on('devicePairingRequestRejected', pubKey => Whisper.events.trigger('devicePairingRequestRejected', pubKey) ); dialog.once('close', () => { From 712afef8e236f531873b7f7ffe636f649564c0eb Mon Sep 17 00:00:00 2001 From: sachaaaaa Date: Mon, 16 Sep 2019 09:36:12 +1000 Subject: [PATCH 3/3] simplify --- libtextsecure/message_receiver.js | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index ba88a42e3..aaadc8818 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -1095,20 +1095,16 @@ MessageReceiver.prototype.extend({ }, async handlePairingRequest(envelope, pairingRequest) { const valid = await this.validateAuthorisation(pairingRequest); - if (!valid) { - return this.removeFromCache(envelope); - } - await window.libloki.storage.savePairingAuthorisation(pairingRequest); - if (Whisper.events.isListenedTo('devicePairingRequestReceived')) { - Whisper.events.trigger( - 'devicePairingRequestReceived', - pairingRequest.secondaryDevicePubKey - ); - } else { - Whisper.events.trigger( - 'devicePairingRequestRejected', - pairingRequest.secondaryDevicePubKey - ); + if (valid) { + // Pairing dialog is open and is listening + if (Whisper.events.isListenedTo('devicePairingRequestReceived')) { + await window.libloki.storage.savePairingAuthorisation(pairingRequest); + Whisper.events.trigger( + 'devicePairingRequestReceived', + pairingRequest.secondaryDevicePubKey + ); + } + // Ignore requests if the dialog is closed } return this.removeFromCache(envelope); },