Merge pull request 'feature/elasticsearch' (#1) from feature/elasticsearch into develop
[akkoma] / test / pleroma / reverse_proxy_test.exs
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
5 defmodule Pleroma.ReverseProxyTest do
6 use Pleroma.Web.ConnCase
7 import ExUnit.CaptureLog
8 import Mox
9
10 alias Pleroma.ReverseProxy
11 alias Pleroma.ReverseProxy.ClientMock
12 alias Plug.Conn
13
14 setup_all do
15 {:ok, _} = Registry.start_link(keys: :unique, name: ClientMock)
16 :ok
17 end
18
19 setup :verify_on_exit!
20
21 defp request_mock(invokes) do
22 ClientMock
23 |> expect(:request, fn :get, url, headers, _body, _opts ->
24 Registry.register(ClientMock, url, 0)
25 body = headers |> Enum.into(%{}) |> Jason.encode!()
26
27 {:ok, 200,
28 [
29 {"content-type", "application/json"},
30 {"content-length", byte_size(body) |> to_string()}
31 ], %{url: url, body: body}}
32 end)
33 |> expect(:stream_body, invokes, fn %{url: url, body: body} = client ->
34 case Registry.lookup(ClientMock, url) do
35 [{_, 0}] ->
36 Registry.update_value(ClientMock, url, &(&1 + 1))
37 {:ok, body, client}
38
39 [{_, 1}] ->
40 Registry.unregister(ClientMock, url)
41 :done
42 end
43 end)
44 end
45
46 describe "reverse proxy" do
47 test "do not track successful request", %{conn: conn} do
48 request_mock(2)
49 url = "/success"
50
51 conn = ReverseProxy.call(conn, url)
52
53 assert conn.status == 200
54 assert Cachex.get(:failed_proxy_url_cache, url) == {:ok, nil}
55 end
56 end
57
58 test "use Pleroma's user agent in the request; don't pass the client's", %{conn: conn} do
59 request_mock(2)
60
61 conn =
62 conn
63 |> Plug.Conn.put_req_header("user-agent", "fake/1.0")
64 |> ReverseProxy.call("/user-agent")
65
66 assert json_response(conn, 200) == %{"user-agent" => Pleroma.Application.user_agent()}
67 end
68
69 test "closed connection", %{conn: conn} do
70 ClientMock
71 |> expect(:request, fn :get, "/closed", _, _, _ -> {:ok, 200, [], %{}} end)
72 |> expect(:stream_body, fn _ -> {:error, :closed} end)
73 |> expect(:close, fn _ -> :ok end)
74
75 conn = ReverseProxy.call(conn, "/closed")
76 assert conn.halted
77 end
78
79 defp stream_mock(invokes, with_close? \\ false) do
80 ClientMock
81 |> expect(:request, fn :get, "/stream-bytes/" <> length, _, _, _ ->
82 Registry.register(ClientMock, "/stream-bytes/" <> length, 0)
83
84 {:ok, 200, [{"content-type", "application/octet-stream"}],
85 %{url: "/stream-bytes/" <> length}}
86 end)
87 |> expect(:stream_body, invokes, fn %{url: "/stream-bytes/" <> length} = client ->
88 max = String.to_integer(length)
89
90 case Registry.lookup(ClientMock, "/stream-bytes/" <> length) do
91 [{_, current}] when current < max ->
92 Registry.update_value(
93 ClientMock,
94 "/stream-bytes/" <> length,
95 &(&1 + 10)
96 )
97
98 {:ok, "0123456789", client}
99
100 [{_, ^max}] ->
101 Registry.unregister(ClientMock, "/stream-bytes/" <> length)
102 :done
103 end
104 end)
105
106 if with_close? do
107 expect(ClientMock, :close, fn _ -> :ok end)
108 end
109 end
110
111 describe "max_body" do
112 test "length returns error if content-length more than option", %{conn: conn} do
113 request_mock(0)
114
115 assert capture_log(fn ->
116 ReverseProxy.call(conn, "/huge-file", max_body_length: 4)
117 end) =~
118 "[error] Elixir.Pleroma.ReverseProxy: request to \"/huge-file\" failed: :body_too_large"
119
120 assert {:ok, true} == Cachex.get(:failed_proxy_url_cache, "/huge-file")
121
122 assert capture_log(fn ->
123 ReverseProxy.call(conn, "/huge-file", max_body_length: 4)
124 end) == ""
125 end
126
127 test "max_body_length returns error if streaming body more than that option", %{conn: conn} do
128 stream_mock(3, true)
129
130 assert capture_log(fn ->
131 ReverseProxy.call(conn, "/stream-bytes/50", max_body_length: 30)
132 end) =~
133 "[warn] Elixir.Pleroma.ReverseProxy request to /stream-bytes/50 failed while reading/chunking: :body_too_large"
134 end
135 end
136
137 describe "HEAD requests" do
138 test "common", %{conn: conn} do
139 ClientMock
140 |> expect(:request, fn :head, "/head", _, _, _ ->
141 {:ok, 200, [{"content-type", "text/html; charset=utf-8"}]}
142 end)
143
144 conn = ReverseProxy.call(Map.put(conn, :method, "HEAD"), "/head")
145 assert html_response(conn, 200) == ""
146 end
147 end
148
149 defp error_mock(status) when is_integer(status) do
150 ClientMock
151 |> expect(:request, fn :get, "/status/" <> _, _, _, _ ->
152 {:error, status}
153 end)
154 end
155
156 describe "returns error on" do
157 test "500", %{conn: conn} do
158 error_mock(500)
159 url = "/status/500"
160
161 capture_log(fn -> ReverseProxy.call(conn, url) end) =~
162 "[error] Elixir.Pleroma.ReverseProxy: request to /status/500 failed with HTTP status 500"
163
164 assert Cachex.get(:failed_proxy_url_cache, url) == {:ok, true}
165
166 {:ok, ttl} = Cachex.ttl(:failed_proxy_url_cache, url)
167 assert ttl <= 60_000
168 end
169
170 test "400", %{conn: conn} do
171 error_mock(400)
172 url = "/status/400"
173
174 capture_log(fn -> ReverseProxy.call(conn, url) end) =~
175 "[error] Elixir.Pleroma.ReverseProxy: request to /status/400 failed with HTTP status 400"
176
177 assert Cachex.get(:failed_proxy_url_cache, url) == {:ok, true}
178 assert Cachex.ttl(:failed_proxy_url_cache, url) == {:ok, nil}
179 end
180
181 test "403", %{conn: conn} do
182 error_mock(403)
183 url = "/status/403"
184
185 capture_log(fn ->
186 ReverseProxy.call(conn, url, failed_request_ttl: :timer.seconds(120))
187 end) =~
188 "[error] Elixir.Pleroma.ReverseProxy: request to /status/403 failed with HTTP status 403"
189
190 {:ok, ttl} = Cachex.ttl(:failed_proxy_url_cache, url)
191 assert ttl > 100_000
192 end
193
194 test "204", %{conn: conn} do
195 url = "/status/204"
196 expect(ClientMock, :request, fn :get, _url, _, _, _ -> {:ok, 204, [], %{}} end)
197
198 capture_log(fn ->
199 conn = ReverseProxy.call(conn, url)
200 assert conn.resp_body == "Request failed: No Content"
201 assert conn.halted
202 end) =~
203 "[error] Elixir.Pleroma.ReverseProxy: request to \"/status/204\" failed with HTTP status 204"
204
205 assert Cachex.get(:failed_proxy_url_cache, url) == {:ok, true}
206 assert Cachex.ttl(:failed_proxy_url_cache, url) == {:ok, nil}
207 end
208 end
209
210 test "streaming", %{conn: conn} do
211 stream_mock(21)
212 conn = ReverseProxy.call(conn, "/stream-bytes/200")
213 assert conn.state == :chunked
214 assert byte_size(conn.resp_body) == 200
215 assert Conn.get_resp_header(conn, "content-type") == ["application/octet-stream"]
216 end
217
218 defp headers_mock(_) do
219 ClientMock
220 |> expect(:request, fn :get, "/headers", headers, _, _ ->
221 Registry.register(ClientMock, "/headers", 0)
222 {:ok, 200, [{"content-type", "application/json"}], %{url: "/headers", headers: headers}}
223 end)
224 |> expect(:stream_body, 2, fn %{url: url, headers: headers} = client ->
225 case Registry.lookup(ClientMock, url) do
226 [{_, 0}] ->
227 Registry.update_value(ClientMock, url, &(&1 + 1))
228 headers = for {k, v} <- headers, into: %{}, do: {String.capitalize(k), v}
229 {:ok, Jason.encode!(%{headers: headers}), client}
230
231 [{_, 1}] ->
232 Registry.unregister(ClientMock, url)
233 :done
234 end
235 end)
236
237 :ok
238 end
239
240 describe "keep request headers" do
241 setup [:headers_mock]
242
243 test "header passes", %{conn: conn} do
244 conn =
245 Conn.put_req_header(
246 conn,
247 "accept",
248 "text/html"
249 )
250 |> ReverseProxy.call("/headers")
251
252 %{"headers" => headers} = json_response(conn, 200)
253 assert headers["Accept"] == "text/html"
254 end
255
256 test "header is filtered", %{conn: conn} do
257 conn =
258 Conn.put_req_header(
259 conn,
260 "accept-language",
261 "en-US"
262 )
263 |> ReverseProxy.call("/headers")
264
265 %{"headers" => headers} = json_response(conn, 200)
266 refute headers["Accept-Language"]
267 end
268 end
269
270 test "returns 400 on non GET, HEAD requests", %{conn: conn} do
271 conn = ReverseProxy.call(Map.put(conn, :method, "POST"), "/ip")
272 assert conn.status == 400
273 end
274
275 describe "cache resp headers" do
276 test "add cache-control", %{conn: conn} do
277 ClientMock
278 |> expect(:request, fn :get, "/cache", _, _, _ ->
279 {:ok, 200, [{"ETag", "some ETag"}], %{}}
280 end)
281 |> expect(:stream_body, fn _ -> :done end)
282
283 conn = ReverseProxy.call(conn, "/cache")
284 assert {"cache-control", "public, max-age=1209600"} in conn.resp_headers
285 end
286 end
287
288 defp disposition_headers_mock(headers) do
289 ClientMock
290 |> expect(:request, fn :get, "/disposition", _, _, _ ->
291 Registry.register(ClientMock, "/disposition", 0)
292
293 {:ok, 200, headers, %{url: "/disposition"}}
294 end)
295 |> expect(:stream_body, 2, fn %{url: "/disposition"} = client ->
296 case Registry.lookup(ClientMock, "/disposition") do
297 [{_, 0}] ->
298 Registry.update_value(ClientMock, "/disposition", &(&1 + 1))
299 {:ok, "", client}
300
301 [{_, 1}] ->
302 Registry.unregister(ClientMock, "/disposition")
303 :done
304 end
305 end)
306 end
307
308 describe "response content disposition header" do
309 test "not atachment", %{conn: conn} do
310 disposition_headers_mock([
311 {"content-type", "image/gif"},
312 {"content-length", "0"}
313 ])
314
315 conn = ReverseProxy.call(conn, "/disposition")
316
317 assert {"content-type", "image/gif"} in conn.resp_headers
318 end
319
320 test "with content-disposition header", %{conn: conn} do
321 disposition_headers_mock([
322 {"content-disposition", "attachment; filename=\"filename.jpg\""},
323 {"content-length", "0"}
324 ])
325
326 conn = ReverseProxy.call(conn, "/disposition")
327
328 assert {"content-disposition", "attachment; filename=\"filename.jpg\""} in conn.resp_headers
329 end
330 end
331 end