1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MediaProxy do
9 alias Pleroma.Web.MediaProxy.Invalidation
11 @base64_opts [padding: false]
12 @cache_table :banned_urls_cache
14 def cache_table, do: @cache_table
16 @spec in_banned_urls(String.t()) :: boolean()
17 def in_banned_urls(url), do: elem(Cachex.exists?(@cache_table, url(url)), 1)
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))
25 def remove_from_banned_urls(url) when is_binary(url) do
26 Cachex.del(@cache_table, url(url))
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))
35 def put_in_banned_urls(url) when is_binary(url) do
36 Cachex.put(@cache_table, url(url), true)
39 def url(url) when is_nil(url) or url == "", do: nil
40 def url("/" <> _ = url), do: url
43 if not enabled?() or not url_proxiable?(url) do
50 @spec url_proxiable?(String.t()) :: boolean()
51 def url_proxiable?(url) do
52 if local?(url) or whitelisted?(url) do
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)
69 def enabled?, do: Config.get([:media_proxy, :enabled], false)
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)
75 def local?(url), do: String.starts_with?(url, Pleroma.Web.base_url())
77 def whitelisted?(url) do
78 %{host: domain} = URI.parse(url)
80 mediaproxy_whitelist_domains =
81 [:media_proxy, :whitelist]
83 |> Enum.map(&maybe_get_domain_from_url/1)
86 if base_url = Config.get([Upload, :base_url]) do
87 %{host: base_domain} = URI.parse(base_url)
88 [base_domain | mediaproxy_whitelist_domains]
90 mediaproxy_whitelist_domains
93 domain in whitelist_domains
96 defp maybe_get_domain_from_url("http" <> _ = url) do
100 defp maybe_get_domain_from_url(domain), do: domain
102 defp base64_sig64(url) do
103 base64 = Base.url_encode64(url, @base64_opts)
108 |> Base.url_encode64(@base64_opts)
113 def encode_url(url) do
114 {base64, sig64} = base64_sig64(url)
116 build_url(sig64, base64, filename(url))
119 def encode_preview_url(url) do
120 {base64, sig64} = base64_sig64(url)
122 build_preview_url(sig64, base64, filename(url))
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)}
130 _ -> {:error, :invalid_signature}
134 defp signed_url(url) do
135 :crypto.hmac(:sha, Config.get([Web.Endpoint, :secret_key_base]), url)
138 def filename(url_or_path) do
139 if path = URI.parse(url_or_path).path, do: Path.basename(path)
142 defp proxy_url(path, sig_base64, url_base64, filename) do
144 Config.get([:media_proxy, :base_url], Web.base_url()),
154 def build_url(sig_base64, url_base64, filename \\ nil) do
155 proxy_url("proxy", sig_base64, url_base64, filename)
158 def build_preview_url(sig_base64, url_base64, filename \\ nil) do
159 proxy_url("proxy/preview", sig_base64, url_base64, filename)
162 def verify_request_path_and_url(
163 %Plug.Conn{params: %{"filename" => _}, request_path: request_path},
166 verify_request_path_and_url(request_path, url)
169 def verify_request_path_and_url(request_path, url) when is_binary(request_path) do
170 filename = filename(url)
172 if filename && not basename_matches?(request_path, filename) do
173 {:wrong_filename, filename}
179 def verify_request_path_and_url(_, _), do: :ok
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