def test_matching_subdomains(monkeypatch): def gethostbyname_patch(x): return "127.0.0.1" monkeypatch.setattr(socket, "gethostbyname", gethostbyname_patch) assert matching_subdomains(None, "mail.nylas.com") is False # Two domains with the same IP but different domains aren't matched. assert matching_subdomains("mail.microsoft.com", "mail.nylas.com") is False assert matching_subdomains("test.nylas.co.uk", "mail.nylas.co.uk") is True assert matching_subdomains("test.servers.nylas.com.au", "mail.nylas.com.au") is True assert matching_subdomains("test.servers.nylas.com", "mail.nylas.com.au") is False assert matching_subdomains("test.servers.co.uk", "evil.co.uk") is False addresses = ["127.0.0.1", "192.168.1.11"] def gethostbyname_patch(x): return addresses.pop() monkeypatch.setattr(socket, "gethostbyname", gethostbyname_patch) addresses = ["127.0.0.1", "192.168.1.11"] def gethostbyname_patch(x): return addresses.pop() # Check that if the domains are the same, we're not doing an # IP address resolution. assert matching_subdomains("nylas.com", "nylas.com") is True
def test_matching_subdomains(monkeypatch): def gethostbyname_patch(x): return "127.0.0.1" monkeypatch.setattr(socket, 'gethostbyname', gethostbyname_patch) assert matching_subdomains(None, 'mail.nylas.com') is False # Two domains with the same IP but different domains aren't matched. assert matching_subdomains('mail.microsoft.com', 'mail.nylas.com') is False assert matching_subdomains('test.nylas.co.uk', 'mail.nylas.co.uk') is True assert matching_subdomains('test.servers.nylas.com.au', 'mail.nylas.com.au') is True assert matching_subdomains('test.servers.nylas.com', 'mail.nylas.com.au') is False assert matching_subdomains('test.servers.co.uk', 'evil.co.uk') is False addresses = ['127.0.0.1', '192.168.1.11'] def gethostbyname_patch(x): return addresses.pop() monkeypatch.setattr(socket, 'gethostbyname', gethostbyname_patch) addresses = ['127.0.0.1', '192.168.1.11'] def gethostbyname_patch(x): return addresses.pop() # Check that if the domains are the same, we're not doing an # IP address resolution. assert matching_subdomains('nylas.com', 'nylas.com') is True
def update_account(self, account, response): account.email_address = response['email'] for attribute in [ 'name', 'imap_username', 'imap_password', 'smtp_username', 'smtp_password', 'password' ]: if response.get(attribute): setattr(account, attribute, response[attribute]) # Shim for back-compatability with legacy auth if response.get('imap_password'): # The new API sends separate IMAP/ SMTP credentials but we need to # set the legacy password attribute. # TODO[k]: Remove once column in dropped. account.password = response['imap_password'] else: # The old API does NOT send these but authentication now uses them # so update them. for attr in ('imap_username', 'smtp_username'): if attr not in response: setattr(account, attr, response['email']) for attr in ('imap_password', 'smtp_password'): if attr not in response: setattr(account, attr, response['password']) account.date = datetime.datetime.utcnow() if self.provider_name == 'custom': for attribute in ('imap_server_host', 'smtp_server_host'): old_value = getattr(account, '_{}'.format(attribute), None) new_value = response.get(attribute) if (new_value is not None and old_value is not None and new_value != old_value): # Before updating the domain name, check if: # 1/ they have the same parent domain # 2/ they direct to the same IP. if not matching_subdomains(new_value, old_value): raise UserRecoverableConfigError( "Updating the IMAP/SMTP servers is not permitted. Please " "verify that the server names you entered are correct. " "If your IMAP/SMTP server has in fact changed, please " "contact Nylas support to update it. More details here: " "https://support.nylas.com/hc/en-us/articles/218006767" ) # If all those conditions are met, update the address. setattr(account, '_{}'.format(attribute), new_value) account.ssl_required = response.get('ssl_required', True) # Ensure account has sync enabled after authing. account.enable_sync() return account
def update_account(self, account, response): account.email_address = response['email'] for attribute in ['name', 'imap_username', 'imap_password', 'smtp_username', 'smtp_password', 'password']: if response.get(attribute): setattr(account, attribute, response[attribute]) # Shim for back-compatability with legacy auth if response.get('imap_password'): # The new API sends separate IMAP/ SMTP credentials but we need to # set the legacy password attribute. # TODO[k]: Remove once column in dropped. account.password = response['imap_password'] else: # The old API does NOT send these but authentication now uses them # so update them. for attr in ('imap_username', 'smtp_username'): if attr not in response: setattr(account, attr, response['email']) for attr in ('imap_password', 'smtp_password'): if attr not in response: setattr(account, attr, response['password']) account.date = datetime.datetime.utcnow() if self.provider_name == 'custom': for attribute in ('imap_server_host', 'smtp_server_host'): old_value = getattr(account, '_{}'.format(attribute), None) new_value = response.get(attribute) if (new_value is not None and old_value is not None and new_value != old_value): # Before updating the domain name, check if: # 1/ they have the same parent domain # 2/ they direct to the same IP. if not matching_subdomains(new_value, old_value): raise UserRecoverableConfigError( "Updating the IMAP/SMTP servers is not permitted. Please " "verify that the server names you entered are correct. " "If your IMAP/SMTP server has in fact changed, please " "contact Nylas support to update it. More details here: " "https://support.nylas.com/hc/en-us/articles/218006767") # If all those conditions are met, update the address. setattr(account, '_{}'.format(attribute), new_value) account.ssl_required = response.get('ssl_required', True) # Ensure account has sync enabled after authing. account.enable_sync() return account