def __init__(self, hs): super().__init__(hs) # An HTTP client for contacting trusted URLs. self.http_client = SimpleHttpClient(hs) # An HTTP client for contacting identity servers specified by clients. self.blacklisting_http_client = SimpleHttpClient( hs, ip_blacklist=hs.config.federation_ip_range_blacklist) self.federation_http_client = hs.get_federation_http_client() self.hs = hs self._web_client_location = hs.config.invite_client_location # Ratelimiters for `/requestToken` endpoints. self._3pid_validation_ratelimiter_ip = Ratelimiter( store=self.store, clock=hs.get_clock(), rate_hz=hs.config.ratelimiting.rc_3pid_validation.per_second, burst_count=hs.config.ratelimiting.rc_3pid_validation.burst_count, ) self._3pid_validation_ratelimiter_address = Ratelimiter( store=self.store, clock=hs.get_clock(), rate_hz=hs.config.ratelimiting.rc_3pid_validation.per_second, burst_count=hs.config.ratelimiting.rc_3pid_validation.burst_count, )
def __init__(self, hs: "HomeServer"): self.store = hs.get_datastore() # An HTTP client for contacting trusted URLs. self.http_client = SimpleHttpClient(hs) # An HTTP client for contacting identity servers specified by clients. self.blacklisting_http_client = SimpleHttpClient( hs, ip_blacklist=hs.config.server.federation_ip_range_blacklist, ip_whitelist=hs.config.server.federation_ip_range_whitelist, ) self.federation_http_client = hs.get_federation_http_client() self.hs = hs self.rewrite_identity_server_urls = ( hs.config.registration.rewrite_identity_server_urls ) self._enable_lookup = hs.config.registration.enable_3pid_lookup self._web_client_location = hs.config.email.invite_client_location # Ratelimiters for `/requestToken` endpoints. self._3pid_validation_ratelimiter_ip = Ratelimiter( store=self.store, clock=hs.get_clock(), rate_hz=hs.config.ratelimiting.rc_3pid_validation.per_second, burst_count=hs.config.ratelimiting.rc_3pid_validation.burst_count, ) self._3pid_validation_ratelimiter_address = Ratelimiter( store=self.store, clock=hs.get_clock(), rate_hz=hs.config.ratelimiting.rc_3pid_validation.per_second, burst_count=hs.config.ratelimiting.rc_3pid_validation.burst_count, )
def __init__(self, hs): super(IdentityHandler, self).__init__(hs) self.http_client = SimpleHttpClient(hs) # We create a blacklisting instance of SimpleHttpClient for contacting identity # servers specified by clients self.blacklisting_http_client = SimpleHttpClient( hs, ip_blacklist=hs.config.federation_ip_range_blacklist) self.federation_http_client = hs.get_http_client() self.hs = hs
def __init__(self, hs): super().__init__(hs) # An HTTP client for contacting trusted URLs. self.http_client = SimpleHttpClient(hs) # An HTTP client for contacting identity servers specified by clients. self.blacklisting_http_client = SimpleHttpClient( hs, ip_blacklist=hs.config.federation_ip_range_blacklist) self.federation_http_client = hs.get_federation_http_client() self.hs = hs self._web_client_location = hs.config.invite_client_location
def __init__(self, hs): super().__init__() self.hs = hs # self.auth = hs.get_auth() self.store = hs.get_datastore() self.http_client = SimpleHttpClient(hs)
def bind_threepid(self, creds, mxid): yield run_on_reactor() logger.debug("binding threepid %r to %s", creds, mxid) http_client = SimpleHttpClient(self.hs) data = None if 'id_server' in creds: id_server = creds['id_server'] elif 'idServer' in creds: id_server = creds['idServer'] else: raise SynapseError(400, "No id_server in creds") if 'client_secret' in creds: client_secret = creds['client_secret'] elif 'clientSecret' in creds: client_secret = creds['clientSecret'] else: raise SynapseError(400, "No client_secret in creds") try: data = yield http_client.post_urlencoded_get_json( "https://%s%s" % (id_server, "/_matrix/identity/api/v1/3pid/bind"), { 'sid': creds['sid'], 'client_secret': client_secret, 'mxid': mxid, }) logger.debug("bound threepid %r to %s", creds, mxid) except CodeMessageException as e: data = json.loads(e.msg) defer.returnValue(data)
def _check_recaptcha(self, authdict, clientip): try: user_response = authdict["response"] except KeyError: # Client tried to provide captcha but didn't give the parameter: # bad request. raise LoginError( 400, "Captcha response is required", errcode=Codes.CAPTCHA_NEEDED ) logger.info( "Submitting recaptcha response %s with remoteip %s", user_response, clientip ) # TODO: get this from the homeserver rather than creating a new one for # each request try: client = SimpleHttpClient(self.hs) data = yield client.post_urlencoded_get_json( "https://www.google.com/recaptcha/api/siteverify", args={ 'secret': self.hs.config.recaptcha_private_key, 'response': user_response, 'remoteip': clientip, } ) except PartialDownloadError as pde: # Twisted is silly data = pde.response resp_body = simplejson.loads(data) if 'success' in resp_body and resp_body['success']: defer.returnValue(True) raise LoginError(401, "", errcode=Codes.UNAUTHORIZED)
def __init__(self, hs, media_repo, media_storage): super().__init__() self.auth = hs.get_auth() self.clock = hs.get_clock() self.filepaths = media_repo.filepaths self.max_spider_size = hs.config.max_spider_size self.server_name = hs.hostname self.store = hs.get_datastore() self.client = SimpleHttpClient( hs, treq_args={"browser_like_redirects": True}, ip_whitelist=hs.config.url_preview_ip_range_whitelist, ip_blacklist=hs.config.url_preview_ip_range_blacklist, ) self.media_repo = media_repo self.primary_base_path = media_repo.primary_base_path self.media_storage = media_storage self.url_preview_url_blacklist = hs.config.url_preview_url_blacklist # memory cache mapping urls to an ObservableDeferred returning # JSON-encoded OG metadata self._cache = ExpiringCache( cache_name="url_previews", clock=self.clock, # don't spider URLs more often than once an hour expiry_ms=60 * 60 * 1000, ) self._cleaner_loop = self.clock.looping_call( self._start_expire_url_cache_data, 10 * 1000)
def get_proxied_http_client(self) -> SimpleHttpClient: """ An HTTP client that uses configured HTTP(S) proxies. """ return SimpleHttpClient( self, http_proxy=os.getenvb(b"http_proxy"), https_proxy=os.getenvb(b"HTTPS_PROXY"), )
def test_client_ip_range_blacklist(self): """Ensure that Synapse does not try to connect to blacklisted IPs""" # Add some DNS entries we'll blacklist self.reactor.lookups["internal"] = "127.0.0.1" self.reactor.lookups["internalv6"] = "fe80:0:0:0:0:8a2e:370:7337" ip_blacklist = IPSet(["127.0.0.0/8", "fe80::/64"]) cl = SimpleHttpClient(self.hs, ip_blacklist=ip_blacklist) # Try making a GET request to a blacklisted IPv4 address # ------------------------------------------------------ # Make the request d = defer.ensureDeferred(cl.get_json("http://internal:8008/foo/bar")) self.pump(1) # Check that it was unable to resolve the address clients = self.reactor.tcpClients self.assertEqual(len(clients), 0) self.failureResultOf(d, DNSLookupError) # Try making a POST request to a blacklisted IPv6 address # ------------------------------------------------------- # Make the request d = defer.ensureDeferred( cl.post_json_get_json("http://internalv6:8008/foo/bar", {})) # Move the reactor forwards self.pump(1) # Check that it was unable to resolve the address clients = self.reactor.tcpClients self.assertEqual(len(clients), 0) # Check that it was due to a blacklisted DNS lookup self.failureResultOf(d, DNSLookupError) # Try making a GET request to a non-blacklisted IPv4 address # ---------------------------------------------------------- # Make the request d = defer.ensureDeferred(cl.get_json("http://testserv:8008/foo/bar")) # Nothing has happened yet self.assertNoResult(d) # Move the reactor forwards self.pump(1) # Check that it was able to resolve the address clients = self.reactor.tcpClients self.assertNotEqual(len(clients), 0) # Connection will still fail as this IP address does not resolve to anything self.failureResultOf(d, RequestTimedOutError)
def get_proxied_blacklisted_http_client(self) -> SimpleHttpClient: """ An HTTP client that uses configured HTTP(S) proxies and blacklists IPs based on the IP range blacklist/whitelist. """ return SimpleHttpClient( self, ip_whitelist=self.config.ip_range_whitelist, ip_blacklist=self.config.ip_range_blacklist, use_proxy=True, )
def get_proxied_blacklisted_http_client(self) -> SimpleHttpClient: """ An HTTP client that uses configured HTTP(S) proxies and blacklists IPs based on the IP range blacklist/whitelist. """ return SimpleHttpClient( self, ip_whitelist=self.config.ip_range_whitelist, ip_blacklist=self.config.ip_range_blacklist, http_proxy=os.getenvb(b"http_proxy"), https_proxy=os.getenvb(b"HTTPS_PROXY"), )
def _query_email(self, email): httpCli = SimpleHttpClient(self.hs) data = yield httpCli.get_json( # TODO FIXME This should be configurable. # XXX: ID servers need to use HTTPS "http://%s%s" % ("matrix.org:8090", "/_matrix/identity/api/v1/lookup"), { 'medium': 'email', 'address': email }) defer.returnValue(data)
def __init__( self, hs: "HomeServer", media_repo: "MediaRepository", media_storage: MediaStorage, ): super().__init__() self.auth = hs.get_auth() self.clock = hs.get_clock() self.filepaths = media_repo.filepaths self.max_spider_size = hs.config.media.max_spider_size self.server_name = hs.hostname self.store = hs.get_datastore() self.client = SimpleHttpClient( hs, treq_args={"browser_like_redirects": True}, ip_whitelist=hs.config.media.url_preview_ip_range_whitelist, ip_blacklist=hs.config.media.url_preview_ip_range_blacklist, use_proxy=True, ) self.media_repo = media_repo self.primary_base_path = media_repo.primary_base_path self.media_storage = media_storage self._oembed = OEmbedProvider(hs) # We run the background jobs if we're the instance specified (or no # instance is specified, where we assume there is only one instance # serving media). instance_running_jobs = hs.config.media.media_instance_running_background_jobs self._worker_run_media_background_jobs = ( instance_running_jobs is None or instance_running_jobs == hs.get_instance_name() ) self.url_preview_url_blacklist = hs.config.media.url_preview_url_blacklist self.url_preview_accept_language = hs.config.media.url_preview_accept_language # memory cache mapping urls to an ObservableDeferred returning # JSON-encoded OG metadata self._cache: ExpiringCache[str, ObservableDeferred] = ExpiringCache( cache_name="url_previews", clock=self.clock, # don't spider URLs more often than once an hour expiry_ms=ONE_HOUR, ) if self._worker_run_media_background_jobs: self._cleaner_loop = self.clock.looping_call( self._start_expire_url_cache_data, 10 * 1000 )
def __init__(self, _hs, profile_tag, user_name, app_id, app_display_name, device_display_name, pushkey, pushkey_ts, data, last_token, last_success, failing_since): super(HttpPusher, self).__init__(_hs, profile_tag, user_name, app_id, app_display_name, device_display_name, pushkey, pushkey_ts, data, last_token, last_success, failing_since) if 'url' not in data: raise PusherConfigException( "'url' required in data for HTTP pusher") self.url = data['url'] self.httpCli = SimpleHttpClient(self.hs) self.data_minus_url = {} self.data_minus_url.update(self.data) del self.data_minus_url['url']
def _bind_threepid(self, creds, mxid): yield logger.debug("binding threepid") httpCli = SimpleHttpClient(self.hs) data = yield httpCli.post_urlencoded_get_json( # XXX: Change when ID servers are all HTTPS "http://%s%s" % (creds['idServer'], "/_matrix/identity/api/v1/3pid/bind"), { 'sid': creds['sid'], 'clientSecret': creds['clientSecret'], 'mxid': mxid, }) logger.debug("bound threepid") defer.returnValue(data)
def _query_email(self, email): http_client = SimpleHttpClient(self.hs) try: data = yield http_client.get_json( # TODO FIXME This should be configurable. # XXX: ID servers need to use HTTPS "http://%s%s" % ("matrix.org:8090", "/_matrix/identity/api/v1/lookup"), { 'medium': 'email', 'address': email }) defer.returnValue(data) except CodeMessageException as e: data = json.loads(e.msg) defer.returnValue(data)
def __init__(self, hs): super().__init__() self.hs = hs # self.get_ver_code_cache = ExpiringCache( # cache_name="get_ver_code_cache", # clock=self._clock, # max_len=1000, # expiry_ms=10 * 60 * 1000, # reset_expiry_on_get=False, # ) self._address_ratelimiter = Ratelimiter( clock=hs.get_clock(), rate_hz=self.hs.config.rc_login_address.per_second, burst_count=self.hs.config.rc_login_address.burst_count, ) self.http_client = SimpleHttpClient(hs)
def threepid_from_creds(self, creds): yield run_on_reactor() # TODO: get this from the homeserver rather than creating a new one for # each request http_client = SimpleHttpClient(self.hs) # XXX: make this configurable! # trustedIdServers = ['matrix.org', 'localhost:8090'] trustedIdServers = ['matrix.org', 'vector.im'] if 'id_server' in creds: id_server = creds['id_server'] elif 'idServer' in creds: id_server = creds['idServer'] else: raise SynapseError(400, "No id_server in creds") if 'client_secret' in creds: client_secret = creds['client_secret'] elif 'clientSecret' in creds: client_secret = creds['clientSecret'] else: raise SynapseError(400, "No client_secret in creds") if id_server not in trustedIdServers: logger.warn( '%s is not a trusted ID server: rejecting 3pid ' + 'credentials', id_server) defer.returnValue(None) data = {} try: data = yield http_client.get_json( "https://%s%s" % (id_server, "/_matrix/identity/api/v1/3pid/getValidated3pid"), { 'sid': creds['sid'], 'client_secret': client_secret }) except CodeMessageException as e: data = json.loads(e.msg) if 'medium' in data: defer.returnValue(data) defer.returnValue(None)
def __init__(self, hs): super().__init__() self.hs = hs logger.info("------------init------") self.auth = hs.get_auth() self._auth_handler = hs.get_auth_handler() self._cache = hs.get_eachchat_cache_for_openid() # self.get_ver_code_cache = ExpiringCache( # cache_name="get_ver_code_cache", # clock=self._clock, # max_len=1000, # expiry_ms=10 * 60 * 1000, # reset_expiry_on_get=False, # ) self._address_ratelimiter = Ratelimiter( clock=hs.get_clock(), rate_hz=self.hs.config.rc_login_address.per_second, burst_count=self.hs.config.rc_login_address.burst_count, ) self.http_client = SimpleHttpClient(hs)
def requestEmailToken(self, id_server, email, client_secret, send_attempt, **kwargs): yield run_on_reactor() http_client = SimpleHttpClient(self.hs) params = { 'email': email, 'client_secret': client_secret, 'send_attempt': send_attempt, } params.update(kwargs) try: data = yield http_client.post_urlencoded_get_json( "https://%s%s" % (id_server, "/_matrix/identity/api/v1/validate/email/requestToken"), params) defer.returnValue(data) except CodeMessageException as e: logger.info("Proxied requestToken failed: %r", e) raise e
def _threepid_from_creds(self, creds): # TODO: get this from the homeserver rather than creating a new one for # each request httpCli = SimpleHttpClient(self.hs) # XXX: make this configurable! trustedIdServers = ['matrix.org:8090'] if not creds['idServer'] in trustedIdServers: logger.warn( '%s is not a trusted ID server: rejecting 3pid ' + 'credentials', creds['idServer']) defer.returnValue(None) data = yield httpCli.get_json( # XXX: This should be HTTPS "http://%s%s" % (creds['idServer'], "/_matrix/identity/api/v1/3pid/getValidated3pid"), { 'sid': creds['sid'], 'clientSecret': creds['clientSecret'] }) if 'medium' in data: defer.returnValue(data) defer.returnValue(None)
def __init__(self, hs: "HomeServer"): super().__init__() self.hs = hs # JWT configuration variables. self.jwt_enabled = hs.config.jwt_enabled self.jwt_secret = hs.config.jwt_secret self.jwt_algorithm = hs.config.jwt_algorithm self.jwt_issuer = hs.config.jwt_issuer self.jwt_audiences = hs.config.jwt_audiences # SSO configuration. self.saml2_enabled = hs.config.saml2_enabled self.cas_enabled = hs.config.cas_enabled self.oidc_enabled = hs.config.oidc_enabled self._msc2858_enabled = hs.config.experimental.msc2858_enabled self.auth = hs.get_auth() self.auth_handler = self.hs.get_auth_handler() self.registration_handler = hs.get_registration_handler() self._sso_handler = hs.get_sso_handler() self._well_known_builder = WellKnownBuilder(hs) self._address_ratelimiter = Ratelimiter( clock=hs.get_clock(), rate_hz=self.hs.config.rc_login_address.per_second, burst_count=self.hs.config.rc_login_address.burst_count, ) self._account_ratelimiter = Ratelimiter( clock=hs.get_clock(), rate_hz=self.hs.config.rc_login_account.per_second, burst_count=self.hs.config.rc_login_account.burst_count, ) self.http_client = SimpleHttpClient(hs) self._cache = hs.get_eachchat_cache_for_openid()
def build_proxied_http_client(self): return SimpleHttpClient( self, http_proxy=os.getenvb(b"http_proxy"), https_proxy=os.getenvb(b"HTTPS_PROXY"), )
def build_simple_http_client(self): return SimpleHttpClient(self)
def get_simple_http_client(self) -> SimpleHttpClient: return SimpleHttpClient(self)
def get_proxied_http_client(self) -> SimpleHttpClient: """ An HTTP client that uses configured HTTP(S) proxies. """ return SimpleHttpClient(self, use_proxy=True)
def get_simple_http_client(self) -> SimpleHttpClient: """ An HTTP client with no special configuration. """ return SimpleHttpClient(self)