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.

181 lines
5.3KB

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