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())
51 test "it restricts based on config values" do
52 limiter_name = :test_plug_opts
56 clear_config([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
57 clear_config([:rate_limit, limiter_name], {scale, limit})
59 plug_opts = RateLimiter.init(name: limiter_name)
60 conn = build_conn(:get, "/")
63 conn = RateLimiter.call(conn, plug_opts)
64 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
68 conn = RateLimiter.call(conn, plug_opts)
69 assert %{"error" => "Throttled"} = ConnTest.json_response(conn, :too_many_requests)
74 conn = build_conn(:get, "/")
76 conn = RateLimiter.call(conn, plug_opts)
77 assert {1, 4} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
79 refute conn.status == Conn.Status.code(:too_many_requests)
85 test "`bucket_name` option overrides default bucket name" do
86 limiter_name = :test_bucket_name
88 clear_config([:rate_limit, limiter_name], {1000, 5})
89 clear_config([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
91 base_bucket_name = "#{limiter_name}:group1"
92 plug_opts = RateLimiter.init(name: limiter_name, bucket_name: base_bucket_name)
94 conn = build_conn(:get, "/")
96 RateLimiter.call(conn, plug_opts)
97 assert {1, 4} = RateLimiter.inspect_bucket(conn, base_bucket_name, plug_opts)
98 assert {:error, :not_found} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
101 test "`params` option allows different queries to be tracked independently" do
102 limiter_name = :test_params
103 clear_config([:rate_limit, limiter_name], {1000, 5})
104 clear_config([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
106 plug_opts = RateLimiter.init(name: limiter_name, params: ["id"])
108 conn = build_conn(:get, "/?id=1")
109 conn = Conn.fetch_query_params(conn)
110 conn_2 = build_conn(:get, "/?id=2")
112 RateLimiter.call(conn, plug_opts)
113 assert {1, 4} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
114 assert {0, 5} = RateLimiter.inspect_bucket(conn_2, limiter_name, plug_opts)
117 test "it supports combination of options modifying bucket name" do
118 limiter_name = :test_options_combo
119 clear_config([:rate_limit, limiter_name], {1000, 5})
120 clear_config([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
122 base_bucket_name = "#{limiter_name}:group1"
125 RateLimiter.init(name: limiter_name, bucket_name: base_bucket_name, params: ["id"])
129 conn = build_conn(:get, "/?id=#{id}")
130 conn = Conn.fetch_query_params(conn)
131 conn_2 = build_conn(:get, "/?id=#{101}")
133 RateLimiter.call(conn, plug_opts)
134 assert {1, 4} = RateLimiter.inspect_bucket(conn, base_bucket_name, plug_opts)
135 assert {0, 5} = RateLimiter.inspect_bucket(conn_2, base_bucket_name, plug_opts)
139 describe "unauthenticated users" do
140 test "are restricted based on remote IP" do
141 limiter_name = :test_unauthenticated
142 clear_config([:rate_limit, limiter_name], [{1000, 5}, {1, 10}])
143 clear_config([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
145 plug_opts = RateLimiter.init(name: limiter_name)
147 conn = %{build_conn(:get, "/") | remote_ip: {127, 0, 0, 2}}
148 conn_2 = %{build_conn(:get, "/") | remote_ip: {127, 0, 0, 3}}
151 conn = RateLimiter.call(conn, plug_opts)
152 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
156 conn = RateLimiter.call(conn, plug_opts)
158 assert %{"error" => "Throttled"} = ConnTest.json_response(conn, :too_many_requests)
161 conn_2 = RateLimiter.call(conn_2, plug_opts)
162 assert {1, 4} = RateLimiter.inspect_bucket(conn_2, limiter_name, plug_opts)
164 refute conn_2.status == Conn.Status.code(:too_many_requests)
165 refute conn_2.resp_body
170 describe "authenticated users" do
172 Ecto.Adapters.SQL.Sandbox.checkout(Pleroma.Repo)
177 test "can have limits separate from unauthenticated connections" do
178 limiter_name = :test_authenticated1
182 clear_config([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
183 clear_config([:rate_limit, limiter_name], [{1000, 1}, {scale, limit}])
185 plug_opts = RateLimiter.init(name: limiter_name)
188 conn = build_conn(:get, "/") |> assign(:user, user)
191 conn = RateLimiter.call(conn, plug_opts)
192 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
196 conn = RateLimiter.call(conn, plug_opts)
198 assert %{"error" => "Throttled"} = ConnTest.json_response(conn, :too_many_requests)
202 test "different users are counted independently" do
203 limiter_name = :test_authenticated2
204 clear_config([:rate_limit, limiter_name], [{1, 10}, {1000, 5}])
205 clear_config([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
207 plug_opts = RateLimiter.init(name: limiter_name)
210 conn = build_conn(:get, "/") |> assign(:user, user)
212 user_2 = insert(:user)
213 conn_2 = build_conn(:get, "/") |> assign(:user, user_2)
216 conn = RateLimiter.call(conn, plug_opts)
217 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
220 conn = RateLimiter.call(conn, plug_opts)
221 assert %{"error" => "Throttled"} = ConnTest.json_response(conn, :too_many_requests)
224 conn_2 = RateLimiter.call(conn_2, plug_opts)
225 assert {1, 4} = RateLimiter.inspect_bucket(conn_2, limiter_name, plug_opts)
226 refute conn_2.status == Conn.Status.code(:too_many_requests)
227 refute conn_2.resp_body
232 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
233 limiter_name = :test_race_condition
234 clear_config([:rate_limit, limiter_name], {1000, 5})
235 clear_config([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
237 opts = RateLimiter.init(name: limiter_name)
239 conn = build_conn(:get, "/")
240 conn_2 = build_conn(:get, "/")
247 RateLimiter.call(conn, opts)
253 send(pid1, :process2_up)
254 RateLimiter.call(conn_2, opts)
260 refute {:err, :not_found} == RateLimiter.inspect_bucket(conn, limiter_name, opts)