Skip to content

Commit b0364c8

Browse files
committed
Fix URL validation
1 parent ea97ec5 commit b0364c8

3 files changed

Lines changed: 131 additions & 26 deletions

File tree

‎CHANGES.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Changelog
55
0.10.2 (2016-06-11)
66
^^^^^^^^^^^^^^^^^^^
77

8-
- Fixed various URL validation issues (#24, pull request courtesy gokhanm)
8+
- Fixed various URL validation issues
99

1010

1111
0.10.1 (2016-04-09)

‎tests/test_url.py‎

Lines changed: 89 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,100 @@
44
from validators import url, ValidationFailure
55

66

7-
@pytest.mark.parametrize(('address',), [
8-
('http://foobar.dk', True),
9-
('http://foobar.dk', True),
10-
('http://foobar.museum/foobar', True),
11-
('http://127.0.0.1/foobar', True),
12-
('http://127.0.0.1:9000/fake', True),
13-
('http://localhost/foobar', False),
14-
('http://foobar', False),
15-
('htp://foobar.com', False),
16-
('http://foobar..com', False),
17-
('http://fo..com', False),
18-
('http://fo.com', True),
7+
@pytest.mark.parametrize('address', [
8+
u'http://foobar.dk',
9+
u'http://foobar.museum/foobar',
10+
u'http://fo.com',
11+
u'http://foo.com/blah_blah',
12+
u'http://foo.com/blah_blah/',
13+
u'http://foo.com/blah_blah_(wikipedia)',
14+
u'http://foo.com/blah_blah_(wikipedia)_(again)',
15+
u'http://www.example.com/wpstyle/?p=364',
16+
u'https://www.example.com/foo/?bar=baz&inga=42&quux',
17+
u'http://✪df.ws/123',
18+
u'http://userid:password@example.com:8080',
19+
u'http://userid:password@example.com:8080/',
20+
u'http://userid@example.com',
21+
u'http://userid@example.com/',
22+
u'http://userid@example.com:8080',
23+
u'http://userid@example.com:8080/',
24+
u'http://userid:password@example.com',
25+
u'http://userid:password@example.com/',
26+
u'http://142.42.1.1/',
27+
u'http://142.42.1.1:8080/',
28+
u'http://➡.ws/䨹',
29+
u'http://⌘.ws',
30+
u'http://⌘.ws/',
31+
u'http://foo.com/blah_(wikipedia)#cite-1',
32+
u'http://foo.com/blah_(wikipedia)_blah#cite-1',
33+
u'http://foo.com/unicode_(✪)_in_parens',
34+
u'http://foo.com/(something)?after=parens',
35+
u'http://☺.damowmow.com/',
36+
u'http://code.google.com/events/#&product=browser',
37+
u'http://j.mp',
38+
u'ftp://foo.bar/baz',
39+
u'http://foo.bar/?q=Test%20URL-encoded%20stuff',
40+
u'http://مثال.إختبار',
41+
u'http://例子.测试',
42+
u'http://उदाहरण.परीक्षा',
43+
u'http://-.~_!$&\'()*+,;=:%40:80%2f::::::@example.com',
44+
u'http://1337.net',
45+
u'http://a.b-c.de',
46+
u'http://223.255.255.254',
1947
])
2048
def test_returns_true_on_valid_url(address):
2149
assert url(address)
2250

2351

24-
@pytest.mark.parametrize(('address',), [
25-
('http://foobar',),
26-
('foobar.dk',),
27-
('http://127.0.0/asdf',),
28-
('http://foobar.d',),
29-
('http://foobar.12',),
30-
('http://localhost:abc/a',),
52+
@pytest.mark.parametrize('address', [
53+
'http://foobar',
54+
'foobar.dk',
55+
'http://127.0.0/asdf',
56+
'http://foobar.d',
57+
'http://foobar.12',
58+
'http://foobar',
59+
'htp://foobar.com',
60+
'http://foobar..com',
61+
'http://fo..com',
62+
'http://',
63+
'http://.',
64+
'http://..',
65+
'http://../',
66+
'http://?',
67+
'http://??',
68+
'http://??/',
69+
'http://#',
70+
'http://##',
71+
'http://##/',
72+
'http://foo.bar?q=Spaces should be encoded',
73+
'//',
74+
'//a',
75+
'///a',
76+
'///',
77+
'http:///a',
78+
'foo.com',
79+
'rdar://1234',
80+
'h://test',
81+
'http:// shouldfail.com',
82+
':// should fail',
83+
'http://foo.bar/foo(bar)baz quux',
84+
'ftps://foo.bar/',
85+
'http://-error-.invalid/',
86+
'http://a.b--c.de/',
87+
'http://-a.b.co',
88+
'http://a.b-.co',
89+
'http://0.0.0.0',
90+
'http://10.1.1.0',
91+
'http://10.1.1.255',
92+
'http://224.1.1.1',
93+
'http://1.1.1.1.1',
94+
'http://123.123.123',
95+
'http://3628126748',
96+
'http://.www.foo.bar/',
97+
'http://www.foo.bar./',
98+
'http://.www.foo.bar./',
99+
'http://10.1.1.1',
100+
'http://10.1.1.254',
31101
])
32102
def test_returns_failed_validation_on_invalid_url(address):
33103
assert isinstance(url(address), ValidationFailure)

‎validators/url.py‎

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,39 @@
22

33
from .utils import validator
44

5-
regex = (
6-
r'^(http(s)?://)?(www\.)?((\w|-)+)(\.(\w|-)+)+(:\d*)?(/(\w|-|\&|\=|\#)*)*'
5+
regex = re.compile(
6+
u"^"
7+
# protocol identifier
8+
u"(?:(?:https?|ftp)://)"
9+
# user:pass authentication
10+
u"(?:\S+(?::\S*)?@)?"
11+
u"(?:"
12+
# IP address exclusion
13+
# 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})"
17+
# IP address dotted notation octets
18+
# excludes loopback network 0.0.0.0
19+
# excludes reserved space >= 224.0.0.0
20+
# excludes network & broadcast addresses
21+
# (first & last IP address of each class)
22+
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]))"
25+
u"|"
26+
# host name
27+
u"(?:(?:[a-z\u00a1-\uffff0-9]-?)*[a-z\u00a1-\uffff0-9]+)"
28+
# domain name
29+
u"(?:\.(?:[a-z\u00a1-\uffff0-9]-?)*[a-z\u00a1-\uffff0-9]+)*"
30+
# TLD identifier
31+
u"(?:\.(?:[a-z\u00a1-\uffff]{2,}))"
32+
u")"
33+
# port number
34+
u"(?::\d{2,5})?"
35+
# resource path
36+
u"(?:/\S*)?"
37+
u"$", re.UNICODE
738
)
839

940
pattern = re.compile(regex)
@@ -17,10 +48,10 @@ def url(value):
1748
If the value is valid URL this function returns ``True``, otherwise
1849
:class:`~validators.utils.ValidationFailure`.
1950
20-
This validator is based on `WTForms URL validator`_.
51+
This validator is based on the wonderful `URL validator of dperini`_.
2152
22-
.. _WTForms URL validator:
23-
https://github.com/wtforms/wtforms/blob/master/wtforms/validators.py
53+
.. _URL validator of dperini:
54+
https://gist.github.com/dperini/729294
2455
2556
Examples::
2657
@@ -32,7 +63,11 @@ def url(value):
3263
3364
.. versionadded:: 0.2
3465
66+
.. versionchanged:: 0.10.2
67+
68+
Added support for various exotic URLs and fixed various false
69+
positives.
70+
3571
:param value: URL address string to validate
3672
"""
37-
3873
return pattern.match(value)

0 commit comments

Comments
 (0)