Merge pull request #7 from servisbot/MVP-6967

Added logic to check for changes in org variable in msg object
This commit is contained in:
Steve Walsh 2021-07-08 09:10:12 +01:00 committed by GitHub
commit 2dba4dae7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 3562 additions and 2347 deletions

1
.gitignore vendored
View File

@ -17,3 +17,4 @@ node_modules
public
locales/zz-ZZ
nodes/core/locales/zz-ZZ
.nyc_output*

69
nodes/PayloadValidator.js Normal file
View File

@ -0,0 +1,69 @@
const clone = require('clone');
const variablesToCheck = [
'logger.metadata.organization',
'payload.system.organization',
'event.event.organization',
'event.event.token.contents.organization'
];
module.exports = class PayloadValidator {
constructor(_before) {
try {
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;
this.isValidBefore = true;
} catch (e) {
console.log('Error while instantiating class with invalid object');
console.log(e);
this.isValidBefore = false;
}
}
getValue(object, location) {
return location.split('.').reduce((p, c) => (p && p[c]) || null, object);
}
verify(after) {
if (this.isValidBefore) {
try {
variablesToCheck.forEach((location) => {
if (this.getValue(this.before, location) !== this.getValue(after, location)) {
const details = {
message: `msg.${location} changed from "${this.getValue(this.before, location)}" to "${this.getValue(after, location)}" 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);
}
} else {
console.log('Error while trying to verify variable changes, wasn\'t initted with correct object');
}
}
};

View File

@ -14,6 +14,8 @@
* limitations under the License.
**/
const PayloadValidator = require('../../PayloadValidator')
module.exports = function(RED) {
"use strict";
var util = require("util");
@ -207,10 +209,12 @@ module.exports = function(RED) {
try {
this.on("input", function(msg) {
try {
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);
payloadValidator.verify(result)
sendResults(this,msg._msgid, result);
var duration = process.hrtime(start);

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
**/
const PayloadValidator = require('../../PayloadValidator')
module.exports = function(RED) {
"use strict";
@ -228,6 +229,7 @@ module.exports = function(RED) {
}
if (valid) {
this.on('input', function(msg) {
const payloadValidator = new PayloadValidator(msg)
for (var i=0; i<this.rules.length; i++) {
if (this.rules[i].t === "move") {
var r = this.rules[i];
@ -248,6 +250,7 @@ module.exports = function(RED) {
return;
}
}
payloadValidator.verify(msg)
node.send(msg);
});
}

5712
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{
"name": "@servisbot/node-red",
"version": "0.18.7-patch-7",
"version": "0.18.7-patch-8",
"description": "A visual tool for wiring the Internet of Things",
"homepage": "http://nodered.org",
"license": "Apache-2.0",
@ -12,7 +12,8 @@
"scripts": {
"start": "node red.js",
"test": "grunt test-nodes",
"build": "grunt build"
"build": "grunt build",
"test:unit": "nyc node_modules/.bin/mocha --recursive test/unit/*"
},
"bin": {
"node-red": "./red.js",
@ -102,15 +103,16 @@
"http-proxy": "^1.16.2",
"istanbul": "0.4.5",
"mocha": "^5.1.1",
"node-red-node-test-helper": "~0.1.7",
"nyc": "^15.1.0",
"should": "^8.4.0",
"sinon": "1.17.7",
"sinon": "^1.17.7",
"stoppable": "^1.0.6",
"supertest": "3.0.0",
"wdio-chromedriver-service": "^0.1.1",
"wdio-mocha-framework": "^0.5.11",
"wdio-spec-reporter": "^0.1.3",
"webdriverio": "^4.9.11",
"node-red-node-test-helper": "^0.1.7"
"webdriverio": "^4.9.11"
},
"engines": {
"node": ">=4"

View File

@ -0,0 +1,25 @@
module.exports = (org) => ({
logger: {
metadata: {
organization: org
}
},
payload: {
system: {
organization: org,
region: 'eu-test-1',
conversationId: 'convo-id',
bot: 'my-bot'
},
},
event: {
event: {
organization: org,
token: {
contents: {
organization: org
}
}
}
}
});

View File

@ -0,0 +1,83 @@
const PayloadValidator = require('../../nodes/PayloadValidator')
const orgEvent = require('./fixtures/data/orgEvent')
const sinon = require('sinon');
const assert = require('assert');
describe.only('Unit: PayloadValidator', () => {
it('Should not log when no changes', () => {
const beforeEvent = orgEvent('before');
const payloadValidator = new PayloadValidator(beforeEvent);
payloadValidator.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 payloadValidator = new PayloadValidator(beforeEvent);
const modifiedEvent = orgEvent('after');
payloadValidator.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 payloadValidator = new PayloadValidator(beforeEvent);
delete beforeEvent.logger.metadata.organization;
delete beforeEvent.payload.system.organization;
delete beforeEvent.event.event.organization;
delete beforeEvent.event.event.token.contents.organization;
payloadValidator.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 payloadValidator = new PayloadValidator(beforeEvent);
const modifiedEvent = orgEvent('after');
payloadValidator.verify(modifiedEvent);
});
it('Should not die with initiating the class with bad object', () => {
const payloadValidator = new PayloadValidator({});
});
it('Should not die with initiating the class with bad object and then calling verify', () => {
const payloadValidator = new PayloadValidator({});
payloadValidator.verify({});
});
});