diff --git a/src/API.ts b/src/API.ts index 0d83b08..f80881a 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,23 @@ 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(config.VERBOSE) { + 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 +114,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 d64719b..b616218 100644 --- a/src/NodeList.ts +++ b/src/NodeList.ts @@ -416,3 +416,35 @@ 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 { + if(config.VERBOSE) Logger.mainLogger.debug('isValidVersion: ', minimumVersion, latestVersion, nodeVersion) + + const equalOrNewer = isEqualOrNewerVersion(minimumVersion, nodeVersion) + const equalOrOlder = isEqualOrOlderVersion(latestVersion, nodeVersion) + + return equalOrNewer && equalOrOlder +}