urlmatch: Implement correct IP matching

This commit is contained in:
Florian Bruhin 2018-02-15 16:52:35 +01:00
parent 2d43a1d2e7
commit 978b90b5b1
2 changed files with 27 additions and 3 deletions

View File

@ -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):

View File

@ -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