Skip to content

Commit 1f7f2a0

Browse files
authored
fix: survive bad dns record (#313)
There's currently a bad txt record in the libp2p bootstrap DNS record so filter out anything that's not in the correct format.
1 parent cdbf7ad commit 1f7f2a0

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/resolvers/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export async function dnsaddrResolver (addr: Multiaddr, options: AbortOptions =
4949

5050
const records = await resolver.resolveTxt(`_dnsaddr.${hostname}`)
5151

52-
let addresses = records.flat().map((a) => a.split('=')[1])
52+
let addresses = records.flat().map((a) => a.split('=')[1]).filter(Boolean)
5353

5454
if (peerId != null) {
5555
addresses = addresses.filter((entry) => entry.includes(peerId))

test/resolvers.spec.ts

+22
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ const dnsaddrStub2 = [
2323
['dnsaddr=/ip6/2604:1380:2000:7a00::1/udp/4001/quic/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb']
2424
]
2525

26+
const dnsaddrStub3 = [
27+
['dnsaddr=/dnsaddr/sv15.bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN'],
28+
['dnsaddr=/dnsaddr/ny5.bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa'],
29+
['dnsaddr_record_value'],
30+
['dnsaddr=/dnsaddr/am6.bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb'],
31+
['dnsaddr=/dnsaddr/sg1.bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt']
32+
]
33+
2634
describe('multiaddr resolve', () => {
2735
it('should throw if no resolver is available', async () => {
2836
const ma = multiaddr('/dnsaddr/bootstrap.libp2p.io')
@@ -98,6 +106,20 @@ describe('multiaddr resolve', () => {
98106
})
99107
})
100108

109+
it('can resolve dnsaddr with bad record', async () => {
110+
const ma = multiaddr('/dnsaddr/am6.bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb')
111+
112+
const stub = sinon.stub(Resolver.prototype, 'resolveTxt')
113+
stub.onCall(0).returns(Promise.resolve(dnsaddrStub3))
114+
115+
// Resolve
116+
const resolvedMas = await ma.resolve()
117+
118+
// Should only have one address with the same peer id and should ignore the bad record
119+
expect(resolvedMas).to.have.lengthOf(1)
120+
expect(resolvedMas[0].toString()).to.equal(ma.toString())
121+
})
122+
101123
it('can cancel resolving', async () => {
102124
const ma = multiaddr('/dnsaddr/bootstrap.libp2p.ii/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nc')
103125
const controller = new AbortController()

0 commit comments

Comments
 (0)