Skip to content

Commit

Permalink
fix: limit observed addresses in address manager
Browse files Browse the repository at this point in the history
This is done in the identify protocol but the address manager
should guard itself against misuse.
  • Loading branch information
achingbrain committed Nov 30, 2024
1 parent 99f5f27 commit 02de925
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
11 changes: 11 additions & 0 deletions packages/libp2p/src/address-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ export interface AddressManagerInit {
* A list of string multiaddrs to add to the list of announced addresses
*/
appendAnnounce?: string[]

/**
* Limits the number of observed addresses we will store
*/
maxObservedAddresses?: number
}

export interface AddressManagerComponents {
Expand Down Expand Up @@ -103,6 +108,7 @@ export class AddressManager implements AddressManagerInterface {
private readonly announceFilter: AddressFilter
private readonly ipDomainMappings: Map<string, DNSMapping>
private readonly publicAddressMappings: Map<string, PublicAddressMapping[]>
private readonly maxObservedAddresses: number

/**
* Responsible for managing the peer addresses.
Expand All @@ -122,6 +128,7 @@ export class AddressManager implements AddressManagerInterface {
this.ipDomainMappings = new Map()
this.publicAddressMappings = new Map()
this.announceFilter = init.announceFilter ?? defaultAddressFilter
this.maxObservedAddresses = init.maxObservedAddresses ?? 10

// this method gets called repeatedly on startup when transports start listening so
// debounce it so we don't cause multiple self:peer:update events to be emitted
Expand Down Expand Up @@ -192,6 +199,10 @@ export class AddressManager implements AddressManagerInterface {
* Add peer observed addresses
*/
addObservedAddr (addr: Multiaddr): void {
if (this.observed.size === this.maxObservedAddresses) {
return
}

addr = stripPeerId(addr, this.components.peerId)
const addrString = addr.toString()

Expand Down
3 changes: 2 additions & 1 deletion packages/libp2p/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const DefaultConfig: Libp2pInit = {
listen: [],
announce: [],
noAnnounce: [],
announceFilter: (multiaddrs: Multiaddr[]) => multiaddrs
announceFilter: (multiaddrs: Multiaddr[]) => multiaddrs,
maxObservedAddresses: 10
},
connectionManager: {
resolvers: {
Expand Down
20 changes: 20 additions & 0 deletions packages/libp2p/test/addresses/address-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,26 @@ describe('Address Manager', () => {
expect(am.getObservedAddrs()).to.have.lengthOf(1)
})

it('should limit observed addresses', () => {
const am = new AddressManager({
peerId,
transportManager: stubInterface<TransportManager>(),
peerStore,
events,
logger: defaultLogger()
}, {
announceFilter: stubInterface<AddressFilter>()
})

expect(am.getObservedAddrs()).to.be.empty()

for (let i = 0; i < 100; i++) {
am.addObservedAddr(multiaddr(`/ip4/123.123.123.123/tcp/392${i}`))
}

expect(am.getObservedAddrs()).to.have.lengthOf(10)
})

it('should allow duplicate listen addresses', () => {
const ma = multiaddr('/ip4/0.0.0.0/tcp/0')
const am = new AddressManager({
Expand Down

0 comments on commit 02de925

Please sign in to comment.