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