From 96520e9fd483197c4f3da5e4e7b05e6e4c95d9de Mon Sep 17 00:00:00 2001 From: lilia Date: Mon, 11 Jan 2016 12:08:54 -0800 Subject: [PATCH] Move envelope decode before ack We should not ack envelope protobufs that fail to decode correctly. If the server happens to send us such a thing it probably indicates a protocol mismatch between it and the client, in which case the client needs to update and re-receive the failed message. // FREEBIE --- js/libtextsecure.js | 7 +++---- libtextsecure/message_receiver.js | 7 +++---- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/js/libtextsecure.js b/js/libtextsecure.js index e484a1e78..07ffa205d 100644 --- a/js/libtextsecure.js +++ b/js/libtextsecure.js @@ -36795,11 +36795,12 @@ MessageReceiver.prototype = { // TODO: handle different types of requests. for now we blindly assume // PUT /messages textsecure.crypto.decryptWebsocketMessage(request.body, this.signalingKey).then(function(plaintext) { + var envelope = textsecure.protobuf.Envelope.decode(plaintext); // After this point, decoding errors are not the server's // fault, and we should handle them gracefully and tell the // user they received an invalid message request.respond(200, 'OK'); - this.queueEnvelope(plaintext); + this.queueEnvelope(envelope); }.bind(this)).catch(function(e) { request.respond(500, 'Bad encrypted websocket message'); @@ -36813,9 +36814,7 @@ MessageReceiver.prototype = { var handleEnvelope = this.handleEnvelope.bind(this, envelope); this.pending = this.pending.then(handleEnvelope, handleEnvelope); }, - handleEnvelope: function(encodedEnvelope) { - var envelope = textsecure.protobuf.Envelope.decode(encodedEnvelope); - + handleEnvelope: function(envelope) { if (envelope.type === textsecure.protobuf.Envelope.Type.RECEIPT) { return this.onDeliveryReceipt(envelope); } else if (envelope.content) { diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index 86bd27f2d..315bbe087 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -63,11 +63,12 @@ MessageReceiver.prototype = { // TODO: handle different types of requests. for now we blindly assume // PUT /messages textsecure.crypto.decryptWebsocketMessage(request.body, this.signalingKey).then(function(plaintext) { + var envelope = textsecure.protobuf.Envelope.decode(plaintext); // After this point, decoding errors are not the server's // fault, and we should handle them gracefully and tell the // user they received an invalid message request.respond(200, 'OK'); - this.queueEnvelope(plaintext); + this.queueEnvelope(envelope); }.bind(this)).catch(function(e) { request.respond(500, 'Bad encrypted websocket message'); @@ -81,9 +82,7 @@ MessageReceiver.prototype = { var handleEnvelope = this.handleEnvelope.bind(this, envelope); this.pending = this.pending.then(handleEnvelope, handleEnvelope); }, - handleEnvelope: function(encodedEnvelope) { - var envelope = textsecure.protobuf.Envelope.decode(encodedEnvelope); - + handleEnvelope: function(envelope) { if (envelope.type === textsecure.protobuf.Envelope.Type.RECEIPT) { return this.onDeliveryReceipt(envelope); } else if (envelope.content) {