From 797cc2b10592613d6c180bc33a3baf29fc1e1976 Mon Sep 17 00:00:00 2001 From: ansuz Date: Mon, 7 Aug 2017 17:21:17 +0200 Subject: [PATCH 01/31] handle messages which fail to decrypt --- www/common/common-messaging.js | 23 ++++++++++++++++++++++- www/common/curve.js | 1 + 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/www/common/common-messaging.js b/www/common/common-messaging.js index 3bc6fc8aa..8b9d03749 100644 --- a/www/common/common-messaging.js +++ b/www/common/common-messaging.js @@ -240,7 +240,28 @@ define([ if (!isId) { return; } var decryptedMsg = channel.encryptor.decrypt(msg); - var parsed = JSON.parse(decryptedMsg); + + if (decryptedMsg === null) { + // console.error('unable to decrypt message'); + // console.error('potentially meant for yourself'); + + // message failed to parse, meaning somebody sent it to you but + // encrypted it with the wrong key, or you're sending a message to + // yourself in a different tab. + return; + } + + if (!decryptedMsg) { + console.error('decrypted message was falsey but not null'); + return; + } + + try { + var parsed = JSON.parse(decryptedMsg); + } catch (e) { + console.error(decryptedMsg); + return; + } if (parsed[0] !== Types.mapId && parsed[0] !== Types.mapIdAck) { return; } if (parsed[2] !== sender || !parsed[1]) { return; } channel.mapId[sender] = parsed[1]; diff --git a/www/common/curve.js b/www/common/curve.js index b33823fd5..0c39ba421 100644 --- a/www/common/curve.js +++ b/www/common/curve.js @@ -35,6 +35,7 @@ define([ var nonce = decodeBase64(unpacked[0]); var box = decodeBase64(unpacked[1]); var message = Nacl.box.open.after(box, nonce, secret); + if (message === false) { return null; } return encodeUTF8(message); }; From d835005905342e76c8c526d4e9d9c8a24b0b313a Mon Sep 17 00:00:00 2001 From: ansuz Date: Mon, 7 Aug 2017 17:22:56 +0200 Subject: [PATCH 02/31] simplify in-memory message format --- www/common/common-messaging.js | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/www/common/common-messaging.js b/www/common/common-messaging.js index 8b9d03749..34f690d0b 100644 --- a/www/common/common-messaging.js +++ b/www/common/common-messaging.js @@ -162,8 +162,16 @@ define([ var parsedMsg = JSON.parse(msg); if (parsedMsg[0] === Types.message) { - parsedMsg.shift(); - channel.messages.push([sig, parsedMsg]); + // TODO validate messages here + var res = { + type: parsedMsg[0], //Types.message, + sig: sig, + channel: parsedMsg[1], + time: parsedMsg[2], + text: parsedMsg[3], + }; + + channel.messages.push(res); return true; } var proxy; @@ -493,24 +501,22 @@ define([ var $msg, msg, date, name; var last = typeof(channel.lastDisplayed) === 'number'? channel.lastDisplayed: -1; for (var i = last + 1; i', {'class': 'message'}).appendTo($messages); // date - date = msg[1] ? new Date(msg[1]).toLocaleString() : '?'; + date = msg.time ? new Date(msg.time).toLocaleString(): '?'; //$('
', {'class':'date'}).text(date).appendTo($msg); $msg.attr('title', date); // name - if (msg[0] !== channel.lastSender) { - name = getFriend(common, msg[0]).displayName; + if (msg.channel !== channel.lastSender) { + name = getFriend(common, msg.channel).displayName; $('
', {'class':'sender'}).text(name).appendTo($msg); } - channel.lastSender = msg[0]; + channel.lastSender = msg.channel; - // content - //$('
', {'class':'content'}).text(msg[2]).appendTo($msg); - $('
', {'class':'content'}).html(parseMessage(msg[2])).appendTo($msg); + $('
', {'class':'content'}).html(parseMessage(msg.text)).appendTo($msg); } $messages.scrollTop($messages[0].scrollHeight); channel.lastDisplayed = i-1; @@ -518,7 +524,7 @@ define([ if (messages.length > 10) { var lastKnownMsg = messages[messages.length - 11]; - data.lastKnownHash = lastKnownMsg[0]; + channel.setLastMessageRead(lastKnownMsg.sig); } }; // Display a new channel @@ -680,6 +686,9 @@ define([ removeUI: function () { removeUI(data.curvePublic); }, updateUI: function (types) { updateUI(data.curvePublic, types); }, updateStatus: function () { updateStatus(data.curvePublic); }, + setLastMessageRead: function (hash) { + data.lastKnownHash = hash; + }, getMessagesSinceDisconnect: function () { getChannelMessagesSince(network, chan, data, keys); }, From c63807fd2b979d1427508ba1911c2c4e10f2b748 Mon Sep 17 00:00:00 2001 From: ansuz Date: Mon, 7 Aug 2017 17:24:16 +0200 Subject: [PATCH 03/31] implement channel.send --- www/common/common-messaging.js | 51 +++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/www/common/common-messaging.js b/www/common/common-messaging.js index 34f690d0b..0880ec296 100644 --- a/www/common/common-messaging.js +++ b/www/common/common-messaging.js @@ -372,6 +372,8 @@ define([ messenger.input = $input[0]; var send = function () { + // TODO implement sending queue + // TODO separate message logic from UI var channel = channels[data.channel]; if (channel.sending) { console.error("still sending"); @@ -385,34 +387,18 @@ define([ console.error("input is disabled"); return; } + + var payload = $input.val(); // Send the message - var msg = [Types.message, proxy.curvePublic, +new Date(), $input.val()]; - var msgStr = JSON.stringify(msg); - var cryptMsg = channel.encryptor.encrypt(msgStr); - channel.sending = true; - - console.log(channel.wc); - var network = common.getNetwork(); - if (!network.webChannels.some(function (wc) { - if (wc.id === channel.wc.id) { - console.error(wc.id, channel.wc.id); - return true; + channel.send(payload, function (e) { + if (e) { + channel.sending = false; + console.error(err); + return; } - console.error(wc.id, channel.wc.id); - //return wc.id === channel.wc.id; - })) { - console.error('no such channel:' + channel.wc.id); - return; - } - - channel.wc.bcast(cryptMsg).then(function () { $input.val(''); - pushMsg(common, channel, cryptMsg); channel.refresh(); channel.sending = false; - }, function (err) { - channel.sending = false; - console.error(err); }); }; $('