From 8ab3d6d1efb5fdaaa406413adf39d0c1079a7eb4 Mon Sep 17 00:00:00 2001 From: Alex Martsinovich Date: Fri, 10 Jan 2025 11:04:50 -0800 Subject: [PATCH] Updte DNS module and DNSResolver behaviour --- guides/migrating_to_1.0.md | 12 ++++++++++++ lib/safeurl/dns_resolver.ex | 3 +-- lib/safeurl/safeurl.ex | 2 +- mix.exs | 2 +- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/guides/migrating_to_1.0.md b/guides/migrating_to_1.0.md index d83203c..d584824 100644 --- a/guides/migrating_to_1.0.md +++ b/guides/migrating_to_1.0.md @@ -28,3 +28,15 @@ iex> Application.put_env(:safeurl, :detailed_error, false) iex> SafeURL.validate("http://localhost") {:error, :restricted} ``` + +### Change modules implementing `SafeURL.DNSResolver` behaviour + +If you have a module that implements `SafeURL.DNSResolver` behaviour, note that +`DNSResolver.resolve/1` signature has changed. Specifically, the ok tuple should now +always return a list of IPs. A single IP is not a valid return type: + +```elixir +# DNS is the default implementation +iex> DNS.resolve("wikipedia.org") +{:ok, [{198, 35, 26, 96}]} +``` \ No newline at end of file diff --git a/lib/safeurl/dns_resolver.ex b/lib/safeurl/dns_resolver.ex index a5747db..14648f1 100644 --- a/lib/safeurl/dns_resolver.ex +++ b/lib/safeurl/dns_resolver.ex @@ -69,6 +69,5 @@ defmodule SafeURL.DNSResolver do """ - @type resolution :: :inet.ip() | [:inet.ip()] - @callback resolve(host :: binary()) :: {:ok, resolution()} | {:error, atom()} + @callback resolve(host :: String.t()) :: {:ok, list()} | {:error, :inet_res.res_error()} end diff --git a/lib/safeurl/safeurl.ex b/lib/safeurl/safeurl.ex index 95b820c..14f7ff4 100644 --- a/lib/safeurl/safeurl.ex +++ b/lib/safeurl/safeurl.ex @@ -239,7 +239,7 @@ defmodule SafeURL do {:error, :einval} -> # TODO: safely handle multiple IPs/round-robin DNS case dns_module.resolve(hostname) do - {:ok, ips} -> ips |> List.wrap() |> List.first() + {:ok, [ip | _]} -> ip {:error, _reason} -> nil end end diff --git a/mix.exs b/mix.exs index 5e5c15f..3793174 100644 --- a/mix.exs +++ b/mix.exs @@ -54,7 +54,7 @@ defmodule SafeURL.MixProject do [ {:httpoison, "~> 1.0 or ~> 2.0", optional: true}, {:inet_cidr, "~> 1.0 and >= 1.0.6"}, - {:dns, "~> 2.2"}, + {:dns, "~> 2.4"}, {:tesla, "~> 1.0", optional: true}, {:ex_doc, ">= 0.0.0", only: :dev, runtime: false} ]