Skip to content

[http] request does not accept host encapsulated within square brackets #39738

@trivikr

Description

@trivikr

Version

v14.17.5

Platform

Darwin <REDACTED> 20.5.0 Darwin Kernel Version 20.5.0: Sat May  8 05:10:33 PDT 2021; root:xnu-7195.121.3~9/RELEASE_X86_64 x86_64

Subsystem

http

What steps will reproduce the bug?

Note: This bug should ideally be Node.js version and platform independent.

To reproduce, run the following code:

Code
// test-node-http-ipv6.mjs
import { request } from "http";
import { lookup } from "dns";
import { promisify } from "util";

const lookupPromise = promisify(lookup);

// Get IPv6 address of example.com
const { address } = await lookupPromise("example.com", 6);

// Create URL from IPv6 address using square brackets.
const { protocol, host, port } = new URL(`http://[${address}]:80`);

// Throws Error: getaddrinfo ENOTFOUND [::ffff:5db8:d822]
// when host from IPv6 address is passed to request.
request({ protocol, host, port }).end();

// Does not throw error when square brackets are removed from host.
request({ protocol, host: host.replace(/^\[(.+)\]$/, "$1"), port }).end();
Output
events.js:377
      throw er; // Unhandled 'error' event
      ^

Error: getaddrinfo ENOTFOUND [::ffff:5db8:d822]
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:69:26)
Emitted 'error' event on ClientRequest instance at:
    at Socket.socketErrorListener (_http_client.js:475:9)
    at Socket.emit (events.js:400:28)
    at emitErrorNT (internal/streams/destroy.js:106:8)
    at emitErrorCloseNT (internal/streams/destroy.js:74:3)
    at processTicksAndRejections (internal/process/task_queues.js:82:21) {
  errno: -3008,
  code: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: '[::ffff:5db8:d822]'
}

How often does it reproduce? Is there a required condition?

Always

What is the expected behavior?

The http.request should accept host encapsulated within square brackets.

What do you see instead?

The http.request internally calls getaddrinfo for host with square brackets, and gets ENOTFOUND error in return.

Additional information

This issue was noticed while testing IPv6 endpoints in AWS SDK for JavaScript in aws/aws-sdk-js-v3#2660

Metadata

Metadata

Assignees

No one assigned

    Labels

    httpIssues or PRs related to the http subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions