Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

168 рядки
4.8KB

  1. defmodule Pleroma.MediaProxyTest do
  2. use ExUnit.Case
  3. import Pleroma.Web.MediaProxy
  4. alias Pleroma.Web.MediaProxy.MediaProxyController
  5. describe "when enabled" do
  6. setup do
  7. enabled = Pleroma.Config.get([:media_proxy, :enabled])
  8. unless enabled do
  9. Pleroma.Config.put([:media_proxy, :enabled], true)
  10. on_exit(fn -> Pleroma.Config.put([:media_proxy, :enabled], enabled) end)
  11. end
  12. :ok
  13. end
  14. test "ignores invalid url" do
  15. assert url(nil) == nil
  16. assert url("") == nil
  17. end
  18. test "ignores relative url" do
  19. assert url("/local") == "/local"
  20. assert url("/") == "/"
  21. end
  22. test "ignores local url" do
  23. local_url = Pleroma.Web.Endpoint.url() <> "/hello"
  24. local_root = Pleroma.Web.Endpoint.url()
  25. assert url(local_url) == local_url
  26. assert url(local_root) == local_root
  27. end
  28. test "encodes and decodes URL" do
  29. url = "https://pleroma.soykaf.com/static/logo.png"
  30. encoded = url(url)
  31. assert String.starts_with?(
  32. encoded,
  33. Pleroma.Config.get([:media_proxy, :base_url], Pleroma.Web.base_url())
  34. )
  35. assert String.ends_with?(encoded, "/logo.png")
  36. assert decode_result(encoded) == url
  37. end
  38. test "encodes and decodes URL without a path" do
  39. url = "https://pleroma.soykaf.com"
  40. encoded = url(url)
  41. assert decode_result(encoded) == url
  42. end
  43. test "encodes and decodes URL without an extension" do
  44. url = "https://pleroma.soykaf.com/path/"
  45. encoded = url(url)
  46. assert String.ends_with?(encoded, "/path")
  47. assert decode_result(encoded) == url
  48. end
  49. test "encodes and decodes URL and ignores query params for the path" do
  50. url = "https://pleroma.soykaf.com/static/logo.png?93939393939&bunny=true"
  51. encoded = url(url)
  52. assert String.ends_with?(encoded, "/logo.png")
  53. assert decode_result(encoded) == url
  54. end
  55. test "ensures urls are url-encoded" do
  56. assert decode_result(url("https://pleroma.social/Hello world.jpg")) ==
  57. "https://pleroma.social/Hello%20world.jpg"
  58. assert decode_result(url("https://pleroma.social/Hello%20world.jpg")) ==
  59. "https://pleroma.social/Hello%20world.jpg"
  60. end
  61. test "validates signature" do
  62. secret_key_base = Pleroma.Config.get([Pleroma.Web.Endpoint, :secret_key_base])
  63. on_exit(fn ->
  64. Pleroma.Config.put([Pleroma.Web.Endpoint, :secret_key_base], secret_key_base)
  65. end)
  66. encoded = url("https://pleroma.social")
  67. Pleroma.Config.put(
  68. [Pleroma.Web.Endpoint, :secret_key_base],
  69. "00000000000000000000000000000000000000000000000"
  70. )
  71. [_, "proxy", sig, base64 | _] = URI.parse(encoded).path |> String.split("/")
  72. assert decode_url(sig, base64) == {:error, :invalid_signature}
  73. end
  74. test "filename_matches matches url encoded paths" do
  75. assert MediaProxyController.filename_matches(
  76. true,
  77. "/Hello%20world.jpg",
  78. "http://pleroma.social/Hello world.jpg"
  79. ) == :ok
  80. assert MediaProxyController.filename_matches(
  81. true,
  82. "/Hello%20world.jpg",
  83. "http://pleroma.social/Hello%20world.jpg"
  84. ) == :ok
  85. end
  86. test "filename_matches matches non-url encoded paths" do
  87. assert MediaProxyController.filename_matches(
  88. true,
  89. "/Hello world.jpg",
  90. "http://pleroma.social/Hello%20world.jpg"
  91. ) == :ok
  92. assert MediaProxyController.filename_matches(
  93. true,
  94. "/Hello world.jpg",
  95. "http://pleroma.social/Hello world.jpg"
  96. ) == :ok
  97. end
  98. test "uses the configured base_url" do
  99. base_url = Pleroma.Config.get([:media_proxy, :base_url])
  100. if base_url do
  101. on_exit(fn ->
  102. Pleroma.Config.put([:media_proxy, :base_url], base_url)
  103. end)
  104. end
  105. Pleroma.Config.put([:media_proxy, :base_url], "https://cache.pleroma.social")
  106. url = "https://pleroma.soykaf.com/static/logo.png"
  107. encoded = url(url)
  108. assert String.starts_with?(encoded, Pleroma.Config.get([:media_proxy, :base_url]))
  109. end
  110. end
  111. describe "when disabled" do
  112. setup do
  113. enabled = Pleroma.Config.get([:media_proxy, :enabled])
  114. if enabled do
  115. Pleroma.Config.put([:media_proxy, :enabled], false)
  116. on_exit(fn ->
  117. Pleroma.Config.put([:media_proxy, :enabled], enabled)
  118. :ok
  119. end)
  120. end
  121. :ok
  122. end
  123. test "does not encode remote urls" do
  124. assert url("https://google.fr") == "https://google.fr"
  125. end
  126. end
  127. defp decode_result(encoded) do
  128. [_, "proxy", sig, base64 | _] = URI.parse(encoded).path |> String.split("/")
  129. {:ok, decoded} = decode_url(sig, base64)
  130. decoded
  131. end
  132. end