Validate hostname against EPMD (#1305)

This commit is contained in:
José Valim 2022-07-29 10:55:13 +02:00 committed by GitHub
parent 1b978b43f4
commit 09c60abd5e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -115,58 +115,35 @@ defmodule Livebook.Application do
# See https://github.com/livebook-dev/livebook/issues/302
defp validate_hostname_resolution!() do
unless Livebook.Config.longname() do
hostname = Livebook.Utils.node_host() |> to_charlist()
case :inet.gethostbyname(hostname) do
{:error, :nxdomain} ->
invalid_hostname!("Your hostname \"#{hostname}\" does not resolve to an IP address")
# We only try the first address, so that's the one we validate.
{:ok, hostent(h_addrtype: :inet, h_addr_list: [address | _])} ->
unless inet_loopback?(address) or inet_if?(address) do
invalid_hostname!(
"Your hostname \"#{hostname}\" does not resolve to a loopback address (127.0.0.0/8)"
)
end
[nodename, hostname] = node() |> Atom.to_charlist() |> :string.split('@')
with {:ok, nodenames} <- :erl_epmd.names(hostname),
true <- List.keymember?(nodenames, nodename, 0) do
:ok
else
_ ->
:ok
Livebook.Config.abort!("""
Your hostname \"#{hostname}\" does not resolve to a loopback address (127.0.0.0/8),
which indicates something wrong in your OS configuration, or EPMD is not running.
Make sure your computer's name resolves locally or start Livebook using \
a long distribution name. Please try one of the fixes below:
* If you are using the Livebook App, please open up a bug report.
* If you are using Livebook's CLI, consider using longnames:
livebook server --name livebook@127.0.0.1
* If you are running it from source, do instead:
MIX_ENV=prod elixir --name livebook@127.0.0.1 -S mix phx.server
""")
end
end
end
defp inet_loopback?(address) do
match?({127, _, _, _}, address)
end
defp inet_if?(address) do
case :inet.getifaddrs() do
{:ok, addrs} -> Enum.any?(addrs, fn {_name, flags} -> {:addr, address} in flags end)
_ -> false
end
end
@spec invalid_hostname!(String.t()) :: no_return()
defp invalid_hostname!(prelude) do
Livebook.Config.abort!("""
#{prelude}, which indicates something wrong in your OS configuration.
Make sure your computer's name resolves locally or start Livebook using \
a long distribution name. Please try one of the fixes below:
* If you are using the Livebook App, please open up a bug report.
* If you are using Livebook's CLI, you can:
livebook server --name livebook@127.0.0.1
* If you are running it from source, do instead:
MIX_ENV=prod elixir --name livebook@127.0.0.1 -S mix phx.server
""")
end
defp set_cookie() do
cookie = Application.fetch_env!(:livebook, :cookie)
Node.set_cookie(cookie)