Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle non-integers passed as connection limits #1572

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/connection-manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,10 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven

this.opts = mergeOptions.call({ ignoreUndefined: true }, defaultOptions, init)

if (this.opts.maxConnections < this.opts.minConnections) {
throw errCode(new Error('Connection Manager maxConnections must be greater than minConnections'), codes.ERR_INVALID_PARAMETERS)
}

log('options: %o', this.opts)

this.validateLimits()

this.components = components

/**
Expand Down Expand Up @@ -352,6 +350,20 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
})
}

validateLimits () {
if (this.opts.minConnections < 0 || !Number.isInteger(this.opts.minConnections)) {
throw errCode(new Error('Connection Manager minConnections must be a positive integer'), codes.ERR_INVALID_PARAMETERS)
}

if (this.opts.maxConnections < 0 || !Number.isInteger(this.opts.maxConnections)) {
throw errCode(new Error('Connection Manager maxConnections must be a positive integer'), codes.ERR_INVALID_PARAMETERS)
}

if (this.opts.maxConnections < this.opts.minConnections) {
throw errCode(new Error('Connection Manager maxConnections must be greater than minConnections'), codes.ERR_INVALID_PARAMETERS)
}
}

async beforeStop () {
// if we are still dialing KEEP_ALIVE peers, abort those dials
this.connectOnStartupController?.abort()
Expand Down
37 changes: 37 additions & 0 deletions test/connection-manager/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type { Dialer } from '@libp2p/interface-connection-manager'
import type { Connection } from '@libp2p/interface-connection'
import type { Upgrader } from '@libp2p/interface-transport'
import type { PeerStore } from '@libp2p/interface-peer-store'
import { codes } from '../../src/errors.js'

const defaultOptions = {
maxConnections: 10,
Expand Down Expand Up @@ -198,6 +199,42 @@ describe('Connection Manager', () => {
expect(spy).to.have.property('callCount', 1)
})

it('should fail if the connection manager has negative maxConnections limit', async () => {
await expect(createNode({
config: createBaseOptions({
connectionManager: { maxConnections: -5 }
}),
started: false
})).to.eventually.rejected('maxConnections must be greater than 0').with.property('code', codes.ERR_INVALID_PARAMETERS)
})

it('should fail if the connection manager has negative minConnections limit', async () => {
await expect(createNode({
config: createBaseOptions({
connectionManager: { minConnections: -5 }
}),
started: false
})).to.eventually.rejected('minConnections must be greater than 0').with.property('code', codes.ERR_INVALID_PARAMETERS)
})

it('should fail if the connection manager has non-integer maxConnections limit', async () => {
await expect(createNode({
config: createBaseOptions({
connectionManager: { maxConnections: 1.5 }
}),
started: false
})).to.eventually.rejected('maxConnections must be an integer').with.property('code', codes.ERR_INVALID_PARAMETERS)
})

it('should fail if the connection manager has non-integer minConnections limit', async () => {
await expect(createNode({
config: createBaseOptions({
connectionManager: { minConnections: 1.5 }
}),
started: false
})).to.eventually.rejected('minConnections must be an integer').with.property('code', codes.ERR_INVALID_PARAMETERS)
})

it('should fail if the connection manager has mismatched connection limit options', async () => {
await expect(createNode({
config: createBaseOptions({
Expand Down