From eb0e74e703460a3b87352eaee21d0376d68de983 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 11 Jan 2026 19:36:03 +0100 Subject: [PATCH] fix: improve configuration file validation and error handling - Add validation for null/empty configurations after parsing - Add validation for missing configurationHash before caching - Add fallback to cached config on file read errors with warning - Remove non-null assertion on configurationHash assignment --- src/charging-station/ChargingStation.ts | 34 ++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index c729d2ae..18083c60 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -1206,7 +1206,10 @@ export class ChargingStation extends EventEmitter { let configuration: ChargingStationConfiguration | undefined if (isNotEmptyString(this.configurationFile) && existsSync(this.configurationFile)) { try { - if (this.sharedLRUCache.hasChargingStationConfiguration(this.configurationFileHash)) { + if ( + isNotEmptyString(this.configurationFileHash) && + this.sharedLRUCache.hasChargingStationConfiguration(this.configurationFileHash) + ) { configuration = this.sharedLRUCache.getChargingStationConfiguration( this.configurationFileHash ) @@ -1217,9 +1220,25 @@ export class ChargingStation extends EventEmitter { readFileSync(this.configurationFile, 'utf8') ) as ChargingStationConfiguration PerformanceStatistics.endMeasure(measureId, beginId) + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition + if (configuration == null || isEmpty(configuration)) { + logger.error( + `${this.logPrefix()} Invalid charging station configuration file ${ + this.configurationFile + }` + ) + return undefined + } + if (!isNotEmptyString(configuration.configurationHash)) { + logger.error( + `${this.logPrefix()} Missing charging station configuration hash in file ${ + this.configurationFile + }` + ) + return undefined + } + this.configurationFileHash = configuration.configurationHash this.sharedLRUCache.setChargingStationConfiguration(configuration) - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.configurationFileHash = configuration.configurationHash! } } catch (error) { handleFileException( @@ -1228,6 +1247,15 @@ export class ChargingStation extends EventEmitter { error as NodeJS.ErrnoException, this.logPrefix() ) + if ( + isNotEmptyString(this.configurationFileHash) && + this.sharedLRUCache.hasChargingStationConfiguration(this.configurationFileHash) + ) { + logger.warn( + `${this.logPrefix()} Using cached charging station configuration due to file read error` + ) + return this.sharedLRUCache.getChargingStationConfiguration(this.configurationFileHash) + } } } return configuration -- 2.43.0