8656b8cad31eb12910d10556ee58f741c34f48a8
[akkoma] / lib / pleroma / web / media_proxy / media_proxy.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.MediaProxy do
6 alias Pleroma.Config
7 alias Pleroma.Helpers.UriHelper
8 alias Pleroma.Upload
9 alias Pleroma.Web
10 alias Pleroma.Web.MediaProxy.Invalidation
11
12 @base64_opts [padding: false]
13 @cache_table :banned_urls_cache
14
15 def cache_table, do: @cache_table
16
17 @spec in_banned_urls(String.t()) :: boolean()
18 def in_banned_urls(url), do: elem(Cachex.exists?(@cache_table, url(url)), 1)
19
20 def remove_from_banned_urls(urls) when is_list(urls) do
21 Cachex.execute!(@cache_table, fn cache ->
22 Enum.each(Invalidation.prepare_urls(urls), &Cachex.del(cache, &1))
23 end)
24 end
25
26 def remove_from_banned_urls(url) when is_binary(url) do
27 Cachex.del(@cache_table, url(url))
28 end
29
30 def put_in_banned_urls(urls) when is_list(urls) do
31 Cachex.execute!(@cache_table, fn cache ->
32 Enum.each(Invalidation.prepare_urls(urls), &Cachex.put(cache, &1, true))
33 end)
34 end
35
36 def put_in_banned_urls(url) when is_binary(url) do
37 Cachex.put(@cache_table, url(url), true)
38 end
39
40 def url(url) when is_nil(url) or url == "", do: nil
41 def url("/" <> _ = url), do: url
42
43 def url(url) do
44 if enabled?() and url_proxiable?(url) do
45 encode_url(url)
46 else
47 url
48 end
49 end
50
51 @spec url_proxiable?(String.t()) :: boolean()
52 def url_proxiable?(url) do
53 not local?(url) and not whitelisted?(url)
54 end
55
56 def preview_url(url, preview_params \\ []) do
57 if preview_enabled?() do
58 encode_preview_url(url, preview_params)
59 else
60 url(url)
61 end
62 end
63
64 def enabled?, do: Config.get([:media_proxy, :enabled], false)
65
66 # Note: media proxy must be enabled for media preview proxy in order to load all
67 # non-local non-whitelisted URLs through it and be sure that body size constraint is preserved.
68 def preview_enabled?, do: enabled?() and !!Config.get([:media_preview_proxy, :enabled])
69
70 def local?(url), do: String.starts_with?(url, Pleroma.Web.base_url())
71
72 def whitelisted?(url) do
73 %{host: domain} = URI.parse(url)
74
75 mediaproxy_whitelist_domains =
76 [:media_proxy, :whitelist]
77 |> Config.get()
78 |> Enum.map(&maybe_get_domain_from_url/1)
79
80 whitelist_domains =
81 if base_url = Config.get([Upload, :base_url]) do
82 %{host: base_domain} = URI.parse(base_url)
83 [base_domain | mediaproxy_whitelist_domains]
84 else
85 mediaproxy_whitelist_domains
86 end
87
88 domain in whitelist_domains
89 end
90
91 defp maybe_get_domain_from_url("http" <> _ = url) do
92 URI.parse(url).host
93 end
94
95 defp maybe_get_domain_from_url(domain), do: domain
96
97 defp base64_sig64(url) do
98 base64 = Base.url_encode64(url, @base64_opts)
99
100 sig64 =
101 base64
102 |> signed_url()
103 |> Base.url_encode64(@base64_opts)
104
105 {base64, sig64}
106 end
107
108 def encode_url(url) do
109 {base64, sig64} = base64_sig64(url)
110
111 build_url(sig64, base64, filename(url))
112 end
113
114 def encode_preview_url(url, preview_params \\ []) do
115 {base64, sig64} = base64_sig64(url)
116
117 build_preview_url(sig64, base64, filename(url), preview_params)
118 end
119
120 def decode_url(sig, url) do
121 with {:ok, sig} <- Base.url_decode64(sig, @base64_opts),
122 signature when signature == sig <- signed_url(url) do
123 {:ok, Base.url_decode64!(url, @base64_opts)}
124 else
125 _ -> {:error, :invalid_signature}
126 end
127 end
128
129 defp signed_url(url) do
130 :crypto.hmac(:sha, Config.get([Web.Endpoint, :secret_key_base]), url)
131 end
132
133 def filename(url_or_path) do
134 if path = URI.parse(url_or_path).path, do: Path.basename(path)
135 end
136
137 def base_url do
138 Config.get([:media_proxy, :base_url], Web.base_url())
139 end
140
141 defp proxy_url(path, sig_base64, url_base64, filename) do
142 [
143 base_url(),
144 path,
145 sig_base64,
146 url_base64,
147 filename
148 ]
149 |> Enum.filter(& &1)
150 |> Path.join()
151 end
152
153 def build_url(sig_base64, url_base64, filename \\ nil) do
154 proxy_url("proxy", sig_base64, url_base64, filename)
155 end
156
157 def build_preview_url(sig_base64, url_base64, filename \\ nil, preview_params \\ []) do
158 uri = proxy_url("proxy/preview", sig_base64, url_base64, filename)
159
160 UriHelper.modify_uri_params(uri, preview_params)
161 end
162
163 def verify_request_path_and_url(
164 %Plug.Conn{params: %{"filename" => _}, request_path: request_path},
165 url
166 ) do
167 verify_request_path_and_url(request_path, url)
168 end
169
170 def verify_request_path_and_url(request_path, url) when is_binary(request_path) do
171 filename = filename(url)
172
173 if filename && not basename_matches?(request_path, filename) do
174 {:wrong_filename, filename}
175 else
176 :ok
177 end
178 end
179
180 def verify_request_path_and_url(_, _), do: :ok
181
182 defp basename_matches?(path, filename) do
183 basename = Path.basename(path)
184 basename == filename or URI.decode(basename) == filename or URI.encode(basename) == filename
185 end
186 end