logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

media_proxy.ex (4982B)


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