Merge branch 'issue/2069' into 'develop'
[akkoma] / test / pleroma / web / 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.Web.Plugs.RateLimiterTest do
6 use Pleroma.Web.ConnCase
7
8 alias Phoenix.ConnTest
9 alias Pleroma.Config
10 alias Pleroma.Web.Plugs.RateLimiter
11 alias Plug.Conn
12
13 import Pleroma.Factory
14 import Pleroma.Tests.Helpers, only: [clear_config: 1, clear_config: 2]
15
16 # Note: each example must work with separate buckets in order to prevent concurrency issues
17 setup do: clear_config([Pleroma.Web.Endpoint, :http, :ip])
18 setup do: clear_config(:rate_limit)
19
20 describe "config" do
21 @limiter_name :test_init
22 setup do: clear_config([Pleroma.Web.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 assert RateLimiter.disabled?(Conn.assign(build_conn(), :remote_ip_found, false))
42 end
43
44 test "it is enabled if remote ip found" do
45 refute RateLimiter.disabled?(Conn.assign(build_conn(), :remote_ip_found, true))
46 end
47
48 test "it is enabled if remote_ip_found flag doesn't exist" do
49 refute RateLimiter.disabled?(build_conn())
50 end
51
52 test "it restricts based on config values" do
53 limiter_name = :test_plug_opts
54 scale = 80
55 limit = 5
56
57 Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
58 Config.put([: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 Config.put([:rate_limit, limiter_name], {1000, 5})
90 Config.put([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 Config.put([:rate_limit, limiter_name], {1000, 5})
105 Config.put([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 Config.put([:rate_limit, limiter_name], {1000, 5})
121 Config.put([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 test "are restricted based on remote IP" do
142 limiter_name = :test_unauthenticated
143 Config.put([:rate_limit, limiter_name], [{1000, 5}, {1, 10}])
144 Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
145
146 plug_opts = RateLimiter.init(name: limiter_name)
147
148 conn = %{build_conn(:get, "/") | remote_ip: {127, 0, 0, 2}}
149 conn_2 = %{build_conn(:get, "/") | remote_ip: {127, 0, 0, 3}}
150
151 for i <- 1..5 do
152 conn = RateLimiter.call(conn, plug_opts)
153 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
154 refute conn.halted
155 end
156
157 conn = RateLimiter.call(conn, plug_opts)
158
159 assert %{"error" => "Throttled"} = ConnTest.json_response(conn, :too_many_requests)
160 assert conn.halted
161
162 conn_2 = RateLimiter.call(conn_2, plug_opts)
163 assert {1, 4} = RateLimiter.inspect_bucket(conn_2, limiter_name, plug_opts)
164
165 refute conn_2.status == Conn.Status.code(:too_many_requests)
166 refute conn_2.resp_body
167 refute conn_2.halted
168 end
169 end
170
171 describe "authenticated users" do
172 setup do
173 Ecto.Adapters.SQL.Sandbox.checkout(Pleroma.Repo)
174
175 :ok
176 end
177
178 test "can have limits separate from unauthenticated connections" do
179 limiter_name = :test_authenticated1
180
181 scale = 50
182 limit = 5
183 Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
184 Config.put([:rate_limit, limiter_name], [{1000, 1}, {scale, limit}])
185
186 plug_opts = RateLimiter.init(name: limiter_name)
187
188 user = insert(:user)
189 conn = build_conn(:get, "/") |> assign(:user, user)
190
191 for i <- 1..5 do
192 conn = RateLimiter.call(conn, plug_opts)
193 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
194 refute conn.halted
195 end
196
197 conn = RateLimiter.call(conn, plug_opts)
198
199 assert %{"error" => "Throttled"} = ConnTest.json_response(conn, :too_many_requests)
200 assert conn.halted
201 end
202
203 test "different users are counted independently" do
204 limiter_name = :test_authenticated2
205 Config.put([:rate_limit, limiter_name], [{1, 10}, {1000, 5}])
206 Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
207
208 plug_opts = RateLimiter.init(name: limiter_name)
209
210 user = insert(:user)
211 conn = build_conn(:get, "/") |> assign(:user, user)
212
213 user_2 = insert(:user)
214 conn_2 = build_conn(:get, "/") |> assign(:user, user_2)
215
216 for i <- 1..5 do
217 conn = RateLimiter.call(conn, plug_opts)
218 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
219 end
220
221 conn = RateLimiter.call(conn, plug_opts)
222 assert %{"error" => "Throttled"} = ConnTest.json_response(conn, :too_many_requests)
223 assert conn.halted
224
225 conn_2 = RateLimiter.call(conn_2, plug_opts)
226 assert {1, 4} = RateLimiter.inspect_bucket(conn_2, limiter_name, plug_opts)
227 refute conn_2.status == Conn.Status.code(:too_many_requests)
228 refute conn_2.resp_body
229 refute conn_2.halted
230 end
231 end
232
233 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
234 limiter_name = :test_race_condition
235 Pleroma.Config.put([:rate_limit, limiter_name], {1000, 5})
236 Pleroma.Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
237
238 opts = RateLimiter.init(name: limiter_name)
239
240 conn = build_conn(:get, "/")
241 conn_2 = build_conn(:get, "/")
242
243 %Task{pid: pid1} =
244 task1 =
245 Task.async(fn ->
246 receive do
247 :process2_up ->
248 RateLimiter.call(conn, opts)
249 end
250 end)
251
252 task2 =
253 Task.async(fn ->
254 send(pid1, :process2_up)
255 RateLimiter.call(conn_2, opts)
256 end)
257
258 Task.await(task1)
259 Task.await(task2)
260
261 refute {:err, :not_found} == RateLimiter.inspect_bucket(conn, limiter_name, opts)
262 end
263 end