Skip to content

Commit 3be774f

Browse files
committed
Improve IPv6 validation
fixes #107
1 parent 29df081 commit 3be774f

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

tests/test_ipv6.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55

66

77
@pytest.mark.parametrize(('address',), [
8+
('::',),
89
('::1',),
9-
('dead:beef:0:0:0:0:42:1',),
10+
('1::',),
11+
('dead:beef:0:0:0:0000:42:1',),
1012
('abcd:ef::42:1',),
1113
('0:0:0:0:0:ffff:1.2.3.4',),
1214
('::192.168.30.2',),
@@ -21,6 +23,10 @@ def test_returns_true_on_valid_ipv6_address(address):
2123
('abcd:1234::123::1',),
2224
('1:2:3:4:5:6:7:8:9',),
2325
('abcd::1ffff',),
26+
('1111:',),
27+
(':8888',),
28+
(':1.2.3.4',),
29+
('18:05',),
2430
])
2531
def test_returns_failed_validation_on_invalid_ipv6_address(address):
2632
assert isinstance(ipv6(address), ValidationFailure)

validators/ip_address.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def ipv4_cidr(value):
5757
@validator
5858
def ipv6(value):
5959
"""
60-
Return whether or not given value is a valid IP version 6 address
60+
Return whether a given value is a valid IP version 6 address
6161
(including IPv4-mapped IPv6 addresses).
6262
6363
This validator is based on `WTForms IPAddress validator`_.
@@ -112,9 +112,13 @@ def ipv6(value):
112112
if not 0 <= num <= 65536:
113113
return False
114114

115-
if count_blank < 2:
115+
if count_blank == 0 and len(ipv6_groups) == max_groups:
116116
return True
117-
elif count_blank == 2 and not ipv6_groups[0] and not ipv6_groups[1]:
117+
elif count_blank == 1 and ipv6_groups[-1] and ipv6_groups[0]:
118+
return True
119+
elif count_blank == 2 and (not ipv6_groups[0] or not ipv6_groups[-1]):
120+
return True
121+
elif count_blank == 3 and len(ipv6_groups) == 3:
118122
return True
119123
return False
120124

0 commit comments

Comments
 (0)