From 9c5c4195dabbbe1481553a495e3c54c70aef9aa5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 20 Aug 2022 15:35:20 +0200 Subject: [PATCH] Factor out in helpers PDU payload validation MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- package-lock.json | 38 +++++++++---------- package.json | 2 +- .../ocpp/1.6/OCPP16IncomingRequestService.ts | 32 ++++++++++------ .../ocpp/1.6/OCPP16RequestService.ts | 32 ++++++++++------ .../ocpp/1.6/OCPP16ResponseService.ts | 32 ++++++++++------ 5 files changed, 80 insertions(+), 56 deletions(-) diff --git a/package-lock.json b/package-lock.json index f82c8061..c56b0ebe 100644 --- a/package-lock.json +++ b/package-lock.json @@ -39,7 +39,7 @@ "@types/express": "^4.17.13", "@types/mocha": "^9.1.1", "@types/mochawesome": "^6.2.1", - "@types/node": "^18.7.6", + "@types/node": "^18.7.8", "@types/proper-lockfile": "^4.1.2", "@types/tar": "^6.1.2", "@types/uuid": "^8.3.4", @@ -1964,9 +1964,9 @@ } }, "node_modules/@types/node": { - "version": "18.7.6", - "resolved": "https://registry.npmjs.org/@types/node/-/node-18.7.6.tgz", - "integrity": "sha512-EdxgKRXgYsNITy5mjjXjVE/CS8YENSdhiagGrLqjG0pvA2owgJ6i4l7wy/PFZGC0B1/H20lWKN7ONVDNYDZm7A==" + "version": "18.7.8", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.7.8.tgz", + "integrity": "sha512-/YP55EMK2341JkODUb8DM9O0x1SIz2aBvyF33Uf1c76St3VpsMXEIW0nxuKkq/5cxnbz0RD9cfwNZHEAZQD3ag==" }, "node_modules/@types/object-path": { "version": "0.11.1", @@ -11215,9 +11215,9 @@ } }, "node_modules/new-github-release-url/node_modules/type-fest": { - "version": "2.18.0", - "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-2.18.0.tgz", - "integrity": "sha512-pRS+/yrW5TjPPHNOvxhbNZexr2bS63WjrMU8a+VzEBhUi9Tz1pZeD+vQz3ut0svZ46P+SRqMEPnJmk2XnvNzTw==", + "version": "2.18.1", + "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-2.18.1.tgz", + "integrity": "sha512-UKCINsd4qiATXD6OIlnQw9t1ux/n2ld+Nl0kzPbCONhCaUIS/BhJbNw14w6584HCQWf3frBK8vmWnGZq/sbPHQ==", "dev": true, "engines": { "node": ">=12.20" @@ -13479,9 +13479,9 @@ } }, "node_modules/release-it/node_modules/boxen/node_modules/type-fest": { - "version": "2.18.0", - "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-2.18.0.tgz", - "integrity": "sha512-pRS+/yrW5TjPPHNOvxhbNZexr2bS63WjrMU8a+VzEBhUi9Tz1pZeD+vQz3ut0svZ46P+SRqMEPnJmk2XnvNzTw==", + "version": "2.18.1", + "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-2.18.1.tgz", + "integrity": "sha512-UKCINsd4qiATXD6OIlnQw9t1ux/n2ld+Nl0kzPbCONhCaUIS/BhJbNw14w6584HCQWf3frBK8vmWnGZq/sbPHQ==", "dev": true, "engines": { "node": ">=12.20" @@ -19309,9 +19309,9 @@ } }, "@types/node": { - "version": "18.7.6", - "resolved": "https://registry.npmjs.org/@types/node/-/node-18.7.6.tgz", - "integrity": "sha512-EdxgKRXgYsNITy5mjjXjVE/CS8YENSdhiagGrLqjG0pvA2owgJ6i4l7wy/PFZGC0B1/H20lWKN7ONVDNYDZm7A==" + "version": "18.7.8", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.7.8.tgz", + "integrity": "sha512-/YP55EMK2341JkODUb8DM9O0x1SIz2aBvyF33Uf1c76St3VpsMXEIW0nxuKkq/5cxnbz0RD9cfwNZHEAZQD3ag==" }, "@types/object-path": { "version": "0.11.1", @@ -26632,9 +26632,9 @@ }, "dependencies": { "type-fest": { - "version": "2.18.0", - "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-2.18.0.tgz", - "integrity": "sha512-pRS+/yrW5TjPPHNOvxhbNZexr2bS63WjrMU8a+VzEBhUi9Tz1pZeD+vQz3ut0svZ46P+SRqMEPnJmk2XnvNzTw==", + "version": "2.18.1", + "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-2.18.1.tgz", + "integrity": "sha512-UKCINsd4qiATXD6OIlnQw9t1ux/n2ld+Nl0kzPbCONhCaUIS/BhJbNw14w6584HCQWf3frBK8vmWnGZq/sbPHQ==", "dev": true } } @@ -28350,9 +28350,9 @@ }, "dependencies": { "type-fest": { - "version": "2.18.0", - "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-2.18.0.tgz", - "integrity": "sha512-pRS+/yrW5TjPPHNOvxhbNZexr2bS63WjrMU8a+VzEBhUi9Tz1pZeD+vQz3ut0svZ46P+SRqMEPnJmk2XnvNzTw==", + "version": "2.18.1", + "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-2.18.1.tgz", + "integrity": "sha512-UKCINsd4qiATXD6OIlnQw9t1ux/n2ld+Nl0kzPbCONhCaUIS/BhJbNw14w6584HCQWf3frBK8vmWnGZq/sbPHQ==", "dev": true } } diff --git a/package.json b/package.json index 0e8d3d27..18ef26ac 100644 --- a/package.json +++ b/package.json @@ -114,7 +114,7 @@ "@types/express": "^4.17.13", "@types/mocha": "^9.1.1", "@types/mochawesome": "^6.2.1", - "@types/node": "^18.7.6", + "@types/node": "^18.7.8", "@types/proper-lockfile": "^4.1.2", "@types/tar": "^6.1.2", "@types/uuid": "^8.3.4", diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index 32387225..6609fa41 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -311,18 +311,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer ChargingStationUtils.isIncomingRequestCommandSupported(commandName, chargingStation) ) { try { - if (this.jsonSchemas.has(commandName)) { - this.validateIncomingRequestPayload( - chargingStation, - commandName, - this.jsonSchemas.get(commandName), - commandPayload - ); - } else { - logger.warn( - `${chargingStation.logPrefix()} ${moduleName}.incomingRequestHandler: No JSON schema found for command ${commandName} PDU validation` - ); - } + this.validatePayload(chargingStation, commandName, commandPayload); // Call the method to build the response response = await this.incomingRequestHandlers.get(commandName)( chargingStation, @@ -367,6 +356,25 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer ); } + private validatePayload( + chargingStation: ChargingStation, + commandName: OCPP16IncomingRequestCommand, + commandPayload: JsonType + ): boolean { + if (this.jsonSchemas.has(commandName)) { + return this.validateIncomingRequestPayload( + chargingStation, + commandName, + this.jsonSchemas.get(commandName), + commandPayload + ); + } + logger.warn( + `${chargingStation.logPrefix()} ${moduleName}.incomingRequestHandler: No JSON schema found for command ${commandName} PDU validation` + ); + return false; + } + // Simulate charging station restart private handleRequestReset( chargingStation: ChargingStation, diff --git a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts index 3ff3bdcb..5a0e0bc1 100644 --- a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts @@ -154,18 +154,7 @@ export default class OCPP16RequestService extends OCPPRequestService { commandName, commandParams ); - if (this.jsonSchemas.has(commandName)) { - this.validateRequestPayload( - chargingStation, - commandName, - this.jsonSchemas.get(commandName), - requestPayload - ); - } else { - logger.warn( - `${chargingStation.logPrefix()} ${moduleName}.requestHandler: No JSON schema found for command ${commandName} PDU validation` - ); - } + this.validatePayload(chargingStation, commandName, requestPayload); return (await this.sendMessage( chargingStation, Utils.generateUUID(), @@ -278,4 +267,23 @@ export default class OCPP16RequestService extends OCPPRequestService { ); } } + + private validatePayload( + chargingStation: ChargingStation, + commandName: OCPP16RequestCommand, + requestPayload: Request + ): boolean { + if (this.jsonSchemas.has(commandName)) { + return this.validateRequestPayload( + chargingStation, + commandName, + this.jsonSchemas.get(commandName), + requestPayload + ); + } + logger.warn( + `${chargingStation.logPrefix()} ${moduleName}.requestHandler: No JSON schema found for command ${commandName} PDU validation` + ); + return false; + } } diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index e3e87d4c..984cb1b3 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -179,18 +179,7 @@ export default class OCPP16ResponseService extends OCPPResponseService { ChargingStationUtils.isRequestCommandSupported(commandName, chargingStation) ) { try { - if (this.jsonSchemas.has(commandName)) { - this.validateResponsePayload( - chargingStation, - commandName, - this.jsonSchemas.get(commandName), - payload - ); - } else { - logger.warn( - `${chargingStation.logPrefix()} ${moduleName}.responseHandler: No JSON schema found for command ${commandName} PDU validation` - ); - } + this.validatePayload(chargingStation, commandName, payload); await this.responseHandlers.get(commandName)(chargingStation, payload, requestPayload); } catch (error) { logger.error(chargingStation.logPrefix() + ' Handle request response error: %j', error); @@ -223,6 +212,25 @@ export default class OCPP16ResponseService extends OCPPResponseService { } } + private validatePayload( + chargingStation: ChargingStation, + commandName: OCPP16RequestCommand, + payload: JsonType + ): boolean { + if (this.jsonSchemas.has(commandName)) { + return this.validateResponsePayload( + chargingStation, + commandName, + this.jsonSchemas.get(commandName), + payload + ); + } + logger.warn( + `${chargingStation.logPrefix()} ${moduleName}.responseHandler: No JSON schema found for command ${commandName} PDU validation` + ); + return false; + } + private handleResponseBootNotification( chargingStation: ChargingStation, payload: OCPP16BootNotificationResponse -- 2.34.1