Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
4.7KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2021 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. defp signed_url(url) do
  95. :crypto.hmac(:sha, Config.get([Endpoint, :secret_key_base]), url)
  96. end
  97. def filename(url_or_path) do
  98. if path = URI.parse(url_or_path).path, do: Path.basename(path)
  99. end
  100. def base_url do
  101. Config.get([:media_proxy, :base_url], Endpoint.url())
  102. end
  103. defp proxy_url(path, sig_base64, url_base64, filename) do
  104. [
  105. base_url(),
  106. path,
  107. sig_base64,
  108. url_base64,
  109. filename
  110. ]
  111. |> Enum.filter(& &1)
  112. |> Path.join()
  113. end
  114. def build_url(sig_base64, url_base64, filename \\ nil) do
  115. proxy_url("proxy", sig_base64, url_base64, filename)
  116. end
  117. def build_preview_url(sig_base64, url_base64, filename \\ nil, preview_params \\ []) do
  118. uri = proxy_url("proxy/preview", sig_base64, url_base64, filename)
  119. UriHelper.modify_uri_params(uri, preview_params)
  120. end
  121. def verify_request_path_and_url(
  122. %Plug.Conn{params: %{"filename" => _}, request_path: request_path},
  123. url
  124. ) do
  125. verify_request_path_and_url(request_path, url)
  126. end
  127. def verify_request_path_and_url(request_path, url) when is_binary(request_path) do
  128. filename = filename(url)
  129. if filename && not basename_matches?(request_path, filename) do
  130. {:wrong_filename, filename}
  131. else
  132. :ok
  133. end
  134. end
  135. def verify_request_path_and_url(_, _), do: :ok
  136. defp basename_matches?(path, filename) do
  137. basename = Path.basename(path)
  138. basename == filename or URI.decode(basename) == filename or URI.encode(basename) == filename
  139. end
  140. end