Skip to content

fix: survive bad dns record #313

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

Merged
merged 1 commit into from
Mar 9, 2023
Merged
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
2 changes: 1 addition & 1 deletion src/resolvers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export async function dnsaddrResolver (addr: Multiaddr, options: AbortOptions =

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

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

if (peerId != null) {
addresses = addresses.filter((entry) => entry.includes(peerId))
Expand Down
22 changes: 22 additions & 0 deletions test/resolvers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ const dnsaddrStub2 = [
['dnsaddr=/ip6/2604:1380:2000:7a00::1/udp/4001/quic/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb']
]

const dnsaddrStub3 = [
['dnsaddr=/dnsaddr/sv15.bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN'],
['dnsaddr=/dnsaddr/ny5.bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa'],
['dnsaddr_record_value'],
['dnsaddr=/dnsaddr/am6.bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb'],
['dnsaddr=/dnsaddr/sg1.bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt']
]

describe('multiaddr resolve', () => {
it('should throw if no resolver is available', async () => {
const ma = multiaddr('/dnsaddr/bootstrap.libp2p.io')
Expand Down Expand Up @@ -98,6 +106,20 @@ describe('multiaddr resolve', () => {
})
})

it('can resolve dnsaddr with bad record', async () => {
const ma = multiaddr('/dnsaddr/am6.bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb')

const stub = sinon.stub(Resolver.prototype, 'resolveTxt')
stub.onCall(0).returns(Promise.resolve(dnsaddrStub3))

// Resolve
const resolvedMas = await ma.resolve()

// Should only have one address with the same peer id and should ignore the bad record
expect(resolvedMas).to.have.lengthOf(1)
expect(resolvedMas[0].toString()).to.equal(ma.toString())
})

it('can cancel resolving', async () => {
const ma = multiaddr('/dnsaddr/bootstrap.libp2p.ii/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nc')
const controller = new AbortController()
Expand Down