Skip to content

fish_config: Bind to localhost on an ephemeral port - #1441

Closed
amluto wants to merge 1 commit into
fish-shell:masterfrom
amluto:bind-localhost
Closed

amluto wants to merge 1 commit into
fish-shell:masterfrom
amluto:bind-localhost

Conversation

@amluto

@amluto amluto commented Apr 28, 2014

Copy link
Copy Markdown
Contributor

Binding to all interfaces opens up an unnecessary attack surface and
alarms users of netstat -lt. The iterative binding hack is also ugly

Instead, just bind to (::1, 0) and read off the assigned ephemeral
port. Then aim the web browser at http://[::1]:PORT/.

This may cause issues on systems without working IPv6 loopback.
The current code was already broken on systems without IPv6,
so I don't expect this to be a problem in practice. If it ends
up being problematic, it wouldn't be too hard to fall back to
IPv4.

@ridiculousfish

Copy link
Copy Markdown
Member

On my system, this results in lots of errors from angular.js, and a weird alien-looking URL. Is there really not an IPv6 hostname equivalent to localhost?

@amluto

amluto commented May 4, 2014

Copy link
Copy Markdown
Contributor Author

Hmm. localhost6 seems to work for me. Is there any easy way to find out how portable it is?

Binding to all interfaces opens up an unnecessary attack surface and
alarms users of netstat -lt.  The iterative binding hack is also ugly

Instead, just bind to (::1, 0) and read off the assigned ephemeral
port and aim the web browser at http://localhost6:PORT/.

This may cause issues on systems without working IPv6 loopback.
The current code was already broken on systems without IPv6,
so I don't expect this to be a problem in practice.  If it ends
up being problematic, it wouldn't be too hard to fall back to
IPv4.
@amluto

amluto commented May 12, 2014

Copy link
Copy Markdown
Contributor Author

I updated the PR with the localhost6 variant.

@zanchey

zanchey commented May 13, 2014

Copy link
Copy Markdown
Member

Sadly, localhost6 does not resolve on Debian Linux (which uses ip6-localhost), Solaris 11 (which uses localhost), or FreeBSD (which uses localhost).

I keep getting bogged down in implementation details, so let me state what I think the goal here is:
"run a server that only accepts connections from the local host, whether the system supports IPv4, IPv6 or a combination of the two".

With the original implementation shipped in 2.0.0 and 2.1.0, we bound to "", which accepted connections from anywhere and may or may not have supported IPv6 (e.g. on systems with net.ipv6.bindv6only or similar was set).

In 6d74978, the bind address was changed to 127.0.0.1. This accepted connections only from the local host, but only over IPv4 and would have required fallback in the case that localhost was specified as ::1 127.0.0.1. There was hope that this would

In 44b35f7, the bind address was changed to ::, but a deliberate check on the remote peer address was added. This - I think - fulfils the functional requirements we are after, although I'm not sure how it works on systems without loopback IPv6 (if these exist anywhere), or if bindv6only is set.

The changes in this PR change the bind address to ::1. This prevents IPv4 connections, and there is no portable way of specifying the hostname (see above). I'm surprised by the angularjs errors that @ridiculousfish is seeing, although I haven't had a chance to try myself.

Binding to the localhost name will not help, as systems which do not have ::1 listed will fail to create the socket as there is no valid address for an AF_INET6 socket.

Really the best thing to do is to bind to both 127.0.0.1 and ::1, but this looks irritatingly difficult to do within the current Python framework.

@amluto

amluto commented May 14, 2014

Copy link
Copy Markdown
Contributor Author

@ridiculousfish, can you post the angularjs error messages? Given the lack of standardization of localhost6, [::1] may be the way to go.

Alternatively, it wouldn't be that hard to make this thing create two listening sockets.

@amluto

amluto commented May 14, 2014

Copy link
Copy Markdown
Contributor Author

Correction: having two listening sockets is a PITA -- they might end up with different ports.

Another option would be to do socket.getaddrinfo('localhost', None, 0, socket.SOCK_STREAM) and to bind to whatever gets returned.

@zanchey

zanchey commented Jun 28, 2014

Copy link
Copy Markdown
Member

angular/angular.js#2864 tracks the problem with angularJS.

As it stands, I don't think we can take this patch for 2.1.1; I don't want to ship a new release of AngularJS in a point release even if it did work. I considered reverting 44b35f7 (and reopening #1324) to take us back to 6d74978 to possibly fix #673, but I think the attack surface is relatively small and the code is quite simple. Unless there is a demonstrated problem with the existing code I will not be merging this for now, but I appreciate the work that's gone into it and I agree that architecturally it's the best solution, so perhaps we can review it in the future.

@zanchey

zanchey commented Jun 28, 2014

Copy link
Copy Markdown
Member

(cc @anders @g--n from the other pull requests/issues)

@amluto

amluto commented Jun 28, 2014

Copy link
Copy Markdown
Contributor Author

Would the approach of binding to whatever localhost resolves to work? That shouldn't require an AngularJS upgrade.

@anders

anders commented Jun 28, 2014

Copy link
Copy Markdown
Contributor

I think supporting IPv6 only is ugly. If you want to listen on localhost while still supporting both address families, I believe the only way is to set up two listening sockets, one on 127.0.0.1 and one on ::1. This is of course not as neat/simple as the current solution (hack?). Limiting it to IPv6 only will lead to problems as there are many (broken, imo) systems out there that don't have IPv6 enabled/compiled in. (edit again: just realized that listening on :: would break on these systems as well ...)

The IPv6 equivalent to localhost is "localhost". It should be both ::1 and 127.0.0.1. "localhost6" is not something standard.

@zanchey: bindv6only is not common, but even if it was: I believe those systems do support IPv6 and ::1 will work fine. It was defined for a while by default in Debian, but they stopped doing that years ago...

@amluto

amluto commented Jun 28, 2014

Copy link
Copy Markdown
Contributor Author

To clarify: I was just going to pick the highest priority hit (IPv4, or
IPv6) for localhost.
On Jun 28, 2014 11:48 AM, "Anders Bergh" [email protected] wrote:

I think supporting IPv6 only is ugly. If you want to listen on localhost I
believe the only way is to set up 2 listening sockets, one on 127.0.0.1 and
one on ::1. This is of course not as neat/simple as the current solution
(hack?). Limiting it to IPv6 only will lead to problems as there are many
(broken, imo) systems out there that don't have IPv6 enabled/compiled in.

The IPv6 equivalent to localhost is "localhost". It should be both ::1 and
127.0.0.1. "localhost6" is not something standard.

—
Reply to this email directly or view it on GitHub
#1441 (comment)
.

@anders

anders commented Jun 29, 2014

Copy link
Copy Markdown
Contributor

Supporting only one is not that nice either, tbh

@amluto

amluto commented Jun 29, 2014

Copy link
Copy Markdown
Contributor Author

Why does it matter? If the browser is going to try IPv6 for localhost, then supporting IPv6 should be all that's needed. If the browser is going to try IPv4, then supporting IPv6 is pointless, since it won't get used.

@faho

faho commented Sep 2, 2015

Copy link
Copy Markdown
Member

Sorry, this has become outdated. If you are still interested, please comment and I'll reopen.

@faho faho closed this Sep 2, 2015
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Apr 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants