Skip to content

Commit

Permalink
Fix #35 - IPv6 addresses in urinorm
Browse files Browse the repository at this point in the history
  • Loading branch information
ziima committed Jul 30, 2020
1 parent d093a09 commit 2a12e05
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
6 changes: 6 additions & 0 deletions openid/test/test_urinorm.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ def test_unsupported_scheme(self):
def test_lowercase_hostname(self):
self.assertEqual(urinorm('http://exaMPLE.COm/'), 'http://example.com/')

def test_ipv4_hostname(self):
self.assertEqual(urinorm('http://127.0.0.1/'), 'http://127.0.0.1/')

def test_ipv6_hostname(self):
self.assertEqual(urinorm('http://[fe80::1]/'), 'http://[fe80::1]/')

def test_idn_hostname(self):
self.assertEqual(urinorm('http://π.example.com/'), 'http://xn--1xa.example.com/')

Expand Down
19 changes: 18 additions & 1 deletion openid/urinorm.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

from .oidutil import string_to_text

try:
from ipaddress import ip_address
except ImportError:
# Not available in python 2.7
ip_address = None


def remove_dot_segments(path):
result_segments = []
Expand Down Expand Up @@ -96,6 +102,14 @@ def urinorm(uri):
except ValueError as error:
raise ValueError('Invalid hostname {!r}: {}'.format(hostname, error))
_check_disallowed_characters(hostname, 'hostname')
if ip_address is not None:
try:
hostname_is_ipv6 = bool(ip_address(hostname).version == 6)
except ValueError:
hostname_is_ipv6 = False
else:
# Python 2 fallback
hostname_is_ipv6 = bool(':' in hostname)

try:
port = split_uri.port
Expand All @@ -106,7 +120,10 @@ def urinorm(uri):
elif (scheme == 'http' and port == 80) or (scheme == 'https' and port == 443):
port = ''

netloc = hostname
if hostname_is_ipv6:
netloc = '[' + hostname + ']'
else:
netloc = hostname
if port:
netloc = netloc + ':' + six.text_type(port)
userinfo_chunks = [i for i in (split_uri.username, split_uri.password) if i is not None]
Expand Down

0 comments on commit 2a12e05

Please sign in to comment.