Skip to content

Commit

Permalink
Add version check for firstnode in restore network
Browse files Browse the repository at this point in the history
  • Loading branch information
tanuj-shardeum committed Aug 12, 2024
1 parent d1b989b commit b613218
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ export function registerRoutes(server: FastifyInstance<Server, IncomingMessage,
publicKey,
}

const networkAccount: AccountDB.AccountCopy | string = getGlobalNetworkAccount(false)
if(typeof networkAccount !== 'string' && !NodeList.isValidVersion(networkAccount?.data?.minVersion, networkAccount?.data?.latestVersion, signedFirstNodeInfo.nodeInfo.version)) {
Logger.mainLogger.error('Invalid version', signedFirstNodeInfo.nodeInfo.version)

Check warning

Code scanning / CodeQL

Log injection Medium

Log entry depends on a
user-provided value
.
reply.send({ success: false, error: 'Invalid version' })
}

Data.initSocketClient(firstNode)

// Add first node to NodeList
Expand Down
2 changes: 1 addition & 1 deletion src/GlobalAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface GlobalAccountsHashAndTimestamp {
export const globalAccountsMap = new Map<string, GlobalAccountsHashAndTimestamp>()
const appliedConfigChanges = new Set<string>()

export function getGlobalNetworkAccount(hash: boolean): object | string {
export function getGlobalNetworkAccount(hash: boolean): AccountDB.AccountCopy | string {
if (hash) {
return cachedGlobalNetworkAccountHash
}
Expand Down
28 changes: 28 additions & 0 deletions src/NodeList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,3 +416,31 @@ export function toggleFirstNode(): void {
foundFirstNode = !foundFirstNode
Logger.mainLogger.debug('foundFirstNode', foundFirstNode)
}

export function isEqualOrNewerVersion(minimumVersion: string, testVersion: string): boolean {
if (minimumVersion === testVersion) {
return true
}

const minVerParts = minimumVersion.split('.')
const testVerParts = testVersion.split('.')
/* eslint-disable security/detect-object-injection */
for (let i = 0; i < testVerParts.length; i++) {
const testV = ~~testVerParts[i] // parse int
const minV = ~~minVerParts[i] // parse int
if (testV > minV) return true
if (testV < minV) return false
}
/* eslint-enable security/detect-object-injection */
return false
}

export function isEqualOrOlderVersion(maximumVersion: string, testVersion: string): boolean {
return isEqualOrNewerVersion(testVersion, maximumVersion)
}

export function isValidVersion(minimumVersion: string, latestVersion: string, testVersion: string): boolean {
const equalOrNewer = isEqualOrNewerVersion(minimumVersion, testVersion)
const equalOrOlder = isEqualOrOlderVersion(latestVersion, testVersion)
return equalOrNewer && equalOrOlder
}
1 change: 1 addition & 0 deletions src/P2P.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface FirstNodeInfo {
externalIp: string
externalPort: number
publicKey: string
version: string
}
}
export interface FirstNodeResponse {
Expand Down

0 comments on commit b613218

Please sign in to comment.