warnings fix
[akkoma] / test / plugs / rate_limiter_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Plugs.RateLimiterTest do
6 use Pleroma.Web.ConnCase
7
8 alias Pleroma.Config
9 alias Pleroma.Plugs.RateLimiter
10
11 import Pleroma.Factory
12 import Pleroma.Tests.Helpers, only: [clear_config: 1, clear_config: 2]
13
14 # Note: each example must work with separate buckets in order to prevent concurrency issues
15
16 clear_config([Pleroma.Web.Endpoint, :http, :ip])
17 clear_config(:rate_limit)
18
19 describe "config" do
20 @limiter_name :test_init
21
22 clear_config([Pleroma.Plugs.RemoteIp, :enabled])
23
24 test "config is required for plug to work" do
25 Config.put([:rate_limit, @limiter_name], {1, 1})
26 Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
27
28 assert %{limits: {1, 1}, name: :test_init, opts: [name: :test_init]} ==
29 [name: @limiter_name]
30 |> RateLimiter.init()
31 |> RateLimiter.action_settings()
32
33 assert nil ==
34 [name: :nonexisting_limiter]
35 |> RateLimiter.init()
36 |> RateLimiter.action_settings()
37 end
38 end
39
40 test "it is disabled if it remote ip plug is enabled but no remote ip is found" do
41 Config.put([Pleroma.Web.Endpoint, :http, :ip], {127, 0, 0, 1})
42 assert RateLimiter.disabled?(Plug.Conn.assign(build_conn(), :remote_ip_found, false))
43 end
44
45 test "it restricts based on config values" do
46 limiter_name = :test_plug_opts
47 scale = 80
48 limit = 5
49
50 Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
51 Config.put([:rate_limit, limiter_name], {scale, limit})
52
53 plug_opts = RateLimiter.init(name: limiter_name)
54 conn = build_conn(:get, "/")
55
56 for i <- 1..5 do
57 conn = RateLimiter.call(conn, plug_opts)
58 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
59 Process.sleep(10)
60 end
61
62 conn = RateLimiter.call(conn, plug_opts)
63 assert %{"error" => "Throttled"} = Phoenix.ConnTest.json_response(conn, :too_many_requests)
64 assert conn.halted
65
66 Process.sleep(50)
67
68 conn = build_conn(:get, "/")
69
70 conn = RateLimiter.call(conn, plug_opts)
71 assert {1, 4} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
72
73 refute conn.status == Plug.Conn.Status.code(:too_many_requests)
74 refute conn.resp_body
75 refute conn.halted
76 end
77
78 describe "options" do
79 test "`bucket_name` option overrides default bucket name" do
80 limiter_name = :test_bucket_name
81
82 Config.put([:rate_limit, limiter_name], {1000, 5})
83 Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
84
85 base_bucket_name = "#{limiter_name}:group1"
86 plug_opts = RateLimiter.init(name: limiter_name, bucket_name: base_bucket_name)
87
88 conn = build_conn(:get, "/")
89
90 RateLimiter.call(conn, plug_opts)
91 assert {1, 4} = RateLimiter.inspect_bucket(conn, base_bucket_name, plug_opts)
92 assert {:error, :not_found} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
93 end
94
95 test "`params` option allows different queries to be tracked independently" do
96 limiter_name = :test_params
97 Config.put([:rate_limit, limiter_name], {1000, 5})
98 Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
99
100 plug_opts = RateLimiter.init(name: limiter_name, params: ["id"])
101
102 conn = build_conn(:get, "/?id=1")
103 conn = Plug.Conn.fetch_query_params(conn)
104 conn_2 = build_conn(:get, "/?id=2")
105
106 RateLimiter.call(conn, plug_opts)
107 assert {1, 4} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
108 assert {0, 5} = RateLimiter.inspect_bucket(conn_2, limiter_name, plug_opts)
109 end
110
111 test "it supports combination of options modifying bucket name" do
112 limiter_name = :test_options_combo
113 Config.put([:rate_limit, limiter_name], {1000, 5})
114 Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
115
116 base_bucket_name = "#{limiter_name}:group1"
117
118 plug_opts =
119 RateLimiter.init(name: limiter_name, bucket_name: base_bucket_name, params: ["id"])
120
121 id = "100"
122
123 conn = build_conn(:get, "/?id=#{id}")
124 conn = Plug.Conn.fetch_query_params(conn)
125 conn_2 = build_conn(:get, "/?id=#{101}")
126
127 RateLimiter.call(conn, plug_opts)
128 assert {1, 4} = RateLimiter.inspect_bucket(conn, base_bucket_name, plug_opts)
129 assert {0, 5} = RateLimiter.inspect_bucket(conn_2, base_bucket_name, plug_opts)
130 end
131 end
132
133 describe "unauthenticated users" do
134 test "are restricted based on remote IP" do
135 limiter_name = :test_unauthenticated
136 Config.put([:rate_limit, limiter_name], [{1000, 5}, {1, 10}])
137 Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
138
139 plug_opts = RateLimiter.init(name: limiter_name)
140
141 conn = %{build_conn(:get, "/") | remote_ip: {127, 0, 0, 2}}
142 conn_2 = %{build_conn(:get, "/") | remote_ip: {127, 0, 0, 3}}
143
144 for i <- 1..5 do
145 conn = RateLimiter.call(conn, plug_opts)
146 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
147 refute conn.halted
148 end
149
150 conn = RateLimiter.call(conn, plug_opts)
151
152 assert %{"error" => "Throttled"} = Phoenix.ConnTest.json_response(conn, :too_many_requests)
153 assert conn.halted
154
155 conn_2 = RateLimiter.call(conn_2, plug_opts)
156 assert {1, 4} = RateLimiter.inspect_bucket(conn_2, limiter_name, plug_opts)
157
158 refute conn_2.status == Plug.Conn.Status.code(:too_many_requests)
159 refute conn_2.resp_body
160 refute conn_2.halted
161 end
162 end
163
164 describe "authenticated users" do
165 setup do
166 Ecto.Adapters.SQL.Sandbox.checkout(Pleroma.Repo)
167
168 :ok
169 end
170
171 test "can have limits separate from unauthenticated connections" do
172 limiter_name = :test_authenticated1
173
174 scale = 50
175 limit = 5
176 Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
177 Config.put([:rate_limit, limiter_name], [{1000, 1}, {scale, limit}])
178
179 plug_opts = RateLimiter.init(name: limiter_name)
180
181 user = insert(:user)
182 conn = build_conn(:get, "/") |> assign(:user, user)
183
184 for i <- 1..5 do
185 conn = RateLimiter.call(conn, plug_opts)
186 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
187 refute conn.halted
188 end
189
190 conn = RateLimiter.call(conn, plug_opts)
191
192 assert %{"error" => "Throttled"} = Phoenix.ConnTest.json_response(conn, :too_many_requests)
193 assert conn.halted
194 end
195
196 test "different users are counted independently" do
197 limiter_name = :test_authenticated2
198 Config.put([:rate_limit, limiter_name], [{1, 10}, {1000, 5}])
199 Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
200
201 plug_opts = RateLimiter.init(name: limiter_name)
202
203 user = insert(:user)
204 conn = build_conn(:get, "/") |> assign(:user, user)
205
206 user_2 = insert(:user)
207 conn_2 = build_conn(:get, "/") |> assign(:user, user_2)
208
209 for i <- 1..5 do
210 conn = RateLimiter.call(conn, plug_opts)
211 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
212 end
213
214 conn = RateLimiter.call(conn, plug_opts)
215 assert %{"error" => "Throttled"} = Phoenix.ConnTest.json_response(conn, :too_many_requests)
216 assert conn.halted
217
218 conn_2 = RateLimiter.call(conn_2, plug_opts)
219 assert {1, 4} = RateLimiter.inspect_bucket(conn_2, limiter_name, plug_opts)
220 refute conn_2.status == Plug.Conn.Status.code(:too_many_requests)
221 refute conn_2.resp_body
222 refute conn_2.halted
223 end
224 end
225
226 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
227 limiter_name = :test_race_condition
228 Pleroma.Config.put([:rate_limit, limiter_name], {1000, 5})
229 Pleroma.Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
230
231 opts = RateLimiter.init(name: limiter_name)
232
233 conn = build_conn(:get, "/")
234 conn_2 = build_conn(:get, "/")
235
236 %Task{pid: pid1} =
237 task1 =
238 Task.async(fn ->
239 receive do
240 :process2_up ->
241 RateLimiter.call(conn, opts)
242 end
243 end)
244
245 task2 =
246 Task.async(fn ->
247 send(pid1, :process2_up)
248 RateLimiter.call(conn_2, opts)
249 end)
250
251 Task.await(task1)
252 Task.await(task2)
253
254 refute {:err, :not_found} == RateLimiter.inspect_bucket(conn, limiter_name, opts)
255 end
256 end