From 2d48daa7b6e1daad13eb5ce05955460a009b9b31 Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Wed, 7 Nov 2018 11:20:43 -0800 Subject: [PATCH] Refine Sealed Sender behaviors --- js/background.js | 3 ++- js/conversation_controller.js | 6 ++++-- js/models/conversations.js | 38 +++++++++++++++++++++-------------- js/models/messages.js | 3 ++- js/modules/startup.js | 4 +++- js/modules/web_api.js | 18 ++++++++++++++--- libtextsecure/sync_request.js | 3 ++- ts/util/lint/exceptions.json | 16 +++++++-------- 8 files changed, 59 insertions(+), 32 deletions(-) diff --git a/js/background.js b/js/background.js index ca6eb226465d..9a23591ea741 100644 --- a/js/background.js +++ b/js/background.js @@ -721,7 +721,8 @@ if (Whisper.Import.isComplete()) { const { wrap, sendOptions } = ConversationController.prepareForSend( - textsecure.storage.user.getNumber() + textsecure.storage.user.getNumber(), + { syncMessage: true } ); wrap( textsecure.messaging.sendRequestConfigurationSyncMessage(sendOptions) diff --git a/js/conversation_controller.js b/js/conversation_controller.js index 33ef1be7a7d7..035963e3bfa9 100644 --- a/js/conversation_controller.js +++ b/js/conversation_controller.js @@ -181,10 +181,12 @@ ); }); }, - prepareForSend(id) { + prepareForSend(id, options) { // id is either a group id or an individual user's id const conversation = this.get(id); - const sendOptions = conversation && conversation.getSendOptions(); + const sendOptions = conversation + ? conversation.getSendOptions(options) + : null; const wrap = conversation ? conversation.wrapSend.bind(conversation) : promise => promise; diff --git a/js/models/conversations.js b/js/models/conversations.js index 6dd8fe4c7bdc..cfa849f49b58 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -342,10 +342,11 @@ // a sync message to our own devices, we need to send the accessKeys down for both // contacts. So we merge their sendOptions. const { sendOptions } = ConversationController.prepareForSend( - this.ourNumber + this.ourNumber, + { syncMessage: true } ); - const recipientSendOptions = this.getSendOptions(); - const options = Object.assign({}, sendOptions, recipientSendOptions); + const contactSendOptions = this.getSendOptions(); + const options = Object.assign({}, sendOptions, contactSendOptions); const promise = textsecure.storage.protocol.loadIdentityKey(number); return promise.then(key => @@ -879,9 +880,9 @@ ); }, - getSendOptions() { + getSendOptions(options = {}) { const senderCertificate = storage.get('senderCertificate'); - const numberInfo = this.getNumberInfo(); + const numberInfo = this.getNumberInfo(options); return { senderCertificate, @@ -889,7 +890,10 @@ }; }, - getNumberInfo({ disableMeCheck } = {}) { + getNumberInfo(options = {}) { + const { syncMessage, disableMeCheck } = options; + + // START: this code has an Expiration date of ~2018/11/21 // We don't want to enable unidentified delivery for send unless it is // also enabled for our own account. const me = ConversationController.getOrCreate(this.ourNumber, 'private'); @@ -899,10 +903,11 @@ ) { return null; } + // END if (!this.isPrivate()) { const infoArray = this.contactCollection.map(conversation => - conversation.getNumberInfo({ disableMeCheck }) + conversation.getNumberInfo(options) ); return Object.assign({}, ...infoArray); } @@ -910,6 +915,11 @@ const accessKey = this.get('accessKey'); const sealedSender = this.get('sealedSender'); + // We never send sync messages as sealed sender + if (syncMessage && this.id === this.ourNumber) { + return null; + } + // If we've never fetched user's profile, we default to what we have if (sealedSender === SEALED_SENDER.UNKNOWN) { return { @@ -1243,19 +1253,17 @@ window.log.info(`Sending ${read.length} read receipts`); // Because syncReadMessages sends to our other devices, and sendReadReceipts goes // to a contact, we need accessKeys for both. - const prep = ConversationController.prepareForSend(this.ourNumber); - const recipientSendOptions = this.getSendOptions(); - const sendOptions = Object.assign( - {}, - prep.sendOptions, - recipientSendOptions + const { sendOptions } = ConversationController.prepareForSend( + this.ourNumber, + { syncMessage: true } ); - await this.wrapSend( textsecure.messaging.syncReadMessages(read, sendOptions) ); if (storage.get('read-receipt-setting')) { + const convoSendOptions = this.getSendOptions(); + await Promise.all( _.map(_.groupBy(read, 'sender'), async (receipts, sender) => { const timestamps = _.map(receipts, 'timestamp'); @@ -1263,7 +1271,7 @@ textsecure.messaging.sendReadReceipts( sender, timestamps, - sendOptions + convoSendOptions ) ); }) diff --git a/js/models/messages.js b/js/models/messages.js index d2b74e5c5624..92a570e3a155 100644 --- a/js/models/messages.js +++ b/js/models/messages.js @@ -981,7 +981,8 @@ sendSyncMessage() { const ourNumber = textsecure.storage.user.getNumber(); const { wrap, sendOptions } = ConversationController.prepareForSend( - ourNumber + ourNumber, + { syncMessage: true } ); this.syncPromise = this.syncPromise || Promise.resolve(); diff --git a/js/modules/startup.js b/js/modules/startup.js index e3810e079b6d..e09a374d8d61 100644 --- a/js/modules/startup.js +++ b/js/modules/startup.js @@ -46,7 +46,9 @@ exports.syncReadReceiptConfiguration = async ({ } try { - const { wrap, sendOptions } = prepareForSend(ourNumber); + const { wrap, sendOptions } = prepareForSend(ourNumber, { + syncMessage: true, + }); await wrap(sendRequestConfigurationSyncMessage(sendOptions)); storage.put(settingName, true); } catch (error) { diff --git a/js/modules/web_api.js b/js/modules/web_api.js index dacdd72d1052..96a28caacfae 100644 --- a/js/modules/web_api.js +++ b/js/modules/web_api.js @@ -1,6 +1,7 @@ const WebSocket = require('websocket').w3cwebsocket; const fetch = require('node-fetch'); const ProxyAgent = require('proxy-agent'); +const { Agent } = require('https'); const is = require('@sindresorhus/is'); @@ -159,6 +160,11 @@ function _createSocket(url, { certificateAuthority, proxyUrl }) { return new WebSocket(url, null, null, null, requestOptions); } +const agents = { + unauth: null, + auth: null, +}; + function _promiseAjax(providedUrl, options) { return new Promise((resolve, reject) => { const url = providedUrl || `${options.host}/${options.path}`; @@ -169,10 +175,16 @@ function _promiseAjax(providedUrl, options) { typeof options.timeout !== 'undefined' ? options.timeout : 10000; const { proxyUrl } = options; - let agent; - if (proxyUrl) { - agent = new ProxyAgent(proxyUrl); + const agentType = options.unathenticated ? 'unauth' : 'auth'; + + if (!agents[agentType]) { + if (proxyUrl) { + agents[agentType] = new ProxyAgent(proxyUrl); + } else { + agents[agentType] = new Agent(); + } } + const agent = agents[agentType]; const fetchOptions = { method: options.type, diff --git a/libtextsecure/sync_request.js b/libtextsecure/sync_request.js index fe85d62e049a..ad8244cc02b5 100644 --- a/libtextsecure/sync_request.js +++ b/libtextsecure/sync_request.js @@ -25,7 +25,8 @@ const ourNumber = textsecure.storage.user.getNumber(); const { wrap, sendOptions } = ConversationController.prepareForSend( - ourNumber + ourNumber, + { syncMessage: true } ); window.log.info('SyncRequest created. Sending contact sync message...'); wrap(sender.sendRequestContactSyncMessage(sendOptions)) diff --git a/ts/util/lint/exceptions.json b/ts/util/lint/exceptions.json index 5d1996c51261..174d78fae96c 100644 --- a/ts/util/lint/exceptions.json +++ b/ts/util/lint/exceptions.json @@ -244,7 +244,7 @@ "rule": "jQuery-wrap(", "path": "js/background.js", "line": " wrap(", - "lineNumber": 726, + "lineNumber": 727, "reasonCategory": "falseMatch", "updated": "2018-10-18T22:23:00.485Z" }, @@ -252,7 +252,7 @@ "rule": "jQuery-wrap(", "path": "js/background.js", "line": " await wrap(", - "lineNumber": 1227, + "lineNumber": 1228, "reasonCategory": "falseMatch", "updated": "2018-10-26T22:43:23.229Z" }, @@ -260,7 +260,7 @@ "rule": "jQuery-load(", "path": "js/conversation_controller.js", "line": " async load() {", - "lineNumber": 208, + "lineNumber": 210, "reasonCategory": "falseMatch", "updated": "2018-10-02T21:00:44.007Z" }, @@ -268,7 +268,7 @@ "rule": "jQuery-load(", "path": "js/conversation_controller.js", "line": " this._initialPromise = load();", - "lineNumber": 237, + "lineNumber": 239, "reasonCategory": "falseMatch", "updated": "2018-10-02T21:00:44.007Z" }, @@ -311,7 +311,7 @@ "rule": "jQuery-wrap(", "path": "js/models/messages.js", "line": " return wrap(", - "lineNumber": 993, + "lineNumber": 994, "reasonCategory": "falseMatch", "updated": "2018-10-05T23:12:28.961Z" }, @@ -383,7 +383,7 @@ "rule": "jQuery-wrap(", "path": "js/modules/startup.js", "line": " await wrap(sendRequestConfigurationSyncMessage(sendOptions));", - "lineNumber": 50, + "lineNumber": 52, "reasonCategory": "falseMatch", "updated": "2018-10-05T23:12:28.961Z" }, @@ -2331,7 +2331,7 @@ "rule": "jQuery-wrap(", "path": "libtextsecure/sync_request.js", "line": " wrap(sender.sendRequestContactSyncMessage(sendOptions))", - "lineNumber": 31, + "lineNumber": 32, "reasonCategory": "falseMatch", "updated": "2018-10-05T23:12:28.961Z" }, @@ -2339,7 +2339,7 @@ "rule": "jQuery-wrap(", "path": "libtextsecure/sync_request.js", "line": " return wrap(sender.sendRequestGroupSyncMessage(sendOptions));", - "lineNumber": 34, + "lineNumber": 35, "reasonCategory": "falseMatch", "updated": "2018-10-05T23:12:28.961Z" },