Skip to content

Commit

Permalink
fix: do not append peer id to path addresses (#1547)
Browse files Browse the repository at this point in the history
Some multiaddrs have a path address as the final component - these
components are terminal in that you cannot append further tuples to
them, so do not do that when one is encountered.
  • Loading branch information
achingbrain authored Jan 16, 2023
1 parent 398e231 commit bd2bdf7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/address-manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ export class DefaultAddressManager extends EventEmitter<AddressManagerEvents> {
return this.announceFilter(Array.from(addrSet)
.map(str => multiaddr(str)))
.map(ma => {
// do not append our peer id to a path multiaddr as it will become invalid
if (ma.protos().pop()?.path === true) {
return ma
}

if (ma.getPeerId() === this.components.peerId.toString()) {
return ma
}
Expand Down
18 changes: 18 additions & 0 deletions test/addresses/address-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,24 @@ describe('Address Manager', () => {
expect(am.getObservedAddrs()).to.have.lengthOf(1)
expect(am.getObservedAddrs().map(ma => ma.toString())).to.include(ma)
})

it('should not add our peer id to path multiaddrs', () => {
const ma = '/unix/foo/bar/baz'
const transportManager = stubInterface<TransportManager>()
const am = new DefaultAddressManager({
peerId,
transportManager
}, {
listen: [ma],
announce: []
})

transportManager.getAddrs.returns([multiaddr(ma)])

const addrs = am.getAddresses()
expect(addrs).to.have.lengthOf(1)
expect(addrs[0].toString()).to.not.include(`/p2p/${peerId.toString()}`)
})
})

describe('libp2p.addressManager', () => {
Expand Down

0 comments on commit bd2bdf7

Please sign in to comment.