From 2d761388c1e6d1c4dfa24ed3cc0086fee0465f21 Mon Sep 17 00:00:00 2001 From: ansuz Date: Mon, 23 Sep 2019 11:43:28 +0200 Subject: [PATCH] always return errors as strings when sending roster messages. fix getLastCheckpointHash logic --- www/common/outer/roster.js | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/www/common/outer/roster.js b/www/common/outer/roster.js index 901751ac5..a5c4c5ba6 100644 --- a/www/common/outer/roster.js +++ b/www/common/outer/roster.js @@ -197,14 +197,22 @@ var factory = function (Util, Hash, CPNetflux, Sortify, nThen, Crypto) { } var members = roster.state.members; - var changed = false; + // validate first... args.forEach(function (curve) { if (!isValidId(curve)) { throw new Error("INVALID_CURVE_KEY"); } - // don't try to remove something that isn't there - if (!members[curve]) { return; } + // even members can remove themselves + if (curve === author) { return; } + + // but if it concerns anyone else, validate that the author has sufficient permissions var role = members[curve].role; if (!canRemoveRole(author, role, members)) { throw new Error("INSUFFICIENT_PERMISSIONS"); } + }); + + var changed = false; + args.forEach(function (curve) { + // don't try to remove something that isn't there + if (!members[curve]) { return; } changed = true; delete members[curve]; }); @@ -352,6 +360,8 @@ var factory = function (Util, Hash, CPNetflux, Sortify, nThen, Crypto) { var keys = config.keys; var me = keys.myCurvePublic; var channel = config.channel; + var lastKnownHash = config.lastKnownHash || -1; + var ref = { state: { members: { }, @@ -360,7 +370,7 @@ var factory = function (Util, Hash, CPNetflux, Sortify, nThen, Crypto) { internal: { initialized: false, sinceLastCheckpoint: 0, - lastCheckpointHash: -1 + lastCheckpointHash: lastKnownHash, }, }; var roster = {}; @@ -462,7 +472,7 @@ var factory = function (Util, Hash, CPNetflux, Sortify, nThen, Crypto) { try { changed = handleCommand(parsed, author, ref); } catch (err) { - error = err; + error = err.message; } var id = getMessageId(hash); @@ -517,7 +527,7 @@ var factory = function (Util, Hash, CPNetflux, Sortify, nThen, Crypto) { // simulate the command before you send it changed = simulate(msg, keys.myCurvePublic, ref); } catch (err) { - return void cb(err); + return void cb(err.message); } if (!changed) { return void cb("NO_CHANGE"); } @@ -533,7 +543,7 @@ var factory = function (Util, Hash, CPNetflux, Sortify, nThen, Crypto) { channel, ciphertext ], function (err) { - if (err) { return response.handle(id, [err]); } + if (err) { return response.handle(id, [err.message || err]); } }); return id; }; @@ -644,7 +654,6 @@ var factory = function (Util, Hash, CPNetflux, Sortify, nThen, Crypto) { return void cb(err); } }).nThen(function () { - var lastKnownHash = config.lastKnownHash || -1; if (typeof(lastKnownHash) === 'string') { console.log("Synchronizing from checkpoint"); }