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