1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Plugs.RateLimiterTest do
6 use Pleroma.Web.ConnCase
9 alias Pleroma.Plugs.RateLimiter
11 import Pleroma.Factory
12 import Pleroma.Tests.Helpers, only: [clear_config: 1, clear_config: 2]
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)
19 @limiter_name :test_init
20 setup do: clear_config([Pleroma.Plugs.RemoteIp, :enabled])
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})
26 assert %{limits: {1, 1}, name: :test_init, opts: [name: :test_init]} ==
29 |> RateLimiter.action_settings()
32 [name: :nonexisting_limiter]
34 |> RateLimiter.action_settings()
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))
43 test "it restricts based on config values" do
44 limiter_name = :test_plug_opts
48 Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
49 Config.put([:rate_limit, limiter_name], {scale, limit})
51 plug_opts = RateLimiter.init(name: limiter_name)
52 conn = build_conn(:get, "/")
55 conn = RateLimiter.call(conn, plug_opts)
56 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
60 conn = RateLimiter.call(conn, plug_opts)
61 assert %{"error" => "Throttled"} = Phoenix.ConnTest.json_response(conn, :too_many_requests)
66 conn = build_conn(:get, "/")
68 conn = RateLimiter.call(conn, plug_opts)
69 assert {1, 4} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
71 refute conn.status == Plug.Conn.Status.code(:too_many_requests)
77 test "`bucket_name` option overrides default bucket name" do
78 limiter_name = :test_bucket_name
80 Config.put([:rate_limit, limiter_name], {1000, 5})
81 Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
83 base_bucket_name = "#{limiter_name}:group1"
84 plug_opts = RateLimiter.init(name: limiter_name, bucket_name: base_bucket_name)
86 conn = build_conn(:get, "/")
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)
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})
98 plug_opts = RateLimiter.init(name: limiter_name, params: ["id"])
100 conn = build_conn(:get, "/?id=1")
101 conn = Plug.Conn.fetch_query_params(conn)
102 conn_2 = build_conn(:get, "/?id=2")
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)
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})
114 base_bucket_name = "#{limiter_name}:group1"
117 RateLimiter.init(name: limiter_name, bucket_name: base_bucket_name, params: ["id"])
121 conn = build_conn(:get, "/?id=#{id}")
122 conn = Plug.Conn.fetch_query_params(conn)
123 conn_2 = build_conn(:get, "/?id=#{101}")
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)
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})
137 plug_opts = RateLimiter.init(name: limiter_name)
139 conn = %{build_conn(:get, "/") | remote_ip: {127, 0, 0, 2}}
140 conn_2 = %{build_conn(:get, "/") | remote_ip: {127, 0, 0, 3}}
143 conn = RateLimiter.call(conn, plug_opts)
144 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
148 conn = RateLimiter.call(conn, plug_opts)
150 assert %{"error" => "Throttled"} = Phoenix.ConnTest.json_response(conn, :too_many_requests)
153 conn_2 = RateLimiter.call(conn_2, plug_opts)
154 assert {1, 4} = RateLimiter.inspect_bucket(conn_2, limiter_name, plug_opts)
156 refute conn_2.status == Plug.Conn.Status.code(:too_many_requests)
157 refute conn_2.resp_body
162 describe "authenticated users" do
164 Ecto.Adapters.SQL.Sandbox.checkout(Pleroma.Repo)
169 test "can have limits separate from unauthenticated connections" do
170 limiter_name = :test_authenticated1
174 Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
175 Config.put([:rate_limit, limiter_name], [{1000, 1}, {scale, limit}])
177 plug_opts = RateLimiter.init(name: limiter_name)
180 conn = build_conn(:get, "/") |> assign(:user, user)
183 conn = RateLimiter.call(conn, plug_opts)
184 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
188 conn = RateLimiter.call(conn, plug_opts)
190 assert %{"error" => "Throttled"} = Phoenix.ConnTest.json_response(conn, :too_many_requests)
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})
199 plug_opts = RateLimiter.init(name: limiter_name)
202 conn = build_conn(:get, "/") |> assign(:user, user)
204 user_2 = insert(:user)
205 conn_2 = build_conn(:get, "/") |> assign(:user, user_2)
208 conn = RateLimiter.call(conn, plug_opts)
209 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
212 conn = RateLimiter.call(conn, plug_opts)
213 assert %{"error" => "Throttled"} = Phoenix.ConnTest.json_response(conn, :too_many_requests)
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
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})
229 opts = RateLimiter.init(name: limiter_name)
231 conn = build_conn(:get, "/")
232 conn_2 = build_conn(:get, "/")
239 RateLimiter.call(conn, opts)
245 send(pid1, :process2_up)
246 RateLimiter.call(conn_2, opts)
252 refute {:err, :not_found} == RateLimiter.inspect_bucket(conn, limiter_name, opts)