From a856dfeaec047bc4747d5eb8dbf38488ccac3549 Mon Sep 17 00:00:00 2001 From: Tanuj Soni Date: Fri, 6 Sep 2024 16:54:33 +0530 Subject: [PATCH] Add version check for firstnode in restore network --- src/API.ts | 64 +++++++++++++++++++++++++++++++++++++++----- src/GlobalAccount.ts | 2 +- src/NodeList.ts | 30 +++++++++++++++++++++ 3 files changed, 88 insertions(+), 8 deletions(-) diff --git a/src/API.ts b/src/API.ts index 0d83b083..9b43518a 100644 --- a/src/API.ts +++ b/src/API.ts @@ -66,7 +66,18 @@ export function registerRoutes(server: FastifyInstance server.get('/myip', function (request, reply) { @@ -77,18 +88,21 @@ export function registerRoutes(server: FastifyInstance { profilerInstance.profileSectionStart('POST_nodelist') nestedCountersInstance.countEvent('consensor', 'POST_nodelist', 1) - const signedFirstNodeInfo = request.body + const requestBody = request.body + // eslint-disable-next-line no-constant-condition + if(true) Logger.mainLogger.debug('POST /nodelist requestBody:', requestBody) if (State.isFirst && NodeList.isEmpty() && !NodeList.foundFirstNode) { try { - const isSignatureValid = Crypto.verify(signedFirstNodeInfo) + const isSignatureValid = Crypto.verify(requestBody) if (!isSignatureValid) { - Logger.mainLogger.error('Invalid signature', signedFirstNodeInfo) + Logger.mainLogger.error('Invalid signature', requestBody) reply.send({ success: false, error: 'Invalid signature' }) return } } catch (e) { Logger.mainLogger.error(e) + Logger.mainLogger.error('Signature verification failed', requestBody) reply.send({ success: false, error: 'Signature verification failed' }) return } @@ -98,9 +112,13 @@ export function registerRoutes(server: FastifyInstance() const appliedConfigChanges = new Set() -export function getGlobalNetworkAccount(hash: boolean): object | string { +export function getGlobalNetworkAccount(hash: boolean): AccountDB.AccountsCopy | string { if (hash) { return cachedGlobalNetworkAccountHash } diff --git a/src/NodeList.ts b/src/NodeList.ts index d64719ba..2c667cb7 100644 --- a/src/NodeList.ts +++ b/src/NodeList.ts @@ -416,3 +416,33 @@ export function toggleFirstNode(): void { foundFirstNode = !foundFirstNode Logger.mainLogger.debug('foundFirstNode', foundFirstNode) } + +export function isEqualOrNewerVersion(minimumVersion: string, nodeVersion: string): boolean { + if (minimumVersion === nodeVersion) { + return true + } + + const minVerParts = minimumVersion.split('.') + const nodeVerParts = nodeVersion.split('.') + + /* eslint-disable security/detect-object-injection */ + for (let i = 0; i < nodeVerParts.length; i++) { + const nodeV = ~~nodeVerParts[i] // parse int + const minV = ~~minVerParts[i] // parse int + if (nodeV > minV) return true + if (nodeV < minV) return false + } + /* eslint-enable security/detect-object-injection */ + return false +} + +export function isEqualOrOlderVersion(maximumVersion: string, nodeVersion: string): boolean { + return isEqualOrNewerVersion(nodeVersion, maximumVersion) +} + +export function isValidVersion(minimumVersion: string, latestVersion: string, nodeVersion: string): boolean { + const equalOrNewer = isEqualOrNewerVersion(minimumVersion, nodeVersion) + const equalOrOlder = isEqualOrOlderVersion(latestVersion, nodeVersion) + + return equalOrNewer && equalOrOlder +}