HiddenServicePort currently connects to 127.0.0.1 by default, but this will fail in configs where localhost is somewhere else in 127.0.0.0/8 or is [::1]. (Such as BSD jails.)
Instead, we can:
resolve "localhost", and check that it's in 127.0.0.0/8 or [::1], and use the IPv4 address first for compatibility with existing configurations
default to 127.0.0.1 if that exists
default to [::1] if 127.0.0.1 does not exist
This is not a security issue, as it results in a failed hidden service connection on unusual configs. It's a minor usability issue.
I think there's a better algorithm here that doesn't require address resolution:
check for interface addresses in 127/8 and ::1
try to connect to an address in 127/8
if there are multiple addresses, choose the one closest to 127.0.0.1
if no IPv4 or IPv6 addresses can be discovered on the system, try 127.0.0.1
if the first connection fails , try to connect to ::1 if
::1 is present on the system, or
no IPv4 or IPv6 addresses can be discovered on the system
This supports IPv4 and IPv6, and preserves the existing IPv4 127.0.0.1 default behaviour, and the existing preference for IPv4 over IPv6, yet also supports IPv6-only systems.
If both connections fail, log a warning that tells the user to use AF_UNIX if their system and backend server support it, or to supply an explicit IP address.
somewhere else in 127.0.0.0/8
Plus
It's a common practice to use addr of current network interface by default (on some systems)
Can it be automated for all local connections? (in tor-client, etc)
somewhere else in 127.0.0.0/8
Plus
It's a common practice to use addr of current network interface by default (on some systems)
Can it be automated for all local connections? (in tor-client, etc)
If the current network interface is not a loopback interface, it's security risk, so we want users to configure it explicitly.
If the current network interface is a loopback interface, then sure. But I think we might have trouble finding a portable API for discovering the current loopback network interface.
Do you know of any?
Also, there's value in cross-platform consistency. Using the same algorithm on different platforms would provide this. I suggested localhost IPv4, localhost IPv6, 127.0.0.1, and [::1], but we might want to consider other 127/8 addresses as well.
We should let the user say what address to point the onion service connections to.
I've seen cases where a local resolve attempt for localhost went out to Comcast's dns servers, which helpfully told me that localhost was 127.0.0.1, so then my application correctly went there.
Let's leave DNS the heck out of local computer decisions. :)