Updated the logic to be easier to follow

This commit is contained in:
Steve Walsh 2021-07-13 17:06:37 +01:00
parent 9a435217ec
commit 3596c030c8
2 changed files with 37 additions and 24 deletions

View File

@ -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;

View File

@ -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;