PAC to the rescue
You can configure Firefox to block access to localhost using a Proxy Auto-Conf (PAC) file. It wouldn't work with Chromium because it shares proxy-configuration with the host operating system.But it will keep random websites from connecting to or port-scanning services on localhost if you use Firefox! Details here: https://www.ctrl.blog/entry/block-localhost-port-scans.html