Merge branch 'feat/rich-media-head' into 'develop'
[akkoma] / lib / pleroma / web / rich_media / helpers.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright _ 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.RichMedia.Helpers do
6 alias Pleroma.Activity
7 alias Pleroma.Config
8 alias Pleroma.HTML
9 alias Pleroma.Object
10 alias Pleroma.Web.RichMedia.Parser
11
12 @rich_media_options [
13 pool: :media,
14 max_body: 2_000_000
15 ]
16
17 @spec validate_page_url(URI.t() | binary()) :: :ok | :error
18 defp validate_page_url(page_url) when is_binary(page_url) do
19 validate_tld = Pleroma.Config.get([Pleroma.Formatter, :validate_tld])
20
21 page_url
22 |> Linkify.Parser.url?(validate_tld: validate_tld)
23 |> parse_uri(page_url)
24 end
25
26 defp validate_page_url(%URI{host: host, scheme: "https", authority: authority})
27 when is_binary(authority) do
28 cond do
29 host in Config.get([:rich_media, :ignore_hosts], []) ->
30 :error
31
32 get_tld(host) in Config.get([:rich_media, :ignore_tld], []) ->
33 :error
34
35 true ->
36 :ok
37 end
38 end
39
40 defp validate_page_url(_), do: :error
41
42 defp parse_uri(true, url) do
43 url
44 |> URI.parse()
45 |> validate_page_url
46 end
47
48 defp parse_uri(_, _), do: :error
49
50 defp get_tld(host) do
51 host
52 |> String.split(".")
53 |> Enum.reverse()
54 |> hd
55 end
56
57 def fetch_data_for_object(object) do
58 with true <- Config.get([:rich_media, :enabled]),
59 false <- object.data["sensitive"] || false,
60 {:ok, page_url} <-
61 HTML.extract_first_external_url_from_object(object),
62 :ok <- validate_page_url(page_url),
63 {:ok, rich_media} <- Parser.parse(page_url) do
64 %{page_url: page_url, rich_media: rich_media}
65 else
66 _ -> %{}
67 end
68 end
69
70 def fetch_data_for_activity(%Activity{data: %{"type" => "Create"}} = activity) do
71 with true <- Config.get([:rich_media, :enabled]),
72 %Object{} = object <- Object.normalize(activity) do
73 fetch_data_for_object(object)
74 else
75 _ -> %{}
76 end
77 end
78
79 def fetch_data_for_activity(_), do: %{}
80
81 def perform(:fetch, %Activity{} = activity) do
82 fetch_data_for_activity(activity)
83 :ok
84 end
85
86 def rich_media_get(url) do
87 headers = [{"user-agent", Pleroma.Application.user_agent() <> "; Bot"}]
88
89 options =
90 if Application.get_env(:tesla, :adapter) == Tesla.Adapter.Hackney do
91 Keyword.merge(@rich_media_options,
92 recv_timeout: 2_000,
93 with_body: true
94 )
95 else
96 @rich_media_options
97 end
98
99 head_check =
100 case Pleroma.HTTP.head(url, headers, adapter: options) do
101 # If the HEAD request didn't reach the server for whatever reason,
102 # we assume the GET that comes right after won't either
103 {:error, _} = e ->
104 e
105
106 {:ok, %Tesla.Env{status: 200, headers: headers}} ->
107 with :ok <- check_content_type(headers),
108 :ok <- check_content_length(headers),
109 do: :ok
110
111 _ ->
112 :ok
113 end
114
115 with :ok <- head_check, do: Pleroma.HTTP.get(url, headers, adapter: options)
116 end
117
118 defp check_content_type(headers) do
119 case List.keyfind(headers, "content-type", 0) do
120 {_, content_type} ->
121 case Plug.Conn.Utils.media_type(content_type) do
122 {:ok, "text", "html", _} -> :ok
123 _ -> {:error, {:content_type, content_type}}
124 end
125
126 _ ->
127 :ok
128 end
129 end
130
131 @max_body @rich_media_options[:max_body]
132 defp check_content_length(headers) do
133 case List.keyfind(headers, "content-length", 0) do
134 {_, maybe_content_length} ->
135 case Integer.parse(maybe_content_length) do
136 {content_length, ""} when content_length <= @max_body -> :ok
137 {_, ""} -> {:error, :body_too_large}
138 _ -> :ok
139 end
140
141 _ ->
142 :ok
143 end
144 end
145 end