diff --git a/qutebrowser/utils/urlmatch.py b/qutebrowser/utils/urlmatch.py index 76a72c32e..5efa5cfb8 100644 --- a/qutebrowser/utils/urlmatch.py +++ b/qutebrowser/utils/urlmatch.py @@ -25,6 +25,7 @@ https://cs.chromium.org/chromium/src/extensions/common/url_pattern.cc https://cs.chromium.org/chromium/src/extensions/common/url_pattern.h """ +import ipaddress import fnmatch import contextlib import urllib.parse @@ -205,11 +206,10 @@ class UrlPattern: if not self._match_subdomains: return False - # FIXME # We don't do subdomain matching against IP addresses, so we can give up now # if the test host is an IP address. - # if (test.HostIsIPAddress()) - # return false; + 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): diff --git a/tests/unit/utils/test_urlmatch.py b/tests/unit/utils/test_urlmatch.py index cc5f4006a..624736a22 100644 --- a/tests/unit/utils/test_urlmatch.py +++ b/tests/unit/utils/test_urlmatch.py @@ -187,3 +187,27 @@ class TestMatchGlobEscaping: ]) def test_urls(self, up, url, expected): assert up.matches(QUrl(url)) == expected + + +class TestMatchIpAddresses: + + @pytest.mark.parametrize('pattern, host, match_subdomains', [ + ("http://127.0.0.1/*", "127.0.0.1", False), + ("http://*.0.0.1/*", "0.0.1", True), + ]) + def test_attrs(self, pattern, host, match_subdomains): + up = urlmatch.UrlPattern(pattern) + assert up._scheme == 'http' + assert up._host == host + assert up._match_subdomains == match_subdomains + assert not up._match_all + assert up._path == '/*' + + @pytest.mark.parametrize('pattern, expected', [ + ("http://127.0.0.1/*", True), + # No subdomain matching is done with IPs + ("http://*.0.0.1/*", False), + ]) + def test_urls(self, pattern, expected): + up = urlmatch.UrlPattern(pattern) + assert up.matches(QUrl("http://127.0.0.1")) == expected