From 3596c030c80513b2cb909c2826c8da4d66a6f4f7 Mon Sep 17 00:00:00 2001 From: Steve Walsh Date: Tue, 13 Jul 2021 17:06:37 +0100 Subject: [PATCH] Updated the logic to be easier to follow --- nodes/PayloadValidator.js | 60 ++++++++++++++++++------------ test/unit/test-payloadValidator.js | 1 - 2 files changed, 37 insertions(+), 24 deletions(-) diff --git a/nodes/PayloadValidator.js b/nodes/PayloadValidator.js index 61e77b2aa..73483b242 100644 --- a/nodes/PayloadValidator.js +++ b/nodes/PayloadValidator.js @@ -65,32 +65,47 @@ module.exports = class PayloadValidator { return _.set(object, location, value); } - check(_after) { - let after = _after; + /** + * Log that we had a invalid payload modification + * @param {*} beforeValue + * @param {*} afterValue + * @param {*} location + */ + logException(beforeValue, afterValue, location) { + const details = { + message: `msg.${location} changed from "${beforeValue}" to "${afterValue}" for bot "${this.bot}"`, + nodeId: this.nodeId, + workerId: this.workerId + }; + 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 + }); + } + + /** + * Ensures that the organization in the before matches the org in the after + * If they are different then it attempts to set the organization back to the correct one + * @param {*} after + * @returns + */ + ensureOrganizationNotModified(after) { try { variablesToCheck.forEach((location) => { const beforeValue = this.getValue(this.before, location); const afterValue = this.getValue(after, location); if (beforeValue !== afterValue) { - const details = { - message: `msg.${location} changed from "${beforeValue}" to "${afterValue}" for bot "${this.bot}"`, - nodeId: this.nodeId, - workerId: this.workerId - }; - 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 - }); + this.logException(beforeValue, afterValue, location); - + // attempt to set the value back to its correct one after = this.setValue(after, location, beforeValue); - + if (!_.has(after, location, beforeValue)) { - throw new Error(`Cant set value as ${location} is no longer present in after`); + this.logger.error(`Cant set value as ${location} is no longer assessable in after`); } } }); @@ -101,15 +116,14 @@ module.exports = class PayloadValidator { return after; } - verify(_after) { - const after = _after; + verify(after) { if (this.isValidBefore) { if (Array.isArray(after)) { - const afterIndex = after.findIndex((msg) => !!msg); - after[afterIndex] = this.check(after[afterIndex]); + const afterIndex = after.findIndex(msg => !!msg); + after[afterIndex] = this.ensureOrganizationNotModified(after[afterIndex]); return after; } - return this.check(after); + return this.ensureOrganizationNotModified(after); } console.log('Error while trying to verify variable changes, wasn\'t initted with correct object'); return after; diff --git a/test/unit/test-payloadValidator.js b/test/unit/test-payloadValidator.js index 8d01ddd59..7c8334e06 100644 --- a/test/unit/test-payloadValidator.js +++ b/test/unit/test-payloadValidator.js @@ -69,7 +69,6 @@ describe('Unit: PayloadValidator', () => { const modifiedEvent = orgEvent('before'); modifiedEvent.payload = 'some text'; const result = payloadValidator.verify(modifiedEvent); - console.log(errorLogStub.callCount); assert(errorLogStub.callCount === 1); assert(appLogStub.callCount === 1); const [[log]] = appLogStub.args;