Skip to content

Commit fa42ff2

Browse files
committed
* Changed url validator to allow private and local IP addresses by default.
If required to validate and ensure the existence of a public address, the flag `public` should be set to True upon calling `url` function. * Included some unit tests to validate the new functionality.
1 parent b0364c8 commit fa42ff2

2 files changed

Lines changed: 50 additions & 9 deletions

File tree

‎tests/test_url.py‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,27 @@
4444
u'http://1337.net',
4545
u'http://a.b-c.de',
4646
u'http://223.255.255.254',
47+
u'http://10.1.1.1',
48+
u'http://10.1.1.254',
49+
u'http://127.0.0.1:8080',
50+
u'http://127.0.10.150',
4751
])
4852
def test_returns_true_on_valid_url(address):
4953
assert url(address)
5054

5155

56+
@pytest.mark.parametrize('address, public', [
57+
(u'http://foo.bar', True),
58+
(u'http://username:password@example.com:4010/', False),
59+
(u'http://username:password@112.168.10.10:4010/', True),
60+
(u'http://username:password@192.168.10.10:4010/', False),
61+
(u'http://10.0.10.1', False),
62+
(u'http://127.0.0.1', False),
63+
])
64+
def test_returns_true_on_valid_public_url(address, public):
65+
assert url(address, public=public)
66+
67+
5268
@pytest.mark.parametrize('address', [
5369
'http://foobar',
5470
'foobar.dk',
@@ -96,8 +112,18 @@ def test_returns_true_on_valid_url(address):
96112
'http://.www.foo.bar/',
97113
'http://www.foo.bar./',
98114
'http://.www.foo.bar./',
99-
'http://10.1.1.1',
100-
'http://10.1.1.254',
115+
'http://127.12.0.260'
101116
])
102117
def test_returns_failed_validation_on_invalid_url(address):
103118
assert isinstance(url(address), ValidationFailure)
119+
120+
121+
@pytest.mark.parametrize('address, public', [
122+
(u'http://username:password@192.168.10.10:4010/', True),
123+
(u'http://10.0.10.1', True),
124+
(u'http://127.0.0.1', True),
125+
(u'http://username:password@127.0.0.1:8080', True),
126+
127+
])
128+
def test_returns_failed_validation_on_invalid_public_url(address, public):
129+
assert isinstance(url(address, public=public), ValidationFailure)

‎validators/url.py‎

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,32 @@
22

33
from .utils import validator
44

5+
ip_middle_octet = u"(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5]))"
6+
ip_last_octet = u"(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))"
7+
58
regex = re.compile(
69
u"^"
710
# protocol identifier
811
u"(?:(?:https?|ftp)://)"
912
# user:pass authentication
1013
u"(?:\S+(?::\S*)?@)?"
1114
u"(?:"
15+
u"(?P<private_ip>"
1216
# IP address exclusion
1317
# private & local networks
14-
u"(?!(?:10|127)(?:\.\d{1,3}){3})"
15-
u"(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})"
16-
u"(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})"
18+
u"(?:(?:10|127)" + ip_middle_octet + u"{2}" + ip_last_octet + u")|"
19+
u"(?:(?:169\.254|192\.168)" + ip_middle_octet + ip_last_octet + u")|"
20+
u"(?:172\.(?:1[6-9]|2\d|3[0-1])" + ip_middle_octet + ip_last_octet + u"))"
21+
u"|"
1722
# IP address dotted notation octets
1823
# excludes loopback network 0.0.0.0
1924
# excludes reserved space >= 224.0.0.0
2025
# excludes network & broadcast addresses
2126
# (first & last IP address of each class)
27+
u"(?P<public_ip>"
2228
u"(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])"
23-
u"(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}"
24-
u"(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))"
29+
u"" + ip_middle_octet + u"{2}"
30+
u"" + ip_last_octet + u")"
2531
u"|"
2632
# host name
2733
u"(?:(?:[a-z\u00a1-\uffff0-9]-?)*[a-z\u00a1-\uffff0-9]+)"
@@ -41,7 +47,7 @@
4147

4248

4349
@validator
44-
def url(value):
50+
def url(value, public=False):
4551
"""
4652
Return whether or not given value is a valid URL.
4753
@@ -56,9 +62,11 @@ def url(value):
5662
Examples::
5763
5864
>>> url('http://foobar.dk')
65+
>>> url('http://10.0.0.1')
5966
True
6067
6168
>>> url('http://foobar.d')
69+
>>> url('http://10.0.0.1', public=True)
6270
ValidationFailure(func=url, ...)
6371
6472
.. versionadded:: 0.2
@@ -69,5 +77,12 @@ def url(value):
6977
positives.
7078
7179
:param value: URL address string to validate
80+
:param public: (default=False) Set True to only allow a public IP address
7281
"""
73-
return pattern.match(value)
82+
if not public:
83+
return pattern.match(value)
84+
85+
if pattern.match(value).groupdict()['private_ip']:
86+
return False
87+
88+
return True

0 commit comments

Comments
 (0)