From 22427713f4d879422dcdefa4c46d19d9af6f055d Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 10 Mar 2024 18:03:15 +0100 Subject: [PATCH] refactor(ui): validate UUID format MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/utils/ChargingStationConfigurationUtils.ts | 4 ++-- ui/web/src/composables/UIClient.ts | 12 +++++++++--- ui/web/src/composables/Utils.ts | 6 ++++++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/utils/ChargingStationConfigurationUtils.ts b/src/utils/ChargingStationConfigurationUtils.ts index 0e32c7e6..7b5346d5 100644 --- a/src/utils/ChargingStationConfigurationUtils.ts +++ b/src/utils/ChargingStationConfigurationUtils.ts @@ -21,7 +21,7 @@ export const buildChargingStationAutomaticTransactionGeneratorConfiguration = ( export const buildConnectorsStatus = (chargingStation: ChargingStation): ConnectorStatus[] => { return [...chargingStation.connectors.values()].map( - ({ transactionSetInterval, ...connectorStatusRest }) => connectorStatusRest + ({ transactionSetInterval, ...connectorStatus }) => connectorStatus ) } @@ -37,7 +37,7 @@ export const buildEvsesStatus = ( // eslint-disable-next-line array-callback-return return [...chargingStation.evses.values()].map(evseStatus => { const connectorsStatus = [...evseStatus.connectors.values()].map( - ({ transactionSetInterval, ...connectorStatusRest }) => connectorStatusRest + ({ transactionSetInterval, ...connectorStatus }) => connectorStatus ) let status: EvseStatusConfiguration switch (outputFormat) { diff --git a/ui/web/src/composables/UIClient.ts b/ui/web/src/composables/UIClient.ts index c2b9d60c..571d6d5b 100644 --- a/ui/web/src/composables/UIClient.ts +++ b/ui/web/src/composables/UIClient.ts @@ -12,7 +12,7 @@ import { type UIServerConfigurationSection } from '@/types' -import { randomUUID } from './Utils' +import { randomUUID, validateUUID } from './Utils' type ResponseHandler = { procedureName: ProcedureName @@ -240,8 +240,8 @@ export class UIClient { try { response = JSON.parse(messageEvent.data) as ProtocolResponse } catch (error) { - useToast().error('Invalid response format') - console.error('Invalid response format', error) + useToast().error('Invalid response JSON format') + console.error('Invalid response JSON format', error) return } @@ -253,6 +253,12 @@ export class UIClient { const [uuid, responsePayload] = response + if (!validateUUID(uuid)) { + useToast().error('Response UUID field is invalid') + console.error('Response UUID field is invalid:', response) + return + } + if (this.responseHandlers.has(uuid)) { const { procedureName, resolve, reject } = this.responseHandlers.get(uuid)! switch (responsePayload.status) { diff --git a/ui/web/src/composables/Utils.ts b/ui/web/src/composables/Utils.ts index 792e4481..0c8a8399 100644 --- a/ui/web/src/composables/Utils.ts +++ b/ui/web/src/composables/Utils.ts @@ -56,6 +56,12 @@ export const randomUUID = (): `${string}-${string}-${string}-${string}-${string} return crypto.randomUUID() } +export const validateUUID = ( + uuid: `${string}-${string}-${string}-${string}-${string}` +): uuid is `${string}-${string}-${string}-${string}-${string}` => { + return /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.test(uuid) +} + export const useUIClient = (): UIClient => { return UIClient.getInstance() } -- 2.34.1