diff options
-rw-r--r-- | lib/pleroma/web/rich_media/parser/card.ex | 15 | ||||
-rw-r--r-- | test/pleroma/web/rich_media/parser/card_test.exs | 59 |
2 files changed, 71 insertions, 3 deletions
diff --git a/lib/pleroma/web/rich_media/parser/card.ex b/lib/pleroma/web/rich_media/parser/card.ex index b29db730b..abae06ab9 100644 --- a/lib/pleroma/web/rich_media/parser/card.ex +++ b/lib/pleroma/web/rich_media/parser/card.ex @@ -40,8 +40,8 @@ defmodule Pleroma.Web.RichMedia.Parser.Card do html: sanitize_html(oembed["html"]), width: oembed["width"], height: oembed["height"], - image: get_image(oembed) |> proxy(), - embed_url: oembed["url"] |> proxy() + image: get_image(oembed) |> fix_uri(url) |> proxy(), + embed_url: oembed["url"] |> fix_uri(url) |> proxy() } |> validate() end @@ -56,7 +56,7 @@ defmodule Pleroma.Web.RichMedia.Parser.Card do type: "link", provider_name: uri.host, provider_url: "#{uri.scheme}://#{uri.host}", - image: get_image(embed) |> proxy() + image: get_image(embed) |> fix_uri(url) |> proxy() } |> validate() end @@ -112,6 +112,15 @@ defmodule Pleroma.Web.RichMedia.Parser.Card do defp stringify_keys(%{} = map), do: Map.new(map, fn {k, v} -> {Atom.to_string(k), v} end) + def fix_uri("http://" <> _ = uri, _base_uri), do: uri + def fix_uri("https://" <> _ = uri, _base_uri), do: uri + def fix_uri("/" <> _ = uri, base_uri), do: URI.merge(base_uri, uri) |> URI.to_string() + + def fix_uri(uri, base_uri) when is_binary(uri), + do: URI.merge(base_uri, "/#{uri}") |> URI.to_string() + + def fix_uri(_uri, _base_uri), do: nil + defp proxy(url) when is_binary(url), do: Pleroma.Web.MediaProxy.url(url) defp proxy(_), do: nil diff --git a/test/pleroma/web/rich_media/parser/card_test.exs b/test/pleroma/web/rich_media/parser/card_test.exs index e09dfa6db..d85491f2e 100644 --- a/test/pleroma/web/rich_media/parser/card_test.exs +++ b/test/pleroma/web/rich_media/parser/card_test.exs @@ -32,6 +32,19 @@ defmodule Pleroma.Web.RichMedia.Parser.CardTest do assert Card.parse(embed) == {:ok, expected} end + + test "converts URL paths into absolute URLs" do + embed = %Embed{ + url: "https://spam.com/luigi", + title: "Watch Luigi not doing anything", + meta: %{ + "og:image" => "/uploads/weegee.jpeg" + } + } + + {:ok, card} = Card.parse(embed) + assert card.image == "https://spam.com/uploads/weegee.jpeg" + end end describe "validate/1" do @@ -44,5 +57,51 @@ defmodule Pleroma.Web.RichMedia.Parser.CardTest do assert {:ok, ^card} = Card.validate(card) end + + test "errors for video embeds without html" do + embed = %Embed{ + url: "https://spam.com/xyz", + oembed: %{ + "type" => "video", + "title" => "Yeeting soda cans" + } + } + + assert {:error, {:invalid_metadata, _}} = Card.validate(embed) + end + end + + describe "fix_uri/2" do + setup do: %{base_uri: "https://benis.xyz/hello/fam"} + + test "two full URLs", %{base_uri: base_uri} do + uri = "https://benis.xyz/images/pic.jpeg" + assert Card.fix_uri(uri, base_uri) == uri + end + + test "URI with leading slash", %{base_uri: base_uri} do + uri = "/images/pic.jpeg" + expected = "https://benis.xyz/images/pic.jpeg" + assert Card.fix_uri(uri, base_uri) == expected + end + + test "URI without leading slash", %{base_uri: base_uri} do + uri = "images/pic.jpeg" + expected = "https://benis.xyz/images/pic.jpeg" + assert Card.fix_uri(uri, base_uri) == expected + end + + test "nil URI", %{base_uri: base_uri} do + assert Card.fix_uri(nil, base_uri) == nil + end + + # https://github.com/elixir-lang/elixir/issues/10771 + test "Elixir #10771", _ do + uri = + "https://images.macrumors.com/t/4riJyi1XC906qyJ41nAfOgpvo1I=/1600x/https://images.macrumors.com/article-new/2020/09/spatialaudiofeature.jpg" + + base_uri = "https://www.macrumors.com/guide/apps-support-apples-spatial-audio-feature/" + assert Card.fix_uri(uri, base_uri) == uri + end end end |