From 9f3cb38012281c596d1aa8c479f07362fa58dacb Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sat, 2 Mar 2019 12:22:02 +0000 Subject: [PATCH] helpers: use AutoLinker to validate URIs as well as the other tests --- lib/pleroma/web/rich_media/helpers.ex | 5 ++++- test/web/rich_media/helpers_test.exs | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/web/rich_media/helpers.ex b/lib/pleroma/web/rich_media/helpers.ex index fc9cbc868..ba57171d3 100644 --- a/lib/pleroma/web/rich_media/helpers.ex +++ b/lib/pleroma/web/rich_media/helpers.ex @@ -12,7 +12,10 @@ defp validate_page_url(nil), do: :error 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]), diff --git a/test/web/rich_media/helpers_test.exs b/test/web/rich_media/helpers_test.exs index 9285f078d..60d93768f 100644 --- a/test/web/rich_media/helpers_test.exs +++ b/test/web/rich_media/helpers_test.exs @@ -20,7 +20,27 @@ test "refuses to crawl incomplete URLs" 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