Skip to content

DNS resolution is ignoring hosts file #308

@waza-ari

Description

@waza-ari

Observed behavior

Currently, when using this library and passing a DNS name to resolve, it will ignore any entries that are potentially configured in a hosts file, e.g. /etc/hosts on Linux. This is very uncommon and should not happen. This also applies when using this library in a Docker container with extra_hosts overrides configured, as they're added to the containers hosts file.

Expected behavior

I expect the library to honor hosts configuration the same way all other applications do. Traced down the issue to the following lines:

export async function nodeResolveHost(s: string): Promise<string[]> {
const a = deferred<string[] | Error>();
const aaaa = deferred<string[] | Error>();
dns.resolve4(s, (err: Error, records: string[]) => {
if (err) {
a.resolve(err);
} else {
a.resolve(records);
}
});

You're using dns.resolve4 here. Citing from the Node.js DNS documentation:

dns.lookup() uses the operating system facilities to perform name resolution. It may not need to perform any network communication. To perform name resolution the way other applications on the same system do, use dns.lookup().
All other functions in the node:dns module connect to an actual DNS server to perform name resolution. They will always use the network to perform DNS queries. These functions do not use the same set of configuration files used by dns.lookup() (e.g. /etc/hosts). Use these functions to always perform DNS queries, bypassing other name-resolution facilities.

Instead of using dns.resolve4, the code should rather use lookup, essentially something along those lines, maintaining the current functionality of resolving multiple IPs if present:

const dns = require('node:dns');

const options = {
  family: 4,
  all: true
};

dns.lookup(s, options, (err, records) => {
    // ...
}

Server and client version

Client 2.29.3
Server doesn't matter

Host environment

Doesn't really matter, in this case it's a macOS host with a Docker container and a NodeJS application (N8n node to be specific). The docker container is launched with an extra_hosts argument which is also added to the containers hosts file, but its ignored when trying to connect.

Steps to reproduce

No response

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