1 # Pleroma: A lightweight social networking server
2 # Copyright _ 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.RichMedia.Helpers do
10 alias Pleroma.Web.RichMedia.Parser
18 @spec validate_page_url(URI.t() | binary()) :: :ok | :error
19 defp validate_page_url(page_url) when is_binary(page_url) do
20 validate_tld = Config.get([Pleroma.Formatter, :validate_tld])
23 |> Linkify.Parser.url?(validate_tld: validate_tld)
24 |> parse_uri(page_url)
27 defp validate_page_url(%URI{host: host, scheme: "https", authority: authority})
28 when is_binary(authority) do
30 host in Config.get([:rich_media, :ignore_hosts], []) ->
33 get_tld(host) in Config.get([:rich_media, :ignore_tld], []) ->
41 defp validate_page_url(_), do: :error
43 defp parse_uri(true, url) do
49 defp parse_uri(_, _), do: :error
58 def fetch_data_for_object(object) do
59 with true <- Config.get([:rich_media, :enabled]),
60 false <- object.data["sensitive"] || false,
62 HTML.extract_first_external_url_from_object(object),
63 :ok <- validate_page_url(page_url),
64 {:ok, rich_media} <- Parser.parse(page_url) do
65 %{page_url: page_url, rich_media: rich_media}
71 def fetch_data_for_activity(%Activity{data: %{"type" => "Create"}} = activity) do
72 with true <- Config.get([:rich_media, :enabled]),
73 %Object{} = object <- Object.normalize(activity) do
74 fetch_data_for_object(object)
80 def fetch_data_for_activity(_), do: %{}
82 def perform(:fetch, %Activity{} = activity) do
83 fetch_data_for_activity(activity)
87 def rich_media_get(url) do
88 headers = [{"user-agent", Pleroma.Application.user_agent() <> "; Bot"}]
91 case Pleroma.HTTP.head(url, headers, @options) do
92 # If the HEAD request didn't reach the server for whatever reason,
93 # we assume the GET that comes right after won't either
97 {:ok, %Tesla.Env{status: 200, headers: headers}} ->
98 with :ok <- check_content_type(headers),
99 :ok <- check_content_length(headers),
106 with :ok <- head_check, do: Pleroma.HTTP.get(url, headers, @options)
109 defp check_content_type(headers) do
110 case List.keyfind(headers, "content-type", 0) do
112 case Plug.Conn.Utils.media_type(content_type) do
113 {:ok, "text", "html", _} -> :ok
114 _ -> {:error, {:content_type, content_type}}
122 @max_body @options[:max_body]
123 defp check_content_length(headers) do
124 case List.keyfind(headers, "content-length", 0) do
125 {_, maybe_content_length} ->
126 case Integer.parse(maybe_content_length) do
127 {content_length, ""} when content_length <= @max_body -> :ok
128 {_, ""} -> {:error, :body_too_large}