urlmatch: Implement correct IP matching
This commit is contained in:
parent
2d43a1d2e7
commit
978b90b5b1
@ -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
|
https://cs.chromium.org/chromium/src/extensions/common/url_pattern.h
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import ipaddress
|
||||||
import fnmatch
|
import fnmatch
|
||||||
import contextlib
|
import contextlib
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
@ -205,11 +206,10 @@ class UrlPattern:
|
|||||||
if not self._match_subdomains:
|
if not self._match_subdomains:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# FIXME
|
|
||||||
# We don't do subdomain matching against IP addresses, so we can give up now
|
# We don't do subdomain matching against IP addresses, so we can give up now
|
||||||
# if the test host is an IP address.
|
# if the test host is an IP address.
|
||||||
# if (test.HostIsIPAddress())
|
if not utils.raises(ValueError, ipaddress.ip_address, host):
|
||||||
# return false;
|
return False
|
||||||
|
|
||||||
# Check if the test host is a subdomain of our host.
|
# Check if the test host is a subdomain of our host.
|
||||||
if len(host) <= (len(self._host) + 1):
|
if len(host) <= (len(self._host) + 1):
|
||||||
|
@ -187,3 +187,27 @@ class TestMatchGlobEscaping:
|
|||||||
])
|
])
|
||||||
def test_urls(self, up, url, expected):
|
def test_urls(self, up, url, expected):
|
||||||
assert up.matches(QUrl(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
|
||||||
|
Loading…
Reference in New Issue
Block a user