Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Improve ratelimit on registration #4804

Merged
merged 6 commits into from
Mar 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/4804.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add configurable rate limiting to the /register endpoint.
24 changes: 13 additions & 11 deletions docs/sample_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,17 @@ federation_rc_reject_limit: 50
#
federation_rc_concurrent: 3

# Number of registration requests a client can send per second.
# Defaults to 1/minute (0.17).
#
#rc_registration_requests_per_second: 0.17

# Number of registration requests a client can send before being
# throttled.
# Defaults to 3.
#
#rc_registration_request_burst_count: 3.0



# Directory where uploaded images and attachments are stored.
Expand Down Expand Up @@ -580,6 +591,8 @@ turn_allow_guests: True


## Registration ##
# Registration can be rate-limited using the parameters in the "Ratelimiting"
# section of this file.

# Enable registration for new users.
enable_registration: False
Expand Down Expand Up @@ -657,17 +670,6 @@ trusted_third_party_id_servers:
#
autocreate_auto_join_rooms: true

# Number of registration requests a client can send per second.
# Defaults to 1/minute (0.17).
#
#rc_registration_requests_per_second: 0.17

# Number of registration requests a client can send before being
# throttled.
# Defaults to 3.
#
#rc_registration_request_burst_count: 3.0


## Metrics ###

Expand Down
18 changes: 18 additions & 0 deletions synapse/config/ratelimiting.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ def read_config(self, config):
self.federation_rc_reject_limit = config["federation_rc_reject_limit"]
self.federation_rc_concurrent = config["federation_rc_concurrent"]

self.rc_registration_requests_per_second = config.get(
"rc_registration_requests_per_second", 0.17,
)
self.rc_registration_request_burst_count = config.get(
"rc_registration_request_burst_count", 3,
)

def default_config(self, **kwargs):
return """\
## Ratelimiting ##
Expand Down Expand Up @@ -62,4 +69,15 @@ def default_config(self, **kwargs):
# single server
#
federation_rc_concurrent: 3

# Number of registration requests a client can send per second.
# Defaults to 1/minute (0.17).
#
#rc_registration_requests_per_second: 0.17

# Number of registration requests a client can send before being
# throttled.
# Defaults to 3.
#
#rc_registration_request_burst_count: 3.0
"""
20 changes: 2 additions & 18 deletions synapse/config/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,6 @@ def read_config(self, config):
config.get("disable_msisdn_registration", False)
)

self.rc_registration_requests_per_second = config.get(
"rc_registration_requests_per_second", 0.17,
)
self.rc_registration_request_burst_count = config.get(
"rc_registration_request_burst_count", 3,
)

def default_config(self, generate_secrets=False, **kwargs):
if generate_secrets:
registration_shared_secret = 'registration_shared_secret: "%s"' % (
Expand All @@ -71,6 +64,8 @@ def default_config(self, generate_secrets=False, **kwargs):

return """\
## Registration ##
# Registration can be rate-limited using the parameters in the "Ratelimiting"
# section of this file.

# Enable registration for new users.
enable_registration: False
Expand Down Expand Up @@ -147,17 +142,6 @@ def default_config(self, generate_secrets=False, **kwargs):
# users cannot be auto-joined since they do not exist.
#
autocreate_auto_join_rooms: true

# Number of registration requests a client can send per second.
# Defaults to 1/minute (0.17).
#
#rc_registration_requests_per_second: 0.17

# Number of registration requests a client can send before being
# throttled.
# Defaults to 3.
#
#rc_registration_request_burst_count: 3.0
""" % locals()

def add_arguments(self, parser):
Expand Down
2 changes: 1 addition & 1 deletion synapse/handlers/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def __init__(self, hs):
self.user_directory_handler = hs.get_user_directory_handler()
self.captcha_client = CaptchaServerHttpClient(hs)
self.identity_handler = self.hs.get_handlers().identity_handler
self.ratelimiter = hs.get_ratelimiter()
self.ratelimiter = hs.get_registration_ratelimiter()

self._next_generated_user_id = None

Expand Down
2 changes: 1 addition & 1 deletion synapse/rest/client/v2_alpha/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def __init__(self, hs):
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.ratelimiter = hs.get_registration_ratelimiter()
self.clock = hs.get_clock()

@interactive_auth_handler
Expand Down
4 changes: 4 additions & 0 deletions synapse/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ def __init__(self, hostname, reactor=None, **kwargs):
self.clock = Clock(reactor)
self.distributor = Distributor()
self.ratelimiter = Ratelimiter()
self.registration_ratelimiter = Ratelimiter()

self.datastore = None

Expand Down Expand Up @@ -251,6 +252,9 @@ def get_distributor(self):
def get_ratelimiter(self):
return self.ratelimiter

def get_registration_ratelimiter(self):
return self.registration_ratelimiter

def build_federation_client(self):
return FederationClient(self)

Expand Down