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
16 clear_config([Pleroma.Web.Endpoint, :http, :ip])
17 clear_config(:rate_limit)
20 @limiter_name :test_init
22 clear_config([Pleroma.Plugs.RemoteIp, :enabled])
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})
28 assert %{limits: {1, 1}, name: :test_init, opts: [name: :test_init]} ==
31 |> RateLimiter.action_settings()
34 [name: :nonexisting_limiter]
36 |> RateLimiter.action_settings()
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))
45 test "it restricts based on config values" do
46 limiter_name = :test_plug_opts
50 Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
51 Config.put([:rate_limit, limiter_name], {scale, limit})
53 plug_opts = RateLimiter.init(name: limiter_name)
54 conn = conn(:get, "/")
57 conn = RateLimiter.call(conn, plug_opts)
58 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
62 conn = RateLimiter.call(conn, plug_opts)
63 assert %{"error" => "Throttled"} = Phoenix.ConnTest.json_response(conn, :too_many_requests)
68 conn = conn(:get, "/")
70 conn = RateLimiter.call(conn, plug_opts)
71 assert {1, 4} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
73 refute conn.status == Plug.Conn.Status.code(:too_many_requests)
79 test "`bucket_name` option overrides default bucket name" do
80 limiter_name = :test_bucket_name
82 Config.put([:rate_limit, limiter_name], {1000, 5})
83 Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
85 base_bucket_name = "#{limiter_name}:group1"
86 plug_opts = RateLimiter.init(name: limiter_name, bucket_name: base_bucket_name)
88 conn = conn(:get, "/")
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)
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})
100 plug_opts = RateLimiter.init(name: limiter_name, params: ["id"])
102 conn = conn(:get, "/?id=1")
103 conn = Plug.Conn.fetch_query_params(conn)
104 conn_2 = conn(:get, "/?id=2")
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)
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})
116 base_bucket_name = "#{limiter_name}:group1"
119 RateLimiter.init(name: limiter_name, bucket_name: base_bucket_name, params: ["id"])
123 conn = conn(:get, "/?id=#{id}")
124 conn = Plug.Conn.fetch_query_params(conn)
125 conn_2 = conn(:get, "/?id=#{101}")
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)
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})
139 plug_opts = RateLimiter.init(name: limiter_name)
141 conn = %{conn(:get, "/") | remote_ip: {127, 0, 0, 2}}
142 conn_2 = %{conn(:get, "/") | remote_ip: {127, 0, 0, 3}}
145 conn = RateLimiter.call(conn, plug_opts)
146 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
150 conn = RateLimiter.call(conn, plug_opts)
152 assert %{"error" => "Throttled"} = Phoenix.ConnTest.json_response(conn, :too_many_requests)
155 conn_2 = RateLimiter.call(conn_2, plug_opts)
156 assert {1, 4} = RateLimiter.inspect_bucket(conn_2, limiter_name, plug_opts)
158 refute conn_2.status == Plug.Conn.Status.code(:too_many_requests)
159 refute conn_2.resp_body
164 describe "authenticated users" do
166 Ecto.Adapters.SQL.Sandbox.checkout(Pleroma.Repo)
171 test "can have limits separate from unauthenticated connections" do
172 limiter_name = :test_authenticated1
176 Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
177 Config.put([:rate_limit, limiter_name], [{1000, 1}, {scale, limit}])
179 plug_opts = RateLimiter.init(name: limiter_name)
182 conn = conn(:get, "/") |> assign(:user, user)
185 conn = RateLimiter.call(conn, plug_opts)
186 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
190 conn = RateLimiter.call(conn, plug_opts)
192 assert %{"error" => "Throttled"} = Phoenix.ConnTest.json_response(conn, :too_many_requests)
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})
201 plug_opts = RateLimiter.init(name: limiter_name)
204 conn = conn(:get, "/") |> assign(:user, user)
206 user_2 = insert(:user)
207 conn_2 = conn(:get, "/") |> assign(:user, user_2)
210 conn = RateLimiter.call(conn, plug_opts)
211 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
214 conn = RateLimiter.call(conn, plug_opts)
215 assert %{"error" => "Throttled"} = Phoenix.ConnTest.json_response(conn, :too_many_requests)
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
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})
231 opts = RateLimiter.init(name: limiter_name)
233 conn = conn(:get, "/")
234 conn_2 = conn(:get, "/")
241 RateLimiter.call(conn, opts)
247 send(pid1, :process2_up)
248 RateLimiter.call(conn_2, opts)
254 refute {:err, :not_found} == RateLimiter.inspect_bucket(conn, limiter_name, opts)