logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

rate_limiter.ex (7576B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Plugs.RateLimiter do
  5. @moduledoc """
  6. ## Configuration
  7. A keyword list of rate limiters where a key is a limiter name and value is the limiter configuration.
  8. The basic configuration is a tuple where:
  9. * The first element: `scale` (Integer). The time scale in milliseconds.
  10. * The second element: `limit` (Integer). How many requests to limit in the time scale provided.
  11. It is also possible to have different limits for unauthenticated and authenticated users: the keyword value must be a
  12. list of two tuples where the first one is a config for unauthenticated users and the second one is for authenticated.
  13. To disable a limiter set its value to `nil`.
  14. ### Example
  15. config :pleroma, :rate_limit,
  16. one: {1000, 10},
  17. two: [{10_000, 10}, {10_000, 50}],
  18. foobar: nil
  19. Here we have three limiters:
  20. * `one` which is not over 10req/1s
  21. * `two` which has two limits: 10req/10s for unauthenticated users and 50req/10s for authenticated users
  22. * `foobar` which is disabled
  23. ## Usage
  24. AllowedSyntax:
  25. plug(Pleroma.Plugs.RateLimiter, name: :limiter_name)
  26. plug(Pleroma.Plugs.RateLimiter, options) # :name is a required option
  27. Allowed options:
  28. * `name` required, always used to fetch the limit values from the config
  29. * `bucket_name` overrides name for counting purposes (e.g. to have a separate limit for a set of actions)
  30. * `params` appends values of specified request params (e.g. ["id"]) to bucket name
  31. Inside a controller:
  32. plug(Pleroma.Plugs.RateLimiter, [name: :one] when action == :one)
  33. plug(Pleroma.Plugs.RateLimiter, [name: :two] when action in [:two, :three])
  34. plug(
  35. Pleroma.Plugs.RateLimiter,
  36. [name: :status_id_action, bucket_name: "status_id_action:fav_unfav", params: ["id"]]
  37. when action in ~w(fav_status unfav_status)a
  38. )
  39. or inside a router pipeline:
  40. pipeline :api do
  41. ...
  42. plug(Pleroma.Plugs.RateLimiter, name: :one)
  43. ...
  44. end
  45. """
  46. import Pleroma.Web.TranslationHelpers
  47. import Plug.Conn
  48. alias Pleroma.Config
  49. alias Pleroma.Plugs.RateLimiter.LimiterSupervisor
  50. alias Pleroma.User
  51. require Logger
  52. @doc false
  53. def init(plug_opts) do
  54. plug_opts
  55. end
  56. def call(conn, plug_opts) do
  57. if disabled?(conn) do
  58. handle_disabled(conn)
  59. else
  60. action_settings = action_settings(plug_opts)
  61. handle(conn, action_settings)
  62. end
  63. end
  64. defp handle_disabled(conn) do
  65. Logger.warn(
  66. "Rate limiter disabled due to forwarded IP not being found. Please ensure your reverse proxy is providing the X-Forwarded-For header or disable the RemoteIP plug/rate limiter."
  67. )
  68. conn
  69. end
  70. defp handle(conn, nil), do: conn
  71. defp handle(conn, action_settings) do
  72. action_settings
  73. |> incorporate_conn_info(conn)
  74. |> check_rate()
  75. |> case do
  76. {:ok, _count} ->
  77. conn
  78. {:error, _count} ->
  79. render_throttled_error(conn)
  80. end
  81. end
  82. def disabled?(conn) do
  83. if Map.has_key?(conn.assigns, :remote_ip_found),
  84. do: !conn.assigns.remote_ip_found,
  85. else: false
  86. end
  87. @inspect_bucket_not_found {:error, :not_found}
  88. def inspect_bucket(conn, bucket_name_root, plug_opts) do
  89. with %{name: _} = action_settings <- action_settings(plug_opts) do
  90. action_settings = incorporate_conn_info(action_settings, conn)
  91. bucket_name = make_bucket_name(%{action_settings | name: bucket_name_root})
  92. key_name = make_key_name(action_settings)
  93. limit = get_limits(action_settings)
  94. case Cachex.get(bucket_name, key_name) do
  95. {:error, :no_cache} ->
  96. @inspect_bucket_not_found
  97. {:ok, nil} ->
  98. {0, limit}
  99. {:ok, value} ->
  100. {value, limit - value}
  101. end
  102. else
  103. _ -> @inspect_bucket_not_found
  104. end
  105. end
  106. def action_settings(plug_opts) do
  107. with limiter_name when is_atom(limiter_name) <- plug_opts[:name],
  108. limits when not is_nil(limits) <- Config.get([:rate_limit, limiter_name]) do
  109. bucket_name_root = Keyword.get(plug_opts, :bucket_name, limiter_name)
  110. %{
  111. name: bucket_name_root,
  112. limits: limits,
  113. opts: plug_opts
  114. }
  115. end
  116. end
  117. defp check_rate(action_settings) do
  118. bucket_name = make_bucket_name(action_settings)
  119. key_name = make_key_name(action_settings)
  120. limit = get_limits(action_settings)
  121. case Cachex.get_and_update(bucket_name, key_name, &increment_value(&1, limit)) do
  122. {:commit, value} ->
  123. {:ok, value}
  124. {:ignore, value} ->
  125. {:error, value}
  126. {:error, :no_cache} ->
  127. initialize_buckets!(action_settings)
  128. check_rate(action_settings)
  129. end
  130. end
  131. defp increment_value(nil, _limit), do: {:commit, 1}
  132. defp increment_value(val, limit) when val >= limit, do: {:ignore, val}
  133. defp increment_value(val, _limit), do: {:commit, val + 1}
  134. defp incorporate_conn_info(action_settings, %{
  135. assigns: %{user: %User{id: user_id}},
  136. params: params
  137. }) do
  138. Map.merge(action_settings, %{
  139. mode: :user,
  140. conn_params: params,
  141. conn_info: "#{user_id}"
  142. })
  143. end
  144. defp incorporate_conn_info(action_settings, %{params: params} = conn) do
  145. Map.merge(action_settings, %{
  146. mode: :anon,
  147. conn_params: params,
  148. conn_info: "#{ip(conn)}"
  149. })
  150. end
  151. defp ip(%{remote_ip: remote_ip}) do
  152. remote_ip
  153. |> Tuple.to_list()
  154. |> Enum.join(".")
  155. end
  156. defp render_throttled_error(conn) do
  157. conn
  158. |> render_error(:too_many_requests, "Throttled")
  159. |> halt()
  160. end
  161. defp make_key_name(action_settings) do
  162. ""
  163. |> attach_selected_params(action_settings)
  164. |> attach_identity(action_settings)
  165. end
  166. defp get_scale(_, {scale, _}), do: scale
  167. defp get_scale(:anon, [{scale, _}, {_, _}]), do: scale
  168. defp get_scale(:user, [{_, _}, {scale, _}]), do: scale
  169. defp get_limits(%{limits: {_scale, limit}}), do: limit
  170. defp get_limits(%{mode: :user, limits: [_, {_, limit}]}), do: limit
  171. defp get_limits(%{limits: [{_, limit}, _]}), do: limit
  172. defp make_bucket_name(%{mode: :user, name: bucket_name_root}),
  173. do: user_bucket_name(bucket_name_root)
  174. defp make_bucket_name(%{mode: :anon, name: bucket_name_root}),
  175. do: anon_bucket_name(bucket_name_root)
  176. defp attach_selected_params(input, %{conn_params: conn_params, opts: plug_opts}) do
  177. params_string =
  178. plug_opts
  179. |> Keyword.get(:params, [])
  180. |> Enum.sort()
  181. |> Enum.map(&Map.get(conn_params, &1, ""))
  182. |> Enum.join(":")
  183. [input, params_string]
  184. |> Enum.join(":")
  185. |> String.replace_leading(":", "")
  186. end
  187. defp initialize_buckets!(%{name: _name, limits: nil}), do: :ok
  188. defp initialize_buckets!(%{name: name, limits: limits}) do
  189. {:ok, _pid} =
  190. LimiterSupervisor.add_or_return_limiter(anon_bucket_name(name), get_scale(:anon, limits))
  191. {:ok, _pid} =
  192. LimiterSupervisor.add_or_return_limiter(user_bucket_name(name), get_scale(:user, limits))
  193. :ok
  194. end
  195. defp attach_identity(base, %{mode: :user, conn_info: conn_info}),
  196. do: "user:#{base}:#{conn_info}"
  197. defp attach_identity(base, %{mode: :anon, conn_info: conn_info}),
  198. do: "ip:#{base}:#{conn_info}"
  199. defp user_bucket_name(bucket_name_root), do: "user:#{bucket_name_root}" |> String.to_atom()
  200. defp anon_bucket_name(bucket_name_root), do: "anon:#{bucket_name_root}" |> String.to_atom()
  201. end