1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Plugs.RateLimiter do
10 A keyword list of rate limiters where a key is a limiter name and value is the limiter configuration. The basic configuration is a tuple where:
12 * The first element: `scale` (Integer). The time scale in milliseconds.
13 * The second element: `limit` (Integer). How many requests to limit in the time scale provided.
15 It is also possible to have different limits for unauthenticated and authenticated users: the keyword value must be a list of two tuples where the first one is a config for unauthenticated users and the second one is for authenticated.
17 To disable a limiter set its value to `nil`.
21 config :pleroma, :rate_limit,
23 two: [{10_000, 10}, {10_000, 50}],
26 Here we have three limiters:
28 * `one` which is not over 10req/1s
29 * `two` which has two limits: 10req/10s for unauthenticated users and 50req/10s for authenticated users
30 * `foobar` which is disabled
36 plug(Pleroma.Plugs.RateLimiter, :limiter_name)
37 plug(Pleroma.Plugs.RateLimiter, {:limiter_name, options})
41 * `bucket_name` overrides bucket name (e.g. to have a separate limit for a set of actions)
42 * `params` appends values of specified request params (e.g. ["id"]) to bucket name
46 plug(Pleroma.Plugs.RateLimiter, :one when action == :one)
47 plug(Pleroma.Plugs.RateLimiter, :two when action in [:two, :three])
50 Pleroma.Plugs.RateLimiter,
51 {:status_id_action, bucket_name: "status_id_action:fav_unfav", params: ["id"]}
52 when action in ~w(fav_status unfav_status)a
55 or inside a router pipeline:
59 plug(Pleroma.Plugs.RateLimiter, :one)
63 import Pleroma.Web.TranslationHelpers
68 def init(limiter_name) when is_atom(limiter_name) do
69 init({limiter_name, []})
72 def init({limiter_name, opts}) do
73 case Pleroma.Config.get([:rate_limit, limiter_name]) do
75 config -> {limiter_name, config, opts}
79 # Do not limit if there is no limiter configuration
80 def call(conn, nil), do: conn
82 def call(conn, settings) do
83 case check_rate(conn, settings) do
88 render_throttled_error(conn)
92 defp bucket_name(conn, limiter_name, opts) do
93 bucket_name = opts[:bucket_name] || limiter_name
95 if params_names = opts[:params] do
96 params_values = for p <- Enum.sort(params_names), do: conn.params[p]
97 Enum.join([bucket_name] ++ params_values, ":")
104 %{assigns: %{user: %User{id: user_id}}} = conn,
105 {limiter_name, [_, {scale, limit}], opts}
107 bucket_name = bucket_name(conn, limiter_name, opts)
108 ExRated.check_rate("#{bucket_name}:#{user_id}", scale, limit)
111 defp check_rate(conn, {limiter_name, [{scale, limit} | _], opts}) do
112 bucket_name = bucket_name(conn, limiter_name, opts)
113 ExRated.check_rate("#{bucket_name}:#{ip(conn)}", scale, limit)
116 defp check_rate(conn, {limiter_name, {scale, limit}, opts}) do
117 check_rate(conn, {limiter_name, [{scale, limit}, {scale, limit}], opts})
120 def ip(%{remote_ip: remote_ip}) do
126 defp render_throttled_error(conn) do
128 |> render_error(:too_many_requests, "Throttled")