From 3a6ef20ab4c64999fb15ba8df7422bc15c595200 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 30 Nov 2023 19:24:39 +0100 Subject: [PATCH] fix: handle invalid hashIds in UI server payload MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../ui-services/AbstractUIService.ts | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/charging-station/ui-server/ui-services/AbstractUIService.ts b/src/charging-station/ui-server/ui-services/AbstractUIService.ts index d0478b99..e0078c19 100644 --- a/src/charging-station/ui-server/ui-services/AbstractUIService.ts +++ b/src/charging-station/ui-server/ui-services/AbstractUIService.ts @@ -140,7 +140,7 @@ export abstract class AbstractUIService { } public getBroadcastChannelExpectedResponses(uuid: string): number { - return this.broadcastChannelRequests.get(uuid) ?? 0; + return this.broadcastChannelRequests.get(uuid)!; } protected handleProtocolRequest( @@ -166,21 +166,25 @@ export abstract class AbstractUIService { if (hashId !== undefined && this.uiServer.chargingStations.has(hashId) === true) { return hashId; } - const msg = `${this.logPrefix( - moduleName, - 'sendBroadcastChannelRequest', - )} Charging station with hashId '${hashId}' not found`; - if (payload.hashIds?.length === 1) { - throw new BaseError(msg); - } else { - logger.warn(msg); - } + logger.warn( + `${this.logPrefix( + moduleName, + 'sendBroadcastChannelRequest', + )} Charging station with hashId '${hashId}' not found`, + ); }) ?.filter((hashId) => !isNullOrUndefined(hashId)) as string[]; + } else { + delete payload.hashIds; } - const expectedNumberOfResponses = isNotEmptyArray(payload.hashIds) - ? payload.hashIds!.length + const expectedNumberOfResponses = Array.isArray(payload.hashIds) + ? payload.hashIds.length : this.uiServer.chargingStations.size; + if (expectedNumberOfResponses === 0) { + throw new BaseError( + 'hashIds array in the request payload does not contain any valid charging station hashId', + ); + } this.uiServiceWorkerBroadcastChannel.sendRequest([uuid, procedureName, payload]); this.broadcastChannelRequests.set(uuid, expectedNumberOfResponses); } -- 2.34.1