def ratelimit(self, key, time_now_s, rate_hz, burst_count, update=True): allowed, time_allowed = self.can_do_action(key, time_now_s, rate_hz, burst_count, update) if not allowed: raise LimitExceededError(retry_after_ms=int( 1000 * (time_allowed - time_now_s)), )
def ratelimit(self, user_id): time_now = self.clock.time() allowed, time_allowed = self.ratelimiter.send_message( user_id, time_now, msg_rate_hz=self.hs.config.rc_messages_per_second, burst_count=self.hs.config.rc_message_burst_count, ) if not allowed: raise LimitExceededError(retry_after_ms=int( 1000 * (time_allowed - time_now)), )
async def ratelimit( self, requester: Optional[Requester], key: Optional[Hashable] = None, rate_hz: Optional[float] = None, burst_count: Optional[int] = None, update: bool = True, n_actions: int = 1, _time_now_s: Optional[int] = None, ): """Checks if an action can be performed. If not, raises a LimitExceededError Checks if the user has ratelimiting disabled in the database by looking for null/zero values in the `ratelimit_override` table. (Non-zero values aren't honoured, as they're specific to the event sending ratelimiter, rather than all ratelimiters) Args: requester: The requester that is doing the action, if any. Used to check for if the user has ratelimits disabled. key: An arbitrary key used to classify an action. Defaults to the requester's user ID. rate_hz: The long term number of actions that can be performed in a second. Overrides the value set during instantiation if set. burst_count: How many actions that can be performed before being limited. Overrides the value set during instantiation if set. update: Whether to count this check as performing the action n_actions: The number of times the user wants to do this action. If the user cannot do all of the actions, the user's action count is not incremented at all. _time_now_s: The current time. Optional, defaults to the current time according to self.clock. Only used by tests. Raises: LimitExceededError: If an action could not be performed, along with the time in milliseconds until the action can be performed again """ time_now_s = _time_now_s if _time_now_s is not None else self.clock.time( ) allowed, time_allowed = await self.can_do_action( requester, key, rate_hz=rate_hz, burst_count=burst_count, update=update, n_actions=n_actions, _time_now_s=time_now_s, ) if not allowed: raise LimitExceededError( retry_after_ms=int(1000 * (time_allowed - time_now_s)))
def ratelimit(self, requester, update=True): """Ratelimits requests. Args: requester (Requester) update (bool): Whether to record that a request is being processed. Set to False when doing multiple checks for one request (e.g. to check up front if we would reject the request), and set to True for the last call for a given request. Raises: LimitExceededError if the request should be ratelimited """ time_now = self.clock.time() user_id = requester.user.to_string() # The AS user itself is never rate limited. app_service = self.store.get_app_service_by_user_id(user_id) if app_service is not None: return # do not ratelimit app service senders # Disable rate limiting of users belonging to any AS that is configured # not to be rate limited in its registration file (rate_limited: true|false). if requester.app_service and not requester.app_service.is_rate_limited( ): return # Check if there is a per user override in the DB. override = yield self.store.get_ratelimit_for_user(user_id) if override: # If overriden with a null Hz then ratelimiting has been entirely # disabled for the user if not override.messages_per_second: return messages_per_second = override.messages_per_second burst_count = override.burst_count else: messages_per_second = self.hs.config.rc_messages_per_second burst_count = self.hs.config.rc_message_burst_count allowed, time_allowed = self.ratelimiter.send_message( user_id, time_now, msg_rate_hz=messages_per_second, burst_count=burst_count, update=update, ) if not allowed: raise LimitExceededError(retry_after_ms=int( 1000 * (time_allowed - time_now)), )
def send_nonmember_event(self, requester, event, context, ratelimit=True): """ Persists and notifies local clients and federation of an event. Args: event (FrozenEvent) the event to send. context (Context) the context of the event. ratelimit (bool): Whether to rate limit this send. is_guest (bool): Whether the sender is a guest. """ if event.type == EventTypes.Member: raise SynapseError( 500, "Tried to send member event through non-member codepath") # We check here if we are currently being rate limited, so that we # don't do unnecessary work. We check again just before we actually # send the event. time_now = self.clock.time() allowed, time_allowed = self.ratelimiter.send_message( event.sender, time_now, msg_rate_hz=self.hs.config.rc_messages_per_second, burst_count=self.hs.config.rc_message_burst_count, update=False, ) if not allowed: raise LimitExceededError(retry_after_ms=int( 1000 * (time_allowed - time_now)), ) user = UserID.from_string(event.sender) assert self.hs.is_mine(user), "User must be our own: %s" % (user, ) if event.is_state(): prev_state = yield self.deduplicate_state_event(event, context) if prev_state is not None: defer.returnValue(prev_state) yield self.handle_new_client_event( requester=requester, event=event, context=context, ratelimit=ratelimit, ) if event.type == EventTypes.Message: presence = self.hs.get_presence_handler() # We don't want to block sending messages on any presence code. This # matters as sometimes presence code can take a while. preserve_fn(presence.bump_presence_active_time)(user)
def ratelimit( self, key: Any, rate_hz: Optional[float] = None, burst_count: Optional[int] = None, update: bool = True, _time_now_s: Optional[int] = None, ): """Checks if an action can be performed. If not, raises a LimitExceededError Args: key: An arbitrary key used to classify an action rate_hz: The long term number of actions that can be performed in a second. Overrides the value set during instantiation if set. burst_count: How many actions that can be performed before being limited. Overrides the value set during instantiation if set. update: Whether to count this check as performing the action _time_now_s: The current time. Optional, defaults to the current time according to self.clock. Only used by tests. Raises: LimitExceededError: If an action could not be performed, along with the time in milliseconds until the action can be performed again """ time_now_s = _time_now_s if _time_now_s is not None else self.clock.time( ) allowed, time_allowed = self.can_do_action( key, rate_hz=rate_hz, burst_count=burst_count, update=update, _time_now_s=time_now_s, ) if not allowed: raise LimitExceededError( retry_after_ms=int(1000 * (time_allowed - time_now_s)))
def ratelimit(self, requester): time_now = self.clock.time() user_id = requester.user.to_string() # The AS user itself is never rate limited. app_service = self.store.get_app_service_by_user_id(user_id) if app_service is not None: return # do not ratelimit app service senders # Disable rate limiting of users belonging to any AS that is configured # not to be rate limited in its registration file (rate_limited: true|false). if requester.app_service and not requester.app_service.is_rate_limited(): return allowed, time_allowed = self.ratelimiter.send_message( user_id, time_now, msg_rate_hz=self.hs.config.rc_messages_per_second, burst_count=self.hs.config.rc_message_burst_count, ) if not allowed: raise LimitExceededError( retry_after_ms=int(1000 * (time_allowed - time_now)), )
async 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.per_second, burst_count=self.hs.config.rc_registration.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 = await self._do_guest_registration(body, address=client_addr) return ret elif kind != b"user": raise UnrecognizedRequestError( "Do not understand membership kind: %s" % (kind.decode("utf8"), )) # we do basic sanity checks here because the auth layer will store these # in sessions. Pull out the username/password provided to us. if "password" in body: if (not isinstance(body["password"], string_types) or len(body["password"]) > 512): raise SynapseError(400, "Invalid password") self.password_policy_handler.validate_password(body["password"]) desired_username = None if "username" in body: if (not isinstance(body["username"], string_types) or len(body["username"]) > 512): raise SynapseError(400, "Invalid username") desired_username = body["username"] appservice = None if self.auth.has_access_token(request): appservice = await self.auth.get_appservice_by_req(request) # fork off as soon as possible for ASes which have completely # different registration flows to normal users # == Application Service Registration == if appservice: # Set the desired user according to the AS API (which uses the # 'user' key not 'username'). Since this is a new addition, we'll # fallback to 'username' if they gave one. desired_username = body.get("user", desired_username) # XXX we should check that desired_username is valid. Currently # we give appservices carte blanche for any insanity in mxids, # because the IRC bridges rely on being able to register stupid # IDs. access_token = self.auth.get_access_token_from_request(request) if isinstance(desired_username, string_types): result = await self._do_appservice_registration( desired_username, access_token, body) return 200, result # we throw for non 200 responses # for regular registration, downcase the provided username before # attempting to register it. This should mean # that people who try to register with upper-case in their usernames # don't get a nasty surprise. (Note that we treat username # case-insenstively in login, so they are free to carry on imagining # that their username is CrAzYh4cKeR if that keeps them happy) if desired_username is not None: desired_username = desired_username.lower() # == Normal User Registration == (everyone else) if not self.hs.config.enable_registration: raise SynapseError(403, "Registration has been disabled") guest_access_token = body.get("guest_access_token", None) if "initial_device_display_name" in body and "password" not in body: # ignore 'initial_device_display_name' if sent without # a password to work around a client bug where it sent # the 'initial_device_display_name' param alone, wiping out # the original registration params logger.warning( "Ignoring initial_device_display_name without password") del body["initial_device_display_name"] session_id = self.auth_handler.get_session_id(body) registered_user_id = None if session_id: # if we get a registered user id out of here, it means we previously # registered a user for this session, so we could just return the # user here. We carry on and go through the auth checks though, # for paranoia. registered_user_id = await self.auth_handler.get_session_data( session_id, "registered_user_id", None) if desired_username is not None: await self.registration_handler.check_username( desired_username, guest_access_token=guest_access_token, assigned_user_id=registered_user_id, ) auth_result, params, session_id = await self.auth_handler.check_auth( self._registration_flows, request, body, self.hs.get_ip_from_request(request), "register a new account", validate_clientdict=False, ) # Check that we're not trying to register a denied 3pid. # # the user-facing checks will probably already have happened in # /register/email/requestToken when we requested a 3pid, but that's not # guaranteed. if auth_result: for login_type in [LoginType.EMAIL_IDENTITY, LoginType.MSISDN]: if login_type in auth_result: medium = auth_result[login_type]["medium"] address = auth_result[login_type]["address"] if not check_3pid_allowed(self.hs, medium, address): raise SynapseError( 403, "Third party identifiers (email/phone numbers)" + " are not authorized on this server", Codes.THREEPID_DENIED, ) if registered_user_id is not None: logger.info("Already registered user ID %r for this session", registered_user_id) # don't re-register the threepids registered = False else: # NB: This may be from the auth handler and NOT from the POST assert_params_in_dict(params, ["password"]) desired_username = params.get("username", None) guest_access_token = params.get("guest_access_token", None) new_password = params.get("password", None) if desired_username is not None: desired_username = desired_username.lower() threepid = None if auth_result: threepid = auth_result.get(LoginType.EMAIL_IDENTITY) # Also check that we're not trying to register a 3pid that's already # been registered. # # This has probably happened in /register/email/requestToken as well, # but if a user hits this endpoint twice then clicks on each link from # the two activation emails, they would register the same 3pid twice. for login_type in [LoginType.EMAIL_IDENTITY, LoginType.MSISDN]: if login_type in auth_result: medium = auth_result[login_type]["medium"] address = auth_result[login_type]["address"] existing_user_id = await self.store.get_user_id_by_threepid( medium, address) if existing_user_id is not None: raise SynapseError( 400, "%s is already in use" % medium, Codes.THREEPID_IN_USE, ) registered_user_id = await self.registration_handler.register_user( localpart=desired_username, password=new_password, guest_access_token=guest_access_token, threepid=threepid, address=client_addr, ) # Necessary due to auth checks prior to the threepid being # written to the db if threepid: if is_threepid_reserved( self.hs.config.mau_limits_reserved_threepids, threepid): await self.store.upsert_monthly_active_user( registered_user_id) # remember that we've now registered that user account, and with # what user ID (since the user may not have specified) await self.auth_handler.set_session_data(session_id, "registered_user_id", registered_user_id) registered = True return_dict = await self._create_registration_details( registered_user_id, params) if registered: await self.registration_handler.post_registration_actions( user_id=registered_user_id, auth_result=auth_result, access_token=return_dict.get("access_token"), ) return 200, return_dict
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.per_second, burst_count=self.hs.config.rc_registration.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, address=client_addr) defer.returnValue(ret) return elif kind != b"user": raise UnrecognizedRequestError( "Do not understand membership kind: %s" % (kind, )) # we do basic sanity checks here because the auth layer will store these # in sessions. Pull out the username/password provided to us. desired_password = None if 'password' in body: if (not isinstance(body['password'], string_types) or len(body['password']) > 512): raise SynapseError(400, "Invalid password") desired_password = body["password"] desired_username = None if 'username' in body: if (not isinstance(body['username'], string_types) or len(body['username']) > 512): raise SynapseError(400, "Invalid username") desired_username = body['username'] appservice = None if self.auth.has_access_token(request): appservice = yield self.auth.get_appservice_by_req(request) # fork off as soon as possible for ASes and shared secret auth which # have completely different registration flows to normal users # == Application Service Registration == if appservice: # Set the desired user according to the AS API (which uses the # 'user' key not 'username'). Since this is a new addition, we'll # fallback to 'username' if they gave one. desired_username = body.get("user", desired_username) # XXX we should check that desired_username is valid. Currently # we give appservices carte blanche for any insanity in mxids, # because the IRC bridges rely on being able to register stupid # IDs. access_token = self.auth.get_access_token_from_request(request) if isinstance(desired_username, string_types): result = yield self._do_appservice_registration( desired_username, access_token, body) defer.returnValue((200, result)) # we throw for non 200 responses return # for either shared secret or regular registration, downcase the # provided username before attempting to register it. This should mean # that people who try to register with upper-case in their usernames # don't get a nasty surprise. (Note that we treat username # case-insenstively in login, so they are free to carry on imagining # that their username is CrAzYh4cKeR if that keeps them happy) if desired_username is not None: desired_username = desired_username.lower() # == Shared Secret Registration == (e.g. create new user scripts) if 'mac' in body: # FIXME: Should we really be determining if this is shared secret # auth based purely on the 'mac' key? result = yield self._do_shared_secret_registration( desired_username, desired_password, body) defer.returnValue((200, result)) # we throw for non 200 responses return # == Normal User Registration == (everyone else) if not self.hs.config.enable_registration: raise SynapseError(403, "Registration has been disabled") guest_access_token = body.get("guest_access_token", None) if ('initial_device_display_name' in body and 'password' not in body): # ignore 'initial_device_display_name' if sent without # a password to work around a client bug where it sent # the 'initial_device_display_name' param alone, wiping out # the original registration params logger.warn( "Ignoring initial_device_display_name without password") del body['initial_device_display_name'] session_id = self.auth_handler.get_session_id(body) registered_user_id = None if session_id: # if we get a registered user id out of here, it means we previously # registered a user for this session, so we could just return the # user here. We carry on and go through the auth checks though, # for paranoia. registered_user_id = self.auth_handler.get_session_data( session_id, "registered_user_id", None) if desired_username is not None: yield self.registration_handler.check_username( desired_username, guest_access_token=guest_access_token, assigned_user_id=registered_user_id, ) # FIXME: need a better error than "no auth flow found" for scenarios # where we required 3PID for registration but the user didn't give one require_email = 'email' in self.hs.config.registrations_require_3pid require_msisdn = 'msisdn' in self.hs.config.registrations_require_3pid show_msisdn = True if self.hs.config.disable_msisdn_registration: show_msisdn = False require_msisdn = False flows = [] if self.hs.config.enable_registration_captcha: # only support 3PIDless registration if no 3PIDs are required if not require_email and not require_msisdn: # Also add a dummy flow here, otherwise if a client completes # recaptcha first we'll assume they were going for this flow # and complete the request, when they could have been trying to # complete one of the flows with email/msisdn auth. flows.extend([[LoginType.RECAPTCHA, LoginType.DUMMY]]) # only support the email-only flow if we don't require MSISDN 3PIDs if not require_msisdn: flows.extend([[LoginType.RECAPTCHA, LoginType.EMAIL_IDENTITY]]) if show_msisdn: # only support the MSISDN-only flow if we don't require email 3PIDs if not require_email: flows.extend([[LoginType.RECAPTCHA, LoginType.MSISDN]]) # always let users provide both MSISDN & email flows.extend([ [ LoginType.RECAPTCHA, LoginType.MSISDN, LoginType.EMAIL_IDENTITY ], ]) else: # only support 3PIDless registration if no 3PIDs are required if not require_email and not require_msisdn: flows.extend([[LoginType.DUMMY]]) # only support the email-only flow if we don't require MSISDN 3PIDs if not require_msisdn: flows.extend([[LoginType.EMAIL_IDENTITY]]) if show_msisdn: # only support the MSISDN-only flow if we don't require email 3PIDs if not require_email or require_msisdn: flows.extend([[LoginType.MSISDN]]) # always let users provide both MSISDN & email flows.extend([[LoginType.MSISDN, LoginType.EMAIL_IDENTITY]]) # Append m.login.terms to all flows if we're requiring consent if self.hs.config.user_consent_at_registration: new_flows = [] for flow in flows: inserted = False # m.login.terms should go near the end but before msisdn or email auth for i, stage in enumerate(flow): if stage == LoginType.EMAIL_IDENTITY or stage == LoginType.MSISDN: flow.insert(i, LoginType.TERMS) inserted = True break if not inserted: flow.append(LoginType.TERMS) flows.extend(new_flows) auth_result, params, session_id = yield self.auth_handler.check_auth( flows, body, self.hs.get_ip_from_request(request)) # Check that we're not trying to register a denied 3pid. # # the user-facing checks will probably already have happened in # /register/email/requestToken when we requested a 3pid, but that's not # guaranteed. if auth_result: for login_type in [LoginType.EMAIL_IDENTITY, LoginType.MSISDN]: if login_type in auth_result: medium = auth_result[login_type]['medium'] address = auth_result[login_type]['address'] if not check_3pid_allowed(self.hs, medium, address): raise SynapseError( 403, "Third party identifiers (email/phone numbers)" + " are not authorized on this server", Codes.THREEPID_DENIED, ) if registered_user_id is not None: logger.info("Already registered user ID %r for this session", registered_user_id) # don't re-register the threepids registered = False else: # NB: This may be from the auth handler and NOT from the POST assert_params_in_dict(params, ["password"]) desired_username = params.get("username", None) guest_access_token = params.get("guest_access_token", None) new_password = params.get("password", None) if desired_username is not None: desired_username = desired_username.lower() threepid = None if auth_result: threepid = auth_result.get(LoginType.EMAIL_IDENTITY) # Also check that we're not trying to register a 3pid that's already # been registered. # # This has probably happened in /register/email/requestToken as well, # but if a user hits this endpoint twice then clicks on each link from # the two activation emails, they would register the same 3pid twice. for login_type in [LoginType.EMAIL_IDENTITY, LoginType.MSISDN]: if login_type in auth_result: medium = auth_result[login_type]['medium'] address = auth_result[login_type]['address'] existingUid = yield self.store.get_user_id_by_threepid( medium, address, ) if existingUid is not None: raise SynapseError( 400, "%s is already in use" % medium, Codes.THREEPID_IN_USE, ) (registered_user_id, _) = yield self.registration_handler.register( localpart=desired_username, password=new_password, 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 if threepid: if is_threepid_reserved( self.hs.config.mau_limits_reserved_threepids, threepid): yield self.store.upsert_monthly_active_user( registered_user_id) # remember that we've now registered that user account, and with # what user ID (since the user may not have specified) self.auth_handler.set_session_data(session_id, "registered_user_id", registered_user_id) registered = True return_dict = yield self._create_registration_details( registered_user_id, params) if registered: yield self.registration_handler.post_registration_actions( user_id=registered_user_id, auth_result=auth_result, access_token=return_dict.get("access_token"), bind_email=params.get("bind_email"), bind_msisdn=params.get("bind_msisdn"), ) defer.returnValue((200, return_dict))
async def update_membership_locked( self, requester: Requester, target: UserID, room_id: str, action: str, txn_id: Optional[str] = None, remote_room_hosts: Optional[List[str]] = None, third_party_signed: Optional[dict] = None, ratelimit: bool = True, content: Optional[dict] = None, require_consent: bool = True, ) -> Tuple[str, int]: """Helper for update_membership. Assumes that the membership linearizer is already held for the room. """ content_specified = bool(content) if content is None: content = {} else: # We do a copy here as we potentially change some keys # later on. content = dict(content) # allow the server notices mxid to set room-level profile is_requester_server_notices_user = ( self._server_notices_mxid is not None and requester.user.to_string() == self._server_notices_mxid) if (not self.allow_per_room_profiles and not is_requester_server_notices_user ) or requester.shadow_banned: # Strip profile data, knowing that new profile data will be added to the # event's content in event_creation_handler.create_event() using the target's # global profile. content.pop("displayname", None) content.pop("avatar_url", None) effective_membership_state = action if action in ["kick", "unban"]: effective_membership_state = "leave" # if this is a join with a 3pid signature, we may need to turn a 3pid # invite into a normal invite before we can handle the join. if third_party_signed is not None: await self.federation_handler.exchange_third_party_invite( third_party_signed["sender"], target.to_string(), room_id, third_party_signed, ) if not remote_room_hosts: remote_room_hosts = [] if effective_membership_state not in ("leave", "ban"): is_blocked = await self.store.is_room_blocked(room_id) if is_blocked: raise SynapseError( 403, "This room has been blocked on this server") if effective_membership_state == Membership.INVITE: # block any attempts to invite the server notices mxid if target.to_string() == self._server_notices_mxid: raise SynapseError(HTTPStatus.FORBIDDEN, "Cannot invite this user") block_invite = False if (self._server_notices_mxid is not None and requester.user.to_string() == self._server_notices_mxid): # allow the server notices mxid to send invites is_requester_admin = True else: is_requester_admin = await self.auth.is_server_admin( requester.user) if not is_requester_admin: if self.config.block_non_admin_invites: logger.info( "Blocking invite: user is not admin and non-admin " "invites disabled") block_invite = True if not await self.spam_checker.user_may_invite( requester.user.to_string(), target.to_string(), room_id): logger.info("Blocking invite due to spam checker") block_invite = True if block_invite: raise SynapseError( 403, "Invites have been disabled on this server") latest_event_ids = await self.store.get_prev_events_for_room(room_id) current_state_ids = await self.state_handler.get_current_state_ids( room_id, latest_event_ids=latest_event_ids) # TODO: Refactor into dictionary of explicitly allowed transitions # between old and new state, with specific error messages for some # transitions and generic otherwise old_state_id = current_state_ids.get( (EventTypes.Member, target.to_string())) if old_state_id: old_state = await self.store.get_event(old_state_id, allow_none=True) old_membership = old_state.content.get( "membership") if old_state else None if action == "unban" and old_membership != "ban": raise SynapseError( 403, "Cannot unban user who was not banned" " (membership=%s)" % old_membership, errcode=Codes.BAD_STATE, ) if old_membership == "ban" and action != "unban": raise SynapseError( 403, "Cannot %s user who was banned" % (action, ), errcode=Codes.BAD_STATE, ) if old_state: same_content = content == old_state.content same_membership = old_membership == effective_membership_state same_sender = requester.user.to_string() == old_state.sender if same_sender and same_membership and same_content: # duplicate event. # we know it was persisted, so must have a stream ordering. assert old_state.internal_metadata.stream_ordering return ( old_state.event_id, old_state.internal_metadata.stream_ordering, ) if old_membership in ["ban", "leave"] and action == "kick": raise AuthError(403, "The target user is not in the room") # we don't allow people to reject invites to the server notice # room, but they can leave it once they are joined. if (old_membership == Membership.INVITE and effective_membership_state == Membership.LEAVE): is_blocked = await self._is_server_notice_room(room_id) if is_blocked: raise SynapseError( HTTPStatus.FORBIDDEN, "You cannot reject this invite", errcode=Codes.CANNOT_LEAVE_SERVER_NOTICE_ROOM, ) else: if action == "kick": raise AuthError(403, "The target user is not in the room") is_host_in_room = await self._is_host_in_room(current_state_ids) if effective_membership_state == Membership.JOIN: if requester.is_guest: guest_can_join = await self._can_guest_join(current_state_ids) if not guest_can_join: # This should be an auth check, but guests are a local concept, # so don't really fit into the general auth process. raise AuthError(403, "Guest access not allowed") if not is_host_in_room: if ratelimit: time_now_s = self.clock.time() ( allowed, time_allowed, ) = self._join_rate_limiter_remote.can_requester_do_action( requester, ) if not allowed: raise LimitExceededError( retry_after_ms=int(1000 * (time_allowed - time_now_s))) inviter = await self._get_inviter(target.to_string(), room_id) if inviter and not self.hs.is_mine(inviter): remote_room_hosts.append(inviter.domain) content["membership"] = Membership.JOIN profile = self.profile_handler if not content_specified: content["displayname"] = await profile.get_displayname( target) content["avatar_url"] = await profile.get_avatar_url(target ) if requester.is_guest: content["kind"] = "guest" remote_join_response = await self._remote_join( requester, remote_room_hosts, room_id, target, content) return remote_join_response elif effective_membership_state == Membership.LEAVE: if not is_host_in_room: # perhaps we've been invited ( current_membership_type, current_membership_event_id, ) = await self.store.get_local_current_membership_for_user_in_room( target.to_string(), room_id) if (current_membership_type != Membership.INVITE or not current_membership_event_id): logger.info( "%s sent a leave request to %s, but that is not an active room " "on this server, and there is no pending invite", target, room_id, ) raise SynapseError(404, "Not a known room") invite = await self.store.get_event(current_membership_event_id ) logger.info("%s rejects invite to %s from %s", target, room_id, invite.sender) if not self.hs.is_mine_id(invite.sender): # send the rejection to the inviter's HS (with fallback to # local event) return await self.remote_reject_invite( invite.event_id, txn_id, requester, content, ) # the inviter was on our server, but has now left. Carry on # with the normal rejection codepath, which will also send the # rejection out to any other servers we believe are still in the room. # thanks to overzealous cleaning up of event_forward_extremities in # `delete_old_current_state_events`, it's possible to end up with no # forward extremities here. If that happens, let's just hang the # rejection off the invite event. # # see: https://github.com/matrix-org/synapse/issues/7139 if len(latest_event_ids) == 0: latest_event_ids = [invite.event_id] return await self._local_membership_update( requester=requester, target=target, room_id=room_id, membership=effective_membership_state, txn_id=txn_id, ratelimit=ratelimit, prev_event_ids=latest_event_ids, content=content, require_consent=require_consent, )
async def _local_membership_update( self, requester: Requester, target: UserID, room_id: str, membership: str, prev_event_ids: List[str], txn_id: Optional[str] = None, ratelimit: bool = True, content: Optional[dict] = None, require_consent: bool = True, ) -> Tuple[str, int]: user_id = target.to_string() if content is None: content = {} content["membership"] = membership if requester.is_guest: content["kind"] = "guest" # Check if we already have an event with a matching transaction ID. (We # do this check just before we persist an event as well, but may as well # do it up front for efficiency.) if txn_id and requester.access_token_id: existing_event_id = await self.store.get_event_id_from_transaction_id( room_id, requester.user.to_string(), requester.access_token_id, txn_id, ) if existing_event_id: event_pos = await self.store.get_position_for_event( existing_event_id) return existing_event_id, event_pos.stream event, context = await self.event_creation_handler.create_event( requester, { "type": EventTypes.Member, "content": content, "room_id": room_id, "sender": requester.user.to_string(), "state_key": user_id, # For backwards compatibility: "membership": membership, }, txn_id=txn_id, prev_event_ids=prev_event_ids, require_consent=require_consent, ) prev_state_ids = await context.get_prev_state_ids() prev_member_event_id = prev_state_ids.get((EventTypes.Member, user_id), None) if event.membership == Membership.JOIN: newly_joined = True if prev_member_event_id: prev_member_event = await self.store.get_event( prev_member_event_id) newly_joined = prev_member_event.membership != Membership.JOIN # Only rate-limit if the user actually joined the room, otherwise we'll end # up blocking profile updates. if newly_joined and ratelimit: time_now_s = self.clock.time() ( allowed, time_allowed, ) = self._join_rate_limiter_local.can_requester_do_action( requester) if not allowed: raise LimitExceededError( retry_after_ms=int(1000 * (time_allowed - time_now_s))) result_event = await self.event_creation_handler.handle_new_client_event( requester, event, context, extra_users=[target], ratelimit=ratelimit, ) if event.membership == Membership.LEAVE: if prev_member_event_id: prev_member_event = await self.store.get_event( prev_member_event_id) if prev_member_event.membership == Membership.JOIN: await self._user_left_room(target, room_id) # we know it was persisted, so should have a stream ordering assert result_event.internal_metadata.stream_ordering return result_event.event_id, result_event.internal_metadata.stream_ordering
def _on_enter(self, request_id): time_now = self.clock.time_msec() self.request_times[:] = [ r for r in self.request_times if time_now - r < self.window_size ] queue_size = len(self.ready_request_queue) + len( self.sleeping_requests) if queue_size > self.reject_limit: raise LimitExceededError(retry_after_ms=int(self.window_size / self.sleep_limit), ) self.request_times.append(time_now) def queue_request(): if len(self.current_processing) > self.concurrent_requests: logger.debug("Ratelimit [%s]: Queue req", id(request_id)) queue_defer = defer.Deferred() self.ready_request_queue[request_id] = queue_defer return queue_defer else: return defer.succeed(None) logger.debug( "Ratelimit [%s]: len(self.request_times)=%d", id(request_id), len(self.request_times), ) if len(self.request_times) > self.sleep_limit: logger.debug( "Ratelimit [%s]: sleeping req", id(request_id), ) ret_defer = run_in_background(sleep, self.sleep_msec / 1000.0) self.sleeping_requests.add(request_id) def on_wait_finished(_): logger.debug( "Ratelimit [%s]: Finished sleeping", id(request_id), ) self.sleeping_requests.discard(request_id) queue_defer = queue_request() return queue_defer ret_defer.addBoth(on_wait_finished) else: ret_defer = queue_request() def on_start(r): logger.debug( "Ratelimit [%s]: Processing req", id(request_id), ) self.current_processing.add(request_id) return r def on_err(r): # XXX: why is this necessary? this is called before we start # processing the request so why would the request be in # current_processing? self.current_processing.discard(request_id) return r def on_both(r): # Ensure that we've properly cleaned up. self.sleeping_requests.discard(request_id) self.ready_request_queue.pop(request_id, None) return r ret_defer.addCallbacks(on_start, on_err) ret_defer.addBoth(on_both) return make_deferred_yieldable(ret_defer)
def register_with_store(self, user_id, token=None, password_hash=None, was_guest=False, make_guest=False, appservice_id=None, create_profile_with_displayname=None, admin=False, user_type=None, address=None): """Register user in the datastore. Args: user_id (str): The desired user ID to register. token (str): The desired access token to use for this user. If this is not None, the given access token is associated with the user id. password_hash (str|None): Optional. The password hash for this user. was_guest (bool): Optional. Whether this is a guest account being upgraded to a non-guest account. make_guest (boolean): True if the the new user should be guest, false to add a regular user account. appservice_id (str|None): The ID of the appservice registering the user. create_profile_with_displayname (unicode|None): Optionally create a profile for the user, setting their displayname to the given value admin (boolean): is an admin user? user_type (str|None): type of user. One of the values from api.constants.UserTypes, or None for a normal user. address (str|None): the IP address used to perform the registration. Returns: Deferred """ # Don't rate limit for app services if appservice_id is None and address is not None: time_now = self.clock.time() allowed, time_allowed = self.ratelimiter.can_do_action( address, time_now_s=time_now, rate_hz=self.hs.config.rc_registration.per_second, burst_count=self.hs.config.rc_registration.burst_count, ) if not allowed: raise LimitExceededError(retry_after_ms=int( 1000 * (time_allowed - time_now)), ) if self.hs.config.worker_app: return self._register_client( user_id=user_id, token=token, password_hash=password_hash, was_guest=was_guest, make_guest=make_guest, appservice_id=appservice_id, create_profile_with_displayname=create_profile_with_displayname, admin=admin, user_type=user_type, address=address, ) else: return self.store.register( user_id=user_id, token=token, password_hash=password_hash, was_guest=was_guest, make_guest=make_guest, appservice_id=appservice_id, create_profile_with_displayname=create_profile_with_displayname, admin=admin, user_type=user_type, )
def _on_enter(self, request_id): time_now = self.clock.time_msec() # remove any entries from request_times which aren't within the window self.request_times[:] = [ r for r in self.request_times if time_now - r < self.window_size ] # reject the request if we already have too many queued up (either # sleeping or in the ready queue). queue_size = len(self.ready_request_queue) + len( self.sleeping_requests) if queue_size > self.reject_limit: raise LimitExceededError(retry_after_ms=int(self.window_size / self.sleep_limit)) self.request_times.append(time_now) def queue_request(): if len(self.current_processing) >= self.concurrent_requests: queue_defer = defer.Deferred() self.ready_request_queue[request_id] = queue_defer logger.info( "Ratelimiter: queueing request (queue now %i items)", len(self.ready_request_queue), ) return queue_defer else: return defer.succeed(None) logger.debug( "Ratelimit [%s]: len(self.request_times)=%d", id(request_id), len(self.request_times), ) if len(self.request_times) > self.sleep_limit: logger.debug("Ratelimiter: sleeping request for %f sec", self.sleep_sec) ret_defer = run_in_background(self.clock.sleep, self.sleep_sec) self.sleeping_requests.add(request_id) def on_wait_finished(_): logger.debug("Ratelimit [%s]: Finished sleeping", id(request_id)) self.sleeping_requests.discard(request_id) queue_defer = queue_request() return queue_defer ret_defer.addBoth(on_wait_finished) else: ret_defer = queue_request() def on_start(r): logger.debug("Ratelimit [%s]: Processing req", id(request_id)) self.current_processing.add(request_id) return r def on_err(r): # XXX: why is this necessary? this is called before we start # processing the request so why would the request be in # current_processing? self.current_processing.discard(request_id) return r def on_both(r): # Ensure that we've properly cleaned up. self.sleeping_requests.discard(request_id) self.ready_request_queue.pop(request_id, None) return r ret_defer.addCallbacks(on_start, on_err) ret_defer.addBoth(on_both) return make_deferred_yieldable(ret_defer)
def ratelimit(self, requester, update=True, is_admin_redaction=False): """Ratelimits requests. Args: requester (Requester) update (bool): Whether to record that a request is being processed. Set to False when doing multiple checks for one request (e.g. to check up front if we would reject the request), and set to True for the last call for a given request. is_admin_redaction (bool): Whether this is a room admin/moderator redacting an event. If so then we may apply different ratelimits depending on config. Raises: LimitExceededError if the request should be ratelimited """ time_now = self.clock.time() user_id = requester.user.to_string() # The AS user itself is never rate limited. app_service = self.store.get_app_service_by_user_id(user_id) if app_service is not None: return # do not ratelimit app service senders # Disable rate limiting of users belonging to any AS that is configured # not to be rate limited in its registration file (rate_limited: true|false). if requester.app_service and not requester.app_service.is_rate_limited(): return # Check if there is a per user override in the DB. override = yield self.store.get_ratelimit_for_user(user_id) if override: # If overriden with a null Hz then ratelimiting has been entirely # disabled for the user if not override.messages_per_second: return messages_per_second = override.messages_per_second burst_count = override.burst_count else: # We default to different values if this is an admin redaction and # the config is set if is_admin_redaction and self.hs.config.rc_admin_redaction: messages_per_second = self.hs.config.rc_admin_redaction.per_second burst_count = self.hs.config.rc_admin_redaction.burst_count else: messages_per_second = self.hs.config.rc_message.per_second burst_count = self.hs.config.rc_message.burst_count if is_admin_redaction and self.hs.config.rc_admin_redaction: # If we have separate config for admin redactions we use a separate # ratelimiter allowed, time_allowed = self.admin_redaction_ratelimiter.can_do_action( user_id, time_now, rate_hz=messages_per_second, burst_count=burst_count, update=update, ) else: allowed, time_allowed = self.ratelimiter.can_do_action( user_id, time_now, rate_hz=messages_per_second, burst_count=burst_count, update=update, ) if not allowed: raise LimitExceededError( retry_after_ms=int(1000 * (time_allowed - time_now)) )
async def _local_membership_update( self, requester: Requester, target: UserID, room_id: str, membership: str, prev_event_ids: List[str], txn_id: Optional[str] = None, ratelimit: bool = True, content: Optional[dict] = None, require_consent: bool = True, ) -> Tuple[str, int]: user_id = target.to_string() if content is None: content = {} content["membership"] = membership if requester.is_guest: content["kind"] = "guest" event, context = await self.event_creation_handler.create_event( requester, { "type": EventTypes.Member, "content": content, "room_id": room_id, "sender": requester.user.to_string(), "state_key": user_id, # For backwards compatibility: "membership": membership, }, token_id=requester.access_token_id, txn_id=txn_id, prev_event_ids=prev_event_ids, require_consent=require_consent, ) # Check if this event matches the previous membership event for the user. duplicate = await self.event_creation_handler.deduplicate_state_event( event, context) if duplicate is not None: # Discard the new event since this membership change is a no-op. _, stream_id = await self.store.get_event_ordering( duplicate.event_id) return duplicate.event_id, stream_id prev_state_ids = await context.get_prev_state_ids() prev_member_event_id = prev_state_ids.get((EventTypes.Member, user_id), None) if event.membership == Membership.JOIN: newly_joined = True if prev_member_event_id: prev_member_event = await self.store.get_event( prev_member_event_id) newly_joined = prev_member_event.membership != Membership.JOIN # Only rate-limit if the user actually joined the room, otherwise we'll end # up blocking profile updates. if newly_joined: time_now_s = self.clock.time() ( allowed, time_allowed, ) = self._join_rate_limiter_local.can_requester_do_action( requester) if not allowed: raise LimitExceededError( retry_after_ms=int(1000 * (time_allowed - time_now_s))) stream_id = await self.event_creation_handler.handle_new_client_event( requester, event, context, extra_users=[target], ratelimit=ratelimit, ) if event.membership == Membership.LEAVE: if prev_member_event_id: prev_member_event = await self.store.get_event( prev_member_event_id) if prev_member_event.membership == Membership.JOIN: await self._user_left_room(target, room_id) return event.event_id, stream_id