1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.ReverseProxyTest do
6 use Pleroma.Web.ConnCase
7 import ExUnit.CaptureLog
10 alias Pleroma.ReverseProxy
11 alias Pleroma.ReverseProxy.ClientMock
15 {:ok, _} = Registry.start_link(keys: :unique, name: ClientMock)
19 setup :verify_on_exit!
21 defp request_mock(invokes) do
23 |> expect(:request, fn :get, url, headers, _body, _opts ->
24 Registry.register(ClientMock, url, 0)
25 body = headers |> Enum.into(%{}) |> Jason.encode!()
29 {"content-type", "application/json"},
30 {"content-length", byte_size(body) |> to_string()}
31 ], %{url: url, body: body}}
33 |> expect(:stream_body, invokes, fn %{url: url, body: body} = client ->
34 case Registry.lookup(ClientMock, url) do
36 Registry.update_value(ClientMock, url, &(&1 + 1))
40 Registry.unregister(ClientMock, url)
46 describe "reverse proxy" do
47 test "do not track successful request", %{conn: conn} do
51 conn = ReverseProxy.call(conn, url)
53 assert conn.status == 200
54 assert Cachex.get(:failed_proxy_url_cache, url) == {:ok, nil}
58 test "use Pleroma's user agent in the request; don't pass the client's", %{conn: conn} do
63 |> Plug.Conn.put_req_header("user-agent", "fake/1.0")
64 |> ReverseProxy.call("/user-agent")
66 assert json_response(conn, 200) == %{"user-agent" => Pleroma.Application.user_agent()}
69 test "closed connection", %{conn: conn} do
71 |> expect(:request, fn :get, "/closed", _, _, _ -> {:ok, 200, [], %{}} end)
72 |> expect(:stream_body, fn _ -> {:error, :closed} end)
73 |> expect(:close, fn _ -> :ok end)
75 conn = ReverseProxy.call(conn, "/closed")
79 defp stream_mock(invokes, with_close? \\ false) do
81 |> expect(:request, fn :get, "/stream-bytes/" <> length, _, _, _ ->
82 Registry.register(ClientMock, "/stream-bytes/" <> length, 0)
84 {:ok, 200, [{"content-type", "application/octet-stream"}],
85 %{url: "/stream-bytes/" <> length}}
87 |> expect(:stream_body, invokes, fn %{url: "/stream-bytes/" <> length} = client ->
88 max = String.to_integer(length)
90 case Registry.lookup(ClientMock, "/stream-bytes/" <> length) do
91 [{_, current}] when current < max ->
92 Registry.update_value(
94 "/stream-bytes/" <> length,
98 {:ok, "0123456789", client}
101 Registry.unregister(ClientMock, "/stream-bytes/" <> length)
107 expect(ClientMock, :close, fn _ -> :ok end)
111 describe "max_body" do
112 test "length returns error if content-length more than option", %{conn: conn} do
115 assert capture_log(fn ->
116 ReverseProxy.call(conn, "/huge-file", max_body_length: 4)
118 "[error] Elixir.Pleroma.ReverseProxy: request to \"/huge-file\" failed: :body_too_large"
120 assert {:ok, true} == Cachex.get(:failed_proxy_url_cache, "/huge-file")
122 assert capture_log(fn ->
123 ReverseProxy.call(conn, "/huge-file", max_body_length: 4)
127 test "max_body_length returns error if streaming body more than that option", %{conn: conn} do
130 assert capture_log(fn ->
131 ReverseProxy.call(conn, "/stream-bytes/50", max_body_length: 30)
133 "Elixir.Pleroma.ReverseProxy request to /stream-bytes/50 failed while reading/chunking: :body_too_large"
137 describe "HEAD requests" do
138 test "common", %{conn: conn} do
140 |> expect(:request, fn :head, "/head", _, _, _ ->
141 {:ok, 200, [{"content-type", "text/html; charset=utf-8"}]}
144 conn = ReverseProxy.call(Map.put(conn, :method, "HEAD"), "/head")
145 assert html_response(conn, 200) == ""
149 defp error_mock(status) when is_integer(status) do
151 |> expect(:request, fn :get, "/status/" <> _, _, _, _ ->
156 describe "returns error on" do
157 test "500", %{conn: conn} do
161 capture_log(fn -> ReverseProxy.call(conn, url) end) =~
162 "[error] Elixir.Pleroma.ReverseProxy: request to /status/500 failed with HTTP status 500"
164 assert Cachex.get(:failed_proxy_url_cache, url) == {:ok, true}
166 {:ok, ttl} = Cachex.ttl(:failed_proxy_url_cache, url)
170 test "400", %{conn: conn} do
174 capture_log(fn -> ReverseProxy.call(conn, url) end) =~
175 "[error] Elixir.Pleroma.ReverseProxy: request to /status/400 failed with HTTP status 400"
177 assert Cachex.get(:failed_proxy_url_cache, url) == {:ok, true}
178 assert Cachex.ttl(:failed_proxy_url_cache, url) == {:ok, nil}
181 test "403", %{conn: conn} do
186 ReverseProxy.call(conn, url, failed_request_ttl: :timer.seconds(120))
188 "[error] Elixir.Pleroma.ReverseProxy: request to /status/403 failed with HTTP status 403"
190 {:ok, ttl} = Cachex.ttl(:failed_proxy_url_cache, url)
194 test "204", %{conn: conn} do
196 expect(ClientMock, :request, fn :get, _url, _, _, _ -> {:ok, 204, [], %{}} end)
199 conn = ReverseProxy.call(conn, url)
200 assert conn.resp_body == "Request failed: No Content"
203 "[error] Elixir.Pleroma.ReverseProxy: request to \"/status/204\" failed with HTTP status 204"
205 assert Cachex.get(:failed_proxy_url_cache, url) == {:ok, true}
206 assert Cachex.ttl(:failed_proxy_url_cache, url) == {:ok, nil}
210 test "streaming", %{conn: conn} do
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"]
218 defp headers_mock(_) do
220 |> expect(:request, fn :get, "/headers", headers, _, _ ->
221 Registry.register(ClientMock, "/headers", 0)
222 {:ok, 200, [{"content-type", "application/json"}], %{url: "/headers", headers: headers}}
224 |> expect(:stream_body, 2, fn %{url: url, headers: headers} = client ->
225 case Registry.lookup(ClientMock, url) do
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}
232 Registry.unregister(ClientMock, url)
240 describe "keep request headers" do
241 setup [:headers_mock]
243 test "header passes", %{conn: conn} do
250 |> ReverseProxy.call("/headers")
252 %{"headers" => headers} = json_response(conn, 200)
253 assert headers["Accept"] == "text/html"
256 test "header is filtered", %{conn: conn} do
263 |> ReverseProxy.call("/headers")
265 %{"headers" => headers} = json_response(conn, 200)
266 refute headers["Accept-Language"]
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
275 describe "cache resp headers" do
276 test "add cache-control", %{conn: conn} do
278 |> expect(:request, fn :get, "/cache", _, _, _ ->
279 {:ok, 200, [{"ETag", "some ETag"}], %{}}
281 |> expect(:stream_body, fn _ -> :done end)
283 conn = ReverseProxy.call(conn, "/cache")
284 assert {"cache-control", "public, max-age=1209600"} in conn.resp_headers
288 defp disposition_headers_mock(headers) do
290 |> expect(:request, fn :get, "/disposition", _, _, _ ->
291 Registry.register(ClientMock, "/disposition", 0)
293 {:ok, 200, headers, %{url: "/disposition"}}
295 |> expect(:stream_body, 2, fn %{url: "/disposition"} = client ->
296 case Registry.lookup(ClientMock, "/disposition") do
298 Registry.update_value(ClientMock, "/disposition", &(&1 + 1))
302 Registry.unregister(ClientMock, "/disposition")
308 describe "response content disposition header" do
309 test "not attachment", %{conn: conn} do
310 disposition_headers_mock([
311 {"content-type", "image/gif"},
312 {"content-length", "0"}
315 conn = ReverseProxy.call(conn, "/disposition")
317 assert {"content-type", "image/gif"} in conn.resp_headers
320 test "with content-disposition header", %{conn: conn} do
321 disposition_headers_mock([
322 {"content-disposition", "attachment; filename=\"filename.jpg\""},
323 {"content-length", "0"}
326 conn = ReverseProxy.call(conn, "/disposition")
328 assert {"content-disposition", "attachment; filename=\"filename.jpg\""} in conn.resp_headers