mirror of
https://git.anonymousland.org/anonymousland/synapse.git
synced 2025-08-17 06:50:13 -04:00
Add rate-limiting on registration (#4735)
* Rate-limiting for registration * Add unit test for registration rate limiting * Add config parameters for rate limiting on auth endpoints * Doc * Fix doc of rate limiting function Co-Authored-By: babolivier <contact@brendanabolivier.com> * Incorporate review * Fix config parsing * Fix linting errors * Set default config for auth rate limiting * Fix tests * Add changelog * Advance reactor instead of mocked clock * Move parameters to registration specific config and give them more sensible default values * Remove unused config options * Don't mock the rate limiter un MAU tests * Rename _register_with_store into register_with_store * Make CI happy * Remove unused import * Update sample config * Fix ratelimiting test for py2 * Add non-guest test
This commit is contained in:
parent
3887e0cd80
commit
a4c3a361b7
17 changed files with 186 additions and 54 deletions
|
@ -25,7 +25,12 @@ from twisted.internet import defer
|
|||
import synapse
|
||||
import synapse.types
|
||||
from synapse.api.constants import LoginType
|
||||
from synapse.api.errors import Codes, SynapseError, UnrecognizedRequestError
|
||||
from synapse.api.errors import (
|
||||
Codes,
|
||||
LimitExceededError,
|
||||
SynapseError,
|
||||
UnrecognizedRequestError,
|
||||
)
|
||||
from synapse.config.server import is_threepid_reserved
|
||||
from synapse.http.servlet import (
|
||||
RestServlet,
|
||||
|
@ -191,18 +196,36 @@ class RegisterRestServlet(RestServlet):
|
|||
self.identity_handler = hs.get_handlers().identity_handler
|
||||
self.room_member_handler = hs.get_room_member_handler()
|
||||
self.macaroon_gen = hs.get_macaroon_generator()
|
||||
self.ratelimiter = hs.get_ratelimiter()
|
||||
self.clock = hs.get_clock()
|
||||
|
||||
@interactive_auth_handler
|
||||
@defer.inlineCallbacks
|
||||
def on_POST(self, request):
|
||||
body = parse_json_object_from_request(request)
|
||||
|
||||
client_addr = request.getClientIP()
|
||||
|
||||
time_now = self.clock.time()
|
||||
|
||||
allowed, time_allowed = self.ratelimiter.can_do_action(
|
||||
client_addr, time_now_s=time_now,
|
||||
rate_hz=self.hs.config.rc_registration_requests_per_second,
|
||||
burst_count=self.hs.config.rc_registration_request_burst_count,
|
||||
update=False,
|
||||
)
|
||||
|
||||
if not allowed:
|
||||
raise LimitExceededError(
|
||||
retry_after_ms=int(1000 * (time_allowed - time_now)),
|
||||
)
|
||||
|
||||
kind = b"user"
|
||||
if b"kind" in request.args:
|
||||
kind = request.args[b"kind"][0]
|
||||
|
||||
if kind == b"guest":
|
||||
ret = yield self._do_guest_registration(body)
|
||||
ret = yield self._do_guest_registration(body, address=client_addr)
|
||||
defer.returnValue(ret)
|
||||
return
|
||||
elif kind != b"user":
|
||||
|
@ -411,6 +434,7 @@ class RegisterRestServlet(RestServlet):
|
|||
guest_access_token=guest_access_token,
|
||||
generate_token=False,
|
||||
threepid=threepid,
|
||||
address=client_addr,
|
||||
)
|
||||
# Necessary due to auth checks prior to the threepid being
|
||||
# written to the db
|
||||
|
@ -522,12 +546,13 @@ class RegisterRestServlet(RestServlet):
|
|||
defer.returnValue(result)
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def _do_guest_registration(self, params):
|
||||
def _do_guest_registration(self, params, address=None):
|
||||
if not self.hs.config.allow_guest_access:
|
||||
raise SynapseError(403, "Guest access is disabled")
|
||||
user_id, _ = yield self.registration_handler.register(
|
||||
generate_token=False,
|
||||
make_guest=True
|
||||
make_guest=True,
|
||||
address=address,
|
||||
)
|
||||
|
||||
# we don't allow guests to specify their own device_id, because
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue