Ensure Gun is Gone
[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 max_body: 2_000_000,
14 receive_timeout: 2_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 = 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 {:ok, page_url} <-
60 HTML.extract_first_external_url_from_object(object),
61 :ok <- validate_page_url(page_url),
62 {:ok, rich_media} <- Parser.parse(page_url) do
63 %{page_url: page_url, rich_media: rich_media}
64 else
65 _ -> %{}
66 end
67 end
68
69 def fetch_data_for_activity(%Activity{data: %{"type" => "Create"}} = activity) do
70 with true <- Config.get([:rich_media, :enabled]),
71 %Object{} = object <- Object.normalize(activity, fetch: false) do
72 fetch_data_for_object(object)
73 else
74 _ -> %{}
75 end
76 end
77
78 def fetch_data_for_activity(_), do: %{}
79
80 def rich_media_get(url) do
81 headers = [{"user-agent", Pleroma.Application.user_agent() <> "; Bot"}]
82
83 head_check =
84 case Pleroma.HTTP.head(url, headers, @options) do
85 # If the HEAD request didn't reach the server for whatever reason,
86 # we assume the GET that comes right after won't either
87 {:error, _} = e ->
88 e
89
90 {:ok, %Tesla.Env{status: 200, headers: headers}} ->
91 with :ok <- check_content_type(headers),
92 :ok <- check_content_length(headers),
93 do: :ok
94
95 _ ->
96 :ok
97 end
98
99 with :ok <- head_check, do: Pleroma.HTTP.get(url, headers, @options)
100 end
101
102 defp check_content_type(headers) do
103 case List.keyfind(headers, "content-type", 0) do
104 {_, content_type} ->
105 case Plug.Conn.Utils.media_type(content_type) do
106 {:ok, "text", "html", _} -> :ok
107 _ -> {:error, {:content_type, content_type}}
108 end
109
110 _ ->
111 :ok
112 end
113 end
114
115 @max_body @options[:max_body]
116 defp check_content_length(headers) do
117 case List.keyfind(headers, "content-length", 0) do
118 {_, maybe_content_length} ->
119 case Integer.parse(maybe_content_length) do
120 {content_length, ""} when content_length <= @max_body -> :ok
121 {_, ""} -> {:error, :body_too_large}
122 _ -> :ok
123 end
124
125 _ ->
126 :ok
127 end
128 end
129 end