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 v…


This content originally appeared on DEV Community and was authored by Kamal Mustafa

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.localhost
Host 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


This content originally appeared on DEV Community and was authored by Kamal Mustafa


Print Share Comment Cite Upload Translate Updates
APA

Kamal Mustafa | Sciencx (2021-05-24T03:01:43+00:00) Firefox and Chrome resolve any localhost domain (*.localhost) to loopback address. Retrieved from https://www.scien.cx/2021/05/24/firefox-and-chrome-resolve-any-localhost-domain-localhost-to-loopback-address/

MLA
" » Firefox and Chrome resolve any localhost domain (*.localhost) to loopback address." Kamal Mustafa | Sciencx - Monday May 24, 2021, https://www.scien.cx/2021/05/24/firefox-and-chrome-resolve-any-localhost-domain-localhost-to-loopback-address/
HARVARD
Kamal Mustafa | Sciencx Monday May 24, 2021 » Firefox and Chrome resolve any localhost domain (*.localhost) to loopback address., viewed ,<https://www.scien.cx/2021/05/24/firefox-and-chrome-resolve-any-localhost-domain-localhost-to-loopback-address/>
VANCOUVER
Kamal Mustafa | Sciencx - » Firefox and Chrome resolve any localhost domain (*.localhost) to loopback address. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/24/firefox-and-chrome-resolve-any-localhost-domain-localhost-to-loopback-address/
CHICAGO
" » Firefox and Chrome resolve any localhost domain (*.localhost) to loopback address." Kamal Mustafa | Sciencx - Accessed . https://www.scien.cx/2021/05/24/firefox-and-chrome-resolve-any-localhost-domain-localhost-to-loopback-address/
IEEE
" » Firefox and Chrome resolve any localhost domain (*.localhost) to loopback address." Kamal Mustafa | Sciencx [Online]. Available: https://www.scien.cx/2021/05/24/firefox-and-chrome-resolve-any-localhost-domain-localhost-to-loopback-address/. [Accessed: ]
rf:citation
» Firefox and Chrome resolve any localhost domain (*.localhost) to loopback address | Kamal Mustafa | Sciencx | https://www.scien.cx/2021/05/24/firefox-and-chrome-resolve-any-localhost-domain-localhost-to-loopback-address/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.