Add lookahead for ip check in AnyUrl (#2512)

This commit is contained in:
Søren Bjerregaard Vrist
2021-05-09 11:49:19 +02:00
committed by GitHub
parent 9cc19e9a8e
commit 08fc8302cf
3 changed files with 16 additions and 2 deletions
+2
View File
@@ -0,0 +1,2 @@
Add lookahead to ip regexes for `AnyUrl` hosts. This allows urls with DNS labels
looking like IPs to validate as they are perfectly valid host names.
+2 -2
View File
@@ -69,8 +69,8 @@ def url_regex() -> Pattern[str]:
r'(?:(?P<scheme>[a-z][a-z0-9+\-.]+)://)?' # scheme https://tools.ietf.org/html/rfc3986#appendix-A
r'(?:(?P<user>[^\s:/]*)(?::(?P<password>[^\s/]*))?@)?' # user info
r'(?:'
r'(?P<ipv4>(?:\d{1,3}\.){3}\d{1,3})|' # ipv4
r'(?P<ipv6>\[[A-F0-9]*:[A-F0-9:]+\])|' # ipv6
r'(?P<ipv4>(?:\d{1,3}\.){3}\d{1,3})(?=$|[/:#?])|' # ipv4
r'(?P<ipv6>\[[A-F0-9]*:[A-F0-9:]+\])(?=$|[/:#?])|' # ipv6
r'(?P<domain>[^\s/:?#]+)' # domain, validation occurs later
r')?'
r'(?::(?P<port>\d+))?' # port
+12
View File
@@ -51,6 +51,8 @@ except ImportError:
AnyUrl('https://example.com', scheme='https', host='example.com'),
'https://exam_ple.com/',
'http://twitter.com/@handle/',
'http://11.11.11.11.example.com/action',
'http://abc.11.11.11.11.example.com/action',
],
)
def test_any_url_success(value):
@@ -159,6 +161,16 @@ def test_ipv4_port():
assert url.password is None
def test_ipv4_no_port():
url = validate_url('ftp://123.45.67.8')
assert url.scheme == 'ftp'
assert url.host == '123.45.67.8'
assert url.host_type == 'ipv4'
assert url.port is None
assert url.user is None
assert url.password is None
def test_ipv6_port():
url = validate_url('wss://[2001:db8::ff00:42]:8329')
assert url.scheme == 'wss'