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