def _is_url_naive(urlstr): """Naive check if given URL is really a URL. Args: urlstr: The URL to check for, as string. Return: True if the URL really is a URL, False otherwise. """ url = qurl_from_user_input(urlstr) try: ipaddress.ip_address(urlstr) except ValueError: pass else: # Valid IPv4/IPv6 address return True # Qt treats things like "23.42" or "1337" or "0xDEAD" as valid URLs # which we don't want to. Note we already filtered *real* valid IPs # above. if ((not utils.raises(ValueError, int, urlstr, 0)) or (not utils.raises(ValueError, float, urlstr))): return False if not url.isValid(): return False elif '.' in url.host(): return True elif url.host() == 'localhost': return True else: return False
def _is_url_naive(urlstr): """Naive check if given URL is really a URL. Args: urlstr: The URL to check for, as string. Return: True if the URL really is a URL, False otherwise. """ url = qurl_from_user_input(urlstr) assert url.isValid() if not utils.raises(ValueError, ipaddress.ip_address, urlstr): # Valid IPv4/IPv6 address return True # Qt treats things like "23.42" or "1337" or "0xDEAD" as valid URLs # which we don't want to. Note we already filtered *real* valid IPs # above. if not QHostAddress(urlstr).isNull(): return False if '.' in url.host(): return True else: return False
def _is_url_dns(urlstr): """Check if a URL is really a URL via DNS. Args: url: The URL to check for as a string. Return: True if the URL really is a URL, False otherwise. """ url = qurl_from_user_input(urlstr) assert url.isValid() if (utils.raises(ValueError, ipaddress.ip_address, urlstr) and not QHostAddress(urlstr).isNull()): log.url.debug("Bogus IP URL -> False") # Qt treats things like "23.42" or "1337" or "0xDEAD" as valid URLs # which we don't want to. return False host = url.host() if not host: log.url.debug("URL has no host -> False") return False log.url.debug("Doing DNS request for {}".format(host)) info = QHostInfo.fromName(host) return not info.error()
def _matches_host(self, host: str) -> bool: # FIXME what about multiple dots? host = host.rstrip('.') # If we have no host in the match pattern, that means that we're # matching all hosts, which means we have a match no matter what the # test host is. # Contrary to Chromium, we don't need to check for # self._match_subdomains, as we want to return True here for e.g. # file:// as well. if self.host is None: return True # If the hosts are exactly equal, we have a match. if host == self.host: return True # Otherwise, we can only match if our match pattern matches subdomains. if not self._match_subdomains: return False # We don't do subdomain matching against IP addresses, so we can give # up now if the test host is an IP address. if not utils.raises(ValueError, ipaddress.ip_address, host): return False # Check if the test host is a subdomain of our host. if len(host) <= (len(self.host) + 1): return False if not host.endswith(self.host): return False return host[len(host) - len(self.host) - 1] == '.'
def _is_url_naive(urlstr): """Naive check if given URL is really a URL. Args: urlstr: The URL to check for, as string. Return: True if the URL really is a URL, False otherwise. """ url = qurl_from_user_input(urlstr) assert url.isValid() if not utils.raises(ValueError, ipaddress.ip_address, urlstr): # Valid IPv4/IPv6 address return True # Qt treats things like "23.42" or "1337" or "0xDEAD" as valid URLs # which we don't want to. Note we already filtered *real* valid IPs # above. if not QHostAddress(urlstr).isNull(): return False if '.' in url.host(): return True else: return False
def _matches_host(self, host): # FIXME what about multiple dots? host = host.rstrip('.') # If we have no host in the match pattern, that means that we're # matching all hosts, which means we have a match no matter what the # test host is. # Contrary to Chromium, we don't need to check for # self._match_subdomains, as we want to return True here for e.g. # file:// as well. if self._host is None: return True # If the hosts are exactly equal, we have a match. if host == self._host: return True # Otherwise, we can only match if our match pattern matches subdomains. if not self._match_subdomains: return False # We don't do subdomain matching against IP addresses, so we can give # up now if the test host is an IP address. if not utils.raises(ValueError, ipaddress.ip_address, host): return False # Check if the test host is a subdomain of our host. if len(host) <= (len(self._host) + 1): return False if not host.endswith(self._host): return False return host[len(host) - len(self._host) - 1] == '.'
def _is_url_dns(urlstr): """Check if a URL is really a URL via DNS. Args: url: The URL to check for as a string. Return: True if the URL really is a URL, False otherwise. """ url = qurl_from_user_input(urlstr) assert url.isValid() if (utils.raises(ValueError, ipaddress.ip_address, urlstr) and not QHostAddress(urlstr).isNull()): log.url.debug("Bogus IP URL -> False") # Qt treats things like "23.42" or "1337" or "0xDEAD" as valid URLs # which we don't want to. return False host = url.host() if not host: log.url.debug("URL has no host -> False") return False log.url.debug("Doing DNS request for {}".format(host)) info = QHostInfo.fromName(host) return not info.error()
def _is_url_naive(urlstr: str) -> bool: """Naive check if given URL is really a URL. Args: urlstr: The URL to check for, as string. Return: True if the URL really is a URL, False otherwise. """ url = QUrl.fromUserInput(urlstr) assert url.isValid() host = url.host() # Valid IPv4/IPv6 address. Qt converts things like "23.42" or "1337" or # "0xDEAD" to IP addresses, which we don't like, so we check if the host # from Qt is part of the input. if (not utils.raises(ValueError, ipaddress.ip_address, host) and host in urlstr): return True tld = r'\.([^.0-9_-]+|xn--[a-z0-9-]+)$' forbidden = r'[\u0000-\u002c\u002f\u003a-\u0060\u007b-\u00b6]' return bool(re.search(tld, host) and not re.search(forbidden, host))
def test_unrelated_exception(self): """Test with an unrelated exception.""" with pytest.raises(Exception): utils.raises(ValueError, self.do_raise)
def test_no_args_false(self): """Test with no args and an exception which does not get raised.""" assert not utils.raises(Exception, self.do_nothing)
def test_no_args_true(self): """Test with no args and an exception which gets raised.""" assert utils.raises(Exception, self.do_raise)
def test_raises_multiple_exc_false(self): """Test raises with multiple exceptions which do not get raised.""" assert not utils.raises((ValueError, TypeError), int, '1')
def test_raises_multiple_exc_true(self): """Test raises with multiple exceptions which get raised.""" self.assertTrue(utils.raises((ValueError, TypeError), int, 'a')) self.assertTrue(utils.raises((ValueError, TypeError), int, None))
def test_raises_single_exc_true(self): """Test raises with a single exception which gets raised.""" assert utils.raises(ValueError, int, 'a')
def test_unrelated_exception(self): """Test with an unrelated exception.""" with self.assertRaises(Exception): utils.raises(ValueError, self.do_raise)
def test_no_args_false(self): """Test with no args and an exception which does not get raised.""" self.assertFalse(utils.raises(Exception, self.do_nothing))
def test_no_args_true(self): """Test with no args and an exception which gets raised.""" self.assertTrue(utils.raises(Exception, self.do_raise))
def test_raises_multiple_exc_false(self): """Test raises with multiple exceptions which do not get raised.""" self.assertFalse(utils.raises((ValueError, TypeError), int, '1'))
def test_raises_single_exc_true(self): """Test raises with a single exception which gets raised.""" self.assertTrue(utils.raises(ValueError, int, 'a'))
def test_raises_multiple_exc_true(self): """Test raises with multiple exceptions which get raised.""" assert utils.raises((ValueError, TypeError), int, 'a') assert utils.raises((ValueError, TypeError), int, None)
def test_raises_single_exc_false(self): """Test raises with a single exception which does not get raised.""" assert not utils.raises(ValueError, int, '1')
def test_raises_single_exc_false(self): """Test raises with a single exception which does not get raised.""" self.assertFalse(utils.raises(ValueError, int, '1'))