1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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 user_agent_mock(user_agent, invokes) do
22 json = Jason.encode!(%{"user-agent": user_agent})
25 |> expect(:request, fn :get, url, _, _, _ ->
26 Registry.register(ClientMock, url, 0)
30 {"content-type", "application/json"},
31 {"content-length", byte_size(json) |> to_string()}
34 |> expect(:stream_body, invokes, fn %{url: url} = client ->
35 case Registry.lookup(ClientMock, url) do
37 Registry.update_value(ClientMock, url, &(&1 + 1))
41 Registry.unregister(ClientMock, url)
47 describe "reverse proxy" do
48 test "do not track successful request", %{conn: conn} do
49 user_agent_mock("hackney/1.15.1", 2)
52 conn = ReverseProxy.call(conn, url)
54 assert conn.status == 200
55 assert Cachex.get(:failed_proxy_url_cache, url) == {:ok, nil}
59 describe "user-agent" do
60 test "don't keep", %{conn: conn} do
61 user_agent_mock("hackney/1.15.1", 2)
62 conn = ReverseProxy.call(conn, "/user-agent")
63 assert json_response(conn, 200) == %{"user-agent" => "hackney/1.15.1"}
66 test "keep", %{conn: conn} do
67 user_agent_mock(Pleroma.Application.user_agent(), 2)
68 conn = ReverseProxy.call(conn, "/user-agent-keep", keep_user_agent: true)
69 assert json_response(conn, 200) == %{"user-agent" => Pleroma.Application.user_agent()}
73 test "closed connection", %{conn: conn} do
75 |> expect(:request, fn :get, "/closed", _, _, _ -> {:ok, 200, [], %{}} end)
76 |> expect(:stream_body, fn _ -> {:error, :closed} end)
77 |> expect(:close, fn _ -> :ok end)
79 conn = ReverseProxy.call(conn, "/closed")
83 defp stream_mock(invokes, with_close? \\ false) do
85 |> expect(:request, fn :get, "/stream-bytes/" <> length, _, _, _ ->
86 Registry.register(ClientMock, "/stream-bytes/" <> length, 0)
88 {:ok, 200, [{"content-type", "application/octet-stream"}],
89 %{url: "/stream-bytes/" <> length}}
91 |> expect(:stream_body, invokes, fn %{url: "/stream-bytes/" <> length} = client ->
92 max = String.to_integer(length)
94 case Registry.lookup(ClientMock, "/stream-bytes/" <> length) do
95 [{_, current}] when current < max ->
96 Registry.update_value(
98 "/stream-bytes/" <> length,
102 {:ok, "0123456789", client}
105 Registry.unregister(ClientMock, "/stream-bytes/" <> length)
111 expect(ClientMock, :close, fn _ -> :ok end)
115 describe "max_body" do
116 test "length returns error if content-length more than option", %{conn: conn} do
117 user_agent_mock("hackney/1.15.1", 0)
119 assert capture_log(fn ->
120 ReverseProxy.call(conn, "/huge-file", max_body_length: 4)
122 "[error] Elixir.Pleroma.ReverseProxy: request to \"/huge-file\" failed: :body_too_large"
124 assert {:ok, true} == Cachex.get(:failed_proxy_url_cache, "/huge-file")
126 assert capture_log(fn ->
127 ReverseProxy.call(conn, "/huge-file", max_body_length: 4)
131 test "max_body_length returns error if streaming body more than that option", %{conn: conn} do
134 assert capture_log(fn ->
135 ReverseProxy.call(conn, "/stream-bytes/50", max_body_length: 30)
137 "[warn] Elixir.Pleroma.ReverseProxy request to /stream-bytes/50 failed while reading/chunking: :body_too_large"
141 describe "HEAD requests" do
142 test "common", %{conn: conn} do
144 |> expect(:request, fn :head, "/head", _, _, _ ->
145 {:ok, 200, [{"content-type", "text/html; charset=utf-8"}]}
148 conn = ReverseProxy.call(Map.put(conn, :method, "HEAD"), "/head")
149 assert html_response(conn, 200) == ""
153 defp error_mock(status) when is_integer(status) do
155 |> expect(:request, fn :get, "/status/" <> _, _, _, _ ->
160 describe "returns error on" do
161 test "500", %{conn: conn} do
165 capture_log(fn -> ReverseProxy.call(conn, url) end) =~
166 "[error] Elixir.Pleroma.ReverseProxy: request to /status/500 failed with HTTP status 500"
168 assert Cachex.get(:failed_proxy_url_cache, url) == {:ok, true}
170 {:ok, ttl} = Cachex.ttl(:failed_proxy_url_cache, url)
174 test "400", %{conn: conn} do
178 capture_log(fn -> ReverseProxy.call(conn, url) end) =~
179 "[error] Elixir.Pleroma.ReverseProxy: request to /status/400 failed with HTTP status 400"
181 assert Cachex.get(:failed_proxy_url_cache, url) == {:ok, true}
182 assert Cachex.ttl(:failed_proxy_url_cache, url) == {:ok, nil}
185 test "403", %{conn: conn} do
190 ReverseProxy.call(conn, url, failed_request_ttl: :timer.seconds(120))
192 "[error] Elixir.Pleroma.ReverseProxy: request to /status/403 failed with HTTP status 403"
194 {:ok, ttl} = Cachex.ttl(:failed_proxy_url_cache, url)
198 test "204", %{conn: conn} do
200 expect(ClientMock, :request, fn :get, _url, _, _, _ -> {:ok, 204, [], %{}} end)
203 conn = ReverseProxy.call(conn, url)
204 assert conn.resp_body == "Request failed: No Content"
207 "[error] Elixir.Pleroma.ReverseProxy: request to \"/status/204\" failed with HTTP status 204"
209 assert Cachex.get(:failed_proxy_url_cache, url) == {:ok, true}
210 assert Cachex.ttl(:failed_proxy_url_cache, url) == {:ok, nil}
214 test "streaming", %{conn: conn} do
216 conn = ReverseProxy.call(conn, "/stream-bytes/200")
217 assert conn.state == :chunked
218 assert byte_size(conn.resp_body) == 200
219 assert Conn.get_resp_header(conn, "content-type") == ["application/octet-stream"]
222 defp headers_mock(_) do
224 |> expect(:request, fn :get, "/headers", headers, _, _ ->
225 Registry.register(ClientMock, "/headers", 0)
226 {:ok, 200, [{"content-type", "application/json"}], %{url: "/headers", headers: headers}}
228 |> expect(:stream_body, 2, fn %{url: url, headers: headers} = client ->
229 case Registry.lookup(ClientMock, url) do
231 Registry.update_value(ClientMock, url, &(&1 + 1))
232 headers = for {k, v} <- headers, into: %{}, do: {String.capitalize(k), v}
233 {:ok, Jason.encode!(%{headers: headers}), client}
236 Registry.unregister(ClientMock, url)
244 describe "keep request headers" do
245 setup [:headers_mock]
247 test "header passes", %{conn: conn} do
254 |> ReverseProxy.call("/headers")
256 %{"headers" => headers} = json_response(conn, 200)
257 assert headers["Accept"] == "text/html"
260 test "header is filtered", %{conn: conn} do
267 |> ReverseProxy.call("/headers")
269 %{"headers" => headers} = json_response(conn, 200)
270 refute headers["Accept-Language"]
274 test "returns 400 on non GET, HEAD requests", %{conn: conn} do
275 conn = ReverseProxy.call(Map.put(conn, :method, "POST"), "/ip")
276 assert conn.status == 400
279 describe "cache resp headers" do
280 test "add cache-control", %{conn: conn} do
282 |> expect(:request, fn :get, "/cache", _, _, _ ->
283 {:ok, 200, [{"ETag", "some ETag"}], %{}}
285 |> expect(:stream_body, fn _ -> :done end)
287 conn = ReverseProxy.call(conn, "/cache")
288 assert {"cache-control", "public, max-age=1209600"} in conn.resp_headers
292 defp disposition_headers_mock(headers) do
294 |> expect(:request, fn :get, "/disposition", _, _, _ ->
295 Registry.register(ClientMock, "/disposition", 0)
297 {:ok, 200, headers, %{url: "/disposition"}}
299 |> expect(:stream_body, 2, fn %{url: "/disposition"} = client ->
300 case Registry.lookup(ClientMock, "/disposition") do
302 Registry.update_value(ClientMock, "/disposition", &(&1 + 1))
306 Registry.unregister(ClientMock, "/disposition")
312 describe "response content disposition header" do
313 test "not atachment", %{conn: conn} do
314 disposition_headers_mock([
315 {"content-type", "image/gif"},
316 {"content-length", "0"}
319 conn = ReverseProxy.call(conn, "/disposition")
321 assert {"content-type", "image/gif"} in conn.resp_headers
324 test "with content-disposition header", %{conn: conn} do
325 disposition_headers_mock([
326 {"content-disposition", "attachment; filename=\"filename.jpg\""},
327 {"content-length", "0"}
330 conn = ReverseProxy.call(conn, "/disposition")
332 assert {"content-disposition", "attachment; filename=\"filename.jpg\""} in conn.resp_headers