axios icon indicating copy to clipboard operation
axios copied to clipboard

[NodeJS] getaddrinfo ENOTFOUND from Host header not requested URL

Open jakguru opened this issue 3 years ago • 3 comments

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

jakguru avatar Nov 20 '22 22:11 jakguru

+1

vfa-tanna avatar May 18 '23 07:05 vfa-tanna

+2, i'm seeing the same error

yeison-jeeves avatar Jul 04 '23 04:07 yeison-jeeves

+3 same issue

botstar-tra avatar Aug 02 '24 10:08 botstar-tra

+4 same issue

huugoncalves avatar Aug 30 '24 13:08 huugoncalves

+5

markcoders1 avatar Feb 21 '25 21:02 markcoders1

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

jasonsaayman avatar Aug 04 '25 19:08 jasonsaayman