Skip to content

vite's localhost is now incompatible with node's #7075

@mrkishi

Description

@mrkishi

Describe the bug

As of v17, node respects the order of IP addresses returned by the system resolver.

This means localhost might resolve to 127.0.0.1 in a system and ::1 in another. As vite defaults to 127.0.0.1 and even translates localhost to it since #2977, it means starting up a vite server without a configured host (or with --host localhost) and doing net.connect(port) or net.connect(port, 'localhost') can fail.

I believe this could be fixed by defaulting to 'localhost' instead of the ipv4 here, but I'm unsure if that has negative complications:

if (
optionsHost === undefined ||
optionsHost === false ||
optionsHost === 'localhost'
) {
// Use a secure default
host = '127.0.0.1'
} else if (optionsHost === true) {

Why was localhost considered insecure? A system might even have reasons to have it resolved to a different, local interface—however unconventional that might be.

If there are no objections, I can open a PR with that change.

Reproduction

I'm not sure how to create a reproduction; I'd have to patch node's dns resolution.

System Info

n/a

Used Package Manager

pnpm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    p3-downstream-blockerBlocking the downstream ecosystem to work properly (priority)

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions