logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

media_proxy.ex (5011B)


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