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