helpers: use AutoLinker to validate URIs as well as the other tests
authorWilliam Pitcock <nenolod@dereferenced.org>
Sat, 2 Mar 2019 12:22:02 +0000 (12:22 +0000)
committerWilliam Pitcock <nenolod@dereferenced.org>
Mon, 4 Mar 2019 18:31:49 +0000 (18:31 +0000)
lib/pleroma/web/rich_media/helpers.ex
test/web/rich_media/helpers_test.exs

index fc9cbc868d8f038eaef8f22b25369de146d30f90..ba57171d34fb0b24e44a23e13f7553cb0dae540f 100644 (file)
@@ -12,7 +12,10 @@ defmodule Pleroma.Web.RichMedia.Helpers do
   defp validate_page_url(%URI{authority: nil}), do: :error
   defp validate_page_url(%URI{scheme: nil}), do: :error
   defp validate_page_url(%URI{}), do: :ok
-  defp validate_page_url(page_url), do: URI.parse(page_url) |> validate_page_url
+
+  defp validate_page_url(page_url) do
+    AutoLinker.Parser.is_url?(page_url, true) && URI.parse(page_url) |> validate_page_url
+  end
 
   def fetch_data_for_activity(%Activity{} = activity) do
     with true <- Pleroma.Config.get([:rich_media, :enabled]),
index 9285f078d3063916fd67646cb5eab9d8dc11df82..60d93768f20f5b1fd5cd968b12a279d64d770e34 100644 (file)
@@ -20,7 +20,27 @@ defmodule Pleroma.Web.RichMedia.HelpersTest do
         "content_type" => "text/markdown"
       })
 
+    Pleroma.Config.put([:rich_media, :enabled], true)
+
+    assert %{} == Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
+
+    Pleroma.Config.put([:rich_media, :enabled], false)
+  end
+
+  test "refuses to crawl malformed URLs" do
+    user = insert(:user)
+
+    {:ok, activity} =
+      CommonAPI.post(user, %{
+        "status" => "[test](example.com[]/ogp)",
+        "content_type" => "text/markdown"
+      })
+
+    Pleroma.Config.put([:rich_media, :enabled], true)
+
     assert %{} == Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
+
+    Pleroma.Config.put([:rich_media, :enabled], false)
   end
 
   test "crawls valid, complete URLs" do