1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Plugs.RateLimiterTest do
6 use Pleroma.Web.ConnCase
9 alias Pleroma.Web.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
16 setup do: clear_config([Pleroma.Web.Endpoint, :http, :ip])
17 setup do: clear_config(:rate_limit)
20 @limiter_name :test_init
21 setup do: clear_config([Pleroma.Web.Plugs.RemoteIp, :enabled])
23 test "config is required for plug to work" do
24 clear_config([:rate_limit, @limiter_name], {1, 1})
25 clear_config([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
27 assert %{limits: {1, 1}, name: :test_init, opts: [name: :test_init]} ==
30 |> RateLimiter.action_settings()
33 [name: :nonexisting_limiter]
35 |> RateLimiter.action_settings()
39 test "it is disabled if it remote ip plug is enabled but no remote ip is found" do
40 assert RateLimiter.disabled?(Conn.assign(build_conn(), :remote_ip_found, false))
43 test "it is enabled if remote ip found" do
44 refute RateLimiter.disabled?(Conn.assign(build_conn(), :remote_ip_found, true))
47 test "it is enabled if remote_ip_found flag doesn't exist" do
48 refute RateLimiter.disabled?(build_conn())
52 test "it restricts based on config values" do
53 limiter_name = :test_plug_opts
57 clear_config([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
58 clear_config([:rate_limit, limiter_name], {scale, limit})
60 plug_opts = RateLimiter.init(name: limiter_name)
61 conn = build_conn(:get, "/")
64 conn = RateLimiter.call(conn, plug_opts)
65 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
69 conn = RateLimiter.call(conn, plug_opts)
70 assert %{"error" => "Throttled"} = ConnTest.json_response(conn, :too_many_requests)
75 conn = build_conn(:get, "/")
77 conn = RateLimiter.call(conn, plug_opts)
78 assert {1, 4} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
80 refute conn.status == Conn.Status.code(:too_many_requests)
86 test "`bucket_name` option overrides default bucket name" do
87 limiter_name = :test_bucket_name
89 clear_config([:rate_limit, limiter_name], {1000, 5})
90 clear_config([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
92 base_bucket_name = "#{limiter_name}:group1"
93 plug_opts = RateLimiter.init(name: limiter_name, bucket_name: base_bucket_name)
95 conn = build_conn(:get, "/")
97 RateLimiter.call(conn, plug_opts)
98 assert {1, 4} = RateLimiter.inspect_bucket(conn, base_bucket_name, plug_opts)
99 assert {:error, :not_found} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
102 test "`params` option allows different queries to be tracked independently" do
103 limiter_name = :test_params
104 clear_config([:rate_limit, limiter_name], {1000, 5})
105 clear_config([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
107 plug_opts = RateLimiter.init(name: limiter_name, params: ["id"])
109 conn = build_conn(:get, "/?id=1")
110 conn = Conn.fetch_query_params(conn)
111 conn_2 = build_conn(:get, "/?id=2")
113 RateLimiter.call(conn, plug_opts)
114 assert {1, 4} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
115 assert {0, 5} = RateLimiter.inspect_bucket(conn_2, limiter_name, plug_opts)
118 test "it supports combination of options modifying bucket name" do
119 limiter_name = :test_options_combo
120 clear_config([:rate_limit, limiter_name], {1000, 5})
121 clear_config([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
123 base_bucket_name = "#{limiter_name}:group1"
126 RateLimiter.init(name: limiter_name, bucket_name: base_bucket_name, params: ["id"])
130 conn = build_conn(:get, "/?id=#{id}")
131 conn = Conn.fetch_query_params(conn)
132 conn_2 = build_conn(:get, "/?id=#{101}")
134 RateLimiter.call(conn, plug_opts)
135 assert {1, 4} = RateLimiter.inspect_bucket(conn, base_bucket_name, plug_opts)
136 assert {0, 5} = RateLimiter.inspect_bucket(conn_2, base_bucket_name, plug_opts)
140 describe "unauthenticated users" do
142 test "are restricted based on remote IP" do
143 limiter_name = :test_unauthenticated
144 clear_config([:rate_limit, limiter_name], [{1000, 5}, {1, 10}])
145 clear_config([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
147 plug_opts = RateLimiter.init(name: limiter_name)
149 conn = %{build_conn(:get, "/") | remote_ip: {127, 0, 0, 2}}
150 conn_2 = %{build_conn(:get, "/") | remote_ip: {127, 0, 0, 3}}
153 conn = RateLimiter.call(conn, plug_opts)
154 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
158 conn = RateLimiter.call(conn, plug_opts)
160 assert %{"error" => "Throttled"} = ConnTest.json_response(conn, :too_many_requests)
163 conn_2 = RateLimiter.call(conn_2, plug_opts)
164 assert {1, 4} = RateLimiter.inspect_bucket(conn_2, limiter_name, plug_opts)
166 refute conn_2.status == Conn.Status.code(:too_many_requests)
167 refute conn_2.resp_body
172 describe "authenticated users" do
174 Ecto.Adapters.SQL.Sandbox.checkout(Pleroma.Repo)
180 test "can have limits separate from unauthenticated connections" do
181 limiter_name = :test_authenticated1
185 clear_config([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
186 clear_config([:rate_limit, limiter_name], [{1000, 1}, {scale, limit}])
188 plug_opts = RateLimiter.init(name: limiter_name)
191 conn = build_conn(:get, "/") |> assign(:user, user)
194 conn = RateLimiter.call(conn, plug_opts)
195 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
199 conn = RateLimiter.call(conn, plug_opts)
201 assert %{"error" => "Throttled"} = ConnTest.json_response(conn, :too_many_requests)
206 test "different users are counted independently" do
207 limiter_name = :test_authenticated2
208 clear_config([:rate_limit, limiter_name], [{1, 10}, {1000, 5}])
209 clear_config([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
211 plug_opts = RateLimiter.init(name: limiter_name)
214 conn = build_conn(:get, "/") |> assign(:user, user)
216 user_2 = insert(:user)
217 conn_2 = build_conn(:get, "/") |> assign(:user, user_2)
220 conn = RateLimiter.call(conn, plug_opts)
221 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
224 conn = RateLimiter.call(conn, plug_opts)
225 assert %{"error" => "Throttled"} = ConnTest.json_response(conn, :too_many_requests)
228 conn_2 = RateLimiter.call(conn_2, plug_opts)
229 assert {1, 4} = RateLimiter.inspect_bucket(conn_2, limiter_name, plug_opts)
230 refute conn_2.status == Conn.Status.code(:too_many_requests)
231 refute conn_2.resp_body
236 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
237 limiter_name = :test_race_condition
238 clear_config([:rate_limit, limiter_name], {1000, 5})
239 clear_config([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
241 opts = RateLimiter.init(name: limiter_name)
243 conn = build_conn(:get, "/")
244 conn_2 = build_conn(:get, "/")
251 RateLimiter.call(conn, opts)
257 send(pid1, :process2_up)
258 RateLimiter.call(conn_2, opts)
264 refute {:err, :not_found} == RateLimiter.inspect_bucket(conn, limiter_name, opts)