szmarczak / cacheable-lookup

A cacheable dns.lookup(…) that respects TTL :tada:
MIT License
193 stars 29 forks source link

Fallback can incorrectly fail to resolve with some /etc/hosts configurations #42

Closed pimterry closed 3 years ago

pimterry commented 3 years ago

Repro:

Having only IPv4 localhost defined but no IPv6 might seem contrived, but it's actually the default in many places, including all Ubuntu releases (see https://gist.github.com/ghoneycutt/e531984406b4b86ace687ea8958a6dc3). localhost can still be resolved to ::1 for IPv6 on these machines, it's just that it's handled elsewhere (no idea why).

This means that on most Ubuntu machines (and probably others) http.get('localhost', { family: 6, lookup: cacheableLookup.lookup }) with a DNS server configured will always incorrectly fail to resolve.

This happens because:

That last step might seem like bad Node.js behaviour, but it's also true in Python apparently so I expect it's something that comes from Linux's DNS resolution itself.

I think this will affect any lookup where only an IPv4 or IPv6 address is available in the hosts file - requests for the other family will simply fail unexpectedly. Haven't tested other OS, no idea if this can happen elsewhere too.

szmarczak commented 3 years ago

Good catch! Thanks for the very detailed issue, I can reproduce this locally.

I think the solution is to duplicate the dns.lookup call for now, so it gets queried separately for IPv4 and IPv6.

szmarczak commented 3 years ago

Released 6.0.2 :tada:

pimterry commented 3 years ago

Amazing, thanks for the quick merge & release! :tada: