Skip to content

Custom DNS lookup failing on Node 20+, works on Node 18 #55762

@geocine

Description

@geocine

Version

20.16.0

Platform

Microsoft Windows NT 10.0.22621.0
x64

Subsystem

No response

What steps will reproduce the bug?

const dns = require('dns');
const https = require('https');

// Custom DNS resolution for github.com
const customDNSLookup = (hostname, options, callback) => {
    if (hostname === 'github.com') {
        // Using one of GitHub's IPs as an example
        return callback(null, '20.205.243.166', 4);
    } else {
        return dns.lookup(hostname, options, callback);
    }
};

// Make a test request using native https
async function testConnection() {
    const options = {
        hostname: 'github.com',
        path: '/',
        method: 'GET',
        headers: {
            'Host': 'github.com'
        },
        agent: new https.Agent({
            rejectUnauthorized: false,
            lookup: customDNSLookup
        })
    };

    return new Promise((resolve, reject) => {
        const req = https.request(options, (res) => {
            console.log('Successfully connected to GitHub');
            console.log('Using IP:', '20.205.243.166');
            console.log('Status:', res.statusCode);
            resolve(res);
        });

        req.on('error', (error) => {
            console.error('Error:', error.message);
            reject(error);
        });

        req.end();
    });
}

testConnection();

Just run this node repro.js

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

You can repro it on node v20+ it works on node v18

What is the expected behavior? Why is that the expected behavior?

Successfully connected to GitHub
Using IP: 20.205.243.166
Status: 200

This is the expected behavior because we have defined a custom lookup method to resolve to this IP

What do you see instead?

Error: Invalid IP address: undefined
node:internal/process/promises:391
    triggerUncaughtException(err, true /* fromPromise */);
    ^

TypeError [ERR_INVALID_IP_ADDRESS]: Invalid IP address: undefined
    at emitLookup (node:net:1465:17)
    at customDNSLookup (C:\Aivan\PL\repro.js:8:16)
    at emitLookup (node:net:1414:5)
    at defaultTriggerAsyncIdScope (node:internal/async_hooks:464:18)
    at lookupAndConnectMultiple (node:net:1413:3)
    at node:net:1359:7
    at defaultTriggerAsyncIdScope (node:internal/async_hooks:464:18)
    at lookupAndConnect (node:net:1358:5)
    at Socket.connect (node:net:1255:5)
    at Object.connect (node:_tls_wrap:1801:13) {
  code: 'ERR_INVALID_IP_ADDRESS'
}

Node.js v20.16.0

Additional information

I am testing on the same machine, same code above. Just different nodejs

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