Skip to content

Commit

Permalink
fix: handle quotes in dnsaddr results (#378)
Browse files Browse the repository at this point in the history
Remove `"` and `'` characters from DNSADDR results as they sometimes
appear in the output.
  • Loading branch information
achingbrain authored May 17, 2024
1 parent c89a872 commit b438d3d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/resolvers/dnsaddr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ export const dnsaddrResolver: Resolver<DNSADDROptions> = async function dnsaddrR
const output: string[] = []

for (const answer of result.Answer) {
const addr = answer.data.split('=')[1]
const addr = answer.data
.replace(/["']/g, '')
.trim()
.split('=')[1]

if (addr == null) {
continue
Expand Down
48 changes: 48 additions & 0 deletions test/resolvers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ const stubs: Record<string, string[]> = {
],
'_dnsaddr.self-referential.io': [
'dnsaddr=/dnsaddr/self-referential.io'
],
'_dnsaddr.double-quoted-answer.io': [
'"dnsaddr=/ip4/147.75.83.83/tcp/4001/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb"'
],
'_dnsaddr.single-quoted-answer.io': [
"'dnsaddr=/ip4/147.75.83.83/tcp/4001/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb'"
],
'_dnsaddr.mixed-quoted-answer.io': [
'"\'""" dnsaddr=/ip4/147.75.83.83/tcp/4001/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb" "'
]
}

Expand Down Expand Up @@ -158,5 +167,44 @@ describe('multiaddr resolve', () => {

await expect(resolvePromise).to.eventually.be.rejected().with.property('code', 'ERR_MAX_RECURSIVE_DEPTH_REACHED')
})

it('should handle double quotes', async () => {
const ma = multiaddr('/dnsaddr/double-quoted-answer.io')

// Resolve
const resolvedMas = await ma.resolve({
dns
})

// Should ignore double quotes
expect(resolvedMas).to.have.lengthOf(1)
expect(resolvedMas[0].toString()).to.equal('/ip4/147.75.83.83/tcp/4001/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb')
})

it('should handle single quotes', async () => {
const ma = multiaddr('/dnsaddr/single-quoted-answer.io')

// Resolve
const resolvedMas = await ma.resolve({
dns
})

// Should ignore double quotes
expect(resolvedMas).to.have.lengthOf(1)
expect(resolvedMas[0].toString()).to.equal('/ip4/147.75.83.83/tcp/4001/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb')
})

it('should handle mixed quotes', async () => {
const ma = multiaddr('/dnsaddr/mixed-quoted-answer.io')

// Resolve
const resolvedMas = await ma.resolve({
dns
})

// Should ignore double quotes
expect(resolvedMas).to.have.lengthOf(1)
expect(resolvedMas[0].toString()).to.equal('/ip4/147.75.83.83/tcp/4001/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb')
})
})
})

0 comments on commit b438d3d

Please sign in to comment.