ratelimiting.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. # Copyright 2014-2016 OpenMarket Ltd
  2. # Copyright 2020 The Matrix.org Foundation C.I.C.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from collections import OrderedDict
  16. from typing import Hashable, Optional, Tuple
  17. from synapse.api.errors import LimitExceededError
  18. from synapse.config.ratelimiting import RateLimitConfig
  19. from synapse.storage.databases.main import DataStore
  20. from synapse.types import Requester
  21. from synapse.util import Clock
  22. class Ratelimiter:
  23. """
  24. Ratelimit actions marked by arbitrary keys.
  25. Args:
  26. clock: A homeserver clock, for retrieving the current time
  27. rate_hz: The long term number of actions that can be performed in a second.
  28. burst_count: How many actions that can be performed before being limited.
  29. """
  30. def __init__(
  31. self, store: DataStore, clock: Clock, rate_hz: float, burst_count: int
  32. ):
  33. self.clock = clock
  34. self.rate_hz = rate_hz
  35. self.burst_count = burst_count
  36. self.store = store
  37. # A ordered dictionary keeping track of actions, when they were last
  38. # performed and how often. Each entry is a mapping from a key of arbitrary type
  39. # to a tuple representing:
  40. # * How many times an action has occurred since a point in time
  41. # * The point in time
  42. # * The rate_hz of this particular entry. This can vary per request
  43. self.actions: OrderedDict[Hashable, Tuple[float, float, float]] = OrderedDict()
  44. async def can_do_action(
  45. self,
  46. requester: Optional[Requester],
  47. key: Optional[Hashable] = None,
  48. rate_hz: Optional[float] = None,
  49. burst_count: Optional[int] = None,
  50. update: bool = True,
  51. n_actions: int = 1,
  52. _time_now_s: Optional[float] = None,
  53. ) -> Tuple[bool, float]:
  54. """Can the entity (e.g. user or IP address) perform the action?
  55. Checks if the user has ratelimiting disabled in the database by looking
  56. for null/zero values in the `ratelimit_override` table. (Non-zero
  57. values aren't honoured, as they're specific to the event sending
  58. ratelimiter, rather than all ratelimiters)
  59. Args:
  60. requester: The requester that is doing the action, if any. Used to check
  61. if the user has ratelimits disabled in the database.
  62. key: An arbitrary key used to classify an action. Defaults to the
  63. requester's user ID.
  64. rate_hz: The long term number of actions that can be performed in a second.
  65. Overrides the value set during instantiation if set.
  66. burst_count: How many actions that can be performed before being limited.
  67. Overrides the value set during instantiation if set.
  68. update: Whether to count this check as performing the action
  69. n_actions: The number of times the user wants to do this action. If the user
  70. cannot do all of the actions, the user's action count is not incremented
  71. at all.
  72. _time_now_s: The current time. Optional, defaults to the current time according
  73. to self.clock. Only used by tests.
  74. Returns:
  75. A tuple containing:
  76. * A bool indicating if they can perform the action now
  77. * The reactor timestamp for when the action can be performed next.
  78. -1 if rate_hz is less than or equal to zero
  79. """
  80. if key is None:
  81. if not requester:
  82. raise ValueError("Must supply at least one of `requester` or `key`")
  83. key = requester.user.to_string()
  84. if requester:
  85. # Disable rate limiting of users belonging to any AS that is configured
  86. # not to be rate limited in its registration file (rate_limited: true|false).
  87. if requester.app_service and not requester.app_service.is_rate_limited():
  88. return True, -1.0
  89. # Check if ratelimiting has been disabled for the user.
  90. #
  91. # Note that we don't use the returned rate/burst count, as the table
  92. # is specifically for the event sending ratelimiter. Instead, we
  93. # only use it to (somewhat cheekily) infer whether the user should
  94. # be subject to any rate limiting or not.
  95. override = await self.store.get_ratelimit_for_user(
  96. requester.authenticated_entity
  97. )
  98. if override and not override.messages_per_second:
  99. return True, -1.0
  100. # Override default values if set
  101. time_now_s = _time_now_s if _time_now_s is not None else self.clock.time()
  102. rate_hz = rate_hz if rate_hz is not None else self.rate_hz
  103. burst_count = burst_count if burst_count is not None else self.burst_count
  104. # Remove any expired entries
  105. self._prune_message_counts(time_now_s)
  106. # Check if there is an existing count entry for this key
  107. action_count, time_start, _ = self.actions.get(key, (0.0, time_now_s, 0.0))
  108. # Check whether performing another action is allowed
  109. time_delta = time_now_s - time_start
  110. performed_count = action_count - time_delta * rate_hz
  111. if performed_count < 0:
  112. performed_count = 0
  113. # Reset the start time and forgive all actions
  114. action_count = 0
  115. time_start = time_now_s
  116. # This check would be easier read as performed_count + n_actions > burst_count,
  117. # but performed_count might be a very precise float (with lots of numbers
  118. # following the point) in which case Python might round it up when adding it to
  119. # n_actions. Writing it this way ensures it doesn't happen.
  120. if performed_count > burst_count - n_actions:
  121. # Deny, we have exceeded our burst count
  122. allowed = False
  123. else:
  124. # We haven't reached our limit yet
  125. allowed = True
  126. action_count = action_count + n_actions
  127. if update:
  128. self.actions[key] = (action_count, time_start, rate_hz)
  129. if rate_hz > 0:
  130. # Find out when the count of existing actions expires
  131. time_allowed = time_start + (action_count - burst_count + 1) / rate_hz
  132. # Don't give back a time in the past
  133. if time_allowed < time_now_s:
  134. time_allowed = time_now_s
  135. else:
  136. # XXX: Why is this -1? This seems to only be used in
  137. # self.ratelimit. I guess so that clients get a time in the past and don't
  138. # feel afraid to try again immediately
  139. time_allowed = -1
  140. return allowed, time_allowed
  141. def _prune_message_counts(self, time_now_s: float) -> None:
  142. """Remove message count entries that have not exceeded their defined
  143. rate_hz limit
  144. Args:
  145. time_now_s: The current time
  146. """
  147. # We create a copy of the key list here as the dictionary is modified during
  148. # the loop
  149. for key in list(self.actions.keys()):
  150. action_count, time_start, rate_hz = self.actions[key]
  151. # Rate limit = "seconds since we started limiting this action" * rate_hz
  152. # If this limit has not been exceeded, wipe our record of this action
  153. time_delta = time_now_s - time_start
  154. if action_count - time_delta * rate_hz > 0:
  155. continue
  156. else:
  157. del self.actions[key]
  158. async def ratelimit(
  159. self,
  160. requester: Optional[Requester],
  161. key: Optional[Hashable] = None,
  162. rate_hz: Optional[float] = None,
  163. burst_count: Optional[int] = None,
  164. update: bool = True,
  165. n_actions: int = 1,
  166. _time_now_s: Optional[float] = None,
  167. ) -> None:
  168. """Checks if an action can be performed. If not, raises a LimitExceededError
  169. Checks if the user has ratelimiting disabled in the database by looking
  170. for null/zero values in the `ratelimit_override` table. (Non-zero
  171. values aren't honoured, as they're specific to the event sending
  172. ratelimiter, rather than all ratelimiters)
  173. Args:
  174. requester: The requester that is doing the action, if any. Used to check for
  175. if the user has ratelimits disabled.
  176. key: An arbitrary key used to classify an action. Defaults to the
  177. requester's user ID.
  178. rate_hz: The long term number of actions that can be performed in a second.
  179. Overrides the value set during instantiation if set.
  180. burst_count: How many actions that can be performed before being limited.
  181. Overrides the value set during instantiation if set.
  182. update: Whether to count this check as performing the action
  183. n_actions: The number of times the user wants to do this action. If the user
  184. cannot do all of the actions, the user's action count is not incremented
  185. at all.
  186. _time_now_s: The current time. Optional, defaults to the current time according
  187. to self.clock. Only used by tests.
  188. Raises:
  189. LimitExceededError: If an action could not be performed, along with the time in
  190. milliseconds until the action can be performed again
  191. """
  192. time_now_s = _time_now_s if _time_now_s is not None else self.clock.time()
  193. allowed, time_allowed = await self.can_do_action(
  194. requester,
  195. key,
  196. rate_hz=rate_hz,
  197. burst_count=burst_count,
  198. update=update,
  199. n_actions=n_actions,
  200. _time_now_s=time_now_s,
  201. )
  202. if not allowed:
  203. raise LimitExceededError(
  204. retry_after_ms=int(1000 * (time_allowed - time_now_s))
  205. )
  206. class RequestRatelimiter:
  207. def __init__(
  208. self,
  209. store: DataStore,
  210. clock: Clock,
  211. rc_message: RateLimitConfig,
  212. rc_admin_redaction: Optional[RateLimitConfig],
  213. ):
  214. self.store = store
  215. self.clock = clock
  216. # The rate_hz and burst_count are overridden on a per-user basis
  217. self.request_ratelimiter = Ratelimiter(
  218. store=self.store, clock=self.clock, rate_hz=0, burst_count=0
  219. )
  220. self._rc_message = rc_message
  221. # Check whether ratelimiting room admin message redaction is enabled
  222. # by the presence of rate limits in the config
  223. if rc_admin_redaction:
  224. self.admin_redaction_ratelimiter: Optional[Ratelimiter] = Ratelimiter(
  225. store=self.store,
  226. clock=self.clock,
  227. rate_hz=rc_admin_redaction.per_second,
  228. burst_count=rc_admin_redaction.burst_count,
  229. )
  230. else:
  231. self.admin_redaction_ratelimiter = None
  232. async def ratelimit(
  233. self,
  234. requester: Requester,
  235. update: bool = True,
  236. is_admin_redaction: bool = False,
  237. ) -> None:
  238. """Ratelimits requests.
  239. Args:
  240. requester
  241. update: Whether to record that a request is being processed.
  242. Set to False when doing multiple checks for one request (e.g.
  243. to check up front if we would reject the request), and set to
  244. True for the last call for a given request.
  245. is_admin_redaction: Whether this is a room admin/moderator
  246. redacting an event. If so then we may apply different
  247. ratelimits depending on config.
  248. Raises:
  249. LimitExceededError if the request should be ratelimited
  250. """
  251. user_id = requester.user.to_string()
  252. # The AS user itself is never rate limited.
  253. app_service = self.store.get_app_service_by_user_id(user_id)
  254. if app_service is not None:
  255. return # do not ratelimit app service senders
  256. messages_per_second = self._rc_message.per_second
  257. burst_count = self._rc_message.burst_count
  258. # Check if there is a per user override in the DB.
  259. override = await self.store.get_ratelimit_for_user(user_id)
  260. if override:
  261. # If overridden with a null Hz then ratelimiting has been entirely
  262. # disabled for the user
  263. if not override.messages_per_second:
  264. return
  265. messages_per_second = override.messages_per_second
  266. burst_count = override.burst_count
  267. if is_admin_redaction and self.admin_redaction_ratelimiter:
  268. # If we have separate config for admin redactions, use a separate
  269. # ratelimiter as to not have user_ids clash
  270. await self.admin_redaction_ratelimiter.ratelimit(requester, update=update)
  271. else:
  272. # Override rate and burst count per-user
  273. await self.request_ratelimiter.ratelimit(
  274. requester,
  275. rate_hz=messages_per_second,
  276. burst_count=burst_count,
  277. update=update,
  278. )