Merge remote-tracking branch 'upstream/develop' into block-behavior
[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 @options [
13 pool: :media,
14 max_body: 2_000_000,
15 recv_timeout: 2_000
16 ]
17
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])
21
22 page_url
23 |> Linkify.Parser.url?(validate_tld: validate_tld)
24 |> parse_uri(page_url)
25 end
26
27 defp validate_page_url(%URI{host: host, scheme: "https", authority: authority})
28 when is_binary(authority) do
29 cond do
30 host in Config.get([:rich_media, :ignore_hosts], []) ->
31 :error
32
33 get_tld(host) in Config.get([:rich_media, :ignore_tld], []) ->
34 :error
35
36 true ->
37 :ok
38 end
39 end
40
41 defp validate_page_url(_), do: :error
42
43 defp parse_uri(true, url) do
44 url
45 |> URI.parse()
46 |> validate_page_url
47 end
48
49 defp parse_uri(_, _), do: :error
50
51 defp get_tld(host) do
52 host
53 |> String.split(".")
54 |> Enum.reverse()
55 |> hd
56 end
57
58 def fetch_data_for_object(object) do
59 with true <- Config.get([:rich_media, :enabled]),
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, fetch: false) 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 rich_media_get(url) do
82 headers = [{"user-agent", Pleroma.Application.user_agent() <> "; Bot"}]
83
84 head_check =
85 case Pleroma.HTTP.head(url, headers, @options) do
86 # If the HEAD request didn't reach the server for whatever reason,
87 # we assume the GET that comes right after won't either
88 {:error, _} = e ->
89 e
90
91 {:ok, %Tesla.Env{status: 200, headers: headers}} ->
92 with :ok <- check_content_type(headers),
93 :ok <- check_content_length(headers),
94 do: :ok
95
96 _ ->
97 :ok
98 end
99
100 with :ok <- head_check, do: Pleroma.HTTP.get(url, headers, @options)
101 end
102
103 defp check_content_type(headers) do
104 case List.keyfind(headers, "content-type", 0) do
105 {_, content_type} ->
106 case Plug.Conn.Utils.media_type(content_type) do
107 {:ok, "text", "html", _} -> :ok
108 _ -> {:error, {:content_type, content_type}}
109 end
110
111 _ ->
112 :ok
113 end
114 end
115
116 @max_body @options[:max_body]
117 defp check_content_length(headers) do
118 case List.keyfind(headers, "content-length", 0) do
119 {_, maybe_content_length} ->
120 case Integer.parse(maybe_content_length) do
121 {content_length, ""} when content_length <= @max_body -> :ok
122 {_, ""} -> {:error, :body_too_large}
123 _ -> :ok
124 end
125
126 _ ->
127 :ok
128 end
129 end
130 end