Skip to content

Commit 2b3a45e

Browse files
authored
Merge pull request #26 from lconceicao/private_IPs
Correctly validate private IP addresses by default
2 parents b0364c8 + f6f2d65 commit 2b3a45e

2 files changed

Lines changed: 56 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: 28 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
@@ -58,9 +64,15 @@ def url(value):
5864
>>> url('http://foobar.dk')
5965
True
6066
67+
>>> url('http://10.0.0.1')
68+
True
69+
6170
>>> url('http://foobar.d')
6271
ValidationFailure(func=url, ...)
6372
73+
>>> url('http://10.0.0.1', public=True)
74+
ValidationFailure(func=url, ...)
75+
6476
.. versionadded:: 0.2
6577
6678
.. versionchanged:: 0.10.2
@@ -69,5 +81,14 @@ def url(value):
6981
positives.
7082
7183
:param value: URL address string to validate
84+
:param public: (default=False) Set True to only allow a public IP address
7285
"""
73-
return pattern.match(value)
86+
if not public:
87+
return pattern.match(value)
88+
89+
match_result = pattern.match(value)
90+
91+
if match_result.groupdict()['private_ip']:
92+
return False
93+
94+
return match_result

0 commit comments

Comments
 (0)