1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.MediaProxyTest do
7 import Pleroma.Web.MediaProxy
8 alias Pleroma.Web.MediaProxy.MediaProxyController
11 enabled = Pleroma.Config.get([:media_proxy, :enabled])
12 on_exit(fn -> Pleroma.Config.put([:media_proxy, :enabled], enabled) end)
16 describe "when enabled" do
18 Pleroma.Config.put([:media_proxy, :enabled], true)
22 test "ignores invalid url" do
23 assert url(nil) == nil
27 test "ignores relative url" do
28 assert url("/local") == "/local"
29 assert url("/") == "/"
32 test "ignores local url" do
33 local_url = Pleroma.Web.Endpoint.url() <> "/hello"
34 local_root = Pleroma.Web.Endpoint.url()
35 assert url(local_url) == local_url
36 assert url(local_root) == local_root
39 test "encodes and decodes URL" do
40 url = "https://pleroma.soykaf.com/static/logo.png"
43 assert String.starts_with?(
45 Pleroma.Config.get([:media_proxy, :base_url], Pleroma.Web.base_url())
48 assert String.ends_with?(encoded, "/logo.png")
50 assert decode_result(encoded) == url
53 test "encodes and decodes URL without a path" do
54 url = "https://pleroma.soykaf.com"
56 assert decode_result(encoded) == url
59 test "encodes and decodes URL without an extension" do
60 url = "https://pleroma.soykaf.com/path/"
62 assert String.ends_with?(encoded, "/path")
63 assert decode_result(encoded) == url
66 test "encodes and decodes URL and ignores query params for the path" do
67 url = "https://pleroma.soykaf.com/static/logo.png?93939393939&bunny=true"
69 assert String.ends_with?(encoded, "/logo.png")
70 assert decode_result(encoded) == url
73 test "ensures urls are url-encoded" do
74 assert decode_result(url("https://pleroma.social/Hello world.jpg")) ==
75 "https://pleroma.social/Hello%20world.jpg"
77 assert decode_result(url("https://pleroma.social/Hello%20world.jpg")) ==
78 "https://pleroma.social/Hello%20world.jpg"
81 test "validates signature" do
82 secret_key_base = Pleroma.Config.get([Pleroma.Web.Endpoint, :secret_key_base])
85 Pleroma.Config.put([Pleroma.Web.Endpoint, :secret_key_base], secret_key_base)
88 encoded = url("https://pleroma.social")
91 [Pleroma.Web.Endpoint, :secret_key_base],
92 "00000000000000000000000000000000000000000000000"
95 [_, "proxy", sig, base64 | _] = URI.parse(encoded).path |> String.split("/")
96 assert decode_url(sig, base64) == {:error, :invalid_signature}
99 test "filename_matches matches url encoded paths" do
100 assert MediaProxyController.filename_matches(
102 "/Hello%20world.jpg",
103 "http://pleroma.social/Hello world.jpg"
106 assert MediaProxyController.filename_matches(
108 "/Hello%20world.jpg",
109 "http://pleroma.social/Hello%20world.jpg"
113 test "filename_matches matches non-url encoded paths" do
114 assert MediaProxyController.filename_matches(
117 "http://pleroma.social/Hello%20world.jpg"
120 assert MediaProxyController.filename_matches(
123 "http://pleroma.social/Hello world.jpg"
127 test "uses the configured base_url" do
128 base_url = Pleroma.Config.get([:media_proxy, :base_url])
132 Pleroma.Config.put([:media_proxy, :base_url], base_url)
136 Pleroma.Config.put([:media_proxy, :base_url], "https://cache.pleroma.social")
138 url = "https://pleroma.soykaf.com/static/logo.png"
141 assert String.starts_with?(encoded, Pleroma.Config.get([:media_proxy, :base_url]))
144 # https://git.pleroma.social/pleroma/pleroma/issues/580
145 test "encoding S3 links (must preserve `%2F`)" do
147 "https://s3.amazonaws.com/example/test.png?X-Amz-Credential=your-access-key-id%2F20130721%2Fus-east-1%2Fs3%2Faws4_request"
150 assert decode_result(encoded) == url
154 describe "when disabled" do
156 enabled = Pleroma.Config.get([:media_proxy, :enabled])
159 Pleroma.Config.put([:media_proxy, :enabled], false)
162 Pleroma.Config.put([:media_proxy, :enabled], enabled)
170 test "does not encode remote urls" do
171 assert url("https://google.fr") == "https://google.fr"
175 defp decode_result(encoded) do
176 [_, "proxy", sig, base64 | _] = URI.parse(encoded).path |> String.split("/")
177 {:ok, decoded} = decode_url(sig, base64)
181 test "mediaproxy whitelist" do
182 Pleroma.Config.put([:media_proxy, :enabled], true)
183 Pleroma.Config.put([:media_proxy, :whitelist], ["google.com", "feld.me"])
184 url = "https://feld.me/foo.png"
187 assert unencoded == url