[NodeJS] getaddrinfo ENOTFOUND from Host header not requested URL
Describe the bug
In NodeJS v16.17.1, when making a request to an IP based host and defining the host header, the DNS validation / lookup is done for the value of the host header instead of IP / hostname set in the URL of the request. In cases where the domain is not a valid domain, or the DNS is not setup appropriately, a getaddrinfo ENOTFOUND is thrown. This affects both requests made with baseURL set and those without.
To Reproduce
In NodeJS 16: Initialize a client with the following configuration:
const client = axios.create({
baseURL: 'http://127.0.0.1',
headers: {
host: 'somefakedomain.com',
origin: 'https://somefakedomain.com/'
},
})
Make a request using any method to either a relative path or an absolute path:
client.get('/').then(({status}) => {
// never reached
}).catch((error) => {
console.log(error.message) // outputs "getaddrinfo ENOTFOUND somefakedomain.com"
})
client.get('http://127.0.0.1/').then(({status}) => {
// never reached
}).catch((error) => {
console.log(error.message) // outputs "getaddrinfo ENOTFOUND somefakedomain.com"
})
Code snippet
const client = axios.create({
baseURL: 'http://127.0.0.1',
headers: {
host: 'somefakedomain.com',
origin: 'https://somefakedomain.com/'
},
})
client.get('/').then(({status}) => {
// never reached
}).catch((error) => {
console.log(error.message) // outputs "getaddrinfo ENOTFOUND somefakedomain.com"
})
client.get('http://127.0.0.1/').then(({status}) => {
// never reached
}).catch((error) => {
console.log(error.message) // outputs "getaddrinfo ENOTFOUND somefakedomain.com"
})
Expected behavior
If behavior is meant to be consistent with command line utilities like cURL, the DNS lookup should be skipped since the request is being made to a specific IP address, thus making a request
Axios Version
1.1.3
Adapter Version
HTTP
Browser
No response
Browser Version
No response
Node.js Version
v16.17.1
OS
OSX 12.6.0
Additional Library Versions
No response
Additional context/Screenshots
No response
+1
+2, i'm seeing the same error
+3 same issue
+4 same issue
+5
using this exact example; which is a working derivitave:
const axios = require('axios');
const client = axios.create({
baseURL: 'http://3.232.35.54',
headers: {
host: 'httpbin.org',
origin: 'https://httpbin.org/'
},
})
client.get('/').then(({ status }) => {
// never reached
}).catch((error) => {
console.log(error.message) // outputs "getaddrinfo ENOTFOUND somefakedomain.com"
})
client.get('http://3.232.35.54/').then(({ status }) => {
// never reached
}).catch((error) => {
console.log(error.message) // outputs "getaddrinfo ENOTFOUND somefakedomain.com"
})
i get no errors, if you are still getting this error please open a new issue with a reproducible example