From 6668e0d182fa5a0c0b4016512a965ef94dd48139 Mon Sep 17 00:00:00 2001 From: Steve Walsh Date: Wed, 7 Jul 2021 16:18:02 +0100 Subject: [PATCH] PR changes --- nodes/StopTheBleed.js | 52 ------------------------ nodes/core/core/80-function.js | 7 ++-- nodes/core/logic/15-change.js | 6 +-- package.json | 2 +- test/unit/test-stopTheBleed.js | 74 ---------------------------------- 5 files changed, 7 insertions(+), 134 deletions(-) delete mode 100644 nodes/StopTheBleed.js delete mode 100644 test/unit/test-stopTheBleed.js diff --git a/nodes/StopTheBleed.js b/nodes/StopTheBleed.js deleted file mode 100644 index 64a6b1b11..000000000 --- a/nodes/StopTheBleed.js +++ /dev/null @@ -1,52 +0,0 @@ -const clone = require('clone'); - -const variablesToCheck = [ - 'logger.metadata.organization', - 'payload.system.organization', - 'event.event.organization', - 'event.event.token.contents.organization' -]; - -module.exports = class StopTheBleed { - constructor(_before) { - const before = clone(_before); - const { - logger, - payload: { - system: { - bot, conversationId, organization, region - } - } - } = before; - this.before = before; - this.logger = logger; - this.bot = bot; - this.conversationId = conversationId; - this.organization = organization; - this.region = region; - } - - verify(after) { - try { - variablesToCheck.forEach((location) => { - const getValue = (object) => location.split('.').reduce((p, c) => (p && p[c]) || null, object); - if (getValue(this.before) !== getValue(after)) { - const details = { - message: `msg.${location} changed from "${getValue(this.before)}" to "${getValue(after)}" for bot "${this.bot}"` - }; - this.logger.error(details.message); - this.logger.app.platform.organization({ - srn: `srn:botnet:${this.region}:${this.organization}:bot:${this.bot}`, - action: 'exception', - actionType: 'invalid-payload-modification', - details, - conversationId: this.conversationId - }); - } - }); - } catch (e) { - console.log('Error while trying to verify variable changes'); - console.log(e); - } - } -}; diff --git a/nodes/core/core/80-function.js b/nodes/core/core/80-function.js index 822c42424..dd856ef59 100644 --- a/nodes/core/core/80-function.js +++ b/nodes/core/core/80-function.js @@ -14,7 +14,7 @@ * limitations under the License. **/ -const StopTheBleed = require('../../StopTheBleed') +const PayloadValidator = require('../../PayloadValidator') module.exports = function(RED) { "use strict"; @@ -209,13 +209,12 @@ module.exports = function(RED) { try { this.on("input", function(msg) { try { - const stopTheBleed = new StopTheBleed(msg) + const payloadValidator = new PayloadValidator(msg) var start = process.hrtime(); sandbox.msg = msg; const vm2Instance = new vm2.VM({ sandbox, timeout: 5000 }); const result = vm2Instance.run(functionText); - console.log('before the bleed check') - stopTheBleed.verify(result) + payloadValidator.verify(result) sendResults(this,msg._msgid, result); var duration = process.hrtime(start); diff --git a/nodes/core/logic/15-change.js b/nodes/core/logic/15-change.js index 5562b8abd..e4c2e31a7 100644 --- a/nodes/core/logic/15-change.js +++ b/nodes/core/logic/15-change.js @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. **/ -const StopTheBleed = require('../../StopTheBleed') +const PayloadValidator = require('../../PayloadValidator') module.exports = function(RED) { "use strict"; @@ -229,7 +229,7 @@ module.exports = function(RED) { } if (valid) { this.on('input', function(msg) { - const stopTheBleed = new StopTheBleed(msg) + const payloadValidator = new PayloadValidator(msg) for (var i=0; i { - it('Should not log when no changes', () => { - const beforeEvent = orgEvent('before'); - const stopTheBleed = new StopTheBleed(beforeEvent); - stopTheBleed.verify(beforeEvent); - }); - - it('Should warn when org is overwritten', () => { - const beforeEvent = orgEvent('before'); - errorLogStub = sinon.stub(); - appLogStub = sinon.stub(); - beforeEvent.logger.error = errorLogStub; - beforeEvent.logger.app = { - platform:{ - organization: appLogStub - } - }; - - const stopTheBleed = new StopTheBleed(beforeEvent); - - const modifiedEvent = orgEvent('after'); - - stopTheBleed.verify(modifiedEvent); - assert(errorLogStub.callCount === 4) - assert(appLogStub.callCount === 4) - const [[log1], [log2], [log3], [log4]] = appLogStub.args - assert(log1.details.message.includes('logger.metadata.organization')) - assert(log2.details.message.includes('payload.system.organization')) - assert(log3.details.message.includes('event.event.organization')) - assert(log4.details.message.includes('event.event.token.contents.organization')) - }); - - - it('Should warn when org is deleted', () => { - const beforeEvent = orgEvent('before'); - errorLogStub = sinon.stub(); - appLogStub = sinon.stub(); - beforeEvent.logger.error = errorLogStub; - beforeEvent.logger.app = { - platform:{ - organization: appLogStub - } - }; - - const stopTheBleed = new StopTheBleed(beforeEvent); - - delete beforeEvent.logger.metadata.organization; - delete beforeEvent.payload.system.organization; - delete beforeEvent.event.event.organization; - delete beforeEvent.event.event.token.contents.organization; - stopTheBleed.verify(beforeEvent); - assert(errorLogStub.callCount === 4) - assert(appLogStub.callCount === 4) - const [[log1], [log2], [log3], [log4]] = appLogStub.args - assert(log1.details.message.includes('logger.metadata.organization')) - assert(log2.details.message.includes('payload.system.organization')) - assert(log3.details.message.includes('event.event.organization')) - assert(log4.details.message.includes('event.event.token.contents.organization')) - }); - - it('Should not die when error', () => { - const beforeEvent = orgEvent('before'); - const stopTheBleed = new StopTheBleed(beforeEvent); - - const modifiedEvent = orgEvent('after'); - - stopTheBleed.verify(modifiedEvent); - }); -}); \ No newline at end of file