mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
Updated the logic to be easier to follow
This commit is contained in:
parent
9a435217ec
commit
3596c030c8
@ -65,32 +65,47 @@ module.exports = class PayloadValidator {
|
|||||||
return _.set(object, location, value);
|
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 {
|
try {
|
||||||
variablesToCheck.forEach((location) => {
|
variablesToCheck.forEach((location) => {
|
||||||
const beforeValue = this.getValue(this.before, location);
|
const beforeValue = this.getValue(this.before, location);
|
||||||
const afterValue = this.getValue(after, location);
|
const afterValue = this.getValue(after, location);
|
||||||
if (beforeValue !== afterValue) {
|
if (beforeValue !== afterValue) {
|
||||||
const details = {
|
this.logException(beforeValue, afterValue, location);
|
||||||
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
|
|
||||||
});
|
|
||||||
|
|
||||||
|
// attempt to set the value back to its correct one
|
||||||
after = this.setValue(after, location, beforeValue);
|
after = this.setValue(after, location, beforeValue);
|
||||||
|
|
||||||
if (!_.has(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;
|
return after;
|
||||||
}
|
}
|
||||||
|
|
||||||
verify(_after) {
|
verify(after) {
|
||||||
const after = _after;
|
|
||||||
if (this.isValidBefore) {
|
if (this.isValidBefore) {
|
||||||
if (Array.isArray(after)) {
|
if (Array.isArray(after)) {
|
||||||
const afterIndex = after.findIndex((msg) => !!msg);
|
const afterIndex = after.findIndex(msg => !!msg);
|
||||||
after[afterIndex] = this.check(after[afterIndex]);
|
after[afterIndex] = this.ensureOrganizationNotModified(after[afterIndex]);
|
||||||
return after;
|
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');
|
console.log('Error while trying to verify variable changes, wasn\'t initted with correct object');
|
||||||
return after;
|
return after;
|
||||||
|
@ -69,7 +69,6 @@ describe('Unit: PayloadValidator', () => {
|
|||||||
const modifiedEvent = orgEvent('before');
|
const modifiedEvent = orgEvent('before');
|
||||||
modifiedEvent.payload = 'some text';
|
modifiedEvent.payload = 'some text';
|
||||||
const result = payloadValidator.verify(modifiedEvent);
|
const result = payloadValidator.verify(modifiedEvent);
|
||||||
console.log(errorLogStub.callCount);
|
|
||||||
assert(errorLogStub.callCount === 1);
|
assert(errorLogStub.callCount === 1);
|
||||||
assert(appLogStub.callCount === 1);
|
assert(appLogStub.callCount === 1);
|
||||||
const [[log]] = appLogStub.args;
|
const [[log]] = appLogStub.args;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user