more safety measure

This commit is contained in:
Steve Walsh 2021-07-07 17:17:25 +01:00
parent a48f3e769c
commit d89076fea0
3 changed files with 61 additions and 34 deletions

1
.gitignore vendored
View File

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

View File

@ -9,44 +9,61 @@ const variablesToCheck = [
module.exports = class PayloadValidator { module.exports = class PayloadValidator {
constructor(_before) { constructor(_before) {
const before = clone(_before); try {
const { const before = clone(_before);
logger, const {
payload: { logger,
system: { payload: {
bot, conversationId, organization, region system: {
bot,
conversationId,
organization,
region
}
} }
} } = before;
} = before; this.before = before;
this.before = before; this.logger = logger;
this.logger = logger; this.bot = bot;
this.bot = bot; this.conversationId = conversationId;
this.conversationId = conversationId; this.organization = organization;
this.organization = organization; this.region = region;
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) { verify(after) {
try { if (this.isValidBefore) {
variablesToCheck.forEach((location) => { try {
const getValue = (object) => location.split('.').reduce((p, c) => (p && p[c]) || null, object); variablesToCheck.forEach((location) => {
if (getValue(this.before) !== getValue(after)) { if (this.getValue(this.before, location) !== this.getValue(after, location)) {
const details = { const details = {
message: `msg.${location} changed from "${getValue(this.before)}" to "${getValue(after)}" for bot "${this.bot}"` 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.error(details.message);
this.logger.app.platform.organization({ this.logger.app.platform.organization({
srn: `srn:botnet:${this.region}:${this.organization}:bot:${this.bot}`, srn: `srn:botnet:${this.region}:${this.organization}:bot:${this.bot}`,
action: 'exception', action: 'exception',
actionType: 'invalid-payload-modification', actionType: 'invalid-payload-modification',
details, details,
conversationId: this.conversationId conversationId: this.conversationId
}); });
} }
}); });
} catch (e) { } catch (e) {
console.log('Error while trying to verify variable changes'); console.log('Error while trying to verify variable changes');
console.log(e); console.log(e);
}
} else {
console.log('Error while trying to verify variable changes, wasn\'t initted with correct object');
} }
} }
}; };

View File

@ -71,4 +71,13 @@ describe.only('Unit: PayloadValidator', () => {
payloadValidator.verify(modifiedEvent); 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({});
});
}); });