Skip to content

dns interceptor runs a DNS lookup for IPv6 literal origins and fails with ENOTFOUND #6060

Open

Description

@sebxt

The dns interceptor is meant to pass requests whose origin is already an IP address straight through. That works for IPv4 literals, but for an IPv6 literal such as http://[::1]:3000 the interceptor performs a lookup for the string [::1], which fails with getaddrinfo ENOTFOUND [::1]. The same request without the interceptor succeeds.

What should happen

A request to an IPv6 literal origin should be dispatched as is, without a lookup, in the same way an IPv4 literal is.

Reproduction

const { Agent, interceptors, request } = require('undici')
const http = require('node:http')

const server = http.createServer((req, res) => res.end('ok')).listen(0, '::1', async () => {
  const { port } = server.address()
  const agent = new Agent().compose(interceptors.dns())
  try {
    const res = await request(`http://[::1]:${port}/`, { dispatcher: agent })
    console.log(res.statusCode, await res.body.text())
  } catch (err) {
    console.log(err.code, err.message)
  }
  server.close()
  agent.close()
})

Output on main (e8f3423):

ENOTFOUND getaddrinfo ENOTFOUND [::1]

Expected: 200 ok.

With a custom lookup option the callback is invoked with origin.hostname === '[::1]', so any user lookup has to special case brackets too.

Root cause

In lib/interceptor/dns.js the bypass check is isIP(origin.hostname) !== 0. URL#hostname keeps the brackets around IPv6 addresses (new URL('http://[::1]').hostname === '[::1]'), and net.isIP('[::1]') returns 0, so the request falls through to runLookup().

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions