Merge remote-tracking branch 'remotes/origin/develop' into feature/object-hashtags...
[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 test "it restricts based on config values" do
52 limiter_name = :test_plug_opts
53 scale = 80
54 limit = 5
55
56 clear_config([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
57 clear_config([:rate_limit, limiter_name], {scale, limit})
58
59 plug_opts = RateLimiter.init(name: limiter_name)
60 conn = build_conn(:get, "/")
61
62 for i <- 1..5 do
63 conn = RateLimiter.call(conn, plug_opts)
64 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
65 Process.sleep(10)
66 end
67
68 conn = RateLimiter.call(conn, plug_opts)
69 assert %{"error" => "Throttled"} = ConnTest.json_response(conn, :too_many_requests)
70 assert conn.halted
71
72 Process.sleep(50)
73
74 conn = build_conn(:get, "/")
75
76 conn = RateLimiter.call(conn, plug_opts)
77 assert {1, 4} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
78
79 refute conn.status == Conn.Status.code(:too_many_requests)
80 refute conn.resp_body
81 refute conn.halted
82 end
83
84 describe "options" do
85 test "`bucket_name` option overrides default bucket name" do
86 limiter_name = :test_bucket_name
87
88 clear_config([:rate_limit, limiter_name], {1000, 5})
89 clear_config([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
90
91 base_bucket_name = "#{limiter_name}:group1"
92 plug_opts = RateLimiter.init(name: limiter_name, bucket_name: base_bucket_name)
93
94 conn = build_conn(:get, "/")
95
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)
99 end
100
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})
105
106 plug_opts = RateLimiter.init(name: limiter_name, params: ["id"])
107
108 conn = build_conn(:get, "/?id=1")
109 conn = Conn.fetch_query_params(conn)
110 conn_2 = build_conn(:get, "/?id=2")
111
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)
115 end
116
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})
121
122 base_bucket_name = "#{limiter_name}:group1"
123
124 plug_opts =
125 RateLimiter.init(name: limiter_name, bucket_name: base_bucket_name, params: ["id"])
126
127 id = "100"
128
129 conn = build_conn(:get, "/?id=#{id}")
130 conn = Conn.fetch_query_params(conn)
131 conn_2 = build_conn(:get, "/?id=#{101}")
132
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)
136 end
137 end
138
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})
144
145 plug_opts = RateLimiter.init(name: limiter_name)
146
147 conn = %{build_conn(:get, "/") | remote_ip: {127, 0, 0, 2}}
148 conn_2 = %{build_conn(:get, "/") | remote_ip: {127, 0, 0, 3}}
149
150 for i <- 1..5 do
151 conn = RateLimiter.call(conn, plug_opts)
152 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
153 refute conn.halted
154 end
155
156 conn = RateLimiter.call(conn, plug_opts)
157
158 assert %{"error" => "Throttled"} = ConnTest.json_response(conn, :too_many_requests)
159 assert conn.halted
160
161 conn_2 = RateLimiter.call(conn_2, plug_opts)
162 assert {1, 4} = RateLimiter.inspect_bucket(conn_2, limiter_name, plug_opts)
163
164 refute conn_2.status == Conn.Status.code(:too_many_requests)
165 refute conn_2.resp_body
166 refute conn_2.halted
167 end
168 end
169
170 describe "authenticated users" do
171 setup do
172 Ecto.Adapters.SQL.Sandbox.checkout(Pleroma.Repo)
173
174 :ok
175 end
176
177 test "can have limits separate from unauthenticated connections" do
178 limiter_name = :test_authenticated1
179
180 scale = 50
181 limit = 5
182 clear_config([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
183 clear_config([:rate_limit, limiter_name], [{1000, 1}, {scale, limit}])
184
185 plug_opts = RateLimiter.init(name: limiter_name)
186
187 user = insert(:user)
188 conn = build_conn(:get, "/") |> assign(:user, user)
189
190 for i <- 1..5 do
191 conn = RateLimiter.call(conn, plug_opts)
192 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
193 refute conn.halted
194 end
195
196 conn = RateLimiter.call(conn, plug_opts)
197
198 assert %{"error" => "Throttled"} = ConnTest.json_response(conn, :too_many_requests)
199 assert conn.halted
200 end
201
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})
206
207 plug_opts = RateLimiter.init(name: limiter_name)
208
209 user = insert(:user)
210 conn = build_conn(:get, "/") |> assign(:user, user)
211
212 user_2 = insert(:user)
213 conn_2 = build_conn(:get, "/") |> assign(:user, user_2)
214
215 for i <- 1..5 do
216 conn = RateLimiter.call(conn, plug_opts)
217 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
218 end
219
220 conn = RateLimiter.call(conn, plug_opts)
221 assert %{"error" => "Throttled"} = ConnTest.json_response(conn, :too_many_requests)
222 assert conn.halted
223
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
228 refute conn_2.halted
229 end
230 end
231
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})
236
237 opts = RateLimiter.init(name: limiter_name)
238
239 conn = build_conn(:get, "/")
240 conn_2 = build_conn(:get, "/")
241
242 %Task{pid: pid1} =
243 task1 =
244 Task.async(fn ->
245 receive do
246 :process2_up ->
247 RateLimiter.call(conn, opts)
248 end
249 end)
250
251 task2 =
252 Task.async(fn ->
253 send(pid1, :process2_up)
254 RateLimiter.call(conn_2, opts)
255 end)
256
257 Task.await(task1)
258 Task.await(task2)
259
260 refute {:err, :not_found} == RateLimiter.inspect_bucket(conn, limiter_name, opts)
261 end
262 end