def _validate_and_clean_url(self, param_name, param): """Validate and clean URL input.""" if param.get('allow_list', False): value = param['value'].split(',') else: value = [param['value']] for url in value: # Django's validator requires a protocol but we accept just a # hostname or IP address. try: ip = ip_address(url) except ValueError: if '://' not in url: url = 'http://%s' % url else: if ip.version == 4: url = 'http://%s' % url else: url = 'http://[%s]' % url # Allow all schemes supported by curl(used with the network # validation test) + icmp. try: validate_url(url, schemes=[ 'icmp', 'file', 'ftp', 'ftps', 'gopher', 'http', 'https', 'imap', 'imaps', 'ldap', 'ldaps', 'pop3', 'pop3s', 'rtmp', 'rtsp', 'scp', 'sftp', 'smb', 'smbs', 'smtp', 'smtps', 'telnet', 'tftp' ]) except ValidationError: set_form_error(self, param_name, 'Invalid URL')
def _validate_and_clean_url(self, param_name, param): """Validate and clean URL input.""" if param.get("allow_list", False): value = param["value"].split(",") else: value = [param["value"]] for url in value: # Django's validator requires a protocol but we accept just a # hostname or IP address. try: ip = ip_address(url) except ValueError: if "://" not in url: url = "http://%s" % url else: if ip.version == 4: url = "http://%s" % url else: url = "http://[%s]" % url # Allow all schemes supported by curl(used with the network # validation test) + icmp. try: validate_url( url, schemes=[ "icmp", "file", "ftp", "ftps", "gopher", "http", "https", "imap", "imaps", "ldap", "ldaps", "pop3", "pop3s", "rtmp", "rtsp", "scp", "sftp", "smb", "smbs", "smtp", "smtps", "telnet", "tftp", ], ) except ValidationError: set_form_error(self, param_name, "Invalid URL")
def test_accepts_git_ssh_scheme(self): self.assertIsNone( validate_url( "git+ssh://[email protected]/example/hg-git.git", schemes=EXTENDED_SCHEMES, ) )
def is_valid_url(auth_url): try: validate_url(auth_url) except ValidationError: return False return True
def test_accepts_git_scheme(self): self.assertIsNone( validate_url("git://example.com/", schemes=EXTENDED_SCHEMES))
def test_accepts_localhost(self): self.assertIsNone( validate_url("file://localhost/path", schemes=EXTENDED_SCHEMES))
def assertAccepts(self, url): """Assertion: the validator accepts `url`.""" try: validate_url(url) except ValidationError as e: raise AssertionError(str(e))