From a0172092e8da1814a15b8a7cfcd4c70f9d7c450c Mon Sep 17 00:00:00 2001 From: Jon Parise Date: Wed, 21 Aug 2013 15:51:39 -0700 Subject: [PATCH] Make sure we have a valid host in norm_netloc(). Previously, if we started with a URL like `http://./`, the code in norm_netloc() would start with a host string of '.', strip the dot, and then run into an IndexError when it attempted to look for IPv6 brackets. --- test_urlnorm.py | 1 + urlnorm.py | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/test_urlnorm.py b/test_urlnorm.py index cc23441..99c25a5 100644 --- a/test_urlnorm.py +++ b/test_urlnorm.py @@ -71,6 +71,7 @@ def pytest_generate_tests(metafunc): for url in [ '-', 'asdf', + 'http://./', 'HTTP://4294967297/test', # one more than max ip > int 'http://[img]http://i790.photobucket.com/albums/yy185/zack-32009/jordan.jpg[/IMG]', ]: diff --git a/urlnorm.py b/urlnorm.py index a3ac65a..633c516 100644 --- a/urlnorm.py +++ b/urlnorm.py @@ -208,6 +208,10 @@ def norm_netloc(scheme, netloc): if host[-1] == '.': host = host[:-1] + # bracket check is for ipv6 hosts + if not host or ('.' not in host and not (host[0] == '[' and host[-1] == ']')): + raise InvalidUrl('host %r is not valid' % host) + authority = lower(host) if 'xn--' in authority: subdomains = [_idn(subdomain) for subdomain in authority.split('.')]