Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 24, 2021 03:01 am GMT

Firefox and Chrome resolve any localhost domain (*.localhost) to loopback address

I was reading this reddit's thread and this comment caught my interest:-

Chrome and Firefox also consider "*.localhost" as secure so you can develop multiple websites with different service workers. They automatically resolve to "localhost" so it's very handy.

I didn't know this and after trying myself on Firefox, that's turn out to be true. I checked my /etc/hosts to make sure I didn't have the name defined that and also checked via host command and dig as well. Both returned domain not found result.

host web01.localhostHost web01.localhost not found: 3(NXDOMAIN)

So it pretty sure coming from Firefox itself. Firefox also has this pretty handy dns lookup tools (accessible via about:networking#dns):-

Alt Text

I can't find much information about this so why not just look directly in the source code? After some googling, I guess the networking portion for Firefox is under the directory called netwerk and made my educated guess that the code could be in nsHostResolver.cpp. Then using browser's Find on page for "localhost", voila! The code looks like this:-

// Check if we have a localhost domain, if so hardcode to loopback    if (IS_ADDR_TYPE(type) && IsLoopbackHostname(host)) {      nsresult rv;      RefPtr<nsHostRecord> result = InitLoopbackRecord(key, &rv);      if (NS_WARN_IF(NS_FAILED(rv))) {        return rv;      }      MOZ_ASSERT(result);      aCallback->OnResolveHostComplete(this, result, NS_OK);      return NS_OK;    }

https://github.com/mozilla/gecko-dev/blob/master/netwerk/dns/nsHostResolver.cpp#L1031


Original Link: https://dev.to/k4ml/firefox-and-chrome-resolve-any-localhost-domain-localhost-to-loopback-address-26d9

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To