Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lookahead for ip check in AnyUrl #2512

Merged
merged 1 commit into from
May 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changes/2512-sbv-csis.md
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 2 additions & 2 deletions pydantic/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions tests/test_networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
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):
Expand Down Expand Up @@ -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'
Expand Down