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