From: Jérôme Benoit Date: Sun, 19 Sep 2021 09:07:47 +0000 (+0200) Subject: Add sanity checks to random integer generation X-Git-Tag: v1.1.4~1 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=a3868ec4e6c4782ba51066ddbba74f9730dbba19;p=e-mobility-charging-stations-simulator.git Add sanity checks to random integer generation Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index a18aa5ad..930c4b81 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -1,7 +1,5 @@ // Partial Copyright Jerome Benoit. 2021. All Rights Reserved. -import * as url from 'url'; - import { ChangeAvailabilityRequest, ChangeConfigurationRequest, ClearChargingProfileRequest, GetConfigurationRequest, GetDiagnosticsRequest, MessageTrigger, OCPP16AvailabilityType, OCPP16IncomingRequestCommand, OCPP16TriggerMessageRequest, RemoteStartTransactionRequest, RemoteStopTransactionRequest, ResetRequest, SetChargingProfileRequest, UnlockConnectorRequest } from '../../../types/ocpp/1.6/Requests'; import { ChangeAvailabilityResponse, ChangeConfigurationResponse, ClearChargingProfileResponse, GetConfigurationResponse, GetDiagnosticsResponse, OCPP16TriggerMessageResponse, SetChargingProfileResponse, UnlockConnectorResponse } from '../../../types/ocpp/1.6/Responses'; import { ChargingProfilePurposeType, OCPP16ChargingProfile } from '../../../types/ocpp/1.6/ChargingProfile'; @@ -19,6 +17,7 @@ import { OCPP16StandardParametersKey } from '../../../types/ocpp/1.6/Configurati import { OCPPConfigurationKey } from '../../../types/ocpp/Configuration'; import OCPPError from '../OCPPError'; import OCPPIncomingRequestService from '../OCPPIncomingRequestService'; +import { URL } from 'url'; import Utils from '../../../utils/Utils'; import fs from 'fs'; import logger from '../../../utils/Logger'; @@ -354,7 +353,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer private async handleRequestGetDiagnostics(commandPayload: GetDiagnosticsRequest): Promise { logger.debug(this.chargingStation.logPrefix() + ' ' + IncomingRequestCommand.GET_DIAGNOSTICS + ' request received: %j', commandPayload); - const uri = new url.URL(commandPayload.location); + const uri = new URL(commandPayload.location); if (uri.protocol.startsWith('ftp:')) { let ftpClient: Client; try { diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index 1fad09af..5d1b04e9 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -1,7 +1,7 @@ // Partial Copyright Jerome Benoit. 2021. All Rights Reserved. import { AuthorizeRequest, OCPP16AuthorizationStatus, OCPP16AuthorizeResponse, OCPP16StartTransactionResponse, OCPP16StopTransactionResponse, StartTransactionRequest, StopTransactionRequest } from '../../../types/ocpp/1.6/Transaction'; -import { HeartbeatRequest, OCPP16BootNotificationRequest, OCPP16RequestCommand, StatusNotificationRequest } from '../../../types/ocpp/1.6/Requests'; +import { HeartbeatRequest, OCPP16RequestCommand, StatusNotificationRequest } from '../../../types/ocpp/1.6/Requests'; import { HeartbeatResponse, OCPP16BootNotificationResponse, OCPP16RegistrationStatus, StatusNotificationResponse } from '../../../types/ocpp/1.6/Responses'; import { MeterValuesRequest, MeterValuesResponse } from '../../../types/ocpp/1.6/MeterValues'; diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index b3947133..3782beeb 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -22,9 +22,9 @@ export default class Utils { const hours = Math.floor(duration / (3600 * 1000)); const minutes = Math.floor((duration / 1000 - (hours * 3600)) / 60); const seconds = duration / 1000 - (hours * 3600) - (minutes * 60); - let hoursStr: string = hours.toString(); - let minutesStr: string = minutes.toString(); - let secondsStr: string = seconds.toString(); + let hoursStr = hours.toString(); + let minutesStr = minutes.toString(); + let secondsStr = seconds.toString(); if (hours < 10) { hoursStr = '0' + hours.toString(); @@ -109,8 +109,14 @@ export default class Utils { } public static getRandomInt(max: number, min = 0): number { + if (max < 0) { + throw new RangeError('Invalid interval'); + } max = Math.floor(max); if (min) { + if (max < min || min < 0) { + throw new RangeError('Invalid interval'); + } min = Math.ceil(min); return Math.floor(Utils.secureRandom() * (max - min + 1)) + min; }