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.Plugs.RateLimiterTest do
6 use ExUnit.Case, async: true
10 alias Pleroma.Plugs.RateLimiter
12 import Pleroma.Factory
13 import Pleroma.Tests.Helpers, only: [clear_config: 1, clear_config: 2]
15 # Note: each example must work with separate buckets in order to prevent concurrency issues
17 clear_config([Pleroma.Web.Endpoint, :http, :ip])
18 clear_config(:rate_limit)
21 @limiter_name :test_init
23 clear_config([Pleroma.Plugs.RemoteIp, :enabled])
25 test "config is required for plug to work" do
26 Config.put([:rate_limit, @limiter_name], {1, 1})
27 Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
29 assert %{limits: {1, 1}, name: :test_init, opts: [name: :test_init]} ==
32 |> RateLimiter.action_settings()
35 [name: :nonexisting_limiter]
37 |> RateLimiter.action_settings()
40 test "it is disabled for localhost" do
41 Config.put([:rate_limit, @limiter_name], {1, 1})
42 Config.put([Pleroma.Web.Endpoint, :http, :ip], {127, 0, 0, 1})
43 Config.put([Pleroma.Plugs.RemoteIp, :enabled], false)
45 assert RateLimiter.disabled?() == true
48 test "it is disabled for socket" do
49 Config.put([:rate_limit, @limiter_name], {1, 1})
50 Config.put([Pleroma.Web.Endpoint, :http, :ip], {:local, "/path/to/pleroma.sock"})
51 Config.put([Pleroma.Plugs.RemoteIp, :enabled], false)
53 assert RateLimiter.disabled?() == true
56 test "it is enabled for socket when remote ip is enabled" do
57 Config.put([:rate_limit, @limiter_name], {1, 1})
58 Config.put([Pleroma.Web.Endpoint, :http, :ip], {:local, "/path/to/pleroma.sock"})
59 Config.put([Pleroma.Plugs.RemoteIp, :enabled], true)
61 assert RateLimiter.disabled?() == false
64 test "it restricts based on config values" do
65 limiter_name = :test_plug_opts
69 Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
70 Config.put([:rate_limit, limiter_name], {scale, limit})
72 plug_opts = RateLimiter.init(name: limiter_name)
73 conn = conn(:get, "/")
76 conn = RateLimiter.call(conn, plug_opts)
77 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
81 conn = RateLimiter.call(conn, plug_opts)
82 assert %{"error" => "Throttled"} = Phoenix.ConnTest.json_response(conn, :too_many_requests)
87 conn = conn(:get, "/")
89 conn = RateLimiter.call(conn, plug_opts)
90 assert {1, 4} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
92 refute conn.status == Plug.Conn.Status.code(:too_many_requests)
99 test "`bucket_name` option overrides default bucket name" do
100 limiter_name = :test_bucket_name
102 Config.put([:rate_limit, limiter_name], {1000, 5})
103 Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
105 base_bucket_name = "#{limiter_name}:group1"
106 plug_opts = RateLimiter.init(name: limiter_name, bucket_name: base_bucket_name)
108 conn = conn(:get, "/")
110 RateLimiter.call(conn, plug_opts)
111 assert {1, 4} = RateLimiter.inspect_bucket(conn, base_bucket_name, plug_opts)
112 assert {:error, :not_found} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
115 test "`params` option allows different queries to be tracked independently" do
116 limiter_name = :test_params
117 Config.put([:rate_limit, limiter_name], {1000, 5})
118 Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
120 plug_opts = RateLimiter.init(name: limiter_name, params: ["id"])
122 conn = conn(:get, "/?id=1")
123 conn = Plug.Conn.fetch_query_params(conn)
124 conn_2 = conn(:get, "/?id=2")
126 RateLimiter.call(conn, plug_opts)
127 assert {1, 4} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
128 assert {0, 5} = RateLimiter.inspect_bucket(conn_2, limiter_name, plug_opts)
131 test "it supports combination of options modifying bucket name" do
132 limiter_name = :test_options_combo
133 Config.put([:rate_limit, limiter_name], {1000, 5})
134 Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
136 base_bucket_name = "#{limiter_name}:group1"
139 RateLimiter.init(name: limiter_name, bucket_name: base_bucket_name, params: ["id"])
143 conn = conn(:get, "/?id=#{id}")
144 conn = Plug.Conn.fetch_query_params(conn)
145 conn_2 = conn(:get, "/?id=#{101}")
147 RateLimiter.call(conn, plug_opts)
148 assert {1, 4} = RateLimiter.inspect_bucket(conn, base_bucket_name, plug_opts)
149 assert {0, 5} = RateLimiter.inspect_bucket(conn_2, base_bucket_name, plug_opts)
153 describe "unauthenticated users" do
154 test "are restricted based on remote IP" do
155 limiter_name = :test_unauthenticated
156 Config.put([:rate_limit, limiter_name], [{1000, 5}, {1, 10}])
157 Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
159 plug_opts = RateLimiter.init(name: limiter_name)
161 conn = %{conn(:get, "/") | remote_ip: {127, 0, 0, 2}}
162 conn_2 = %{conn(:get, "/") | remote_ip: {127, 0, 0, 3}}
165 conn = RateLimiter.call(conn, plug_opts)
166 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
170 conn = RateLimiter.call(conn, plug_opts)
172 assert %{"error" => "Throttled"} = Phoenix.ConnTest.json_response(conn, :too_many_requests)
175 conn_2 = RateLimiter.call(conn_2, plug_opts)
176 assert {1, 4} = RateLimiter.inspect_bucket(conn_2, limiter_name, plug_opts)
178 refute conn_2.status == Plug.Conn.Status.code(:too_many_requests)
179 refute conn_2.resp_body
184 describe "authenticated users" do
186 Ecto.Adapters.SQL.Sandbox.checkout(Pleroma.Repo)
191 test "can have limits separate from unauthenticated connections" do
192 limiter_name = :test_authenticated1
196 Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
197 Config.put([:rate_limit, limiter_name], [{1000, 1}, {scale, limit}])
199 plug_opts = RateLimiter.init(name: limiter_name)
202 conn = conn(:get, "/") |> assign(:user, user)
205 conn = RateLimiter.call(conn, plug_opts)
206 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
210 conn = RateLimiter.call(conn, plug_opts)
212 assert %{"error" => "Throttled"} = Phoenix.ConnTest.json_response(conn, :too_many_requests)
216 test "different users are counted independently" do
217 limiter_name = :test_authenticated2
218 Config.put([:rate_limit, limiter_name], [{1, 10}, {1000, 5}])
219 Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
221 plug_opts = RateLimiter.init(name: limiter_name)
224 conn = conn(:get, "/") |> assign(:user, user)
226 user_2 = insert(:user)
227 conn_2 = conn(:get, "/") |> assign(:user, user_2)
230 conn = RateLimiter.call(conn, plug_opts)
231 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
234 conn = RateLimiter.call(conn, plug_opts)
235 assert %{"error" => "Throttled"} = Phoenix.ConnTest.json_response(conn, :too_many_requests)
238 conn_2 = RateLimiter.call(conn_2, plug_opts)
239 assert {1, 4} = RateLimiter.inspect_bucket(conn_2, limiter_name, plug_opts)
240 refute conn_2.status == Plug.Conn.Status.code(:too_many_requests)
241 refute conn_2.resp_body
246 test "doesn't crash due to a race condition when multiple requests are made at the same time and the bucket is not yet initialized" do
247 limiter_name = :test_race_condition
248 Pleroma.Config.put([:rate_limit, limiter_name], {1000, 5})
249 Pleroma.Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
251 opts = RateLimiter.init(name: limiter_name)
253 conn = conn(:get, "/")
254 conn_2 = conn(:get, "/")
261 RateLimiter.call(conn, opts)
267 send(pid1, :process2_up)
268 RateLimiter.call(conn_2, opts)
274 refute {:err, :not_found} == RateLimiter.inspect_bucket(conn, limiter_name, opts)