mirror of
				https://github.com/node-red/node-red.git
				synced 2025-03-01 10:36:34 +00:00 
			
		
		
		
	more safety measure
This commit is contained in:
		
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -17,3 +17,4 @@ node_modules | ||||
| public | ||||
| locales/zz-ZZ | ||||
| nodes/core/locales/zz-ZZ | ||||
| .nyc_output* | ||||
| @@ -9,12 +9,16 @@ const variablesToCheck = [ | ||||
|  | ||||
| module.exports = class PayloadValidator { | ||||
|   constructor(_before) { | ||||
|     try { | ||||
|       const before = clone(_before); | ||||
|       const { | ||||
|         logger, | ||||
|         payload: { | ||||
|           system: { | ||||
|           bot, conversationId, organization, region | ||||
|             bot, | ||||
|             conversationId, | ||||
|             organization, | ||||
|             region | ||||
|           } | ||||
|         } | ||||
|       } = before; | ||||
| @@ -24,15 +28,25 @@ module.exports = class PayloadValidator { | ||||
|       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) => { | ||||
|         const getValue = (object) => location.split('.').reduce((p, c) => (p && p[c]) || null, object); | ||||
|         if (getValue(this.before) !== getValue(after)) { | ||||
|           if (this.getValue(this.before, location) !== this.getValue(after, location)) { | ||||
|             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.app.platform.organization({ | ||||
| @@ -48,5 +62,8 @@ module.exports = class PayloadValidator { | ||||
|         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'); | ||||
|     } | ||||
|   } | ||||
| }; | ||||
|   | ||||
| @@ -71,4 +71,13 @@ describe.only('Unit: PayloadValidator', () => { | ||||
|  | ||||
|     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({}); | ||||
|   }); | ||||
| }); | ||||
		Reference in New Issue
	
	Block a user