You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

163 lines
6.1 KiB

  1. # Copyright 2014-2016 OpenMarket Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import Any, Dict, Optional
  15. import attr
  16. from synapse.types import JsonDict
  17. from ._base import Config
  18. class RatelimitSettings:
  19. def __init__(
  20. self,
  21. config: Dict[str, float],
  22. defaults: Optional[Dict[str, float]] = None,
  23. ):
  24. defaults = defaults or {"per_second": 0.17, "burst_count": 3.0}
  25. self.per_second = config.get("per_second", defaults["per_second"])
  26. self.burst_count = int(config.get("burst_count", defaults["burst_count"]))
  27. @attr.s(auto_attribs=True)
  28. class FederationRatelimitSettings:
  29. window_size: int = 1000
  30. sleep_limit: int = 10
  31. sleep_delay: int = 500
  32. reject_limit: int = 50
  33. concurrent: int = 3
  34. class RatelimitConfig(Config):
  35. section = "ratelimiting"
  36. def read_config(self, config: JsonDict, **kwargs: Any) -> None:
  37. # Load the new-style messages config if it exists. Otherwise fall back
  38. # to the old method.
  39. if "rc_message" in config:
  40. self.rc_message = RatelimitSettings(
  41. config["rc_message"], defaults={"per_second": 0.2, "burst_count": 10.0}
  42. )
  43. else:
  44. self.rc_message = RatelimitSettings(
  45. {
  46. "per_second": config.get("rc_messages_per_second", 0.2),
  47. "burst_count": config.get("rc_message_burst_count", 10.0),
  48. }
  49. )
  50. # Load the new-style federation config, if it exists. Otherwise, fall
  51. # back to the old method.
  52. if "rc_federation" in config:
  53. self.rc_federation = FederationRatelimitSettings(**config["rc_federation"])
  54. else:
  55. self.rc_federation = FederationRatelimitSettings(
  56. **{
  57. k: v
  58. for k, v in {
  59. "window_size": config.get("federation_rc_window_size"),
  60. "sleep_limit": config.get("federation_rc_sleep_limit"),
  61. "sleep_delay": config.get("federation_rc_sleep_delay"),
  62. "reject_limit": config.get("federation_rc_reject_limit"),
  63. "concurrent": config.get("federation_rc_concurrent"),
  64. }.items()
  65. if v is not None
  66. }
  67. )
  68. self.rc_registration = RatelimitSettings(config.get("rc_registration", {}))
  69. self.rc_registration_token_validity = RatelimitSettings(
  70. config.get("rc_registration_token_validity", {}),
  71. defaults={"per_second": 0.1, "burst_count": 5},
  72. )
  73. # It is reasonable to login with a bunch of devices at once (i.e. when
  74. # setting up an account), but it is *not* valid to continually be
  75. # logging into new devices.
  76. rc_login_config = config.get("rc_login", {})
  77. self.rc_login_address = RatelimitSettings(
  78. rc_login_config.get("address", {}),
  79. defaults={"per_second": 0.003, "burst_count": 5},
  80. )
  81. self.rc_login_account = RatelimitSettings(
  82. rc_login_config.get("account", {}),
  83. defaults={"per_second": 0.003, "burst_count": 5},
  84. )
  85. self.rc_login_failed_attempts = RatelimitSettings(
  86. rc_login_config.get("failed_attempts", {})
  87. )
  88. self.federation_rr_transactions_per_room_per_second = config.get(
  89. "federation_rr_transactions_per_room_per_second", 50
  90. )
  91. rc_admin_redaction = config.get("rc_admin_redaction")
  92. self.rc_admin_redaction = None
  93. if rc_admin_redaction:
  94. self.rc_admin_redaction = RatelimitSettings(rc_admin_redaction)
  95. self.rc_joins_local = RatelimitSettings(
  96. config.get("rc_joins", {}).get("local", {}),
  97. defaults={"per_second": 0.1, "burst_count": 10},
  98. )
  99. self.rc_joins_remote = RatelimitSettings(
  100. config.get("rc_joins", {}).get("remote", {}),
  101. defaults={"per_second": 0.01, "burst_count": 10},
  102. )
  103. # Track the rate of joins to a given room. If there are too many, temporarily
  104. # prevent local joins and remote joins via this server.
  105. self.rc_joins_per_room = RatelimitSettings(
  106. config.get("rc_joins_per_room", {}),
  107. defaults={"per_second": 1, "burst_count": 10},
  108. )
  109. # Ratelimit cross-user key requests:
  110. # * For local requests this is keyed by the sending device.
  111. # * For requests received over federation this is keyed by the origin.
  112. #
  113. # Note that this isn't exposed in the configuration as it is obscure.
  114. self.rc_key_requests = RatelimitSettings(
  115. config.get("rc_key_requests", {}),
  116. defaults={"per_second": 20, "burst_count": 100},
  117. )
  118. self.rc_3pid_validation = RatelimitSettings(
  119. config.get("rc_3pid_validation") or {},
  120. defaults={"per_second": 0.003, "burst_count": 5},
  121. )
  122. self.rc_invites_per_room = RatelimitSettings(
  123. config.get("rc_invites", {}).get("per_room", {}),
  124. defaults={"per_second": 0.3, "burst_count": 10},
  125. )
  126. self.rc_invites_per_user = RatelimitSettings(
  127. config.get("rc_invites", {}).get("per_user", {}),
  128. defaults={"per_second": 0.003, "burst_count": 5},
  129. )
  130. self.rc_invites_per_issuer = RatelimitSettings(
  131. config.get("rc_invites", {}).get("per_issuer", {}),
  132. defaults={"per_second": 0.3, "burst_count": 10},
  133. )
  134. self.rc_third_party_invite = RatelimitSettings(
  135. config.get("rc_third_party_invite", {}),
  136. defaults={"per_second": 0.0025, "burst_count": 5},
  137. )