Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gethostbyname "localhost" is not a reliable solution for localhost addresses #1167

Closed
andrew-aladev opened this issue Dec 2, 2016 · 1 comment · Fixed by #1318
Closed
Labels

Comments

@andrew-aladev
Copy link

andrew-aladev commented Dec 2, 2016

Steps to reproduce

Related jruby issue.

class Server < Sinatra::Base
  configure do
    set :server, :puma
    set :bind, "localhost"
  end
end
  1. rvm use jruby-9.1.6.0

  2. Try to start sinatra app with jruby.

Expected behavior

Server will be accessible on both 127.0.0.1 and ::1.

Actual behavior

Server is accessible only on 127.0.0.1

System configuration

Ruby version: jruby 9.1.6.0 (2.3.1) 2016-11-09 0150a76 OpenJDK 64-Bit Server VM 25.101-b13 on 1.8.0_101-b13 +jit [linux-x86_64]

Fix

You are using TCPSocket.gethostbyname "localhost" here to get the list of localhost addresses.

In MRI ruby we have a comment:

 * Note that it is not guaranteed to be able to convert to IP address using gethostbyname, getaddrinfo, etc.
 * If you need local IP address, use Socket.ip_address_list.

We can use something like:

Socket.ip_address_list.select do |address|
  if address.ipv4?
    not address.ipv4_multicast? and not address.ipv4_private?
  else
    not address.ipv6_linklocal?
  end
end
.map do |address|
  address.ip_address
end

Thank you.

@nateberkopec
Copy link
Member

If you could provide some feedback on #1318 please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants