diff --git a/src/resolvers/dnsaddr.ts b/src/resolvers/dnsaddr.ts index fa42fef1..cf276c52 100644 --- a/src/resolvers/dnsaddr.ts +++ b/src/resolvers/dnsaddr.ts @@ -45,7 +45,10 @@ export const dnsaddrResolver: Resolver = 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 diff --git a/test/resolvers.spec.ts b/test/resolvers.spec.ts index f7204eda..f1200617 100644 --- a/test/resolvers.spec.ts +++ b/test/resolvers.spec.ts @@ -39,6 +39,15 @@ const stubs: Record = { ], '_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" "' ] } @@ -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') + }) }) })