Merge branch 'develop' into default-db-name
[akkoma] / test / pleroma / web / plugs / rate_limiter_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Plugs.RateLimiterTest do
6 use Pleroma.Web.ConnCase
7
8 alias Phoenix.ConnTest
9 alias Pleroma.Web.Plugs.RateLimiter
10 alias Plug.Conn
11
12 import Pleroma.Factory
13 import Pleroma.Tests.Helpers, only: [clear_config: 1, clear_config: 2]
14
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)
18
19 describe "config" do
20 @limiter_name :test_init
21 setup do: clear_config([Pleroma.Web.Plugs.RemoteIp, :enabled])
22
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})
26
27 assert %{limits: {1, 1}, name: :test_init, opts: [name: :test_init]} ==
28 [name: @limiter_name]
29 |> RateLimiter.init()
30 |> RateLimiter.action_settings()
31
32 assert nil ==
33 [name: :nonexisting_limiter]
34 |> RateLimiter.init()
35 |> RateLimiter.action_settings()
36 end
37 end
38
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))
41 end
42
43 test "it is enabled if remote ip found" do
44 refute RateLimiter.disabled?(Conn.assign(build_conn(), :remote_ip_found, true))
45 end
46
47 test "it is enabled if remote_ip_found flag doesn't exist" do
48 refute RateLimiter.disabled?(build_conn())
49 end
50
51 @tag :erratic
52 test "it restricts based on config values" do
53 limiter_name = :test_plug_opts
54 scale = 80
55 limit = 5
56
57 clear_config([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
58 clear_config([:rate_limit, limiter_name], {scale, limit})
59
60 plug_opts = RateLimiter.init(name: limiter_name)
61 conn = build_conn(:get, "/")
62
63 for i <- 1..5 do
64 conn = RateLimiter.call(conn, plug_opts)
65 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
66 Process.sleep(10)
67 end
68
69 conn = RateLimiter.call(conn, plug_opts)
70 assert %{"error" => "Throttled"} = ConnTest.json_response(conn, :too_many_requests)
71 assert conn.halted
72
73 Process.sleep(50)
74
75 conn = build_conn(:get, "/")
76
77 conn = RateLimiter.call(conn, plug_opts)
78 assert {1, 4} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
79
80 refute conn.status == Conn.Status.code(:too_many_requests)
81 refute conn.resp_body
82 refute conn.halted
83 end
84
85 describe "options" do
86 test "`bucket_name` option overrides default bucket name" do
87 limiter_name = :test_bucket_name
88
89 clear_config([:rate_limit, limiter_name], {1000, 5})
90 clear_config([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
91
92 base_bucket_name = "#{limiter_name}:group1"
93 plug_opts = RateLimiter.init(name: limiter_name, bucket_name: base_bucket_name)
94
95 conn = build_conn(:get, "/")
96
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)
100 end
101
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})
106
107 plug_opts = RateLimiter.init(name: limiter_name, params: ["id"])
108
109 conn = build_conn(:get, "/?id=1")
110 conn = Conn.fetch_query_params(conn)
111 conn_2 = build_conn(:get, "/?id=2")
112
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)
116 end
117
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})
122
123 base_bucket_name = "#{limiter_name}:group1"
124
125 plug_opts =
126 RateLimiter.init(name: limiter_name, bucket_name: base_bucket_name, params: ["id"])
127
128 id = "100"
129
130 conn = build_conn(:get, "/?id=#{id}")
131 conn = Conn.fetch_query_params(conn)
132 conn_2 = build_conn(:get, "/?id=#{101}")
133
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)
137 end
138 end
139
140 describe "unauthenticated users" do
141 @tag :erratic
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})
146
147 plug_opts = RateLimiter.init(name: limiter_name)
148
149 conn = %{build_conn(:get, "/") | remote_ip: {127, 0, 0, 2}}
150 conn_2 = %{build_conn(:get, "/") | remote_ip: {127, 0, 0, 3}}
151
152 for i <- 1..5 do
153 conn = RateLimiter.call(conn, plug_opts)
154 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
155 refute conn.halted
156 end
157
158 conn = RateLimiter.call(conn, plug_opts)
159
160 assert %{"error" => "Throttled"} = ConnTest.json_response(conn, :too_many_requests)
161 assert conn.halted
162
163 conn_2 = RateLimiter.call(conn_2, plug_opts)
164 assert {1, 4} = RateLimiter.inspect_bucket(conn_2, limiter_name, plug_opts)
165
166 refute conn_2.status == Conn.Status.code(:too_many_requests)
167 refute conn_2.resp_body
168 refute conn_2.halted
169 end
170 end
171
172 describe "authenticated users" do
173 setup do
174 Ecto.Adapters.SQL.Sandbox.checkout(Pleroma.Repo)
175
176 :ok
177 end
178
179 @tag :erratic
180 test "can have limits separate from unauthenticated connections" do
181 limiter_name = :test_authenticated1
182
183 scale = 50
184 limit = 5
185 clear_config([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
186 clear_config([:rate_limit, limiter_name], [{1000, 1}, {scale, limit}])
187
188 plug_opts = RateLimiter.init(name: limiter_name)
189
190 user = insert(:user)
191 conn = build_conn(:get, "/") |> assign(:user, user)
192
193 for i <- 1..5 do
194 conn = RateLimiter.call(conn, plug_opts)
195 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
196 refute conn.halted
197 end
198
199 conn = RateLimiter.call(conn, plug_opts)
200
201 assert %{"error" => "Throttled"} = ConnTest.json_response(conn, :too_many_requests)
202 assert conn.halted
203 end
204
205 @tag :erratic
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})
210
211 plug_opts = RateLimiter.init(name: limiter_name)
212
213 user = insert(:user)
214 conn = build_conn(:get, "/") |> assign(:user, user)
215
216 user_2 = insert(:user)
217 conn_2 = build_conn(:get, "/") |> assign(:user, user_2)
218
219 for i <- 1..5 do
220 conn = RateLimiter.call(conn, plug_opts)
221 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
222 end
223
224 conn = RateLimiter.call(conn, plug_opts)
225 assert %{"error" => "Throttled"} = ConnTest.json_response(conn, :too_many_requests)
226 assert conn.halted
227
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
232 refute conn_2.halted
233 end
234 end
235
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})
240
241 opts = RateLimiter.init(name: limiter_name)
242
243 conn = build_conn(:get, "/")
244 conn_2 = build_conn(:get, "/")
245
246 %Task{pid: pid1} =
247 task1 =
248 Task.async(fn ->
249 receive do
250 :process2_up ->
251 RateLimiter.call(conn, opts)
252 end
253 end)
254
255 task2 =
256 Task.async(fn ->
257 send(pid1, :process2_up)
258 RateLimiter.call(conn_2, opts)
259 end)
260
261 Task.await(task1)
262 Task.await(task2)
263
264 refute {:err, :not_found} == RateLimiter.inspect_bucket(conn, limiter_name, opts)
265 end
266 end