2019-07-10 07:13:23 +02:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-06-11 09:27:41 +02:00
|
|
|
defmodule Pleroma.Plugs.RateLimiterTest do
|
|
|
|
use ExUnit.Case, async: true
|
|
|
|
use Plug.Test
|
|
|
|
|
2020-02-27 16:46:05 +01:00
|
|
|
alias Pleroma.Config
|
2019-06-11 09:27:41 +02:00
|
|
|
alias Pleroma.Plugs.RateLimiter
|
|
|
|
|
|
|
|
import Pleroma.Factory
|
2020-02-27 16:46:05 +01:00
|
|
|
import Pleroma.Tests.Helpers, only: [clear_config: 1, clear_config: 2]
|
2019-06-11 09:27:41 +02:00
|
|
|
|
2019-07-13 13:49:39 +02:00
|
|
|
# Note: each example must work with separate buckets in order to prevent concurrency issues
|
2019-06-11 09:27:41 +02:00
|
|
|
|
2020-02-27 16:46:05 +01:00
|
|
|
clear_config([Pleroma.Web.Endpoint, :http, :ip])
|
|
|
|
clear_config(:rate_limit)
|
|
|
|
|
2019-11-11 13:13:06 +01:00
|
|
|
describe "config" do
|
2020-02-27 16:46:05 +01:00
|
|
|
@limiter_name :test_init
|
|
|
|
|
|
|
|
clear_config([Pleroma.Plugs.RemoteIp, :enabled])
|
|
|
|
|
2019-11-11 13:13:06 +01:00
|
|
|
test "config is required for plug to work" do
|
2020-02-27 16:46:05 +01:00
|
|
|
Config.put([:rate_limit, @limiter_name], {1, 1})
|
|
|
|
Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
|
2019-06-11 09:27:41 +02:00
|
|
|
|
2019-11-11 13:13:06 +01:00
|
|
|
assert %{limits: {1, 1}, name: :test_init, opts: [name: :test_init]} ==
|
2020-02-27 16:46:05 +01:00
|
|
|
[name: @limiter_name]
|
|
|
|
|> RateLimiter.init()
|
|
|
|
|> RateLimiter.action_settings()
|
|
|
|
|
|
|
|
assert nil ==
|
|
|
|
[name: :nonexisting_limiter]
|
|
|
|
|> RateLimiter.init()
|
|
|
|
|> RateLimiter.action_settings()
|
2019-11-11 13:13:06 +01:00
|
|
|
end
|
2019-06-11 09:27:41 +02:00
|
|
|
|
2019-12-13 17:00:26 +01:00
|
|
|
test "it is disabled for localhost" do
|
2020-02-27 16:46:05 +01:00
|
|
|
Config.put([:rate_limit, @limiter_name], {1, 1})
|
|
|
|
Config.put([Pleroma.Web.Endpoint, :http, :ip], {127, 0, 0, 1})
|
|
|
|
Config.put([Pleroma.Plugs.RemoteIp, :enabled], false)
|
2019-12-13 17:00:26 +01:00
|
|
|
|
|
|
|
assert RateLimiter.disabled?() == true
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it is disabled for socket" do
|
2020-02-27 16:46:05 +01:00
|
|
|
Config.put([:rate_limit, @limiter_name], {1, 1})
|
|
|
|
Config.put([Pleroma.Web.Endpoint, :http, :ip], {:local, "/path/to/pleroma.sock"})
|
|
|
|
Config.put([Pleroma.Plugs.RemoteIp, :enabled], false)
|
2019-12-13 17:00:26 +01:00
|
|
|
|
|
|
|
assert RateLimiter.disabled?() == true
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it is enabled for socket when remote ip is enabled" do
|
2020-02-27 16:46:05 +01:00
|
|
|
Config.put([:rate_limit, @limiter_name], {1, 1})
|
|
|
|
Config.put([Pleroma.Web.Endpoint, :http, :ip], {:local, "/path/to/pleroma.sock"})
|
|
|
|
Config.put([Pleroma.Plugs.RemoteIp, :enabled], true)
|
2019-12-13 17:00:26 +01:00
|
|
|
|
|
|
|
assert RateLimiter.disabled?() == false
|
|
|
|
end
|
|
|
|
|
2019-11-11 13:13:06 +01:00
|
|
|
test "it restricts based on config values" do
|
2020-02-27 16:46:05 +01:00
|
|
|
limiter_name = :test_plug_opts
|
2019-11-13 12:07:53 +01:00
|
|
|
scale = 80
|
2019-11-11 13:13:06 +01:00
|
|
|
limit = 5
|
2019-06-11 09:27:41 +02:00
|
|
|
|
2020-02-27 16:46:05 +01:00
|
|
|
Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
|
|
|
|
Config.put([:rate_limit, limiter_name], {scale, limit})
|
2019-06-11 09:27:41 +02:00
|
|
|
|
2020-02-27 16:46:05 +01:00
|
|
|
plug_opts = RateLimiter.init(name: limiter_name)
|
2019-11-11 13:13:06 +01:00
|
|
|
conn = conn(:get, "/")
|
2019-06-11 09:27:41 +02:00
|
|
|
|
2019-11-11 13:13:06 +01:00
|
|
|
for i <- 1..5 do
|
2020-02-27 16:46:05 +01:00
|
|
|
conn = RateLimiter.call(conn, plug_opts)
|
|
|
|
assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
|
2019-11-11 13:13:06 +01:00
|
|
|
Process.sleep(10)
|
|
|
|
end
|
2019-06-11 09:27:41 +02:00
|
|
|
|
2020-02-27 16:46:05 +01:00
|
|
|
conn = RateLimiter.call(conn, plug_opts)
|
2019-11-11 13:13:06 +01:00
|
|
|
assert %{"error" => "Throttled"} = Phoenix.ConnTest.json_response(conn, :too_many_requests)
|
|
|
|
assert conn.halted
|
2019-06-11 09:27:41 +02:00
|
|
|
|
2019-11-11 13:13:06 +01:00
|
|
|
Process.sleep(50)
|
2019-06-11 09:27:41 +02:00
|
|
|
|
2019-11-11 13:13:06 +01:00
|
|
|
conn = conn(:get, "/")
|
2019-06-11 09:27:41 +02:00
|
|
|
|
2020-02-27 16:46:05 +01:00
|
|
|
conn = RateLimiter.call(conn, plug_opts)
|
|
|
|
assert {1, 4} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
|
2019-06-11 09:27:41 +02:00
|
|
|
|
2019-11-11 13:13:06 +01:00
|
|
|
refute conn.status == Plug.Conn.Status.code(:too_many_requests)
|
|
|
|
refute conn.resp_body
|
|
|
|
refute conn.halted
|
|
|
|
end
|
|
|
|
end
|
2019-06-11 09:27:41 +02:00
|
|
|
|
2019-11-11 13:13:06 +01:00
|
|
|
describe "options" do
|
|
|
|
test "`bucket_name` option overrides default bucket name" do
|
|
|
|
limiter_name = :test_bucket_name
|
2019-06-11 09:27:41 +02:00
|
|
|
|
2020-02-27 16:46:05 +01:00
|
|
|
Config.put([:rate_limit, limiter_name], {1000, 5})
|
|
|
|
Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
|
2019-06-11 09:27:41 +02:00
|
|
|
|
2019-11-11 13:13:06 +01:00
|
|
|
base_bucket_name = "#{limiter_name}:group1"
|
2020-02-27 16:46:05 +01:00
|
|
|
plug_opts = RateLimiter.init(name: limiter_name, bucket_name: base_bucket_name)
|
2019-06-11 09:27:41 +02:00
|
|
|
|
2019-11-11 13:13:06 +01:00
|
|
|
conn = conn(:get, "/")
|
2019-06-11 09:27:41 +02:00
|
|
|
|
2020-02-27 16:46:05 +01:00
|
|
|
RateLimiter.call(conn, plug_opts)
|
|
|
|
assert {1, 4} = RateLimiter.inspect_bucket(conn, base_bucket_name, plug_opts)
|
2020-02-29 20:04:09 +01:00
|
|
|
assert {:error, :not_found} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
|
2019-11-11 13:13:06 +01:00
|
|
|
end
|
2019-06-11 09:27:41 +02:00
|
|
|
|
2019-11-11 13:13:06 +01:00
|
|
|
test "`params` option allows different queries to be tracked independently" do
|
|
|
|
limiter_name = :test_params
|
2020-02-27 16:46:05 +01:00
|
|
|
Config.put([:rate_limit, limiter_name], {1000, 5})
|
|
|
|
Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
|
2019-07-13 13:49:39 +02:00
|
|
|
|
2020-02-27 16:46:05 +01:00
|
|
|
plug_opts = RateLimiter.init(name: limiter_name, params: ["id"])
|
2019-07-13 13:49:39 +02:00
|
|
|
|
2019-11-11 13:13:06 +01:00
|
|
|
conn = conn(:get, "/?id=1")
|
|
|
|
conn = Plug.Conn.fetch_query_params(conn)
|
|
|
|
conn_2 = conn(:get, "/?id=2")
|
2019-07-13 13:49:39 +02:00
|
|
|
|
2020-02-27 16:46:05 +01: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 13:13:06 +01:00
|
|
|
end
|
2019-07-13 13:49:39 +02:00
|
|
|
|
2019-11-11 13:13:06 +01:00
|
|
|
test "it supports combination of options modifying bucket name" do
|
|
|
|
limiter_name = :test_options_combo
|
2020-02-27 16:46:05 +01:00
|
|
|
Config.put([:rate_limit, limiter_name], {1000, 5})
|
|
|
|
Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
|
2019-07-13 13:49:39 +02:00
|
|
|
|
2019-11-11 13:13:06 +01:00
|
|
|
base_bucket_name = "#{limiter_name}:group1"
|
2020-02-27 16:46:05 +01:00
|
|
|
|
|
|
|
plug_opts =
|
|
|
|
RateLimiter.init(name: limiter_name, bucket_name: base_bucket_name, params: ["id"])
|
|
|
|
|
2019-11-11 13:13:06 +01:00
|
|
|
id = "100"
|
2019-07-13 13:49:39 +02:00
|
|
|
|
2019-11-11 13:13:06 +01:00
|
|
|
conn = conn(:get, "/?id=#{id}")
|
|
|
|
conn = Plug.Conn.fetch_query_params(conn)
|
|
|
|
conn_2 = conn(:get, "/?id=#{101}")
|
2019-07-13 13:49:39 +02:00
|
|
|
|
2020-02-27 16:46:05 +01: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 13:13:06 +01:00
|
|
|
end
|
2019-07-13 13:49:39 +02:00
|
|
|
end
|
|
|
|
|
2019-11-11 13:13:06 +01:00
|
|
|
describe "unauthenticated users" do
|
|
|
|
test "are restricted based on remote IP" do
|
|
|
|
limiter_name = :test_unauthenticated
|
2020-02-27 16:46:05 +01:00
|
|
|
Config.put([:rate_limit, limiter_name], [{1000, 5}, {1, 10}])
|
|
|
|
Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
|
2019-11-11 13:13:06 +01:00
|
|
|
|
2020-02-27 16:46:05 +01:00
|
|
|
plug_opts = RateLimiter.init(name: limiter_name)
|
2019-07-13 13:49:39 +02:00
|
|
|
|
2019-11-11 13:13:06 +01:00
|
|
|
conn = %{conn(:get, "/") | remote_ip: {127, 0, 0, 2}}
|
|
|
|
conn_2 = %{conn(:get, "/") | remote_ip: {127, 0, 0, 3}}
|
2019-07-13 13:49:39 +02:00
|
|
|
|
2019-11-11 13:13:06 +01:00
|
|
|
for i <- 1..5 do
|
2020-02-27 16:46:05 +01:00
|
|
|
conn = RateLimiter.call(conn, plug_opts)
|
|
|
|
assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
|
2019-11-11 13:13:06 +01:00
|
|
|
refute conn.halted
|
|
|
|
end
|
2019-07-13 13:49:39 +02:00
|
|
|
|
2020-02-27 16:46:05 +01:00
|
|
|
conn = RateLimiter.call(conn, plug_opts)
|
2019-07-13 13:49:39 +02:00
|
|
|
|
2019-11-11 13:13:06 +01:00
|
|
|
assert %{"error" => "Throttled"} = Phoenix.ConnTest.json_response(conn, :too_many_requests)
|
|
|
|
assert conn.halted
|
|
|
|
|
2020-02-27 16:46:05 +01:00
|
|
|
conn_2 = RateLimiter.call(conn_2, plug_opts)
|
|
|
|
assert {1, 4} = RateLimiter.inspect_bucket(conn_2, limiter_name, plug_opts)
|
2019-11-11 13:13:06 +01:00
|
|
|
|
|
|
|
refute conn_2.status == Plug.Conn.Status.code(:too_many_requests)
|
|
|
|
refute conn_2.resp_body
|
|
|
|
refute conn_2.halted
|
|
|
|
end
|
2019-07-13 13:49:39 +02:00
|
|
|
end
|
|
|
|
|
2019-11-11 13:13:06 +01:00
|
|
|
describe "authenticated users" do
|
|
|
|
setup do
|
|
|
|
Ecto.Adapters.SQL.Sandbox.checkout(Pleroma.Repo)
|
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
2020-02-27 16:46:05 +01:00
|
|
|
test "can have limits separate from unauthenticated connections" do
|
|
|
|
limiter_name = :test_authenticated1
|
2019-11-11 13:13:06 +01:00
|
|
|
|
2019-12-15 23:03:13 +01:00
|
|
|
scale = 50
|
2019-11-11 13:13:06 +01:00
|
|
|
limit = 5
|
2020-02-27 16:46:05 +01:00
|
|
|
Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
|
|
|
|
Config.put([:rate_limit, limiter_name], [{1000, 1}, {scale, limit}])
|
2019-11-11 13:13:06 +01:00
|
|
|
|
2020-02-27 16:46:05 +01:00
|
|
|
plug_opts = RateLimiter.init(name: limiter_name)
|
2019-06-11 09:27:41 +02:00
|
|
|
|
2019-11-11 13:13:06 +01:00
|
|
|
user = insert(:user)
|
|
|
|
conn = conn(:get, "/") |> assign(:user, user)
|
2019-06-11 09:27:41 +02:00
|
|
|
|
2019-11-11 13:13:06 +01:00
|
|
|
for i <- 1..5 do
|
2020-02-27 16:46:05 +01:00
|
|
|
conn = RateLimiter.call(conn, plug_opts)
|
|
|
|
assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
|
2019-11-11 13:13:06 +01:00
|
|
|
refute conn.halted
|
|
|
|
end
|
2019-06-11 09:27:41 +02:00
|
|
|
|
2020-02-27 16:46:05 +01:00
|
|
|
conn = RateLimiter.call(conn, plug_opts)
|
2019-06-11 09:27:41 +02:00
|
|
|
|
2019-11-11 13:13:06 +01:00
|
|
|
assert %{"error" => "Throttled"} = Phoenix.ConnTest.json_response(conn, :too_many_requests)
|
|
|
|
assert conn.halted
|
|
|
|
end
|
2019-06-11 09:27:41 +02:00
|
|
|
|
2020-02-27 16:46:05 +01:00
|
|
|
test "different users are counted independently" do
|
|
|
|
limiter_name = :test_authenticated2
|
|
|
|
Config.put([:rate_limit, limiter_name], [{1, 10}, {1000, 5}])
|
|
|
|
Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
|
2019-06-11 09:27:41 +02:00
|
|
|
|
2020-02-27 16:46:05 +01:00
|
|
|
plug_opts = RateLimiter.init(name: limiter_name)
|
2019-06-11 09:27:41 +02:00
|
|
|
|
2019-11-11 13:13:06 +01:00
|
|
|
user = insert(:user)
|
|
|
|
conn = conn(:get, "/") |> assign(:user, user)
|
2019-06-11 09:27:41 +02:00
|
|
|
|
2019-11-11 13:13:06 +01:00
|
|
|
user_2 = insert(:user)
|
|
|
|
conn_2 = conn(:get, "/") |> assign(:user, user_2)
|
2019-06-11 09:27:41 +02:00
|
|
|
|
2019-11-11 13:13:06 +01:00
|
|
|
for i <- 1..5 do
|
2020-02-27 16:46:05 +01:00
|
|
|
conn = RateLimiter.call(conn, plug_opts)
|
|
|
|
assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
|
2019-11-11 13:13:06 +01:00
|
|
|
end
|
2019-06-11 09:27:41 +02:00
|
|
|
|
2020-02-27 16:46:05 +01:00
|
|
|
conn = RateLimiter.call(conn, plug_opts)
|
2019-11-11 13:13:06 +01:00
|
|
|
assert %{"error" => "Throttled"} = Phoenix.ConnTest.json_response(conn, :too_many_requests)
|
|
|
|
assert conn.halted
|
2019-06-11 09:27:41 +02:00
|
|
|
|
2020-02-27 16:46:05 +01:00
|
|
|
conn_2 = RateLimiter.call(conn_2, plug_opts)
|
|
|
|
assert {1, 4} = RateLimiter.inspect_bucket(conn_2, limiter_name, plug_opts)
|
2019-11-11 13:13:06 +01:00
|
|
|
refute conn_2.status == Plug.Conn.Status.code(:too_many_requests)
|
|
|
|
refute conn_2.resp_body
|
|
|
|
refute conn_2.halted
|
|
|
|
end
|
2019-06-11 09:27:41 +02:00
|
|
|
end
|
|
|
|
end
|