Webfinger: Handle bogus ids better.
authorlain <lain@soykaf.club>
Tue, 25 Aug 2020 15:35:59 +0000 (17:35 +0200)
committerlain <lain@soykaf.club>
Tue, 25 Aug 2020 15:35:59 +0000 (17:35 +0200)
lib/pleroma/web/web_finger/web_finger.ex
test/web/web_finger/web_finger_test.exs

index 71ccf251a8c79cdaf540e6a28193f60e87b6cbc1..c4051e63e40979321304eef1b13614ffcae87f6e 100644 (file)
@@ -149,6 +149,18 @@ defmodule Pleroma.Web.WebFinger do
     end
   end
 
+  defp get_address_from_domain(domain, encoded_account) when is_binary(domain) do
+    case find_lrdd_template(domain) do
+      {:ok, template} ->
+        String.replace(template, "{uri}", encoded_account)
+
+      _ ->
+        "https://#{domain}/.well-known/webfinger?resource=#{encoded_account}"
+    end
+  end
+
+  defp get_address_from_domain(_, _), do: nil
+
   @spec finger(String.t()) :: {:ok, map()} | {:error, any()}
   def finger(account) do
     account = String.trim_leading(account, "@")
@@ -163,16 +175,8 @@ defmodule Pleroma.Web.WebFinger do
 
     encoded_account = URI.encode("acct:#{account}")
 
-    address =
-      case find_lrdd_template(domain) do
-        {:ok, template} ->
-          String.replace(template, "{uri}", encoded_account)
-
-        _ ->
-          "https://#{domain}/.well-known/webfinger?resource=#{encoded_account}"
-      end
-
-    with response <-
+    with address when is_binary(address) <- get_address_from_domain(domain, encoded_account),
+         response <-
            HTTP.get(
              address,
              [{"accept", "application/xrd+xml,application/jrd+json"}]
index f4884e0a2466c720ef615cc2294e89aa303844d8..96fc0bbaa7b1419fe1230468d262c16df41cfca3 100644 (file)
@@ -40,6 +40,11 @@ defmodule Pleroma.Web.WebFingerTest do
   end
 
   describe "fingering" do
+    test "returns error for nonsensical input" do
+      assert {:error, _} = WebFinger.finger("bliblablu")
+      assert {:error, _} = WebFinger.finger("pleroma.social")
+    end
+
     test "returns error when fails parse xml or json" do
       user = "invalid_content@social.heldscal.la"
       assert {:error, %Jason.DecodeError{}} = WebFinger.finger(user)