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