2019-07-09 23:13:23 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-12 23:49:20 -07:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2019-07-09 23:13:23 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-06-23 09:16:47 -06:00
|
|
|
defmodule Pleroma.Web.Plugs.RateLimiterTest do
|
2023-08-01 04:43:50 -06:00
|
|
|
use Pleroma.Web.ConnCase, async: false
|
2019-06-11 01:27:41 -06:00
|
|
|
|
2020-04-15 06:27:34 -06:00
|
|
|
alias Phoenix.ConnTest
|
2020-06-24 00:35:00 -06:00
|
|
|
alias Pleroma.Web.Plugs.RateLimiter
|
2020-04-15 06:27:34 -06:00
|
|
|
alias Plug.Conn
|
2019-06-11 01:27:41 -06:00
|
|
|
|
|
|
|
import Pleroma.Factory
|
2020-02-27 08:46:05 -07:00
|
|
|
import Pleroma.Tests.Helpers, only: [clear_config: 1, clear_config: 2]
|
2019-06-11 01:27:41 -06:00
|
|
|
|
2019-07-13 05:49:39 -06:00
|
|
|
# Note: each example must work with separate buckets in order to prevent concurrency issues
|
2020-03-20 09:33:00 -06:00
|
|
|
setup do: clear_config([Pleroma.Web.Endpoint, :http, :ip])
|
|
|
|
setup do: clear_config(:rate_limit)
|
2020-02-27 08:46:05 -07:00
|
|
|
|
2019-11-11 05:13:06 -07:00
|
|
|
describe "config" do
|
2020-02-27 08:46:05 -07:00
|
|
|
@limiter_name :test_init
|
2020-06-24 00:30:32 -06:00
|
|
|
setup do: clear_config([Pleroma.Web.Plugs.RemoteIp, :enabled])
|
2020-02-27 08:46:05 -07:00
|
|
|
|
2019-11-11 05:13:06 -07:00
|
|
|
test "config is required for plug to work" do
|
2021-01-26 10:58:43 -07:00
|
|
|
clear_config([:rate_limit, @limiter_name], {1, 1})
|
|
|
|
clear_config([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
|
2019-06-11 01:27:41 -06:00
|
|
|
|
2019-11-11 05:13:06 -07:00
|
|
|
assert %{limits: {1, 1}, name: :test_init, opts: [name: :test_init]} ==
|
2020-02-27 08:46:05 -07:00
|
|
|
[name: @limiter_name]
|
|
|
|
|> RateLimiter.init()
|
|
|
|
|> RateLimiter.action_settings()
|
|
|
|
|
|
|
|
assert nil ==
|
|
|
|
[name: :nonexisting_limiter]
|
|
|
|
|> RateLimiter.init()
|
|
|
|
|> RateLimiter.action_settings()
|
2019-11-11 05:13:06 -07:00
|
|
|
end
|
2020-03-13 12:15:42 -06:00
|
|
|
end
|
2019-06-11 01:27:41 -06:00
|
|
|
|
2020-03-13 12:15:42 -06:00
|
|
|
test "it is disabled if it remote ip plug is enabled but no remote ip is found" do
|
2020-04-15 06:27:34 -06:00
|
|
|
assert RateLimiter.disabled?(Conn.assign(build_conn(), :remote_ip_found, false))
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it is enabled if remote ip found" do
|
|
|
|
refute RateLimiter.disabled?(Conn.assign(build_conn(), :remote_ip_found, true))
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it is enabled if remote_ip_found flag doesn't exist" do
|
|
|
|
refute RateLimiter.disabled?(build_conn())
|
2020-03-13 12:15:42 -06:00
|
|
|
end
|
2019-12-13 09:00:26 -07:00
|
|
|
|
2020-03-13 12:15:42 -06:00
|
|
|
test "it restricts based on config values" do
|
|
|
|
limiter_name = :test_plug_opts
|
|
|
|
scale = 80
|
|
|
|
limit = 5
|
2019-12-13 09:00:26 -07:00
|
|
|
|
2022-11-01 08:25:54 -06:00
|
|
|
clear_config([Pleroma.Web.Endpoint, :http, :ip], {127, 0, 0, 1})
|
2021-01-26 10:58:43 -07:00
|
|
|
clear_config([:rate_limit, limiter_name], {scale, limit})
|
2019-12-13 09:00:26 -07:00
|
|
|
|
2020-03-13 12:15:42 -06:00
|
|
|
plug_opts = RateLimiter.init(name: limiter_name)
|
2020-03-16 05:19:36 -06:00
|
|
|
conn = build_conn(:get, "/")
|
2019-12-13 09:00:26 -07:00
|
|
|
|
2022-11-01 08:25:54 -06:00
|
|
|
for _ <- 1..5 do
|
|
|
|
conn_limited = RateLimiter.call(conn, plug_opts)
|
|
|
|
|
|
|
|
refute conn_limited.status == Conn.Status.code(:too_many_requests)
|
|
|
|
refute conn_limited.resp_body
|
|
|
|
refute conn_limited.halted
|
2019-12-13 09:00:26 -07:00
|
|
|
end
|
|
|
|
|
2022-11-01 08:25:54 -06:00
|
|
|
conn_limited = RateLimiter.call(conn, plug_opts)
|
|
|
|
assert %{"error" => "Throttled"} = ConnTest.json_response(conn_limited, :too_many_requests)
|
|
|
|
assert conn_limited.halted
|
2019-06-11 01:27:41 -06:00
|
|
|
|
2022-11-01 08:25:54 -06:00
|
|
|
expire_ttl(conn, limiter_name)
|
2019-06-11 01:27:41 -06:00
|
|
|
|
2022-11-01 08:25:54 -06:00
|
|
|
for _ <- 1..5 do
|
|
|
|
conn_limited = RateLimiter.call(conn, plug_opts)
|
2019-06-11 01:27:41 -06:00
|
|
|
|
2022-11-01 08:25:54 -06:00
|
|
|
refute conn_limited.status == Conn.Status.code(:too_many_requests)
|
|
|
|
refute conn_limited.resp_body
|
|
|
|
refute conn_limited.halted
|
|
|
|
end
|
2019-06-11 01:27:41 -06:00
|
|
|
|
2022-11-01 08:25:54 -06:00
|
|
|
conn_limited = RateLimiter.call(conn, plug_opts)
|
|
|
|
assert %{"error" => "Throttled"} = ConnTest.json_response(conn_limited, :too_many_requests)
|
|
|
|
assert conn_limited.halted
|
2019-11-11 05:13:06 -07:00
|
|
|
end
|
2019-06-11 01:27:41 -06:00
|
|
|
|
2019-11-11 05:13:06 -07:00
|
|
|
describe "options" do
|
|
|
|
test "`bucket_name` option overrides default bucket name" do
|
|
|
|
limiter_name = :test_bucket_name
|
2019-06-11 01:27:41 -06:00
|
|
|
|
2021-01-26 10:58:43 -07:00
|
|
|
clear_config([:rate_limit, limiter_name], {1000, 5})
|
|
|
|
clear_config([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
|
2019-06-11 01:27:41 -06:00
|
|
|
|
2019-11-11 05:13:06 -07:00
|
|
|
base_bucket_name = "#{limiter_name}:group1"
|
2020-02-27 08:46:05 -07:00
|
|
|
plug_opts = RateLimiter.init(name: limiter_name, bucket_name: base_bucket_name)
|
2019-06-11 01:27:41 -06:00
|
|
|
|
2020-03-16 05:19:36 -06:00
|
|
|
conn = build_conn(:get, "/")
|
2019-06-11 01:27:41 -06:00
|
|
|
|
2020-02-27 08:46:05 -07:00
|
|
|
RateLimiter.call(conn, plug_opts)
|
|
|
|
assert {1, 4} = RateLimiter.inspect_bucket(conn, base_bucket_name, plug_opts)
|
2020-02-29 12:04:09 -07:00
|
|
|
assert {:error, :not_found} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
|
2019-11-11 05:13:06 -07:00
|
|
|
end
|
2019-06-11 01:27:41 -06:00
|
|
|
|
2019-11-11 05:13:06 -07:00
|
|
|
test "`params` option allows different queries to be tracked independently" do
|
|
|
|
limiter_name = :test_params
|
2021-01-26 10:58:43 -07:00
|
|
|
clear_config([:rate_limit, limiter_name], {1000, 5})
|
|
|
|
clear_config([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
|
2019-07-13 05:49:39 -06:00
|
|
|
|
2020-02-27 08:46:05 -07:00
|
|
|
plug_opts = RateLimiter.init(name: limiter_name, params: ["id"])
|
2019-07-13 05:49:39 -06:00
|
|
|
|
2020-03-16 05:19:36 -06:00
|
|
|
conn = build_conn(:get, "/?id=1")
|
2020-04-15 06:27:34 -06:00
|
|
|
conn = Conn.fetch_query_params(conn)
|
2020-03-16 05:19:36 -06:00
|
|
|
conn_2 = build_conn(:get, "/?id=2")
|
2019-07-13 05:49:39 -06:00
|
|
|
|
2020-02-27 08:46:05 -07:00
|
|
|
RateLimiter.call(conn, plug_opts)
|
|
|
|
assert {1, 4} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
|
|
|
|
assert {0, 5} = RateLimiter.inspect_bucket(conn_2, limiter_name, plug_opts)
|
2019-11-11 05:13:06 -07:00
|
|
|
end
|
2019-07-13 05:49:39 -06:00
|
|
|
|
2019-11-11 05:13:06 -07:00
|
|
|
test "it supports combination of options modifying bucket name" do
|
|
|
|
limiter_name = :test_options_combo
|
2021-01-26 10:58:43 -07:00
|
|
|
clear_config([:rate_limit, limiter_name], {1000, 5})
|
|
|
|
clear_config([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
|
2019-07-13 05:49:39 -06:00
|
|
|
|
2019-11-11 05:13:06 -07:00
|
|
|
base_bucket_name = "#{limiter_name}:group1"
|
2020-02-27 08:46:05 -07:00
|
|
|
|
|
|
|
plug_opts =
|
|
|
|
RateLimiter.init(name: limiter_name, bucket_name: base_bucket_name, params: ["id"])
|
|
|
|
|
2019-11-11 05:13:06 -07:00
|
|
|
id = "100"
|
2019-07-13 05:49:39 -06:00
|
|
|
|
2020-03-16 05:19:36 -06:00
|
|
|
conn = build_conn(:get, "/?id=#{id}")
|
2020-04-15 06:27:34 -06:00
|
|
|
conn = Conn.fetch_query_params(conn)
|
2020-03-16 05:19:36 -06:00
|
|
|
conn_2 = build_conn(:get, "/?id=#{101}")
|
2019-07-13 05:49:39 -06:00
|
|
|
|
2020-02-27 08:46:05 -07:00
|
|
|
RateLimiter.call(conn, plug_opts)
|
|
|
|
assert {1, 4} = RateLimiter.inspect_bucket(conn, base_bucket_name, plug_opts)
|
|
|
|
assert {0, 5} = RateLimiter.inspect_bucket(conn_2, base_bucket_name, plug_opts)
|
2019-11-11 05:13:06 -07:00
|
|
|
end
|
2019-07-13 05:49:39 -06:00
|
|
|
end
|
|
|
|
|
2019-11-11 05:13:06 -07:00
|
|
|
describe "unauthenticated users" do
|
2021-12-21 21:04:15 -07:00
|
|
|
@tag :erratic
|
2019-11-11 05:13:06 -07:00
|
|
|
test "are restricted based on remote IP" do
|
|
|
|
limiter_name = :test_unauthenticated
|
2021-01-26 10:58:43 -07:00
|
|
|
clear_config([:rate_limit, limiter_name], [{1000, 5}, {1, 10}])
|
|
|
|
clear_config([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
|
2019-11-11 05:13:06 -07:00
|
|
|
|
2020-02-27 08:46:05 -07:00
|
|
|
plug_opts = RateLimiter.init(name: limiter_name)
|
2019-07-13 05:49:39 -06:00
|
|
|
|
2020-03-16 05:19:36 -06:00
|
|
|
conn = %{build_conn(:get, "/") | remote_ip: {127, 0, 0, 2}}
|
|
|
|
conn_2 = %{build_conn(:get, "/") | remote_ip: {127, 0, 0, 3}}
|
2019-07-13 05:49:39 -06:00
|
|
|
|
2019-11-11 05:13:06 -07:00
|
|
|
for i <- 1..5 do
|
2020-02-27 08:46:05 -07:00
|
|
|
conn = RateLimiter.call(conn, plug_opts)
|
|
|
|
assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
|
2019-11-11 05:13:06 -07:00
|
|
|
refute conn.halted
|
|
|
|
end
|
2019-07-13 05:49:39 -06:00
|
|
|
|
2020-02-27 08:46:05 -07:00
|
|
|
conn = RateLimiter.call(conn, plug_opts)
|
2019-07-13 05:49:39 -06:00
|
|
|
|
2020-04-15 06:27:34 -06:00
|
|
|
assert %{"error" => "Throttled"} = ConnTest.json_response(conn, :too_many_requests)
|
2019-11-11 05:13:06 -07:00
|
|
|
assert conn.halted
|
|
|
|
|
2020-02-27 08:46:05 -07:00
|
|
|
conn_2 = RateLimiter.call(conn_2, plug_opts)
|
|
|
|
assert {1, 4} = RateLimiter.inspect_bucket(conn_2, limiter_name, plug_opts)
|
2019-11-11 05:13:06 -07:00
|
|
|
|
2020-04-15 06:27:34 -06:00
|
|
|
refute conn_2.status == Conn.Status.code(:too_many_requests)
|
2019-11-11 05:13:06 -07:00
|
|
|
refute conn_2.resp_body
|
|
|
|
refute conn_2.halted
|
|
|
|
end
|
2019-07-13 05:49:39 -06:00
|
|
|
end
|
|
|
|
|
2019-11-11 05:13:06 -07:00
|
|
|
describe "authenticated users" do
|
|
|
|
setup do
|
|
|
|
Ecto.Adapters.SQL.Sandbox.checkout(Pleroma.Repo)
|
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
2021-12-21 21:04:15 -07:00
|
|
|
@tag :erratic
|
2020-02-27 08:46:05 -07:00
|
|
|
test "can have limits separate from unauthenticated connections" do
|
|
|
|
limiter_name = :test_authenticated1
|
2019-11-11 05:13:06 -07:00
|
|
|
|
2019-12-15 15:03:13 -07:00
|
|
|
scale = 50
|
2019-11-11 05:13:06 -07:00
|
|
|
limit = 5
|
2021-01-26 10:58:43 -07:00
|
|
|
clear_config([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
|
|
|
|
clear_config([:rate_limit, limiter_name], [{1000, 1}, {scale, limit}])
|
2019-11-11 05:13:06 -07:00
|
|
|
|
2020-02-27 08:46:05 -07:00
|
|
|
plug_opts = RateLimiter.init(name: limiter_name)
|
2019-06-11 01:27:41 -06:00
|
|
|
|
2019-11-11 05:13:06 -07:00
|
|
|
user = insert(:user)
|
2020-03-16 05:19:36 -06:00
|
|
|
conn = build_conn(:get, "/") |> assign(:user, user)
|
2019-06-11 01:27:41 -06:00
|
|
|
|
2019-11-11 05:13:06 -07:00
|
|
|
for i <- 1..5 do
|
2020-02-27 08:46:05 -07:00
|
|
|
conn = RateLimiter.call(conn, plug_opts)
|
|
|
|
assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
|
2019-11-11 05:13:06 -07:00
|
|
|
refute conn.halted
|
|
|
|
end
|
2019-06-11 01:27:41 -06:00
|
|
|
|
2020-02-27 08:46:05 -07:00
|
|
|
conn = RateLimiter.call(conn, plug_opts)
|
2019-06-11 01:27:41 -06:00
|
|
|
|
2020-04-15 06:27:34 -06:00
|
|
|
assert %{"error" => "Throttled"} = ConnTest.json_response(conn, :too_many_requests)
|
2019-11-11 05:13:06 -07:00
|
|
|
assert conn.halted
|
|
|
|
end
|
2019-06-11 01:27:41 -06:00
|
|
|
|
2021-12-21 21:04:15 -07:00
|
|
|
@tag :erratic
|
2020-02-27 08:46:05 -07:00
|
|
|
test "different users are counted independently" do
|
|
|
|
limiter_name = :test_authenticated2
|
2021-01-26 10:58:43 -07:00
|
|
|
clear_config([:rate_limit, limiter_name], [{1, 10}, {1000, 5}])
|
|
|
|
clear_config([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
|
2019-06-11 01:27:41 -06:00
|
|
|
|
2020-02-27 08:46:05 -07:00
|
|
|
plug_opts = RateLimiter.init(name: limiter_name)
|
2019-06-11 01:27:41 -06:00
|
|
|
|
2019-11-11 05:13:06 -07:00
|
|
|
user = insert(:user)
|
2020-03-16 05:19:36 -06:00
|
|
|
conn = build_conn(:get, "/") |> assign(:user, user)
|
2019-06-11 01:27:41 -06:00
|
|
|
|
2019-11-11 05:13:06 -07:00
|
|
|
user_2 = insert(:user)
|
2020-03-16 05:19:36 -06:00
|
|
|
conn_2 = build_conn(:get, "/") |> assign(:user, user_2)
|
2019-06-11 01:27:41 -06:00
|
|
|
|
2019-11-11 05:13:06 -07:00
|
|
|
for i <- 1..5 do
|
2020-02-27 08:46:05 -07:00
|
|
|
conn = RateLimiter.call(conn, plug_opts)
|
|
|
|
assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
|
2019-11-11 05:13:06 -07:00
|
|
|
end
|
2019-06-11 01:27:41 -06:00
|
|
|
|
2020-02-27 08:46:05 -07:00
|
|
|
conn = RateLimiter.call(conn, plug_opts)
|
2020-04-15 06:27:34 -06:00
|
|
|
assert %{"error" => "Throttled"} = ConnTest.json_response(conn, :too_many_requests)
|
2019-11-11 05:13:06 -07:00
|
|
|
assert conn.halted
|
2019-06-11 01:27:41 -06:00
|
|
|
|
2020-02-27 08:46:05 -07:00
|
|
|
conn_2 = RateLimiter.call(conn_2, plug_opts)
|
|
|
|
assert {1, 4} = RateLimiter.inspect_bucket(conn_2, limiter_name, plug_opts)
|
2020-04-15 06:27:34 -06:00
|
|
|
refute conn_2.status == Conn.Status.code(:too_many_requests)
|
2019-11-11 05:13:06 -07:00
|
|
|
refute conn_2.resp_body
|
|
|
|
refute conn_2.halted
|
|
|
|
end
|
2019-06-11 01:27:41 -06:00
|
|
|
end
|
2020-02-28 07:35:01 -07:00
|
|
|
|
|
|
|
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
|
|
|
|
limiter_name = :test_race_condition
|
2021-01-26 10:58:43 -07:00
|
|
|
clear_config([:rate_limit, limiter_name], {1000, 5})
|
|
|
|
clear_config([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
|
2020-02-28 07:35:01 -07:00
|
|
|
|
|
|
|
opts = RateLimiter.init(name: limiter_name)
|
|
|
|
|
2020-03-16 05:19:36 -06:00
|
|
|
conn = build_conn(:get, "/")
|
|
|
|
conn_2 = build_conn(:get, "/")
|
2020-02-28 07:35:01 -07:00
|
|
|
|
|
|
|
%Task{pid: pid1} =
|
|
|
|
task1 =
|
|
|
|
Task.async(fn ->
|
|
|
|
receive do
|
|
|
|
:process2_up ->
|
|
|
|
RateLimiter.call(conn, opts)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
task2 =
|
|
|
|
Task.async(fn ->
|
|
|
|
send(pid1, :process2_up)
|
|
|
|
RateLimiter.call(conn_2, opts)
|
|
|
|
end)
|
|
|
|
|
|
|
|
Task.await(task1)
|
|
|
|
Task.await(task2)
|
|
|
|
|
|
|
|
refute {:err, :not_found} == RateLimiter.inspect_bucket(conn, limiter_name, opts)
|
|
|
|
end
|
2022-11-01 08:25:54 -06:00
|
|
|
|
|
|
|
def expire_ttl(%{remote_ip: remote_ip} = _conn, bucket_name_root) do
|
|
|
|
bucket_name = "anon:#{bucket_name_root}" |> String.to_atom()
|
|
|
|
key_name = "ip::#{remote_ip |> Tuple.to_list() |> Enum.join(".")}"
|
|
|
|
|
|
|
|
{:ok, bucket_value} = Cachex.get(bucket_name, key_name)
|
|
|
|
Cachex.put(bucket_name, key_name, bucket_value, ttl: -1)
|
|
|
|
end
|
2019-06-11 01:27:41 -06:00
|
|
|
end
|