Page MenuHomePhorge

No OneTemporary

Authored By
Unknown
Size
17 KB
Referenced Files
None
Subscribers
None
diff --git a/changelog.d/rich-media-oembed-image.fix b/changelog.d/rich-media-oembed-image.fix
new file mode 100644
index 000000000..47435a5fa
--- /dev/null
+++ b/changelog.d/rich-media-oembed-image.fix
@@ -0,0 +1 @@
+Rich media: use the oEmbed thumbnail as the card image
diff --git a/changelog.d/rich-media-oembed-json.fix b/changelog.d/rich-media-oembed-json.fix
new file mode 100644
index 000000000..2a8b68792
--- /dev/null
+++ b/changelog.d/rich-media-oembed-json.fix
@@ -0,0 +1 @@
+Rich media: accept JSON responses when fetching oEmbed data
diff --git a/changelog.d/rich-media-oembed-url-validation.security b/changelog.d/rich-media-oembed-url-validation.security
new file mode 100644
index 000000000..3ff029968
--- /dev/null
+++ b/changelog.d/rich-media-oembed-url-validation.security
@@ -0,0 +1 @@
+Rich media: validate oEmbed URLs found on pages like the page URL itself
diff --git a/changelog.d/rich-media-youtube-oembed.fix b/changelog.d/rich-media-youtube-oembed.fix
new file mode 100644
index 000000000..8824717a6
--- /dev/null
+++ b/changelog.d/rich-media-youtube-oembed.fix
@@ -0,0 +1 @@
+Rich media: get YouTube cards from the YouTube oEmbed endpoint, as YouTube often blocks page fetches from servers
diff --git a/lib/pleroma/web/rich_media/helpers.ex b/lib/pleroma/web/rich_media/helpers.ex
index 7b4ca677f..748af7e8a 100644
--- a/lib/pleroma/web/rich_media/helpers.ex
+++ b/lib/pleroma/web/rich_media/helpers.ex
@@ -11,21 +11,73 @@ defmodule Pleroma.Web.RichMedia.Helpers do
@type get_errors :: {:error, :body_too_large | :content_type | :head | :get}
- @spec rich_media_get(String.t()) :: {:ok, String.t()} | get_errors()
- def rich_media_get(url) do
+ @doc "Checks that a URL is an https URL on a host rich media may fetch."
+ @spec validate_page_url(URI.t() | binary()) :: :ok | :error
+ def validate_page_url(page_url) when is_binary(page_url) do
+ validate_tld = Config.get([Pleroma.Formatter, :validate_tld])
+
+ page_url
+ |> Linkify.Parser.url?(validate_tld: validate_tld)
+ |> parse_uri(page_url)
+ end
+
+ def validate_page_url(%URI{host: host, scheme: "https"}) do
+ cond do
+ Linkify.Parser.ip?(host) ->
+ :error
+
+ host in Config.get([:rich_media, :ignore_hosts], []) ->
+ :error
+
+ get_tld(host) in Config.get([:rich_media, :ignore_tld], []) ->
+ :error
+
+ true ->
+ :ok
+ end
+ end
+
+ def validate_page_url(_), do: :error
+
+ defp parse_uri(true, url) do
+ url
+ |> URI.parse()
+ |> validate_page_url
+ end
+
+ defp parse_uri(_, _), do: :error
+
+ defp get_tld(host) do
+ host
+ |> String.split(".")
+ |> Enum.reverse()
+ |> hd
+ end
+
+ @html_types [{"text", "html"}]
+ @json_types [{"application", "json"}, {"application", "json+oembed"}, {"text", "json"}]
+
+ @doc """
+ Fetches a page for rich media. Pass `accept: :json` to fetch an oEmbed
+ JSON document instead of an HTML page.
+ """
+ @spec rich_media_get(String.t(), keyword()) :: {:ok, String.t()} | get_errors()
+ def rich_media_get(url, opts \\ []) do
+ types = if opts[:accept] == :json, do: @json_types, else: @html_types
+
case Pleroma.HTTP.AdapterHelper.can_stream?() do
- true -> stream(url)
- false -> head_first(url)
+ true -> stream(url, types)
+ false -> head_first(url, types)
end
|> handle_result(url)
end
- defp stream(url) do
+ defp stream(url, types) do
case Pleroma.HTTP.get(url, req_headers(), http_options()) do
{:ok, %Tesla.Env{status: 200, body: stream_body, headers: headers}} ->
result =
try do
- with {_, :ok} <- {:content_type, check_content_type(headers)},
+ with {_, :ok} <- {:content_type, check_content_type(headers, types)},
{_, :ok} <- {:content_length, check_content_length(headers)},
{:read_stream, {:ok, body}} <- {:read_stream, read_stream(stream_body)} do
{:ok, body}
@@ -48,10 +100,10 @@ defmodule Pleroma.Web.RichMedia.Helpers do
end
end
- defp head_first(url) do
+ defp head_first(url, types) do
with {_, {:ok, %Tesla.Env{status: 200, headers: headers}}} <-
{:head, Pleroma.HTTP.head(url, req_headers(), http_options())},
- {_, :ok} <- {:content_type, check_content_type(headers)},
+ {_, :ok} <- {:content_type, check_content_type(headers, types)},
{_, :ok} <- {:content_length, check_content_length(headers)},
{_, {:ok, %Tesla.Env{status: 200, body: body}}} <-
{:get, Pleroma.HTTP.get(url, req_headers(), http_options())} do
@@ -86,11 +138,13 @@ defmodule Pleroma.Web.RichMedia.Helpers do
end
end
- defp check_content_type(headers) do
+ defp check_content_type(headers, types) do
case List.keyfind(headers, "content-type", 0) do
{_, content_type} ->
- case Plug.Conn.Utils.media_type(content_type) do
- {:ok, "text", "html", _} -> :ok
+ with {:ok, type, subtype, _} <- Plug.Conn.Utils.media_type(content_type),
+ true <- {type, subtype} in types do
+ :ok
+ else
_ -> {:error, content_type}
end
diff --git a/lib/pleroma/web/rich_media/parser.ex b/lib/pleroma/web/rich_media/parser.ex
index 9c8ec7a9f..51028612d 100644
--- a/lib/pleroma/web/rich_media/parser.ex
+++ b/lib/pleroma/web/rich_media/parser.ex
@@ -4,6 +4,7 @@
defmodule Pleroma.Web.RichMedia.Parser do
alias Pleroma.Web.RichMedia.Helpers
+ alias Pleroma.Web.RichMedia.Parsers.OEmbed
import Pleroma.Web.Metadata.Utils, only: [scrub_html_and_truncate: 2]
require Logger
@@ -19,7 +20,7 @@ defmodule Pleroma.Web.RichMedia.Parser do
{:ok, map()} | parse_errors() | Helpers.get_errors()
def parse(url) when is_binary(url) do
with {_, true} <- {:config, @config_impl.get([:rich_media, :enabled])},
- {_, :ok} <- {:validate, validate_page_url(url)},
+ {_, :ok} <- {:validate, Helpers.validate_page_url(url)},
{_, {:ok, data}} <- {:parse, parse_url(url)} do
data = Map.put(data, "url", url)
{:ok, data}
@@ -31,6 +32,16 @@ defmodule Pleroma.Web.RichMedia.Parser do
end
defp parse_url(url) do
+ with true <- OEmbed in parsers(),
+ {:ok, data} <- OEmbed.direct_lookup(url),
+ {:ok, data} <- data |> clean_parsed_data() |> check_parsed_data() do
+ {:ok, data}
+ else
+ _ -> parse_page(url)
+ end
+ end
+
+ defp parse_page(url) do
with {:ok, body} <- Helpers.rich_media_get(url),
{:ok, html} <- Floki.parse_document(body) do
html
@@ -77,46 +88,4 @@ defmodule Pleroma.Web.RichMedia.Parser do
do: %{data | "description" => scrub_html_and_truncate(desc, 200)}
defp truncate_desc(data), do: data
-
- @spec validate_page_url(URI.t() | binary()) :: :ok | :error
- defp validate_page_url(page_url) when is_binary(page_url) do
- validate_tld = @config_impl.get([Pleroma.Formatter, :validate_tld])
-
- page_url
- |> Linkify.Parser.url?(validate_tld: validate_tld)
- |> parse_uri(page_url)
- end
-
- defp validate_page_url(%URI{host: host, scheme: "https"}) do
- cond do
- Linkify.Parser.ip?(host) ->
- :error
-
- host in @config_impl.get([:rich_media, :ignore_hosts], []) ->
- :error
-
- get_tld(host) in @config_impl.get([:rich_media, :ignore_tld], []) ->
- :error
-
- true ->
- :ok
- end
- end
-
- defp validate_page_url(_), do: :error
-
- defp parse_uri(true, url) do
- url
- |> URI.parse()
- |> validate_page_url
- end
-
- defp parse_uri(_, _), do: :error
-
- defp get_tld(host) do
- host
- |> String.split(".")
- |> Enum.reverse()
- |> hd
- end
end
diff --git a/lib/pleroma/web/rich_media/parsers/o_embed.ex b/lib/pleroma/web/rich_media/parsers/o_embed.ex
index 35ec14426..995f1c963 100644
--- a/lib/pleroma/web/rich_media/parsers/o_embed.ex
+++ b/lib/pleroma/web/rich_media/parsers/o_embed.ex
@@ -3,11 +3,31 @@
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.RichMedia.Parsers.OEmbed do
+ # YouTube often serves server IPs a captcha redirect or a page without
+ # metadata, while its oEmbed endpoint keeps working. Ask it directly.
+ @youtube_hosts ~w(youtube.com www.youtube.com m.youtube.com music.youtube.com youtu.be)
+ @youtube_endpoint "https://www.youtube.com/oembed"
+
+ @doc """
+ Fetches oEmbed data straight from the provider endpoint for known hosts
+ (YouTube), without loading the page first.
+ """
+ @spec direct_lookup(String.t()) :: {:ok, map()} | :error
+ def direct_lookup(url) do
+ with %URI{host: host} when is_binary(host) <- URI.parse(url),
+ true <- String.downcase(host) in @youtube_hosts do
+ get_oembed_data(@youtube_endpoint <> "?" <> URI.encode_query(url: url, format: "json"))
+ else
+ _ -> :error
+ end
+ end
+
def parse(html, _data) do
with elements = [_ | _] <- get_discovery_data(html),
oembed_url when is_binary(oembed_url) <- get_oembed_url(elements),
- {:ok, oembed_data = %{"html" => html}} <- get_oembed_data(oembed_url) do
- %{oembed_data | "html" => Pleroma.HTML.filter_tags(html)}
+ :ok <- Pleroma.Web.RichMedia.Helpers.validate_page_url(oembed_url),
+ {:ok, oembed_data} <- get_oembed_data(oembed_url) do
+ oembed_data
else
_e -> %{}
end
@@ -22,8 +42,19 @@ defmodule Pleroma.Web.RichMedia.Parsers.OEmbed do
end
defp get_oembed_data(url) do
- with {:ok, json} <- Pleroma.Web.RichMedia.Helpers.rich_media_get(url) do
- Jason.decode(json)
+ with {:ok, json} <- Pleroma.Web.RichMedia.Helpers.rich_media_get(url, accept: :json),
+ {:ok, %{"html" => html} = data} when is_binary(html) <- Jason.decode(json) do
+ {:ok,
+ data
+ |> Map.put("html", Pleroma.HTML.filter_tags(html))
+ |> put_image()}
+ else
+ _ -> :error
end
end
+
+ defp put_image(%{"thumbnail_url" => url} = data) when is_binary(url),
+ do: Map.put_new(data, "image", url)
+
+ defp put_image(data), do: data
end
diff --git a/test/fixtures/rich_media/oembed_internal.html b/test/fixtures/rich_media/oembed_internal.html
new file mode 100644
index 000000000..9da79a445
--- /dev/null
+++ b/test/fixtures/rich_media/oembed_internal.html
@@ -0,0 +1 @@
+<link rel="alternate" type="application/json+oembed" href="http://127.0.0.1:9200/internal.json" title="Internal" />
diff --git a/test/fixtures/rich_media/youtube_oembed.json b/test/fixtures/rich_media/youtube_oembed.json
new file mode 100644
index 000000000..a8037bb02
--- /dev/null
+++ b/test/fixtures/rich_media/youtube_oembed.json
@@ -0,0 +1 @@
+{"title":"Test Video Title","author_name":"Test Channel","author_url":"https://www.youtube.com/@testchannel","type":"video","height":113,"width":200,"version":"1.0","provider_name":"YouTube","provider_url":"https://www.youtube.com/","thumbnail_height":360,"thumbnail_width":480,"thumbnail_url":"https://i.ytimg.com/vi/YOUTUBETEST1/hqdefault.jpg","html":"<iframe width=\"200\" height=\"113\" src=\"https://www.youtube.com/embed/YOUTUBETEST1?feature=oembed\" frameborder=\"0\" allowfullscreen title=\"Test Video Title\"></iframe>"}
diff --git a/test/pleroma/web/rich_media/parser_test.exs b/test/pleroma/web/rich_media/parser_test.exs
index 1f01d657a..e1c377011 100644
--- a/test/pleroma/web/rich_media/parser_test.exs
+++ b/test/pleroma/web/rich_media/parser_test.exs
@@ -77,6 +77,7 @@ defmodule Pleroma.Web.RichMedia.ParserTest do
"cache_age" => 3600,
"flickr_type" => "photo",
"height" => "768",
+ "image" => "https://farm4.staticflickr.com/3040/2362225867_4a87ab8baf_q.jpg",
"html" =>
"<a href=\"https://www.flickr.com/photos/bees/2362225867/\" title=\"Bacon Lollys by \u202E\u202D\u202Cbees\u202C, on Flickr\"><img src=\"https://farm4.staticflickr.com/3040/2362225867_4a87ab8baf_b.jpg\" width=\"1024\" height=\"768\" alt=\"Bacon Lollys\"/></a>",
"license" => "All Rights Reserved",
@@ -97,6 +98,46 @@ defmodule Pleroma.Web.RichMedia.ParserTest do
}}
end
+ describe "YouTube" do
+ test "uses the oEmbed endpoint without fetching the watch page" do
+ # The watch page has no mock: YouTube often answers server IPs with a
+ # captcha redirect, so the card must not depend on it.
+ assert {:ok, data} = Parser.parse("https://www.youtube.com/watch?v=YOUTUBETEST1")
+
+ assert %{
+ "title" => "Test Video Title",
+ "author_name" => "Test Channel",
+ "provider_name" => "YouTube",
+ "image" => "https://i.ytimg.com/vi/YOUTUBETEST1/hqdefault.jpg",
+ "url" => "https://www.youtube.com/watch?v=YOUTUBETEST1"
+ } = data
+ end
+
+ test "works for youtu.be links" do
+ assert {:ok, %{"title" => "Test Video Title", "url" => "https://youtu.be/YOUTUBETEST1"}} =
+ Parser.parse("https://youtu.be/YOUTUBETEST1")
+ end
+
+ test "is skipped when the OEmbed parser is disabled" do
+ clear_config([:rich_media, :parsers], [Pleroma.Web.RichMedia.Parsers.TwitterCard])
+
+ assert {:error, _} = Parser.parse("https://www.youtube.com/watch?v=YOUTUBETEST1")
+ end
+
+ test "falls back to the page when the oEmbed endpoint fails" do
+ assert {:ok, %{"title" => "The Rock"}} =
+ Parser.parse("https://www.youtube.com/watch?v=NOOEMBED")
+ end
+ end
+
+ test "does not fetch oEmbed URLs that fail page URL validation" do
+ assert {:error, :invalid_metadata} = Parser.parse("https://example.com/oembed-internal")
+ end
+
+ test "ignores oEmbed data where html is not a string" do
+ assert {:error, :invalid_metadata} = Parser.parse("https://example.com/oembed-null-html")
+ end
+
test "rejects invalid OGP data" do
assert {:error, _} = Parser.parse("https://example.com/malformed")
end
diff --git a/test/support/http_request_mock.ex b/test/support/http_request_mock.ex
index 94be59536..04d9bdbb9 100644
--- a/test/support/http_request_mock.ex
+++ b/test/support/http_request_mock.ex
@@ -1497,8 +1497,61 @@ defmodule HttpRequestMock do
{:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/oembed.html")}}
end
+ def get("https://example.com/oembed-null-html", _, _, _) do
+ {:ok,
+ %Tesla.Env{
+ status: 200,
+ body: ~s(<link type="application/json+oembed" href="https://example.com/oembed-null.json">)
+ }}
+ end
+
+ def get("https://example.com/oembed-null.json", _, _, _) do
+ {:ok, %Tesla.Env{status: 200, body: ~s({"title": "Null", "html": null})}}
+ end
+
+ def get("https://example.com/oembed-internal", _, _, _) do
+ {:ok,
+ %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/oembed_internal.html")}}
+ end
+
+ def get("http://127.0.0.1:9200/internal.json", _, _, _) do
+ {:ok, %Tesla.Env{status: 200, body: ~s({"title": "secret", "html": "secret"})}}
+ end
+
def get("https://example.com/oembed.json", _, _, _) do
- {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/oembed.json")}}
+ {:ok,
+ %Tesla.Env{
+ status: 200,
+ headers: [{"content-type", "application/json; charset=utf-8"}],
+ body: File.read!("test/fixtures/rich_media/oembed.json")
+ }}
+ end
+
+ @youtube_oembed_json [
+ "https://www.youtube.com/oembed?url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DYOUTUBETEST1&format=json",
+ "https://www.youtube.com/oembed?url=https%3A%2F%2Fyoutu.be%2FYOUTUBETEST1&format=json"
+ ]
+
+ def get(url, _, _, _) when url in @youtube_oembed_json do
+ {:ok,
+ %Tesla.Env{
+ status: 200,
+ headers: [{"content-type", "application/json"}],
+ body: File.read!("test/fixtures/rich_media/youtube_oembed.json")
+ }}
+ end
+
+ def get(
+ "https://www.youtube.com/oembed?url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DNOOEMBED&format=json",
+ _,
+ _,
+ _
+ ) do
+ {:ok, %Tesla.Env{status: 404, body: "Not Found"}}
+ end
+
+ def get("https://www.youtube.com/watch?v=NOOEMBED", _, _, _) do
+ {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/ogp.html")}}
end
def get("https://example.com/twitter-card", _, _, _) do
@@ -1763,11 +1816,15 @@ defmodule HttpRequestMock do
"https://example.com/malformed",
"https://example.com/non-ogp",
"https://example.com/oembed",
- "https://example.com/oembed.json",
+ "https://example.com/oembed-internal",
+ "https://example.com/oembed-null-html",
+ "https://example.com/oembed-null.json",
+ "http://127.0.0.1:9200/internal.json",
"https://example.com/ogp",
"https://example.com/ogp-missing-data",
"https://example.com/ogp-missing-title",
"https://example.com/twitter-card",
+ "https://www.youtube.com/watch?v=NOOEMBED",
"https://google.com/",
"https://pleroma.local/notice/9kCP7V",
"https://yahoo.com/",
@@ -1778,6 +1835,23 @@ defmodule HttpRequestMock do
{:ok, %Tesla.Env{status: 200, body: ""}}
end
+ def head(url, _, _, _) when url in @youtube_oembed_json do
+ {:ok, %Tesla.Env{status: 200, headers: [{"content-type", "application/json"}]}}
+ end
+
+ def head(
+ "https://www.youtube.com/oembed?url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DNOOEMBED&format=json",
+ _,
+ _,
+ _
+ ) do
+ {:ok, %Tesla.Env{status: 404}}
+ end
+
+ def head("https://example.com/oembed.json", _, _, _) do
+ {:ok, %Tesla.Env{status: 200, headers: [{"content-type", "application/json; charset=utf-8"}]}}
+ end
+
def head("https://example.com/pdf-file", _, _, _) do
{:ok,
%Tesla.Env{

File Metadata

Mime Type
text/x-diff
Expires
Tue, Nov 17, 8:54 PM (15 h, 58 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1839800
Default Alt Text
(17 KB)

Event Timeline