Page MenuHomePhorge

No OneTemporary

Authored By
Unknown
Size
6 KB
Referenced Files
None
Subscribers
None
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/lib/pleroma/web/rich_media/helpers.ex b/lib/pleroma/web/rich_media/helpers.ex
index 7b4ca677f..e58db38aa 100644
--- a/lib/pleroma/web/rich_media/helpers.ex
+++ b/lib/pleroma/web/rich_media/helpers.ex
@@ -11,6 +11,49 @@ defmodule Pleroma.Web.RichMedia.Helpers do
@type get_errors :: {:error, :body_too_large | :content_type | :head | :get}
+ @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
+
@spec rich_media_get(String.t()) :: {:ok, String.t()} | get_errors()
def rich_media_get(url) do
case Pleroma.HTTP.AdapterHelper.can_stream?() do
diff --git a/lib/pleroma/web/rich_media/parser.ex b/lib/pleroma/web/rich_media/parser.ex
index 9c8ec7a9f..f1445a301 100644
--- a/lib/pleroma/web/rich_media/parser.ex
+++ b/lib/pleroma/web/rich_media/parser.ex
@@ -19,7 +19,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}
@@ -77,46 +77,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..a89c492f3 100644
--- a/lib/pleroma/web/rich_media/parsers/o_embed.ex
+++ b/lib/pleroma/web/rich_media/parsers/o_embed.ex
@@ -6,6 +6,7 @@ defmodule Pleroma.Web.RichMedia.Parsers.OEmbed do
def parse(html, _data) do
with elements = [_ | _] <- get_discovery_data(html),
oembed_url when is_binary(oembed_url) <- get_oembed_url(elements),
+ :ok <- Pleroma.Web.RichMedia.Helpers.validate_page_url(oembed_url),
{:ok, oembed_data = %{"html" => html}} <- get_oembed_data(oembed_url) do
%{oembed_data | "html" => Pleroma.HTML.filter_tags(html)}
else
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/pleroma/web/rich_media/parser_test.exs b/test/pleroma/web/rich_media/parser_test.exs
index 1f01d657a..48b1cbf9a 100644
--- a/test/pleroma/web/rich_media/parser_test.exs
+++ b/test/pleroma/web/rich_media/parser_test.exs
@@ -97,6 +97,10 @@ defmodule Pleroma.Web.RichMedia.ParserTest do
}}
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 "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..e080da621 100644
--- a/test/support/http_request_mock.ex
+++ b/test/support/http_request_mock.ex
@@ -1497,6 +1497,15 @@ defmodule HttpRequestMock do
{:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/oembed.html")}}
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")}}
end
@@ -1764,6 +1773,8 @@ defmodule HttpRequestMock do
"https://example.com/non-ogp",
"https://example.com/oembed",
"https://example.com/oembed.json",
+ "https://example.com/oembed-internal",
+ "http://127.0.0.1:9200/internal.json",
"https://example.com/ogp",
"https://example.com/ogp-missing-data",
"https://example.com/ogp-missing-title",

File Metadata

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

Event Timeline